document.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2005 Simon TOSSER <simon@kornog-computing.com>
  7. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  8. * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
  9. * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
  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. /**
  25. * \file htdocs/product/document.php
  26. * \ingroup product
  27. * \brief Page des documents joints sur les produits
  28. */
  29. require '../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  35. if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL)) {
  36. require_once DOL_DOCUMENT_ROOT.'/product/class/propalmergepdfproduct.class.php';
  37. }
  38. // Load translation files required by the page
  39. $langs->loadLangs(array('other', 'products'));
  40. $id = GETPOST('id', 'int');
  41. $ref = GETPOST('ref', 'alpha');
  42. $action = GETPOST('action', 'aZ09');
  43. $confirm = GETPOST('confirm', 'alpha');
  44. // Security check
  45. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  46. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  47. if ($user->socid) {
  48. $socid = $user->socid;
  49. }
  50. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  51. $hookmanager->initHooks(array('productdocuments'));
  52. // Get parameters
  53. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  54. $sortfield = GETPOST('sortfield', 'aZ09comma');
  55. $sortorder = GETPOST('sortorder', 'aZ09comma');
  56. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  57. if (empty($page) || $page == -1) {
  58. $page = 0;
  59. } // If $page is not defined, or '' or -1
  60. $offset = $limit * $page;
  61. $pageprev = $page - 1;
  62. $pagenext = $page + 1;
  63. if (!$sortorder) {
  64. $sortorder = "ASC";
  65. }
  66. if (!$sortfield) {
  67. $sortfield = "position_name";
  68. }
  69. $object = new Product($db);
  70. if ($id > 0 || !empty($ref)) {
  71. $result = $object->fetch($id, $ref);
  72. if (!empty($conf->product->enabled)) {
  73. $upload_dir = $conf->product->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
  74. } elseif (!empty($conf->service->enabled)) {
  75. $upload_dir = $conf->service->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
  76. }
  77. if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) { // For backward compatiblity, we scan also old dirs
  78. if (!empty($conf->product->enabled)) {
  79. $upload_dirold = $conf->product->multidir_output[$object->entity].'/'.substr(substr("000".$object->id, -2), 1, 1).'/'.substr(substr("000".$object->id, -2), 0, 1).'/'.$object->id."/photos";
  80. } else {
  81. $upload_dirold = $conf->service->multidir_output[$object->entity].'/'.substr(substr("000".$object->id, -2), 1, 1).'/'.substr(substr("000".$object->id, -2), 0, 1).'/'.$object->id."/photos";
  82. }
  83. }
  84. }
  85. $modulepart = 'produit';
  86. if ($object->id > 0) {
  87. if ($object->type == $object::TYPE_PRODUCT) {
  88. restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
  89. }
  90. if ($object->type == $object::TYPE_SERVICE) {
  91. restrictedArea($user, 'service', $object->id, 'product&product', '', '');
  92. }
  93. } else {
  94. restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  95. }
  96. $permissiontoadd = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->creer));
  97. /*
  98. * Actions
  99. */
  100. $parameters = array('id'=>$id);
  101. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  102. if ($reshook < 0) {
  103. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  104. }
  105. if (empty($reshook)) {
  106. // Delete line if product propal merge is linked to a file
  107. if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL)) {
  108. if ($action == 'confirm_deletefile' && $confirm == 'yes' && $permissiontoadd) {
  109. //extract file name
  110. $urlfile = GETPOST('urlfile', 'alpha');
  111. $filename = basename($urlfile);
  112. $filetomerge = new Propalmergepdfproduct($db);
  113. $filetomerge->fk_product = $object->id;
  114. $filetomerge->file_name = $filename;
  115. $result = $filetomerge->delete_by_file($user);
  116. if ($result < 0) {
  117. setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
  118. }
  119. }
  120. }
  121. // Action submit/delete file/link
  122. include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
  123. }
  124. if ($action == 'filemerge' && $permissiontoadd) {
  125. $is_refresh = GETPOST('refresh');
  126. if (empty($is_refresh)) {
  127. $filetomerge_file_array = GETPOST('filetoadd');
  128. $filetomerge_file_array = GETPOST('filetoadd');
  129. if ($conf->global->MAIN_MULTILANGS) {
  130. $lang_id = GETPOST('lang_id', 'aZ09');
  131. }
  132. // Delete all file already associated
  133. $filetomerge = new Propalmergepdfproduct($db);
  134. if ($conf->global->MAIN_MULTILANGS) {
  135. $result = $filetomerge->delete_by_product($user, $object->id, $lang_id);
  136. } else {
  137. $result = $filetomerge->delete_by_product($user, $object->id);
  138. }
  139. if ($result < 0) {
  140. setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
  141. }
  142. // for each file checked add it to the product
  143. if (is_array($filetomerge_file_array)) {
  144. foreach ($filetomerge_file_array as $filetomerge_file) {
  145. $filetomerge->fk_product = $object->id;
  146. $filetomerge->file_name = $filetomerge_file;
  147. if ($conf->global->MAIN_MULTILANGS) {
  148. $filetomerge->lang = $lang_id;
  149. }
  150. $result = $filetomerge->create($user);
  151. if ($result < 0) {
  152. setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
  153. }
  154. }
  155. }
  156. }
  157. }
  158. /*
  159. * View
  160. */
  161. $form = new Form($db);
  162. $title = $langs->trans('ProductServiceCard');
  163. $helpurl = '';
  164. $shortlabel = dol_trunc($object->label, 16);
  165. if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
  166. $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Documents');
  167. $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  168. }
  169. if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
  170. $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Documents');
  171. $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  172. }
  173. llxHeader('', $title, $helpurl);
  174. if ($object->id) {
  175. $head = product_prepare_head($object);
  176. $titre = $langs->trans("CardProduct".$object->type);
  177. $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
  178. print dol_get_fiche_head($head, 'documents', $titre, -1, $picto);
  179. $parameters = array();
  180. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  181. print $hookmanager->resPrint;
  182. if ($reshook < 0) {
  183. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  184. }
  185. // Build file list
  186. $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1);
  187. if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) { // For backward compatiblity, we scan also old dirs
  188. $filearrayold = dol_dir_list($upload_dirold, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1);
  189. $filearray = array_merge($filearray, $filearrayold);
  190. }
  191. $totalsize = 0;
  192. foreach ($filearray as $key => $file) {
  193. $totalsize += $file['size'];
  194. }
  195. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  196. $object->next_prev_filter = " fk_product_type = ".$object->type;
  197. $shownav = 1;
  198. if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
  199. $shownav = 0;
  200. }
  201. dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
  202. print '<div class="fichecenter">';
  203. print '<div class="underbanner clearboth"></div>';
  204. print '<table class="border tableforfield centpercent">';
  205. print '<tr><td class="titlefield">'.$langs->trans("NbOfAttachedFiles").'</td><td colspan="3">'.count($filearray).'</td></tr>';
  206. print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td colspan="3">'.dol_print_size($totalsize, 1, 1).'</td></tr>';
  207. print '</table>';
  208. print '</div>';
  209. print '<div style="clear:both"></div>';
  210. print dol_get_fiche_end();
  211. $param = '&id='.$object->id;
  212. include DOL_DOCUMENT_ROOT.'/core/tpl/document_actions_post_headers.tpl.php';
  213. // Merge propal PDF document PDF files
  214. if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL)) {
  215. $filetomerge = new Propalmergepdfproduct($db);
  216. if ($conf->global->MAIN_MULTILANGS) {
  217. $lang_id = GETPOST('lang_id', 'aZ09');
  218. $result = $filetomerge->fetch_by_product($object->id, $lang_id);
  219. } else {
  220. $result = $filetomerge->fetch_by_product($object->id);
  221. }
  222. $form = new Form($db);
  223. $filearray = dol_dir_list($upload_dir, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1);
  224. if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) { // For backward compatiblity, we scan also old dirs
  225. $filearray = array_merge($filearray, dol_dir_list($upload_dirold, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1));
  226. }
  227. // For each file build select list with PDF extention
  228. if (count($filearray) > 0) {
  229. print '<br>';
  230. // Actual file to merge is :
  231. if (count($filetomerge->lines) > 0) {
  232. print $langs->trans('PropalMergePdfProductActualFile');
  233. }
  234. print '<form name="filemerge" action="'.DOL_URL_ROOT.'/product/document.php?id='.$object->id.'" method="post">';
  235. print '<input type="hidden" name="token" value="'.newToken().'">';
  236. print '<input type="hidden" name="action" value="filemerge">';
  237. if (count($filetomerge->lines) == 0) {
  238. print $langs->trans('PropalMergePdfProductChooseFile');
  239. }
  240. print '<table class="noborder">';
  241. // Get language
  242. if ($conf->global->MAIN_MULTILANGS) {
  243. $langs->load("languages");
  244. print '<tr class="liste_titre"><td>';
  245. $default_lang = empty($lang_id) ? $langs->getDefaultLang() : $lang_id;
  246. $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
  247. print Form::selectarray('lang_id', $langs_available, $default_lang, 0, 0, 0, '', 0, 0, 0, 'ASC');
  248. if ($conf->global->MAIN_MULTILANGS) {
  249. print '<input type="submit" class="button" name="refresh" value="'.$langs->trans('Refresh').'">';
  250. }
  251. print '</td></tr>';
  252. }
  253. foreach ($filearray as $filetoadd) {
  254. if ($ext = pathinfo($filetoadd['name'], PATHINFO_EXTENSION) == 'pdf') {
  255. $checked = '';
  256. $filename = $filetoadd['name'];
  257. if ($conf->global->MAIN_MULTILANGS) {
  258. if (array_key_exists($filetoadd['name'].'_'.$default_lang, $filetomerge->lines)) {
  259. $filename = $filetoadd['name'].' - '.$langs->trans('Language_'.$default_lang);
  260. $checked = ' checked ';
  261. }
  262. } else {
  263. if (array_key_exists($filetoadd['name'], $filetomerge->lines)) {
  264. $checked = ' checked ';
  265. }
  266. }
  267. print '<tr class="oddeven"><td>';
  268. print '<input type="checkbox" '.$checked.' name="filetoadd[]" id="filetoadd" value="'.$filetoadd['name'].'">'.$filename.'</input>';
  269. print '</td></tr>';
  270. }
  271. }
  272. print '<tr><td>';
  273. print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
  274. print '</td></tr>';
  275. print '</table>';
  276. print '</form>';
  277. }
  278. }
  279. } else {
  280. print $langs->trans("ErrorUnknown");
  281. }
  282. // End of page
  283. llxFooter();
  284. $db->close();