docmine.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /* Copyright (C) 2008-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/ecm/docmine.php
  19. * \ingroup ecm
  20. * \brief Card of a directory for ECM module
  21. * \author Laurent Destailleur
  22. */
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
  28. // Load traductions files
  29. $langs->load("ecm");
  30. $langs->load("companies");
  31. $langs->load("other");
  32. $action=GETPOST('action','alpha');
  33. $confirm=GETPOST('confirm','alpha');
  34. // Get parameters
  35. $sortfield = GETPOST("sortfield",'alpha');
  36. $sortorder = GETPOST("sortorder",'alpha');
  37. $page = GETPOST("page",'int');
  38. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  39. $offset = $conf->liste_limit * $page;
  40. $pageprev = $page - 1;
  41. $pagenext = $page + 1;
  42. if (! $sortorder) $sortorder="ASC";
  43. if (! $sortfield) $sortfield="name";
  44. $section=GETPOST("section");
  45. if (! $section)
  46. {
  47. dol_print_error('',"ErrorSectionParamNotDefined");
  48. exit;
  49. }
  50. // Load ecm object
  51. $ecmdir = new EcmDirectory($db);
  52. $result=$ecmdir->fetch(GETPOST("section"));
  53. if (! $result > 0)
  54. {
  55. dol_print_error($db,$ecmdir->error);
  56. exit;
  57. }
  58. $relativepath=$ecmdir->getRelativePath();
  59. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  60. /*
  61. * Actions
  62. */
  63. // Upload file
  64. if (GETPOST("sendit") && ! empty($conf->global->MAIN_UPLOAD_DOC))
  65. {
  66. if (dol_mkdir($upload_dir) >= 0)
  67. {
  68. $resupload = dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir . "/" . dol_unescapefile($_FILES['userfile']['name']),0,0,$_FILES['userfile']['error']);
  69. if (is_numeric($resupload) && $resupload > 0)
  70. {
  71. $result=$ecmdir->changeNbOfFiles('+');
  72. }
  73. else
  74. {
  75. $langs->load("errors");
  76. if ($resupload < 0) // Unknown error
  77. {
  78. setEventMessages($langs->trans("ErrorFileNotUploaded"), null, 'errors');
  79. }
  80. else if (preg_match('/ErrorFileIsInfectedWithAVirus/',$resupload)) // Files infected by a virus
  81. {
  82. setEventMessages($langs->trans("ErrorFileIsInfectedWithAVirus"), null, 'errors');
  83. }
  84. else // Known error
  85. {
  86. setEventMessages($langs->trans($resupload), null, 'errors');
  87. }
  88. }
  89. }
  90. else
  91. {
  92. // Failed transfer (exceeding the limit file?)
  93. $langs->load("errors");
  94. setEventMessages($langs->trans("ErrorFailToCreateDir",$upload_dir), null, 'errors');
  95. }
  96. }
  97. // Remove file
  98. if ($action == 'confirm_deletefile' && $confirm == 'yes')
  99. {
  100. $langs->load("other");
  101. $file = $upload_dir . "/" . GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  102. $ret=dol_delete_file($file);
  103. if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
  104. else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
  105. $result=$ecmdir->changeNbOfFiles('-');
  106. }
  107. // Remove dir
  108. if ($action == 'confirm_deletedir' && $confirm == 'yes')
  109. {
  110. // Fetch was already done
  111. $result=$ecmdir->delete($user);
  112. if ($result > 0)
  113. {
  114. header("Location: ".DOL_URL_ROOT."/ecm/index.php");
  115. exit;
  116. }
  117. else
  118. {
  119. $langs->load('errors');
  120. setEventMessages($langs->trans($ecmdir->error,$ecmdir->label), null, 'errors');
  121. }
  122. }
  123. // Update description
  124. if ($action == 'update' && ! GETPOST('cancel'))
  125. {
  126. $error=0;
  127. $db->begin();
  128. $oldlabel=$ecmdir->label;
  129. $olddir=$ecmdir->getRelativePath(0);
  130. $olddir=$conf->ecm->dir_output.'/'.$olddir;
  131. // Fetch was already done
  132. $ecmdir->label = dol_sanitizeFileName(GETPOST("label"));
  133. $ecmdir->description = GETPOST("description");
  134. $result=$ecmdir->update($user);
  135. if ($result > 0)
  136. {
  137. // Try to rename file if changed
  138. if ($oldlabel != $ecmdir->label && file_exists($olddir))
  139. {
  140. $newdir=$ecmdir->getRelativePath(1); // return "xxx/zzz/" from ecm directory
  141. $newdir=$conf->ecm->dir_output.'/'.$newdir;
  142. //print $olddir.'-'.$newdir;
  143. $result=@rename($olddir,$newdir);
  144. if (! $result)
  145. {
  146. $langs->load('errors');
  147. setEventMessages($langs->trans('ErrorFailToRenameDir',$olddir,$newdir), null, 'errors');
  148. $error++;
  149. }
  150. }
  151. if (! $error)
  152. {
  153. $db->commit();
  154. $relativepath=$ecmdir->getRelativePath();
  155. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  156. }
  157. else
  158. {
  159. $db->rollback();
  160. }
  161. }
  162. else
  163. {
  164. $db->rollback();
  165. setEventMessages($ecmdir->error, $ecmdir->errors, 'errors');
  166. }
  167. }
  168. /*******************************************************************
  169. * PAGE
  170. *
  171. * Put here all code to do according to value of "action" parameter
  172. ********************************************************************/
  173. llxHeader();
  174. $form=new Form($db);
  175. // Built the file List
  176. $filearray=dol_dir_list($upload_dir,"files",0,'','(\.meta|_preview.*\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
  177. $totalsize=0;
  178. foreach($filearray as $key => $file)
  179. {
  180. $totalsize+=$file['size'];
  181. }
  182. $head = ecm_prepare_head($ecmdir);
  183. dol_fiche_head($head, 'card', $langs->trans("ECMSectionManual"), '', 'dir');
  184. if ($action == 'edit')
  185. {
  186. print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  187. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  188. print '<input type="hidden" name="section" value="'.$section.'">';
  189. print '<input type="hidden" name="action" value="update">';
  190. }
  191. print '<table class="border" width="100%">';
  192. print '<tr><td class="titlefield">'.$langs->trans("Ref").'</td><td>';
  193. $s='';
  194. $tmpecmdir=new EcmDirectory($db); // Need to create a new one
  195. $tmpecmdir->fetch($ecmdir->id);
  196. $result = 1;
  197. $i=0;
  198. while ($tmpecmdir && $result > 0)
  199. {
  200. $tmpecmdir->ref=$tmpecmdir->label;
  201. if ($i == 0 && $action == 'edit')
  202. {
  203. $s='<input type="text" name="label" class="minwidth300" maxlength="32" value="'.$tmpecmdir->label.'">';
  204. }
  205. else $s=$tmpecmdir->getNomUrl(1).$s;
  206. if ($tmpecmdir->fk_parent)
  207. {
  208. $s=' -> '.$s;
  209. $result=$tmpecmdir->fetch($tmpecmdir->fk_parent);
  210. }
  211. else
  212. {
  213. $tmpecmdir=0;
  214. }
  215. $i++;
  216. }
  217. print img_picto('','object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> ';
  218. print $s;
  219. print '</td></tr>';
  220. print '<tr><td class="tdtop">'.$langs->trans("Description").'</td><td>';
  221. if ($action == 'edit')
  222. {
  223. print '<textarea class="flat" name="description" cols="80">';
  224. print $ecmdir->description;
  225. print '</textarea>';
  226. }
  227. else print dol_nl2br($ecmdir->description);
  228. print '</td></tr>';
  229. print '<tr><td>'.$langs->trans("ECMCreationUser").'</td><td>';
  230. $userecm=new User($db);
  231. $userecm->fetch($ecmdir->fk_user_c);
  232. print $userecm->getNomUrl(1);
  233. print '</td></tr>';
  234. print '<tr><td>'.$langs->trans("ECMCreationDate").'</td><td>';
  235. print dol_print_date($ecmdir->date_c,'dayhour');
  236. print '</td></tr>';
  237. print '<tr><td>'.$langs->trans("ECMDirectoryForFiles").'</td><td>';
  238. print '/ecm/'.$relativepath;
  239. print '</td></tr>';
  240. print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
  241. $nbofiles=count($filearray);
  242. print $nbofiles;
  243. // Test if nb is same than in cache
  244. if ($nbofiles != $ecmdir->cachenbofdoc)
  245. {
  246. $ecmdir->changeNbOfFiles((string) $nbofiles);
  247. }
  248. print '</td></tr>';
  249. print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
  250. print dol_print_size($totalsize);
  251. print '</td></tr>';
  252. if ($action == 'edit')
  253. {
  254. print '<tr><td colspan="2" align="center">';
  255. print '<input type="submit" class="button" name="submit" value="'.$langs->trans("Save").'">';
  256. print ' &nbsp; &nbsp; ';
  257. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  258. print '</td></tr>';
  259. }
  260. print '</table>';
  261. if ($action == 'edit')
  262. {
  263. print '</form>';
  264. }
  265. print '</div>';
  266. // Actions buttons
  267. if ($action != 'edit' && $action != 'delete')
  268. {
  269. print '<div class="tabsAction">';
  270. if ($user->rights->ecm->setup)
  271. {
  272. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&section='.$section.'">'.$langs->trans('Edit').'</a>';
  273. }
  274. if ($user->rights->ecm->setup)
  275. {
  276. print '<a class="butAction" href="'.DOL_URL_ROOT.'/ecm/docdir.php?action=create&catParent='.$section.'">'.$langs->trans('ECMAddSection').'</a>';
  277. }
  278. else
  279. {
  280. print '<a class="butActionRefused" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('ECMAddSection').'</a>';
  281. }
  282. if (count($filearray) == 0)
  283. {
  284. if ($user->rights->ecm->setup)
  285. {
  286. print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete_dir&section='.$section.'">'.$langs->trans('Delete').'</a>';
  287. }
  288. else
  289. {
  290. print '<a class="butActionDeleteRefused" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('Delete').'</a>';
  291. }
  292. }
  293. else
  294. {
  295. print '<a class="butActionRefused" href="#" title="'.$langs->trans("CannotRemoveDirectoryContainsFiles").'">'.$langs->trans('Delete').'</a>';
  296. }
  297. print '</div>';
  298. }
  299. // Confirm remove file
  300. if ($action == 'delete')
  301. {
  302. print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.$_REQUEST["section"].'&amp;urlfile='.urlencode($_GET["urlfile"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile');
  303. }
  304. // Confirm remove file
  305. if ($action == 'delete_dir')
  306. {
  307. $relativepathwithoutslash=preg_replace('/[\/]$/','',$relativepath);
  308. print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.$_REQUEST["section"], $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection',$relativepathwithoutslash), 'confirm_deletedir', '', 1, 1);
  309. }
  310. $formfile=new FormFile($db);
  311. /*
  312. // Display upload form
  313. if ($user->rights->ecm->upload)
  314. {
  315. $formfile->form_attach_new_file(DOL_URL_ROOT.'/ecm/docmine.php','',0,$section);
  316. }
  317. // List of document
  318. if ($user->rights->ecm->read)
  319. {
  320. $param='&amp;section='.$section;
  321. $formfile->list_of_documents($filearray,'','ecm',$param,1,$relativepath,$user->rights->ecm->upload);
  322. }
  323. */
  324. // End of page
  325. llxFooter();
  326. $db->close();