dir_card.php 16 KB

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