dolibarr 24.0.0-beta
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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
9 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31// Load Dolibarr environment
32require '../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/categories.lib.php';
37
46// Load translation files required by the page
47$langs->loadlangs(array('categories', 'bills', 'mrp'));
48
49
50$id = GETPOSTINT('id');
51$label = GETPOST('label', 'alpha');
52$action = GETPOST('action', 'aZ09');
53$confirm = GETPOST('confirm');
54
55if ($id == '' && $label == '') {
56 dol_print_error(null, 'Missing parameter id');
57 exit();
58}
59
60// Initialize a technical object to manage hooks. Note that conf->hooks_modules contains array array
61$hookmanager->initHooks(array('categorycard'));
62
63$object = new Categorie($db);
64$result = $object->fetch($id, $label);
65if ($result <= 0) {
67 exit;
68}
69
70$type = $object->type;
71if (is_numeric($type)) {
72 $type = array_search($type, $object->MAP_ID); // For backward compatibility
73}
74
75$upload_dir = $conf->categorie->multidir_output[$object->entity ?? $conf->entity];
76
77// Security check
78$result = restrictedArea($user, 'categorie', $id, '&category');
79
80$permissiontoadd = $user->hasRight('categorie', 'creer');
81
82
83/*
84 * Actions
85 */
86
87$parameters = array('id' => $id, 'label' => $label, 'confirm' => $confirm, 'type' => $type, 'uploaddir' => $upload_dir, 'sendfile' => (GETPOST("sendit") ? true : false));
88// Note that $action and $object may be modified by some hooks
89$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
90if ($reshook < 0) {
91 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
92}
93
94if (empty($reshook)) {
95 if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0 && GETPOST("sendit") && getDolGlobalString('MAIN_UPLOAD_DOC')) {
96 if ($object->id) {
97 $file = $_FILES['userfile'];
98 if (is_array($file['name']) && count($file['name']) > 0) {
99 foreach ($file['name'] as $i => $name) {
100 if (empty($file['tmp_name'][$i]) || (getDolGlobalInt('MAIN_UPLOAD_DOC') * 1000) <= filesize($file['tmp_name'][$i])) {
101 setEventMessage($file['name'][$i].' : '.$langs->trans(empty($file['tmp_name'][$i]) ? 'ErrorFailedToSaveFile' : 'MaxSizeForUploadedFiles'), 'errors');
102 unset($file['name'][$i], $file['type'][$i], $file['tmp_name'][$i], $file['error'][$i], $file['size'][$i]);
103 }
104 }
105 }
106
107 if (!empty($file['tmp_name'])) {
108 $object->add_photo($upload_dir, $file);
109 }
110 }
111 }
112
113 if ($action == 'confirm_delete' && GETPOST("file") && $confirm == 'yes' && $permissiontoadd) {
114 $object->delete_photo($upload_dir."/".GETPOST("file"));
115 }
116
117 if ($action == 'addthumb' && GETPOST("file") && $permissiontoadd) {
118 $object->addThumbs($upload_dir."/".GETPOST("file"));
119 }
120}
121
122/*
123 * View
124 */
125
126llxHeader("", "", $langs->trans("Categories"));
127
128$form = new Form($db);
129$formother = new FormOther($db);
130
131if ($object->id) {
132 $title = $langs->trans("Categories");
133 $title .= ' ('.$langs->trans(empty(Categorie::$MAP_TYPE_TITLE_AREA[$type]) ? ucfirst($type) : Categorie::$MAP_TYPE_TITLE_AREA[$type]).')';
134
135 $head = categories_prepare_head($object, $type);
136 print dol_get_fiche_head($head, 'photos', $langs->trans($title), -1, 'category');
137
138 $backtolist = (GETPOST('backtolist') ? GETPOST('backtolist') : DOL_URL_ROOT.'/categories/categorie_list.php?leftmenu=cat&type='.urlencode($type));
139 $linkback = '<a href="'.dol_sanitizeUrl($backtolist).'">'.$langs->trans("BackToList").'</a>';
140 $object->next_prev_filter = 'type:=:'.((int) $object->type);
141 $object->ref = $object->label;
142 $morehtmlref = '<br><div class="refidno"><a href="'.DOL_URL_ROOT.'/categories/categorie_list.php?leftmenu=cat&type='.$type.'">'.$langs->trans("Root").'</a> >> ';
143 $ways = $object->print_all_ways("auto", '', 1);
144 foreach ($ways as $way) {
145 $morehtmlref .= $way."<br>\n";
146 }
147 $morehtmlref .= '</div>';
148
149 dol_banner_tab($object, 'label', $linkback, ($user->socid ? 0 : 1), 'label', 'label', $morehtmlref, '&type='.$type, 0, '', '', 1);
150
151 /*
152 * Confirmation deletion of picture
153 */
154 if ($action == 'delete') {
155 print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&type='.urlencode($type).'&file='.urlencode(GETPOST("file")), $langs->trans('DeletePicture'), $langs->trans('ConfirmDeletePicture'), 'confirm_delete', '', 0, 1);
156 }
157
158 print '<br>';
159
160 print '<div class="fichecenter">';
161 print '<div class="underbanner clearboth"></div>';
162 print '<table class="border centpercent tableforfield">';
163
164 // Label
165 print '<tr><td class="titlefield notopnoleft">';
166 print $langs->trans("Label").'</td><td>';
167 print dol_htmlentitiesbr($object->label);
168 print '</td></tr>';
169
170 // Description
171 // print '<tr><td class="titlefield notopnoleft">';
172 // print $langs->trans("Description").'</td><td>';
173 // print dol_htmlentitiesbr($object->description);
174 // print '</td></tr>';
175
176 // Color
177 print '<tr><td class="notopnoleft">';
178 print $langs->trans("Color").'</td><td>';
179 print $formother->showColor($object->color);
180 print '</td></tr>';
181
182 print "</table>\n";
183 print '</div>';
184
185 print dol_get_fiche_end();
186
187
188
189 /*
190 * Action bar
191 */
192 print '<div class="tabsAction">'."\n";
193
194 if ($action != 'ajout_photo' && $user->hasRight('categorie', 'creer')) {
195 if (getDolGlobalString('MAIN_UPLOAD_DOC')) {
196 print '<a class="butAction hideonsmartphone" href="'.$_SERVER['PHP_SELF'].'?action=ajout_photo&amp;id='.$object->id.'&amp;type='.$type.'">';
197 print $langs->trans("AddPhoto").'</a>';
198 } else {
199 print '<a class="butActionRefused classfortooltip hideonsmartphone" href="#">';
200 print $langs->trans("AddPhoto").'</a>';
201 }
202 }
203
204 print '</div>'."\n";
205
206 /*
207 * Ajouter une photo
208 */
209 if ($action == 'ajout_photo' && $user->hasRight('categorie', 'creer') && getDolGlobalString('MAIN_UPLOAD_DOC')) {
210 // Affiche formulaire upload
211 $formfile = new FormFile($db);
212 $formfile->form_attach_new_file($_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;type='.$type, $langs->trans("AddPhoto"), 1, 0, $user->hasRight('categorie', 'creer'), 50, $object, '', 0, '', 0);
213 }
214
215 // Affiche photos
216 if ($action != 'ajout_photo') {
217 $nbphoto = 0;
218 $nbbyrow = 5;
219
220 $maxWidth = 160;
221 $maxHeight = 120;
222
223 $pdir = get_exdir($object->id, 2, 0, 0, $object, 'category').$object->id."/photos/";
224 $dir = $upload_dir.'/'.$pdir;
225
226 $listofphoto = $object->liste_photos($dir);
227
228 if (is_array($listofphoto) && count($listofphoto)) {
229 print '<br>';
230 print '<table width="100%" valign="top" class="center centpercent">';
231
232 foreach ($listofphoto as $key => $obj) {
233 $nbphoto++;
234
235 if ($nbbyrow && ($nbphoto % $nbbyrow == 1)) {
236 print '<tr class"center valignmiddle" border="1">';
237 }
238 if ($nbbyrow) {
239 print '<td width="'.ceil(100 / $nbbyrow).'%" class="photo">';
240 }
241
242 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">';
243
244 // Use the file's miniature if it is available, otherwise use the original image
245 if ($obj['photo_vignette']) {
246 $filename = $obj['photo_vignette'];
247 } else {
248 $filename = $obj['photo'];
249 }
250
251 // Nom affiche
252 $viewfilename = $obj['photo'];
253
254 // Taille de l'image
255 $object->get_image_size($dir.$filename);
256 $imgWidth = ($object->imgWidth < $maxWidth) ? $object->imgWidth : $maxWidth;
257 $imgHeight = ($object->imgHeight < $maxHeight) ? $object->imgHeight : $maxHeight;
258
259 print '<img border="0" width="'.$imgWidth.'" height="'.$imgHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=category&entity='.$object->entity.'&file='.urlencode($pdir.$filename).'">';
260
261 print '</a>';
262 print '<br>'.$viewfilename;
263 print '<br>';
264
265 // On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
266 if (!$obj['photo_vignette'] && preg_match('/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i', $obj['photo']) && ($object->imgWidth > $maxWidth || $object->imgHeight > $maxHeight)) {
267 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>';
268 }
269 if ($user->hasRight('categorie', 'creer')) {
270 print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken().'&type='.$type.'&file='.urlencode($pdir.$viewfilename).'">';
271 print img_delete().'</a>';
272 }
273 if ($nbbyrow) {
274 print '</td>';
275 }
276 if ($nbbyrow && ($nbphoto % $nbbyrow == 0)) {
277 print '</tr>';
278 }
279 }
280
281 // Ferme tableau
282 while ($nbphoto % $nbbyrow) {
283 print '<td width="'.ceil(100 / $nbbyrow).'%">&nbsp;</td>';
284 $nbphoto++;
285 }
286
287 print '</table>';
288 }
289
290 if ($nbphoto < 1) {
291 print '<div class="opacitymedium">'.$langs->trans("NoPhotoYet")."</div>";
292 }
293 }
294} else {
295 print $langs->trans("ErrorUnknown");
296}
297
298// End of page
299llxFooter();
300$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
categories_prepare_head(Categorie $object, $type)
Prepare array with list of tabs.
Class to manage categories.
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
Class to help generate other html components Only common components are here.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
setEventMessage($mesgs, $style='mesgs', $noduplicate=0, $attop=0)
Set event message in dol_events session object.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
restrictedArea(User $user, $features, $object=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.