dir_card.php 16 KB

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