dir_card.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/ecm/dir_card.php
  19. * \ingroup ecm
  20. * \brief Card of a directory for ECM module
  21. */
  22. // Load Dolibarr environment
  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. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/ecm/class/htmlecm.form.class.php';
  30. // Load translation files required by page
  31. $langs->loadLangs(array('ecm', 'companies', 'other'));
  32. $action = GETPOST('action', 'alpha');
  33. $cancel = GETPOST('cancel', 'aZ09');
  34. $backtopage = GETPOST('backtopage', 'alpha');
  35. $confirm = GETPOST('confirm', 'alpha');
  36. $module = GETPOST('module', 'alpha');
  37. $website = GETPOST('website', 'alpha');
  38. $pageid = GETPOST('pageid', 'int');
  39. if (empty($module)) {
  40. $module = 'ecm';
  41. }
  42. // Get parameters
  43. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  44. $sortfield = GETPOST('sortfield', 'aZ09comma');
  45. $sortorder = GETPOST('sortorder', 'aZ09comma');
  46. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  47. if (empty($page) || $page == -1) {
  48. $page = 0;
  49. } // If $page is not defined, or '' or -1
  50. $offset = $limit * $page;
  51. $pageprev = $page - 1;
  52. $pagenext = $page + 1;
  53. if (!$sortorder) {
  54. $sortorder = "ASC";
  55. }
  56. if (!$sortfield) {
  57. $sortfield = "name";
  58. }
  59. $section = GETPOST("section", 'alpha') ? GETPOST("section", 'alpha') : GETPOST("relativedir", 'alpha');
  60. if (!$section) {
  61. dol_print_error('', "ErrorSectionParamNotDefined");
  62. exit;
  63. }
  64. // Load ecm object
  65. $ecmdir = new EcmDirectory($db);
  66. if ($module == 'ecm') {
  67. // $section should be an int except if it is dir not yet created into EcmDirectory
  68. $result = $ecmdir->fetch($section);
  69. if ($result > 0) {
  70. $relativepath = $ecmdir->getRelativePath();
  71. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  72. } else {
  73. $relativepath = $section;
  74. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  75. }
  76. } else { // For example $module == 'medias'
  77. $relativepath = $section;
  78. $upload_dir = $conf->medias->multidir_output[$conf->entity].'/'.$relativepath;
  79. }
  80. // Permissions
  81. $permissiontoread = 0;
  82. $permissiontoadd = 0;
  83. $permissiontoupload = 0;
  84. if ($module == 'ecm') {
  85. $permissiontoread = $user->hasRight("ecm", "read");
  86. $permissiontoadd = $user->hasRight("ecm", "setup");
  87. $permissiontoupload = $user->hasRight("ecm", "upload");
  88. }
  89. if ($module == 'medias') {
  90. $permissiontoread = ($user->hasRight("mailing", "lire") || $user->hasRight("website", "read"));
  91. $permissiontoadd = ($user->hasRight("mailing", "creer") || $user->hasRight("website", "write"));
  92. $permissiontoupload = ($user->hasRight("mailing", "creer") || $user->hasRight("website", "write"));
  93. }
  94. if (!$permissiontoread) {
  95. accessforbidden();
  96. }
  97. /*
  98. * Actions
  99. */
  100. // Upload file
  101. if (GETPOST("sendit") && getDolGlobalString('MAIN_UPLOAD_DOC') && $permissiontoupload) {
  102. if (dol_mkdir($upload_dir) >= 0) {
  103. $resupload = dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir."/".dol_unescapefile($_FILES['userfile']['name']), 0, 0, $_FILES['userfile']['error']);
  104. if (is_numeric($resupload) && $resupload > 0) {
  105. $result = $ecmdir->changeNbOfFiles('+');
  106. } else {
  107. $langs->load("errors");
  108. if ($resupload < 0) { // Unknown error
  109. setEventMessages($langs->trans("ErrorFileNotUploaded"), null, 'errors');
  110. } elseif (preg_match('/ErrorFileIsInfectedWithAVirus/', $resupload)) {
  111. // Files infected by a virus
  112. setEventMessages($langs->trans("ErrorFileIsInfectedWithAVirus"), null, 'errors');
  113. } else { // Known error
  114. setEventMessages($langs->trans($resupload), null, 'errors');
  115. }
  116. }
  117. } else {
  118. // Failed transfer (exceeding the limit file?)
  119. $langs->load("errors");
  120. setEventMessages($langs->trans("ErrorFailToCreateDir", $upload_dir), null, 'errors');
  121. }
  122. }
  123. // Remove file
  124. if ($action == 'confirm_deletefile' && $confirm == 'yes' && $permissiontoupload) {
  125. $langs->load("other");
  126. $file = $upload_dir."/".GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  127. $ret = dol_delete_file($file);
  128. if ($ret) {
  129. setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
  130. } else {
  131. setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
  132. }
  133. $result = $ecmdir->changeNbOfFiles('-');
  134. }
  135. // Remove dir
  136. if ($action == 'confirm_deletedir' && $confirm == 'yes' && $permissiontoupload) {
  137. $backtourl = DOL_URL_ROOT."/ecm/index.php";
  138. if ($module == 'medias') {
  139. $backtourl = DOL_URL_ROOT."/website/index.php?file_manager=1";
  140. }
  141. $deletedirrecursive = (GETPOST('deletedirrecursive', 'alpha') == 'on' ? 1 : 0);
  142. if ($module == 'ecm' && $ecmdir->id > 0) { // If manual ECM and directory is indexed into database
  143. // Fetch was already done
  144. $result = $ecmdir->delete($user, 'all', $deletedirrecursive);
  145. if ($result <= 0) {
  146. $langs->load('errors');
  147. setEventMessages($langs->trans($ecmdir->error, $ecmdir->label), null, 'errors');
  148. }
  149. } else {
  150. if ($deletedirrecursive) {
  151. $resbool = dol_delete_dir_recursive($upload_dir, 0, 1);
  152. } else {
  153. $resbool = dol_delete_dir($upload_dir, 1);
  154. }
  155. if ($resbool) {
  156. $result = 1;
  157. } else {
  158. $langs->load('errors');
  159. setEventMessages($langs->trans("ErrorFailToDeleteDir", $upload_dir), null, 'errors');
  160. $result = 0;
  161. }
  162. }
  163. if ($result > 0) {
  164. header("Location: ".$backtourl);
  165. exit;
  166. }
  167. }
  168. // Update dirname or description
  169. if ($action == 'update' && !GETPOST('cancel', 'alpha') && $permissiontoadd) {
  170. $error = 0;
  171. if ($module == 'ecm') {
  172. $oldlabel = $ecmdir->label;
  173. $olddir = $ecmdir->getRelativePath(0);
  174. $olddir = $conf->ecm->dir_output.'/'.$olddir;
  175. } else {
  176. $olddir = GETPOST('section', 'alpha');
  177. $olddir = $conf->medias->multidir_output[$conf->entity].'/'.$relativepath;
  178. }
  179. if ($module == 'ecm') {
  180. $db->begin();
  181. // Fetch was already done
  182. $ecmdir->label = dol_sanitizeFileName(GETPOST("label"));
  183. $fk_parent = GETPOST("catParent", 'int');
  184. if ($fk_parent == "-1") {
  185. $ecmdir->fk_parent = "0";
  186. } else {
  187. $ecmdir->fk_parent = $fk_parent;
  188. }
  189. $ecmdir->description = GETPOST("description");
  190. $ret = $extrafields->setOptionalsFromPost(null, $ecmdir);
  191. if ($ret < 0) {
  192. $error++;
  193. }
  194. if (!$error) {
  195. // Actions on extra fields
  196. $result = $ecmdir->insertExtraFields();
  197. if ($result < 0) {
  198. setEventMessages($ecmdir->error, $ecmdir->errors, 'errors');
  199. $error++;
  200. }
  201. }
  202. $result = $ecmdir->update($user);
  203. if ($result > 0) {
  204. $newdir = $ecmdir->getRelativePath(1);
  205. $newdir = $conf->ecm->dir_output.'/'.$newdir;
  206. // Try to rename file if changed
  207. if (($oldlabel != $ecmdir->label && file_exists($olddir)) || ($olddir != $newdir && file_exists($olddir))) {
  208. $newdir = $ecmdir->getRelativePath(1); // return "xxx/zzz/" from ecm directory
  209. $newdir = $conf->ecm->dir_output.'/'.$newdir;
  210. //print $olddir.'-'.$newdir;
  211. $result = @rename($olddir, $newdir);
  212. if (!$result) {
  213. $langs->load('errors');
  214. setEventMessages($langs->trans('ErrorFailToRenameDir', $olddir, $newdir), null, 'errors');
  215. $error++;
  216. }
  217. }
  218. if (!$error) {
  219. $db->commit();
  220. // Set new value after renaming
  221. $relativepath = $ecmdir->getRelativePath();
  222. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  223. } else {
  224. $db->rollback();
  225. }
  226. } else {
  227. $db->rollback();
  228. setEventMessages($ecmdir->error, $ecmdir->errors, 'errors');
  229. }
  230. } else {
  231. $newdir = $conf->medias->multidir_output[$conf->entity].'/'.GETPOST('oldrelparentdir', 'alpha').'/'.GETPOST('label', 'alpha');
  232. $result = @rename($olddir, $newdir);
  233. if (!$result) {
  234. $langs->load('errors');
  235. setEventMessages($langs->trans('ErrorFailToRenameDir', $olddir, $newdir), null, 'errors');
  236. $error++;
  237. }
  238. if (!$error) {
  239. // Set new value after renaming
  240. $relativepath = GETPOST('oldrelparentdir', 'alpha').'/'.GETPOST('label', 'alpha');
  241. $upload_dir = $conf->medias->multidir_output[$conf->entity].'/'.$relativepath;
  242. $section = $relativepath;
  243. }
  244. }
  245. }
  246. /*
  247. * View
  248. */
  249. $form = new Form($db);
  250. $formecm = new FormEcm($db);
  251. $object = new EcmDirectory($db); // Need to create a new one instance
  252. $extrafields = new ExtraFields($db);
  253. // fetch optionals attributes and labels
  254. $extrafields->fetch_name_optionals_label($object->table_element);
  255. if ($module == 'ecm' && $ecmdir->id > 0) {
  256. $object->fetch($ecmdir->id);
  257. }
  258. llxHeader();
  259. // Built the file List
  260. $filearrayall = dol_dir_list($upload_dir, "all", 0, '', '', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
  261. $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
  262. $totalsize = 0;
  263. foreach ($filearray as $key => $file) {
  264. $totalsize += $file['size'];
  265. }
  266. $head = ecm_prepare_head($ecmdir, $module, $section);
  267. print dol_get_fiche_head($head, 'card', $langs->trans("ECMSectionManual"), -1, 'dir');
  268. if ($action == 'edit') {
  269. print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  270. print '<input type="hidden" name="token" value="'.newToken().'">';
  271. print '<input type="hidden" name="section" value="'.$section.'">';
  272. print '<input type="hidden" name="module" value="'.$module.'">';
  273. print '<input type="hidden" name="action" value="update">';
  274. }
  275. $morehtml = '';
  276. $morehtmlref = '/'.$module.'/'.$relativepath;
  277. if ($module == 'ecm') {
  278. $s = '';
  279. $result = 1;
  280. $i = 0;
  281. $tmpecmdir = new EcmDirectory($db); // Need to create a new one
  282. if ($ecmdir->id > 0) {
  283. $tmpecmdir->fetch($ecmdir->id);
  284. while ($tmpecmdir && $result > 0) {
  285. $tmpecmdir->ref = $tmpecmdir->label;
  286. $s = $tmpecmdir->getNomUrl(1).$s;
  287. if ($tmpecmdir->fk_parent) {
  288. $s = ' -> '.$s;
  289. $result = $tmpecmdir->fetch($tmpecmdir->fk_parent);
  290. } else {
  291. $tmpecmdir = 0;
  292. }
  293. $i++;
  294. }
  295. } else {
  296. $s .= join(' -> ', explode('/', $section));
  297. }
  298. $morehtmlref = '<a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> '.$s;
  299. }
  300. if ($module == 'medias') {
  301. $s = 'medias -> ';
  302. $result = 1;
  303. $subdirs = explode('/', $section);
  304. $i = 0;
  305. foreach ($subdirs as $subdir) {
  306. if ($i == (count($subdirs) - 1)) {
  307. if ($action == 'edit') {
  308. $s .= '<input type="text" name="label" class="minwidth300" maxlength="32" value="'.$subdir.'">';
  309. $s .= '<input type="hidden" name="oldrelparentdir" value="'.dirname($section).'">';
  310. $s .= '<input type="hidden" name="oldreldir" value="'.basename($section).'">';
  311. } else {
  312. $s .= $subdir;
  313. }
  314. }
  315. if ($i < (count($subdirs) - 1)) {
  316. $s .= $subdir.' -> ';
  317. }
  318. $i++;
  319. }
  320. $morehtmlref = $s;
  321. }
  322. dol_banner_tab($object, '', $morehtml, 0, '', '', $morehtmlref);
  323. print '<div class="fichecenter">';
  324. print '<div class="underbanner clearboth"></div>';
  325. print '<table class="border centpercent tableforfield">';
  326. /*print '<tr><td class="titlefield">'.$langs->trans("Ref").'</td><td>';
  327. print img_picto('','object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> ';
  328. print $s;
  329. print '</td></tr>';*/
  330. if ($module == 'ecm') {
  331. if ($action == 'edit') {
  332. print '<tr><td class="titlefield tdtop">'.$langs->trans("ECMDirName").'</td><td>';
  333. print '<input type="text" name="label" class="minwidth300" maxlength="32" value="'.$ecmdir->label.'">';
  334. print '</td></tr>';
  335. print '<tr><td class="titlefield tdtop">'.$langs->trans("ECMParentDirectory").'</td><td>';
  336. print $formecm->selectAllSections($ecmdir->fk_parent, '', 'ecm', array($ecmdir->id));
  337. print '</td><td>';
  338. print '</td></tr>';
  339. }
  340. print '<tr><td class="titlefield tdtop">'.$langs->trans("Description").'</td><td>';
  341. if ($action == 'edit') {
  342. print '<textarea class="flat quatrevingtpercent" name="description">';
  343. print $ecmdir->description;
  344. print '</textarea>';
  345. } else {
  346. print dol_nl2br($ecmdir->description);
  347. }
  348. print '</td></tr>';
  349. print '<tr><td class="titlefield">'.$langs->trans("ECMCreationUser").'</td><td>';
  350. if ($ecmdir->fk_user_c > 0) {
  351. $userecm = new User($db);
  352. $userecm->fetch($ecmdir->fk_user_c);
  353. print $userecm->getNomUrl(1);
  354. }
  355. print '</td></tr>';
  356. }
  357. print '<tr><td class="titlefield">'.$langs->trans("ECMCreationDate").'</td><td>';
  358. if ($module == 'ecm') {
  359. print dol_print_date($ecmdir->date_c, 'dayhour');
  360. } else {
  361. //var_dump($upload_dir);
  362. print dol_print_date(dol_filemtime($upload_dir), 'dayhour');
  363. }
  364. print '</td></tr>';
  365. print '<tr><td>'.$langs->trans("ECMDirectoryForFiles").'</td><td>';
  366. if ($module == 'ecm') {
  367. print '/ecm/'.$relativepath;
  368. } else {
  369. print '/'.$module.'/'.$relativepath;
  370. }
  371. print '</td></tr>';
  372. print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
  373. $nbofiles = count($filearray);
  374. print $nbofiles;
  375. if ($ecmdir->id > 0) {
  376. // Test if nb is same than in cache
  377. if ($nbofiles != $ecmdir->cachenbofdoc) {
  378. $ecmdir->changeNbOfFiles((string) $nbofiles);
  379. }
  380. }
  381. print '</td></tr>';
  382. print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
  383. print dol_print_size($totalsize);
  384. print '</td></tr>';
  385. print $object->showOptionals($extrafields, ($action == 'edit' ? 'edit' : 'view'));
  386. print '</table>';
  387. if ($action == 'edit') {
  388. print $form->buttonsSaveCancel();
  389. }
  390. print '</div>';
  391. if ($action == 'edit') {
  392. print '</form>';
  393. }
  394. print dol_get_fiche_end();
  395. // Actions buttons
  396. if ($action != 'edit' && $action != 'delete' && $action != 'deletefile') {
  397. print '<div class="tabsAction">';
  398. if ($permissiontoadd) {
  399. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&token='.newToken().($module ? '&module='.$module : '').'&section='.$section.'">'.$langs->trans('Edit').'</a>';
  400. }
  401. if ($permissiontoadd) {
  402. print '<a class="butAction" href="'.DOL_URL_ROOT.'/ecm/dir_add_card.php?action=create&token='.newToken().($module ? '&module='.$module : '').'&catParent='.$section.'">'.$langs->trans('ECMAddSection').'</a>';
  403. } else {
  404. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('ECMAddSection').'</a>';
  405. }
  406. print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken().($module ? '&module='.urlencode($module) : '').'&section='.urlencode($section).($backtopage ? '&backtopage='.urlencode($backtopage) : ''), '', $permissiontoadd);
  407. print '</div>';
  408. }
  409. // Confirm remove file
  410. if ($action == 'deletefile') {
  411. print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.urlencode(GETPOST("section", 'alpha')).'&urlfile='.urlencode(GETPOST("urlfile")).($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile');
  412. }
  413. // Confirm remove dir
  414. if ($action == 'delete' || $action == 'delete_dir') {
  415. $relativepathwithoutslash = preg_replace('/[\/]$/', '', $relativepath);
  416. //Form to close proposal (signed or not)
  417. if (count($filearrayall) > 0) {
  418. $langs->load("other");
  419. $formquestion = array(
  420. array('type' => 'checkbox', 'name' => 'deletedirrecursive', 'label' => $langs->trans("ContentOfDirectoryIsNotEmpty").'<br>'.$langs->trans("DeleteAlsoContentRecursively"), 'value' => '0') // Field to complete private note (not replace)
  421. );
  422. }
  423. print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.urlencode(GETPOST('section', 'alpha')).($module ? '&module='.$module : '').($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', $relativepathwithoutslash), 'confirm_deletedir', $formquestion, 1, 1);
  424. }
  425. /*
  426. $formfile=new FormFile($db);
  427. // Display upload form
  428. if ($user->rights->ecm->upload)
  429. {
  430. $formfile->form_attach_new_file(DOL_URL_ROOT.'/ecm/dir_card.php','',0,$section);
  431. }
  432. // List of document
  433. if ($user->rights->ecm->read)
  434. {
  435. $param='&amp;section='.$section;
  436. $formfile->list_of_documents($filearray,'','ecm',$param,1,$relativepath,$user->rights->ecm->upload);
  437. }
  438. */
  439. // End of page
  440. llxFooter();
  441. $db->close();