dolibarr  9.0.0
dir_card.php
Go to the documentation of this file.
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 
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
31 
32 // Load translation files required by page
33 $langs->loadLangs(array('ecm', 'companies', 'other'));
34 
35 $action = GETPOST('action','alpha');
36 $cancel = GETPOST('cancel', 'aZ09');
37 $backtopage = GETPOST('backtopage', 'alpha');
38 $confirm = GETPOST('confirm','alpha');
39 
40 $module = GETPOST('module', 'alpha');
41 $website = GETPOST('website', 'alpha');
42 $pageid = GETPOST('pageid', 'int');
43 if (empty($module)) $module='ecm';
44 
45 // Get parameters
46 $sortfield = GETPOST("sortfield",'alpha');
47 $sortorder = GETPOST("sortorder",'alpha');
48 $page = GETPOST("page",'int');
49 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
50 $offset = $conf->liste_limit * $page;
51 $pageprev = $page - 1;
52 $pagenext = $page + 1;
53 if (! $sortorder) $sortorder="ASC";
54 if (! $sortfield) $sortfield="name";
55 
56 $section=GETPOST("section", 'alpha')?GETPOST("section", 'alpha'):GETPOST("relativedir", 'alpha');
57 if (! $section)
58 {
59  dol_print_error('',"ErrorSectionParamNotDefined");
60  exit;
61 }
62 
63 // Load ecm object
64 $ecmdir = new EcmDirectory($db);
65 
66 if ($module == 'ecm')
67 {
68  $result=$ecmdir->fetch($section);
69  if (! $result > 0)
70  {
71  dol_print_error($db, $ecmdir->error);
72  exit;
73  }
74 
75  $relativepath=$ecmdir->getRelativePath();
76  $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
77 }
78 else // For example $module == 'medias'
79 {
80  $relativepath = $section;
81  $upload_dir = $conf->medias->multidir_output[$conf->entity].'/'.$relativepath;
82 }
83 
84 // Permissions
85 $permtoadd = 0;
86 $permtoupload = 0;
87 if ($module == 'ecm')
88 {
89  $permtoadd = $user->rights->ecm->setup;
90  $permtoupload = $user->rights->ecm->upload;
91 }
92 if ($module == 'medias')
93 {
94  $permtoadd = ($user->rights->mailing->creer || $user->rights->website->write);
95  $permtoupload = ($user->rights->mailing->creer || $user->rights->website->write);
96 }
97 
98 
99 
100 /*
101  * Actions
102  */
103 
104 // Upload file
105 if (GETPOST("sendit") && ! empty($conf->global->MAIN_UPLOAD_DOC))
106 {
107  if (dol_mkdir($upload_dir) >= 0)
108  {
109  $resupload = dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir . "/" . dol_unescapefile($_FILES['userfile']['name']),0,0,$_FILES['userfile']['error']);
110  if (is_numeric($resupload) && $resupload > 0)
111  {
112  $result=$ecmdir->changeNbOfFiles('+');
113  }
114  else
115  {
116  $langs->load("errors");
117  if ($resupload < 0) // Unknown error
118  {
119  setEventMessages($langs->trans("ErrorFileNotUploaded"), null, 'errors');
120  }
121  else if (preg_match('/ErrorFileIsInfectedWithAVirus/',$resupload)) // Files infected by a virus
122  {
123  setEventMessages($langs->trans("ErrorFileIsInfectedWithAVirus"), null, 'errors');
124  }
125  else // Known error
126  {
127  setEventMessages($langs->trans($resupload), null, 'errors');
128  }
129  }
130  }
131  else
132  {
133  // Failed transfer (exceeding the limit file?)
134  $langs->load("errors");
135  setEventMessages($langs->trans("ErrorFailToCreateDir",$upload_dir), null, 'errors');
136  }
137 }
138 
139 // Remove file
140 if ($action == 'confirm_deletefile' && $confirm == 'yes')
141 {
142  $langs->load("other");
143  $file = $upload_dir . "/" . GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
144  $ret=dol_delete_file($file);
145  if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
146  else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
147 
148  $result=$ecmdir->changeNbOfFiles('-');
149 }
150 
151 // Remove dir
152 if ($action == 'confirm_deletedir' && $confirm == 'yes')
153 {
154  $backtourl = DOL_URL_ROOT."/ecm/index.php";
155  if ($module == 'medias') $backtourl = DOL_URL_ROOT."/website/index.php?file_manager=1";
156 
157  $deletedirrecursive = (GETPOST('deletedirrecursive','alpha') == 'on' ? 1 : 0);
158 
159  if ($module == 'ecm')
160  {
161  // Fetch was already done
162  $result=$ecmdir->delete($user, 'all', $deletedirrecursive);
163  if ($result <= 0)
164  {
165  $langs->load('errors');
166  setEventMessages($langs->trans($ecmdir->error,$ecmdir->label), null, 'errors');
167  }
168  }
169  else
170  {
171  if ($deletedirrecursive)
172  {
173  $resbool = dol_delete_dir_recursive($upload_dir, 0, 1);
174  }
175  else
176  {
177  $resbool = dol_delete_dir($upload_dir, 1);
178  }
179  if ($resbool) $result = 1;
180  else
181  {
182  $langs->load('errors');
183  setEventMessages($langs->trans("ErrorFailToDeleteDir", $upload_dir), null, 'errors');
184  $result = 0;
185  }
186  }
187  if ($result > 0)
188  {
189  header("Location: ".$backtourl);
190  exit;
191  }
192 }
193 
194 // Update dirname or description
195 if ($action == 'update' && ! GETPOST('cancel','alpha'))
196 {
197  $error=0;
198 
199  if ($module == 'ecm')
200  {
201  $oldlabel=$ecmdir->label;
202  $olddir=$ecmdir->getRelativePath(0);
203  $olddir=$conf->ecm->dir_output.'/'.$olddir;
204  }
205  else
206  {
207  $olddir=GETPOST('section','alpha');
208  $olddir=$conf->medias->multidir_output[$conf->entity].'/'.$relativepath;
209  }
210 
211  if ($module == 'ecm')
212  {
213  $db->begin();
214 
215  // Fetch was already done
216  $ecmdir->label = dol_sanitizeFileName(GETPOST("label"));
217  $ecmdir->description = GETPOST("description");
218  $result=$ecmdir->update($user);
219  if ($result > 0)
220  {
221  // Try to rename file if changed
222  if ($oldlabel != $ecmdir->label && file_exists($olddir))
223  {
224  $newdir=$ecmdir->getRelativePath(1); // return "xxx/zzz/" from ecm directory
225  $newdir=$conf->ecm->dir_output.'/'.$newdir;
226  //print $olddir.'-'.$newdir;
227  $result=@rename($olddir,$newdir);
228  if (! $result)
229  {
230  $langs->load('errors');
231  setEventMessages($langs->trans('ErrorFailToRenameDir',$olddir,$newdir), null, 'errors');
232  $error++;
233  }
234  }
235 
236  if (! $error)
237  {
238  $db->commit();
239 
240  // Set new value after renaming
241  $relativepath=$ecmdir->getRelativePath();
242  $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
243  }
244  else
245  {
246  $db->rollback();
247  }
248  }
249  else
250  {
251  $db->rollback();
252  setEventMessages($ecmdir->error, $ecmdir->errors, 'errors');
253  }
254  }
255  else
256  {
257  $newdir = $conf->medias->multidir_output[$conf->entity].'/'.GETPOST('oldrelparentdir','alpha').'/'.GETPOST('label','alpha');
258 
259  $result=@rename($olddir,$newdir);
260  if (! $result)
261  {
262  $langs->load('errors');
263  setEventMessages($langs->trans('ErrorFailToRenameDir',$olddir,$newdir), null, 'errors');
264  $error++;
265  }
266 
267  if (! $error)
268  {
269  // Set new value after renaming
270  $relativepath=GETPOST('oldrelparentdir','alpha').'/'.GETPOST('label','alpha');
271  $upload_dir = $conf->medias->multidir_output[$conf->entity].'/'.$relativepath;
272  $section = $relativepath;
273  }
274  }
275 }
276 
277 
278 
279 /*******************************************************************
280 * View
281 ********************************************************************/
282 
283 $form=new Form($db);
284 
285 $object=new EcmDirectory($db); // Need to create a new one instance
286 
287 if ($module == 'ecm')
288 {
289  $object->fetch($ecmdir->id);
290 }
291 
292 llxHeader();
293 
294 // Built the file List
295 $filearrayall=dol_dir_list($upload_dir,"all",0,'','',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
296 $filearray=dol_dir_list($upload_dir,"files",0,'','(\.meta|_preview.*\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
297 $totalsize=0;
298 foreach($filearray as $key => $file)
299 {
300  $totalsize+=$file['size'];
301 }
302 
303 
304 $head = ecm_prepare_head($ecmdir, $module, $section);
305 dol_fiche_head($head, 'card', $langs->trans("ECMSectionManual"), -1, 'dir');
306 
307 
308 if ($action == 'edit')
309 {
310  print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
311  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
312  print '<input type="hidden" name="section" value="'.$section.'">';
313  print '<input type="hidden" name="module" value="'.$module.'">';
314  print '<input type="hidden" name="action" value="update">';
315 }
316 
317 
318 $morehtml='';
319 
320 $morehtmlref = '/'.$module.'/'.$relativepath;
321 
322 if ($module == 'ecm')
323 {
324  $s='';
325  $result = 1;
326  $i=0;
327  $tmpecmdir=new EcmDirectory($db); // Need to create a new one
328  $tmpecmdir->fetch($ecmdir->id);
329  while ($tmpecmdir && $result > 0)
330  {
331  $tmpecmdir->ref=$tmpecmdir->label;
332  if ($i == 0 && $action == 'edit')
333  {
334  $s='<input type="text" name="label" class="minwidth300" maxlength="32" value="'.$tmpecmdir->label.'">';
335  }
336  else $s=$tmpecmdir->getNomUrl(1).$s;
337  if ($tmpecmdir->fk_parent)
338  {
339  $s=' -> '.$s;
340  $result=$tmpecmdir->fetch($tmpecmdir->fk_parent);
341  }
342  else
343  {
344  $tmpecmdir=0;
345  }
346  $i++;
347  }
348 
349  $morehtmlref = '<a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> '.$s;
350 }
351 if ($module == 'medias')
352 {
353  $s='medias -> ';
354  $result = 1;
355  $subdirs=explode('/', $section);
356  $i=0;
357  foreach($subdirs as $subdir)
358  {
359  if ($i == (count($subdirs) - 1))
360  {
361  if ($action == 'edit')
362  {
363  $s.='<input type="text" name="label" class="minwidth300" maxlength="32" value="'.$subdir.'">';
364  $s.='<input type="hidden" name="oldrelparentdir" value="'.dirname($section).'">';
365  $s.='<input type="hidden" name="oldreldir" value="'.basename($section).'">';
366  }
367  else $s.=$subdir;
368  }
369  if ($i < (count($subdirs) - 1))
370  {
371  $s.=$subdir.' -> ';
372  }
373  $i++;
374  }
375 
376  $morehtmlref = $s;
377 }
378 
379 
380 dol_banner_tab($object, '', $morehtml, 0, '', '', $morehtmlref);
381 
382 print '<div class="fichecenter">';
383 
384 print '<div class="underbanner clearboth"></div>';
385 print '<table class="border centpercent">';
386 /*print '<tr><td class="titlefield">'.$langs->trans("Ref").'</td><td>';
387 print img_picto('','object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> ';
388 print $s;
389 print '</td></tr>';*/
390 if ($module == 'ecm')
391 {
392  print '<tr><td class="titlefield tdtop">'.$langs->trans("Description").'</td><td>';
393  if ($action == 'edit')
394  {
395  print '<textarea class="flat quatrevingtpercent" name="description">';
396  print $ecmdir->description;
397  print '</textarea>';
398  }
399  else print dol_nl2br($ecmdir->description);
400  print '</td></tr>';
401 
402  print '<tr><td class="titlefield">'.$langs->trans("ECMCreationUser").'</td><td>';
403  $userecm=new User($db);
404  $userecm->fetch($ecmdir->fk_user_c);
405  print $userecm->getNomUrl(1);
406  print '</td></tr>';
407 }
408 print '<tr><td class="titlefield">'.$langs->trans("ECMCreationDate").'</td><td>';
409 if ($module == 'ecm')
410 {
411  print dol_print_date($ecmdir->date_c,'dayhour');
412 }
413 else
414 {
415  //var_dump($upload_dir);
416  print dol_print_date(dol_filemtime($upload_dir), 'dayhour');
417 }
418 print '</td></tr>';
419 print '<tr><td>'.$langs->trans("ECMDirectoryForFiles").'</td><td>';
420 if ($module == 'ecm')
421 {
422  print '/ecm/'.$relativepath;
423 }
424 else
425 {
426  print '/'.$module.'/'.$relativepath;
427 }
428 print '</td></tr>';
429 print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
430 $nbofiles=count($filearray);
431 print $nbofiles;
432 if ($ecmdir->id > 0)
433 {
434  // Test if nb is same than in cache
435  if ($nbofiles != $ecmdir->cachenbofdoc)
436  {
437  $ecmdir->changeNbOfFiles((string) $nbofiles);
438  }
439 }
440 print '</td></tr>';
441 print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
442 print dol_print_size($totalsize);
443 print '</td></tr>';
444 print '</table>';
445 
446 if ($action == 'edit')
447 {
448  print '<br><div align="center">';
449  print '<input type="submit" class="button" name="submit" value="'.$langs->trans("Save").'">';
450  print ' &nbsp; &nbsp; ';
451  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
452  print '</div>';
453 }
454 
455 print '</div>';
456 if ($action == 'edit')
457 {
458  print '</form>';
459 }
460 
461 dol_fiche_end();
462 
463 
464 
465 // Actions buttons
466 if ($action != 'edit' && $action != 'delete')
467 {
468  print '<div class="tabsAction">';
469 
470  if ($permtoadd)
471  {
472  print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit'.($module?'&module='.$module:'').'&section='.$section.'">'.$langs->trans('Edit').'</a>';
473  }
474 
475  if ($permtoadd)
476  {
477  print '<a class="butAction" href="'.DOL_URL_ROOT.'/ecm/dir_add_card.php?action=create'.($module?'&module='.$module:'').'&catParent='.$section.'">'.$langs->trans('ECMAddSection').'</a>';
478  }
479  else
480  {
481  print '<a class="butActionRefused" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('ECMAddSection').'</a>';
482  }
483 
484  //if (count($filearrayall) == 0)
485  //{
486  if ($permtoadd)
487  {
488  print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete_dir'.($module?'&module='.$module:'').'&section='.$section.'">'.$langs->trans('Delete').'</a>';
489  }
490  else
491  {
492  print '<a class="butActionDeleteRefused" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('Delete').'</a>';
493  }
494  /*}
495  else
496  {
497  if (count($filearray) > 0)
498  print '<a class="butActionRefused" href="#" title="'.$langs->trans("CannotRemoveDirectoryContainsFiles").'">'.$langs->trans('Delete').'</a>';
499  else
500  print '<a class="butActionRefused" href="#" title="'.$langs->trans("CannotRemoveDirectoryContainsFilesOrDirs").'">'.$langs->trans('Delete').'</a>';
501  }*/
502  print '</div>';
503 }
504 
505 // Confirm remove file
506 if ($action == 'delete')
507 {
508  print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.GETPOST("section",'alpha').'&urlfile='.urlencode($_GET["urlfile"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile');
509 }
510 
511 // Confirm remove file
512 if ($action == 'delete_dir')
513 {
514  $relativepathwithoutslash=preg_replace('/[\/]$/','',$relativepath);
515 
516  //Form to close proposal (signed or not)
517  if (count($filearrayall) > 0)
518  {
519  $langs->load("other");
520  $formquestion = array(
521  array('type' => 'checkbox', 'name' => 'deletedirrecursive', 'label' => $langs->trans("ContentOfDirectoryIsNotEmpty").'<br>'.$langs->trans("DeleteAlsoContentRecursively"),'value' => '0') // Field to complete private note (not replace)
522  );
523  }
524 
525  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);
526 }
527 
528 
529 /*
530 $formfile=new FormFile($db);
531 
532 // Display upload form
533 if ($user->rights->ecm->upload)
534 {
535  $formfile->form_attach_new_file(DOL_URL_ROOT.'/ecm/dir_card.php','',0,$section);
536 }
537 
538 // List of document
539 if ($user->rights->ecm->read)
540 {
541  $param='&amp;section='.$section;
542  $formfile->list_of_documents($filearray,'','ecm',$param,1,$relativepath,$user->rights->ecm->upload);
543 }
544 */
545 
546 // End of page
547 llxFooter();
548 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
ecm_prepare_head($object, $module='ecm', $section='')
Prepare array with list of tabs.
Definition: ecm.lib.php:75
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_unescapefile($filename)
Unescape a file submitted by upload.
Definition: files.lib.php:942
print
Draft customers invoices.
Definition: index.php:91
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile')
Make control on an uploaded file from an GUI page and move it to final destination.
Definition: files.lib.php:996
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Class to manage Dolibarr users.
Definition: user.class.php:41
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to manage generation of HTML components Only common components must be here.
dol_fiche_end($notab=0)
Show tab footer of a card.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
Definition: files.lib.php:1273
llxHeader()
Empty header.
Definition: wrapper.php:44
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
Definition: files.lib.php:1139
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:59
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_size($size, $shortvalue=0, $shortunit=0)
Return string with formated size.
dol_filemtime($pathoffile)
Return time of a file.
Definition: files.lib.php:564
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
dol_delete_dir($dir, $nophperrors=0)
Remove a directory (not recursive, so content must be empty).
Definition: files.lib.php:1249
Class to manage ECM directories.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.