document.php 12 KB

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