dir_card.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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/dir_card.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 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)) $module='ecm';
  38. // Get parameters
  39. $sortfield = GETPOST("sortfield",'alpha');
  40. $sortorder = GETPOST("sortorder",'alpha');
  41. $page = GETPOST("page",'int');
  42. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  43. $offset = $conf->liste_limit * $page;
  44. $pageprev = $page - 1;
  45. $pagenext = $page + 1;
  46. if (! $sortorder) $sortorder="ASC";
  47. if (! $sortfield) $sortfield="name";
  48. $section=GETPOST("section", 'alpha')?GETPOST("section", 'alpha'):GETPOST("relativedir", 'alpha');
  49. if (! $section)
  50. {
  51. dol_print_error('',"ErrorSectionParamNotDefined");
  52. exit;
  53. }
  54. // Load ecm object
  55. $ecmdir = new EcmDirectory($db);
  56. if ($module == 'ecm')
  57. {
  58. $result=$ecmdir->fetch($section);
  59. if (! $result > 0)
  60. {
  61. dol_print_error($db, $ecmdir->error);
  62. exit;
  63. }
  64. $relativepath=$ecmdir->getRelativePath();
  65. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  66. }
  67. else // For example $module == 'medias'
  68. {
  69. $relativepath = $section;
  70. $upload_dir = $conf->medias->multidir_output[$conf->entity].'/'.$relativepath;
  71. }
  72. // Permissions
  73. $permtoadd = 0;
  74. $permtoupload = 0;
  75. if ($module == 'ecm')
  76. {
  77. $permtoadd = $user->rights->ecm->setup;
  78. $permtoupload = $user->rights->ecm->upload;
  79. }
  80. if ($module == 'medias')
  81. {
  82. $permtoadd = ($user->rights->mailing->creer || $user->rights->website->write);
  83. $permtoupload = ($user->rights->mailing->creer || $user->rights->website->write);
  84. }
  85. /*
  86. * Actions
  87. */
  88. // Upload file
  89. if (GETPOST("sendit") && ! empty($conf->global->MAIN_UPLOAD_DOC))
  90. {
  91. if (dol_mkdir($upload_dir) >= 0)
  92. {
  93. $resupload = dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir . "/" . dol_unescapefile($_FILES['userfile']['name']),0,0,$_FILES['userfile']['error']);
  94. if (is_numeric($resupload) && $resupload > 0)
  95. {
  96. $result=$ecmdir->changeNbOfFiles('+');
  97. }
  98. else
  99. {
  100. $langs->load("errors");
  101. if ($resupload < 0) // Unknown error
  102. {
  103. setEventMessages($langs->trans("ErrorFileNotUploaded"), null, 'errors');
  104. }
  105. else if (preg_match('/ErrorFileIsInfectedWithAVirus/',$resupload)) // Files infected by a virus
  106. {
  107. setEventMessages($langs->trans("ErrorFileIsInfectedWithAVirus"), null, 'errors');
  108. }
  109. else // Known error
  110. {
  111. setEventMessages($langs->trans($resupload), null, 'errors');
  112. }
  113. }
  114. }
  115. else
  116. {
  117. // Failed transfer (exceeding the limit file?)
  118. $langs->load("errors");
  119. setEventMessages($langs->trans("ErrorFailToCreateDir",$upload_dir), null, 'errors');
  120. }
  121. }
  122. // Remove file
  123. if ($action == 'confirm_deletefile' && $confirm == 'yes')
  124. {
  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) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
  129. else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
  130. $result=$ecmdir->changeNbOfFiles('-');
  131. }
  132. // Remove dir
  133. if ($action == 'confirm_deletedir' && $confirm == 'yes')
  134. {
  135. $backtourl = DOL_URL_ROOT."/ecm/index.php";
  136. if ($module == 'medias') $backtourl = DOL_URL_ROOT."/website/index.php?file_manager=1";
  137. $deletedirrecursive = (GETPOST('deletedirrecursive','alpha') == 'on' ? 1 : 0);
  138. if ($module == 'ecm')
  139. {
  140. // Fetch was already done
  141. $result=$ecmdir->delete($user, 'all', $deletedirrecursive);
  142. if ($result <= 0)
  143. {
  144. $langs->load('errors');
  145. setEventMessages($langs->trans($ecmdir->error,$ecmdir->label), null, 'errors');
  146. }
  147. }
  148. else
  149. {
  150. if ($deletedirrecursive)
  151. {
  152. $resbool = dol_delete_dir_recursive($upload_dir, 0, 1);
  153. }
  154. else
  155. {
  156. $resbool = dol_delete_dir($upload_dir, 1);
  157. }
  158. if ($resbool) $result = 1;
  159. else
  160. {
  161. $langs->load('errors');
  162. setEventMessages($langs->trans("ErrorFailToDeleteDir", $upload_dir), null, 'errors');
  163. $result = 0;
  164. }
  165. }
  166. if ($result > 0)
  167. {
  168. header("Location: ".$backtourl);
  169. exit;
  170. }
  171. }
  172. // Update dirname or description
  173. if ($action == 'update' && ! GETPOST('cancel','alpha'))
  174. {
  175. $error=0;
  176. if ($module == 'ecm')
  177. {
  178. $oldlabel=$ecmdir->label;
  179. $olddir=$ecmdir->getRelativePath(0);
  180. $olddir=$conf->ecm->dir_output.'/'.$olddir;
  181. }
  182. else
  183. {
  184. $olddir=GETPOST('section','alpha');
  185. $olddir=$conf->medias->multidir_output[$conf->entity].'/'.$relativepath;
  186. }
  187. if ($module == 'ecm')
  188. {
  189. $db->begin();
  190. // Fetch was already done
  191. $ecmdir->label = dol_sanitizeFileName(GETPOST("label"));
  192. $ecmdir->description = GETPOST("description");
  193. $result=$ecmdir->update($user);
  194. if ($result > 0)
  195. {
  196. // Try to rename file if changed
  197. if ($oldlabel != $ecmdir->label && file_exists($olddir))
  198. {
  199. $newdir=$ecmdir->getRelativePath(1); // return "xxx/zzz/" from ecm directory
  200. $newdir=$conf->ecm->dir_output.'/'.$newdir;
  201. //print $olddir.'-'.$newdir;
  202. $result=@rename($olddir,$newdir);
  203. if (! $result)
  204. {
  205. $langs->load('errors');
  206. setEventMessages($langs->trans('ErrorFailToRenameDir',$olddir,$newdir), null, 'errors');
  207. $error++;
  208. }
  209. }
  210. if (! $error)
  211. {
  212. $db->commit();
  213. // Set new value after renaming
  214. $relativepath=$ecmdir->getRelativePath();
  215. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  216. }
  217. else
  218. {
  219. $db->rollback();
  220. }
  221. }
  222. else
  223. {
  224. $db->rollback();
  225. setEventMessages($ecmdir->error, $ecmdir->errors, 'errors');
  226. }
  227. }
  228. else
  229. {
  230. $newdir = $conf->medias->multidir_output[$conf->entity].'/'.GETPOST('oldrelparentdir','alpha').'/'.GETPOST('label','alpha');
  231. $result=@rename($olddir,$newdir);
  232. if (! $result)
  233. {
  234. $langs->load('errors');
  235. setEventMessages($langs->trans('ErrorFailToRenameDir',$olddir,$newdir), null, 'errors');
  236. $error++;
  237. }
  238. if (! $error)
  239. {
  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. $object=new EcmDirectory($db); // Need to create a new one instance
  252. if ($module == 'ecm')
  253. {
  254. $object->fetch($ecmdir->id);
  255. }
  256. llxHeader();
  257. // Built the file List
  258. $filearrayall=dol_dir_list($upload_dir,"all",0,'','',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
  259. $filearray=dol_dir_list($upload_dir,"files",0,'','(\.meta|_preview.*\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
  260. $totalsize=0;
  261. foreach($filearray as $key => $file)
  262. {
  263. $totalsize+=$file['size'];
  264. }
  265. $head = ecm_prepare_head($ecmdir, $module, $section);
  266. dol_fiche_head($head, 'card', $langs->trans("ECMSectionManual"), -1, 'dir');
  267. if ($action == 'edit')
  268. {
  269. print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  270. print '<input type="hidden" name="token" value="'.$_SESSION['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. {
  279. $s='';
  280. $result = 1;
  281. $i=0;
  282. $tmpecmdir=new EcmDirectory($db); // Need to create a new one
  283. $tmpecmdir->fetch($ecmdir->id);
  284. while ($tmpecmdir && $result > 0)
  285. {
  286. $tmpecmdir->ref=$tmpecmdir->label;
  287. if ($i == 0 && $action == 'edit')
  288. {
  289. $s='<input type="text" name="label" class="minwidth300" maxlength="32" value="'.$tmpecmdir->label.'">';
  290. }
  291. else $s=$tmpecmdir->getNomUrl(1).$s;
  292. if ($tmpecmdir->fk_parent)
  293. {
  294. $s=' -> '.$s;
  295. $result=$tmpecmdir->fetch($tmpecmdir->fk_parent);
  296. }
  297. else
  298. {
  299. $tmpecmdir=0;
  300. }
  301. $i++;
  302. }
  303. $morehtmlref = '<a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> '.$s;
  304. }
  305. if ($module == 'medias')
  306. {
  307. $s='medias -> ';
  308. $result = 1;
  309. $subdirs=explode('/', $section);
  310. $i=0;
  311. foreach($subdirs as $subdir)
  312. {
  313. if ($i == (count($subdirs) - 1))
  314. {
  315. if ($action == 'edit')
  316. {
  317. $s.='<input type="text" name="label" class="minwidth300" maxlength="32" value="'.$subdir.'">';
  318. $s.='<input type="hidden" name="oldrelparentdir" value="'.dirname($section).'">';
  319. $s.='<input type="hidden" name="oldreldir" value="'.basename($section).'">';
  320. }
  321. else $s.=$subdir;
  322. }
  323. if ($i < (count($subdirs) - 1))
  324. {
  325. $s.=$subdir.' -> ';
  326. }
  327. $i++;
  328. }
  329. $morehtmlref = $s;
  330. }
  331. dol_banner_tab($object, '', $morehtml, 0, '', '', $morehtmlref);
  332. print '<div class="fichecenter">';
  333. print '<div class="underbanner clearboth"></div>';
  334. print '<table class="border centpercent">';
  335. /*print '<tr><td class="titlefield">'.$langs->trans("Ref").'</td><td>';
  336. print img_picto('','object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> ';
  337. print $s;
  338. print '</td></tr>';*/
  339. if ($module == 'ecm')
  340. {
  341. print '<tr><td class="titlefield tdtop">'.$langs->trans("Description").'</td><td>';
  342. if ($action == 'edit')
  343. {
  344. print '<textarea class="flat quatrevingtpercent" name="description">';
  345. print $ecmdir->description;
  346. print '</textarea>';
  347. }
  348. else print dol_nl2br($ecmdir->description);
  349. print '</td></tr>';
  350. print '<tr><td class="titlefield">'.$langs->trans("ECMCreationUser").'</td><td>';
  351. $userecm=new User($db);
  352. $userecm->fetch($ecmdir->fk_user_c);
  353. print $userecm->getNomUrl(1);
  354. print '</td></tr>';
  355. }
  356. print '<tr><td class="titlefield">'.$langs->trans("ECMCreationDate").'</td><td>';
  357. if ($module == 'ecm')
  358. {
  359. print dol_print_date($ecmdir->date_c,'dayhour');
  360. }
  361. else
  362. {
  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. {
  370. print '/ecm/'.$relativepath;
  371. }
  372. else
  373. {
  374. print '/'.$module.'/'.$relativepath;
  375. }
  376. print '</td></tr>';
  377. print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
  378. $nbofiles=count($filearray);
  379. print $nbofiles;
  380. if ($ecmdir->id > 0)
  381. {
  382. // Test if nb is same than in cache
  383. if ($nbofiles != $ecmdir->cachenbofdoc)
  384. {
  385. $ecmdir->changeNbOfFiles((string) $nbofiles);
  386. }
  387. }
  388. print '</td></tr>';
  389. print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
  390. print dol_print_size($totalsize);
  391. print '</td></tr>';
  392. print '</table>';
  393. if ($action == 'edit')
  394. {
  395. print '<br><div align="center">';
  396. print '<input type="submit" class="button" name="submit" value="'.$langs->trans("Save").'">';
  397. print ' &nbsp; &nbsp; ';
  398. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  399. print '</div>';
  400. }
  401. print '</div>';
  402. if ($action == 'edit')
  403. {
  404. print '</form>';
  405. }
  406. dol_fiche_end();
  407. // Actions buttons
  408. if ($action != 'edit' && $action != 'delete')
  409. {
  410. print '<div class="tabsAction">';
  411. if ($permtoadd)
  412. {
  413. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit'.($module?'&module='.$module:'').'&section='.$section.'">'.$langs->trans('Edit').'</a>';
  414. }
  415. if ($permtoadd)
  416. {
  417. print '<a class="butAction" href="'.DOL_URL_ROOT.'/ecm/dir_add_card.php?action=create'.($module?'&module='.$module:'').'&catParent='.$section.'">'.$langs->trans('ECMAddSection').'</a>';
  418. }
  419. else
  420. {
  421. print '<a class="butActionRefused" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('ECMAddSection').'</a>';
  422. }
  423. //if (count($filearrayall) == 0)
  424. //{
  425. if ($permtoadd)
  426. {
  427. print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete_dir'.($module?'&module='.$module:'').'&section='.$section.'">'.$langs->trans('Delete').'</a>';
  428. }
  429. else
  430. {
  431. print '<a class="butActionDeleteRefused" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('Delete').'</a>';
  432. }
  433. /*}
  434. else
  435. {
  436. if (count($filearray) > 0)
  437. print '<a class="butActionRefused" href="#" title="'.$langs->trans("CannotRemoveDirectoryContainsFiles").'">'.$langs->trans('Delete').'</a>';
  438. else
  439. print '<a class="butActionRefused" href="#" title="'.$langs->trans("CannotRemoveDirectoryContainsFilesOrDirs").'">'.$langs->trans('Delete').'</a>';
  440. }*/
  441. print '</div>';
  442. }
  443. // Confirm remove file
  444. if ($action == 'delete')
  445. {
  446. print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.GETPOST("section",'alpha').'&urlfile='.urlencode($_GET["urlfile"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile');
  447. }
  448. // Confirm remove file
  449. if ($action == 'delete_dir')
  450. {
  451. $relativepathwithoutslash=preg_replace('/[\/]$/','',$relativepath);
  452. //Form to close proposal (signed or not)
  453. if (count($filearrayall) > 0)
  454. {
  455. $langs->load("other");
  456. $formquestion = array(
  457. array('type' => 'checkbox', 'name' => 'deletedirrecursive', 'label' => $langs->trans("ContentOfDirectoryIsNotEmpty").'<br>'.$langs->trans("DeleteAlsoContentRecursively"),'value' => '0') // Field to complete private note (not replace)
  458. );
  459. }
  460. print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.GETPOST('section','alpha').($module?'&module='.$module:''), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection',$relativepathwithoutslash), 'confirm_deletedir', $formquestion, 1, 1);
  461. }
  462. /*
  463. $formfile=new FormFile($db);
  464. // Display upload form
  465. if ($user->rights->ecm->upload)
  466. {
  467. $formfile->form_attach_new_file(DOL_URL_ROOT.'/ecm/dir_card.php','',0,$section);
  468. }
  469. // List of document
  470. if ($user->rights->ecm->read)
  471. {
  472. $param='&amp;section='.$section;
  473. $formfile->list_of_documents($filearray,'','ecm',$param,1,$relativepath,$user->rights->ecm->upload);
  474. }
  475. */
  476. // End of page
  477. llxFooter();
  478. $db->close();