photos.php 9.0 KB

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