photos.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /**
  23. * \file htdocs/categories/photos.php
  24. * \ingroup category
  25. * \brief Gestion des photos d'une categorie
  26. */
  27. // Load Dolibarr environment
  28. require '../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/categories.lib.php';
  33. // Load translation files required by the page
  34. $langs->loadlangs(array('categories', 'bills'));
  35. $id = GETPOST('id', 'int');
  36. $label = GETPOST('label', 'alpha');
  37. $action = GETPOST('action', 'aZ09');
  38. $confirm = GETPOST('confirm');
  39. if ($id == '' && $label == '') {
  40. dol_print_error('', 'Missing parameter id');
  41. exit();
  42. }
  43. // Security check
  44. $result = restrictedArea($user, 'categorie', $id, '&category');
  45. $object = new Categorie($db);
  46. $result = $object->fetch($id, $label);
  47. if ($result <= 0) {
  48. dol_print_error($db, $object->error);
  49. exit;
  50. }
  51. $type = $object->type;
  52. if (is_numeric($type)) {
  53. $type = Categorie::$MAP_ID_TO_CODE[$type]; // For backward compatibility
  54. }
  55. $upload_dir = $conf->categorie->multidir_output[$object->entity];
  56. $hookmanager->initHooks(array('categorycard'));
  57. /*
  58. * Actions
  59. */
  60. $parameters = array('id' => $id, 'label' => $label, 'confirm' => $confirm, 'type' => $type, 'uploaddir' => $upload_dir, 'sendfile' => (GETPOST("sendit") ? true : false));
  61. // Note that $action and $object may be modified by some hooks
  62. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
  63. if ($reshook < 0) {
  64. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  65. }
  66. if (empty($reshook)) {
  67. if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0 && GETPOST("sendit") && getDolGlobalString('MAIN_UPLOAD_DOC')) {
  68. if ($object->id) {
  69. $file = $_FILES['userfile'];
  70. if (is_array($file['name']) && count($file['name']) > 0) {
  71. foreach ($file['name'] as $i => $name) {
  72. if (empty($file['tmp_name'][$i]) || intval($conf->global->MAIN_UPLOAD_DOC) * 1000 <= filesize($file['tmp_name'][$i])) {
  73. setEventMessage($file['name'][$i].' : '.$langs->trans(empty($file['tmp_name'][$i]) ? 'ErrorFailedToSaveFile' : 'MaxSizeForUploadedFiles'));
  74. unset($file['name'][$i], $file['type'][$i], $file['tmp_name'][$i], $file['error'][$i], $file['size'][$i]);
  75. }
  76. }
  77. }
  78. if (!empty($file['tmp_name'])) {
  79. $object->add_photo($upload_dir, $file);
  80. }
  81. }
  82. }
  83. if ($action == 'confirm_delete' && $_GET["file"] && $confirm == 'yes' && $user->hasRight('categorie', 'creer')) {
  84. $object->delete_photo($upload_dir."/".$_GET["file"]);
  85. }
  86. if ($action == 'addthumb' && $_GET["file"]) {
  87. $object->addThumbs($upload_dir."/".$_GET["file"]);
  88. }
  89. }
  90. /*
  91. * View
  92. */
  93. llxHeader("", "", $langs->trans("Categories"));
  94. $form = new Form($db);
  95. $formother = new FormOther($db);
  96. if ($object->id) {
  97. $title = Categorie::$MAP_TYPE_TITLE_AREA[$type];
  98. $head = categories_prepare_head($object, $type);
  99. print dol_get_fiche_head($head, 'photos', $langs->trans($title), -1, 'category');
  100. $backtolist = (GETPOST('backtolist') ? GETPOST('backtolist') : DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.urlencode($type));
  101. $linkback = '<a href="'.dol_sanitizeUrl($backtolist).'">'.$langs->trans("BackToList").'</a>';
  102. $object->next_prev_filter = 'type = '.((int) $object->type);
  103. $object->ref = $object->label;
  104. $morehtmlref = '<br><div class="refidno"><a href="'.DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.$type.'">'.$langs->trans("Root").'</a> >> ';
  105. $ways = $object->print_all_ways(" &gt;&gt; ", '', 1);
  106. foreach ($ways as $way) {
  107. $morehtmlref .= $way."<br>\n";
  108. }
  109. $morehtmlref .= '</div>';
  110. dol_banner_tab($object, 'label', $linkback, ($user->socid ? 0 : 1), 'label', 'label', $morehtmlref, '&type='.$type, 0, '', '', 1);
  111. /*
  112. * Confirmation de la suppression de photo
  113. */
  114. if ($action == 'delete') {
  115. print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&type='.$type.'&file='.$_GET["file"], $langs->trans('DeletePicture'), $langs->trans('ConfirmDeletePicture'), 'confirm_delete', '', 0, 1);
  116. }
  117. print '<br>';
  118. print '<div class="fichecenter">';
  119. print '<div class="underbanner clearboth"></div>';
  120. print '<table class="border centpercent tableforfield">';
  121. // Description
  122. print '<tr><td class="titlefield notopnoleft">';
  123. print $langs->trans("Description").'</td><td>';
  124. print dol_htmlentitiesbr($object->description);
  125. print '</td></tr>';
  126. // Color
  127. print '<tr><td class="notopnoleft">';
  128. print $langs->trans("Color").'</td><td>';
  129. print $formother->showColor($object->color);
  130. print '</td></tr>';
  131. print "</table>\n";
  132. print '</div>';
  133. print dol_get_fiche_end();
  134. /*
  135. * Action bar
  136. */
  137. print '<div class="tabsAction">'."\n";
  138. if ($action != 'ajout_photo' && $user->hasRight('categorie', 'creer')) {
  139. if (getDolGlobalString('MAIN_UPLOAD_DOC')) {
  140. print '<a class="butAction hideonsmartphone" href="'.$_SERVER['PHP_SELF'].'?action=ajout_photo&amp;id='.$object->id.'&amp;type='.$type.'">';
  141. print $langs->trans("AddPhoto").'</a>';
  142. } else {
  143. print '<a class="butActionRefused classfortooltip hideonsmartphone" href="#">';
  144. print $langs->trans("AddPhoto").'</a>';
  145. }
  146. }
  147. print '</div>'."\n";
  148. /*
  149. * Ajouter une photo
  150. */
  151. if ($action == 'ajout_photo' && $user->hasRight('categorie', 'creer') && getDolGlobalString('MAIN_UPLOAD_DOC')) {
  152. // Affiche formulaire upload
  153. $formfile = new FormFile($db);
  154. $formfile->form_attach_new_file($_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;type='.$type, $langs->trans("AddPhoto"), 1, '', $user->hasRight('categorie', 'creer'), 50, $object, '', false, '', 0);
  155. }
  156. // Affiche photos
  157. if ($action != 'ajout_photo') {
  158. $nbphoto = 0;
  159. $nbbyrow = 5;
  160. $maxWidth = 160;
  161. $maxHeight = 120;
  162. $pdir = get_exdir($object->id, 2, 0, 0, $object, 'category').$object->id."/photos/";
  163. $dir = $upload_dir.'/'.$pdir;
  164. $listofphoto = $object->liste_photos($dir);
  165. if (is_array($listofphoto) && count($listofphoto)) {
  166. print '<br>';
  167. print '<table width="100%" valign="top" class="center centpercent">';
  168. foreach ($listofphoto as $key => $obj) {
  169. $nbphoto++;
  170. if ($nbbyrow && ($nbphoto % $nbbyrow == 1)) {
  171. print '<tr class"center valignmiddle" border="1">';
  172. }
  173. if ($nbbyrow) {
  174. print '<td width="'.ceil(100 / $nbbyrow).'%" class="photo">';
  175. }
  176. 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">';
  177. // Si fichier vignette disponible, on l'utilise, sinon on utilise photo origine
  178. if ($obj['photo_vignette']) {
  179. $filename = $obj['photo_vignette'];
  180. } else {
  181. $filename = $obj['photo'];
  182. }
  183. // Nom affiche
  184. $viewfilename = $obj['photo'];
  185. // Taille de l'image
  186. $object->get_image_size($dir.$filename);
  187. $imgWidth = ($object->imgWidth < $maxWidth) ? $object->imgWidth : $maxWidth;
  188. $imgHeight = ($object->imgHeight < $maxHeight) ? $object->imgHeight : $maxHeight;
  189. print '<img border="0" width="'.$imgWidth.'" height="'.$imgHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=category&entity='.$object->entity.'&file='.urlencode($pdir.$filename).'">';
  190. print '</a>';
  191. print '<br>'.$viewfilename;
  192. print '<br>';
  193. // On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
  194. if (!$obj['photo_vignette'] && preg_match('/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i', $obj['photo']) && ($object->imgWidth > $maxWidth || $object->imgHeight > $maxHeight)) {
  195. 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>';
  196. }
  197. if ($user->hasRight('categorie', 'creer')) {
  198. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken().'&type='.$type.'&file='.urlencode($pdir.$viewfilename).'">';
  199. print img_delete().'</a>';
  200. }
  201. if ($nbbyrow) {
  202. print '</td>';
  203. }
  204. if ($nbbyrow && ($nbphoto % $nbbyrow == 0)) {
  205. print '</tr>';
  206. }
  207. }
  208. // Ferme tableau
  209. while ($nbphoto % $nbbyrow) {
  210. print '<td width="'.ceil(100 / $nbbyrow).'%">&nbsp;</td>';
  211. $nbphoto++;
  212. }
  213. print '</table>';
  214. }
  215. if ($nbphoto < 1) {
  216. print '<div class="opacitymedium">'.$langs->trans("NoPhotoYet")."</div>";
  217. }
  218. }
  219. } else {
  220. print $langs->trans("ErrorUnknown");
  221. }
  222. // End of page
  223. llxFooter();
  224. $db->close();