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