document.php 12 KB

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