document.php 12 KB

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