dolibarr  16.0.5
photos.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com>
5  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
6  * Copyright (C) 2014 Jean-François Ferry <jfefe@aternatik.fr>
7  * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
29 require '../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/categories.lib.php';
34 
35 // Load translation files required by the page
36 $langs->loadlangs(array('categories', 'bills'));
37 
38 
39 $id = GETPOST('id', 'int');
40 $label = GETPOST('label', 'alpha');
41 $action = GETPOST('action', 'aZ09');
42 $confirm = GETPOST('confirm');
43 
44 if ($id == '' && $label == '') {
45  dol_print_error('', 'Missing parameter id');
46  exit();
47 }
48 
49 // Security check
50 $result = restrictedArea($user, 'categorie', $id, '&category');
51 
52 $object = new Categorie($db);
53 $result = $object->fetch($id, $label);
54 if ($result <= 0) {
55  dol_print_error($db, $object->error); exit;
56 }
57 
58 $type = $object->type;
59 if (is_numeric($type)) {
60  $type = Categorie::$MAP_ID_TO_CODE[$type]; // For backward compatibility
61 }
62 
63 $upload_dir = $conf->categorie->multidir_output[$object->entity];
64 
65 /*
66  * Actions
67  */
68 
69 if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0 && GETPOST("sendit") && !empty($conf->global->MAIN_UPLOAD_DOC)) {
70  if ($object->id) {
71  $file = $_FILES['userfile'];
72  if (is_array($file['name']) && count($file['name']) > 0) {
73  foreach ($file['name'] as $i => $name) {
74  if (empty($file['tmp_name'][$i]) || intval($conf->global->MAIN_UPLOAD_DOC) * 1000 <= filesize($file['tmp_name'][$i])) {
75  setEventMessage($file['name'][$i].' : '.$langs->trans(empty($file['tmp_name'][$i]) ? 'ErrorFailedToSaveFile' : 'MaxSizeForUploadedFiles'));
76  unset($file['name'][$i], $file['type'][$i], $file['tmp_name'][$i], $file['error'][$i], $file['size'][$i]);
77  }
78  }
79  }
80 
81  if (!empty($file['tmp_name'])) {
82  $object->add_photo($upload_dir, $file);
83  }
84  }
85 }
86 
87 if ($action == 'confirm_delete' && $_GET["file"] && $confirm == 'yes' && $user->rights->categorie->creer) {
88  $object->delete_photo($upload_dir."/".$_GET["file"]);
89 }
90 
91 if ($action == 'addthumb' && $_GET["file"]) {
92  $object->addThumbs($upload_dir."/".$_GET["file"]);
93 }
94 
95 
96 /*
97  * View
98  */
99 
100 llxHeader("", "", $langs->trans("Categories"));
101 
102 $form = new Form($db);
103 $formother = new FormOther($db);
104 
105 if ($object->id) {
106  $title = Categorie::$MAP_TYPE_TITLE_AREA[$type];
107 
108  $head = categories_prepare_head($object, $type);
109  print dol_get_fiche_head($head, 'photos', $langs->trans($title), -1, 'category');
110 
111  $backtolist = (GETPOST('backtolist') ? GETPOST('backtolist') : DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.urlencode($type));
112  $linkback = '<a href="'.dol_sanitizeUrl($backtolist).'">'.$langs->trans("BackToList").'</a>';
113  $object->next_prev_filter = ' type = '.$object->type;
114  $object->ref = $object->label;
115  $morehtmlref = '<br><div class="refidno"><a href="'.DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.$type.'">'.$langs->trans("Root").'</a> >> ';
116  $ways = $object->print_all_ways(" &gt;&gt; ", '', 1);
117  foreach ($ways as $way) {
118  $morehtmlref .= $way."<br>\n";
119  }
120  $morehtmlref .= '</div>';
121 
122  dol_banner_tab($object, 'label', $linkback, ($user->socid ? 0 : 1), 'label', 'label', $morehtmlref, '&type='.$type, 0, '', '', 1);
123 
124  /*
125  * Confirmation de la suppression de photo
126  */
127  if ($action == 'delete') {
128  print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&type='.$type.'&file='.$_GET["file"], $langs->trans('DeletePicture'), $langs->trans('ConfirmDeletePicture'), 'confirm_delete', '', 0, 1);
129  }
130 
131  print '<br>';
132 
133  print '<div class="fichecenter">';
134  print '<div class="underbanner clearboth"></div>';
135  print '<table class="border centpercent tableforfield">';
136 
137  // Description
138  print '<tr><td class="titlefield notopnoleft">';
139  print $langs->trans("Description").'</td><td>';
140  print dol_htmlentitiesbr($object->description);
141  print '</td></tr>';
142 
143  // Color
144  print '<tr><td class="notopnoleft">';
145  print $langs->trans("Color").'</td><td>';
146  print $formother->showColor($object->color);
147  print '</td></tr>';
148 
149  print "</table>\n";
150  print '</div>';
151 
152  print dol_get_fiche_end();
153 
154 
155 
156  /*
157  * Action bar
158  */
159  print '<div class="tabsAction">'."\n";
160 
161  if ($action != 'ajout_photo' && $user->rights->categorie->creer) {
162  if (!empty($conf->global->MAIN_UPLOAD_DOC)) {
163  print '<a class="butAction hideonsmartphone" href="'.$_SERVER['PHP_SELF'].'?action=ajout_photo&amp;id='.$object->id.'&amp;type='.$type.'">';
164  print $langs->trans("AddPhoto").'</a>';
165  } else {
166  print '<a class="butActionRefused classfortooltip hideonsmartphone" href="#">';
167  print $langs->trans("AddPhoto").'</a>';
168  }
169  }
170 
171  print '</div>'."\n";
172 
173  /*
174  * Ajouter une photo
175  */
176  if ($action == 'ajout_photo' && $user->rights->categorie->creer && !empty($conf->global->MAIN_UPLOAD_DOC)) {
177  // Affiche formulaire upload
178  $formfile = new FormFile($db);
179  $formfile->form_attach_new_file($_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;type='.$type, $langs->trans("AddPhoto"), 1, '', $user->rights->categorie->creer, 50, $object, '', false, '', 0);
180  }
181 
182  // Affiche photos
183  if ($action != 'ajout_photo') {
184  $nbphoto = 0;
185  $nbbyrow = 5;
186 
187  $maxWidth = 160;
188  $maxHeight = 120;
189 
190  $pdir = get_exdir($object->id, 2, 0, 0, $object, 'category').$object->id."/photos/";
191  $dir = $upload_dir.'/'.$pdir;
192 
193  $listofphoto = $object->liste_photos($dir);
194 
195  if (is_array($listofphoto) && count($listofphoto)) {
196  print '<br>';
197  print '<table width="100%" valign="top" class="center centpercent">';
198 
199  foreach ($listofphoto as $key => $obj) {
200  $nbphoto++;
201 
202  if ($nbbyrow && ($nbphoto % $nbbyrow == 1)) {
203  print '<tr class"center valignmiddle" border="1">';
204  }
205  if ($nbbyrow) {
206  print '<td width="'.ceil(100 / $nbbyrow).'%" class="photo">';
207  }
208 
209  print '<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart=category&entity='.$object->entity.'&file='.urlencode($pdir.$obj['photo']).'" alt="Original size" target="_blank" rel="noopener noreferrer">';
210 
211  // Si fichier vignette disponible, on l'utilise, sinon on utilise photo origine
212  if ($obj['photo_vignette']) {
213  $filename = $obj['photo_vignette'];
214  } else {
215  $filename = $obj['photo'];
216  }
217 
218  // Nom affiche
219  $viewfilename = $obj['photo'];
220 
221  // Taille de l'image
222  $object->get_image_size($dir.$filename);
223  $imgWidth = ($object->imgWidth < $maxWidth) ? $object->imgWidth : $maxWidth;
224  $imgHeight = ($object->imgHeight < $maxHeight) ? $object->imgHeight : $maxHeight;
225 
226  print '<img border="0" width="'.$imgWidth.'" height="'.$imgHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=category&entity='.$object->entity.'&file='.urlencode($pdir.$filename).'">';
227 
228  print '</a>';
229  print '<br>'.$viewfilename;
230  print '<br>';
231 
232  // On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
233  if (!$obj['photo_vignette'] && preg_match('/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i', $obj['photo']) && ($object->imgWidth > $maxWidth || $object->imgHeight > $maxHeight)) {
234  print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&token='.newToken().'&action=addthumb&type='.$type.'&file='.urlencode($pdir.$viewfilename).'">'.img_picto($langs->trans('GenerateThumb'), 'refresh').'&nbsp;&nbsp;</a>';
235  }
236  if ($user->rights->categorie->creer) {
237  print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken().'&type='.$type.'&file='.urlencode($pdir.$viewfilename).'">';
238  print img_delete().'</a>';
239  }
240  if ($nbbyrow) {
241  print '</td>';
242  }
243  if ($nbbyrow && ($nbphoto % $nbbyrow == 0)) {
244  print '</tr>';
245  }
246  }
247 
248  // Ferme tableau
249  while ($nbphoto % $nbbyrow) {
250  print '<td width="'.ceil(100 / $nbbyrow).'%">&nbsp;</td>';
251  $nbphoto++;
252  }
253 
254  print '</table>';
255  }
256 
257  if ($nbphoto < 1) {
258  print '<div class="opacitymedium">'.$langs->trans("NoPhotoYet")."</div>";
259  }
260  }
261 } else {
262  print $langs->trans("ErrorUnknown");
263 }
264 
265 // End of page
266 llxFooter();
267 $db->close();
restrictedArea
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
FormOther
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formother.class.php:39
Categorie
Class to manage categories.
Definition: categorie.class.php:47
dol_banner_tab
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Definition: functions.lib.php:2046
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
get_exdir
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
Definition: functions.lib.php:6549
img_delete
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
Definition: functions.lib.php:4429
FormFile
Class to offer components to list and upload files.
Definition: html.formfile.class.php:36
setEventMessage
setEventMessage($mesgs, $style='mesgs')
Set event message in dol_events session object.
Definition: functions.lib.php:8108
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
dol_htmlentitiesbr
dol_htmlentitiesbr($stringtoencode, $nl2brmode=0, $pagecodefrom='UTF-8', $removelasteolbr=1)
This function is called to encode a string into a HTML string but differs from htmlentities because a...
Definition: functions.lib.php:6991
categories_prepare_head
categories_prepare_head(Categorie $object, $type)
Prepare array with list of tabs.
Definition: categories.lib.php:32
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59