dir_card.php 17 KB

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