dolibarr  7.0.0-beta
index_auto.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2008-2014 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2008-2010 Regis Houssin <regis.houssin@capnetworks.com>
4  * Copyright (C) 2016 Alexandre Spangaro <aspangaro@zendsi.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
32 
33 // Load traductions files
34 $langs->loadLangs(array("ecm","companies","other","users","orders","propal","bills","contracts"));
35 
36 // Security check
37 if ($user->societe_id) $socid=$user->societe_id;
38 $result = restrictedArea($user, 'ecm', 0);
39 
40 // Get parameters
41 $socid=GETPOST('socid','int');
42 $action=GETPOST('action','aZ09');
43 $section=GETPOST('section','int')?GETPOST('section','int'):GETPOST('section_id','int');
44 $module=GETPOST('module','alpha');
45 if (! $section) $section=0;
46 $section_dir=GETPOST('section_dir','alpha');
47 
48 $search_doc_ref=GETPOST('search_doc_ref','alpha');
49 
50 $sortfield = GETPOST("sortfield",'alpha');
51 $sortorder = GETPOST("sortorder",'alpha');
52 $page = GETPOST("page",'int');
53 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
54 $offset = $conf->liste_limit * $page;
55 $pageprev = $page - 1;
56 $pagenext = $page + 1;
57 if (! $sortorder) $sortorder="ASC";
58 if (! $sortfield) $sortfield="fullname";
59 if ($module == 'invoice_supplier' && $sortfield == "fullname") $sortfield="level1name";
60 
61 $ecmdir = new EcmDirectory($db);
62 if ($section)
63 {
64  $result=$ecmdir->fetch($section);
65  if (! $result > 0)
66  {
67  dol_print_error($db,$ecmdir->error);
68  exit;
69  }
70 }
71 
72 $form=new Form($db);
73 $ecmdirstatic = new EcmDirectory($db);
74 $userstatic = new User($db);
75 
76 $error=0;
77 
78 
79 /*
80  * Actions
81  */
82 
83 // Purge search criteria
84 if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter','alpha')) // All tests are required to be compatible with all browsers
85 {
86  $search_doc_ref='';
87 }
88 
89 
90 
91 
92 // Add directory
93 if ($action == 'add' && $user->rights->ecm->setup)
94 {
95  $ecmdir->ref = 'NOTUSEDYET';
96  $ecmdir->label = GETPOST("label");
97  $ecmdir->description = GETPOST("desc");
98 
99  $id = $ecmdir->create($user);
100  if ($id > 0)
101  {
102  header("Location: ".$_SERVER["PHP_SELF"]);
103  exit;
104  }
105  else
106  {
107  setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
108  $action = "create";
109  }
110 
111  clearstatcache();
112 }
113 
114 // Remove file
115 if ($action == 'confirm_deletefile')
116 {
117  if (GETPOST('confirm') == 'yes')
118  {
119  $langs->load("other");
120  if ($section)
121  {
122  $result=$ecmdir->fetch($section);
123  if (! ($result > 0))
124  {
125  dol_print_error($db,$ecmdir->error);
126  exit;
127  }
128  $relativepath=$ecmdir->getRelativePath();
129  }
130  else $relativepath='';
131  $upload_dir = $conf->ecm->dir_output.($relativepath?'/'.$relativepath:'');
132  $file = $upload_dir . "/" . GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_POST are already decoded by PHP).
133 
134  $ret=dol_delete_file($file);
135  if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
136  else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
137 
138  $result=$ecmdir->changeNbOfFiles('-');
139 
140  clearstatcache();
141  }
142  $action='file_manager';
143 }
144 
145 // Remove directory
146 if ($action == 'confirm_deletesection' && GETPOST('confirm') == 'yes')
147 {
148  $result=$ecmdir->delete($user);
149  setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
150 
151  clearstatcache();
152 }
153 
154 // Refresh directory view
155 // This refresh list of dirs, not list of files (for preformance reason). List of files is refresh only if dir was not synchronized.
156 // To refresh content of dir with cache, just open the dir in edit mode.
157 if ($action == 'refreshmanual')
158 {
159  $ecmdirtmp = new EcmDirectory($db);
160 
161  // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
162  clearstatcache();
163 
164  $diroutputslash=str_replace('\\','/',$conf->ecm->dir_output);
165  $diroutputslash.='/';
166 
167  // Scan directory tree on disk
168  $disktree=dol_dir_list($conf->ecm->dir_output,'directories',1,'','^temp$','','',0);
169 
170  // Scan directory tree in database
171  $sqltree=$ecmdirstatic->get_full_arbo(0);
172 
173  $adirwascreated=0;
174 
175  // Now we compare both trees to complete missing trees into database
176  //var_dump($disktree);
177  //var_dump($sqltree);
178  foreach($disktree as $dirdesc) // Loop on tree onto disk
179  {
180  $dirisindatabase=0;
181  foreach($sqltree as $dirsqldesc)
182  {
183  if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname'])
184  {
185  $dirisindatabase=1;
186  break;
187  }
188  }
189 
190  if (! $dirisindatabase)
191  {
192  $txt="Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
193  dol_syslog($txt);
194  //print $txt."<br>\n";
195 
196  // We must first find the fk_parent of directory to create $dirdesc['fullname']
197  $fk_parent=-1;
198  $relativepathmissing=str_replace($diroutputslash,'',$dirdesc['fullname']);
199  $relativepathtosearchparent=$relativepathmissing;
200  //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
201  if (preg_match('/\//',$relativepathtosearchparent))
202  //while (preg_match('/\//',$relativepathtosearchparent))
203  {
204  $relativepathtosearchparent=preg_replace('/\/[^\/]*$/','',$relativepathtosearchparent);
205  $txt="Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
206  dol_syslog($txt);
207  //print $txt." -> ";
208  $parentdirisindatabase=0;
209  foreach($sqltree as $dirsqldesc)
210  {
211  if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent)
212  {
213  $parentdirisindatabase=$dirsqldesc['id'];
214  break;
215  }
216  }
217  if ($parentdirisindatabase > 0)
218  {
219  dol_syslog("Yes with id ".$parentdirisindatabase);
220  //print "Yes with id ".$parentdirisindatabase."<br>\n";
221  $fk_parent=$parentdirisindatabase;
222  //break; // We found parent, we can stop the while loop
223  }
224  else
225  {
226  dol_syslog("No");
227  //print "No<br>\n";
228  }
229  }
230  else
231  {
232  dol_syslog("Parent is root");
233  $fk_parent=0; // Parent is root
234  }
235 
236  if ($fk_parent >= 0)
237  {
238  $ecmdirtmp->ref = 'NOTUSEDYET';
239  $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
240  $ecmdirtmp->description = '';
241  $ecmdirtmp->fk_parent = $fk_parent;
242 
243  $txt="We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
244  dol_syslog($txt);
245  //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
246  $id = $ecmdirtmp->create($user);
247  if ($id > 0)
248  {
249  $newdirsql=array('id'=>$id,
250  'id_mere'=>$ecmdirtmp->fk_parent,
251  'label'=>$ecmdirtmp->label,
252  'description'=>$ecmdirtmp->description,
253  'fullrelativename'=>$relativepathmissing);
254  $sqltree[]=$newdirsql; // We complete fulltree for following loops
255  //var_dump($sqltree);
256  $adirwascreated=1;
257  }
258  else
259  {
260  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
261  }
262  }
263  else {
264  $txt="Parent of ".$dirdesc['fullname']." not found";
265  dol_syslog($txt);
266  //print $txt."<br>\n";
267  }
268  }
269  }
270 
271  // Loop now on each sql tree to check if dir exists
272  foreach($sqltree as $dirdesc) // Loop on each sqltree to check dir is on disk
273  {
274  $dirtotest=$conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
275  if (! dol_is_dir($dirtotest))
276  {
277  $ecmdirtmp->id=$dirdesc['id'];
278  $ecmdirtmp->delete($user,'databaseonly');
279  //exit;
280  }
281  }
282 
283  $sql="UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
284  dol_syslog("sql = ".$sql);
285  $db->query($sql);
286 
287  // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
288  // it to be sure that fulltree array is not used without reloading it.
289  if ($adirwascreated) $sqltree=null;
290 }
291 
292 
293 
294 /*
295  * View
296  */
297 
298 // Define height of file area (depends on $_SESSION["dol_screenheight"])
299 //print $_SESSION["dol_screenheight"];
300 $maxheightwin=(isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466)?($_SESSION["dol_screenheight"]-136):660; // Also into index.php file
301 
302 $moreheadcss='';
303 $moreheadjs='';
304 
305 //$morejs=array();
306 $morejs=array('includes/jquery/plugins/blockUI/jquery.blockUI.js','core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
307 if (empty($conf->global->MAIN_ECM_DISABLE_JS)) $morejs[]="includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
308 
309 $moreheadjs.='<script type="text/javascript">'."\n";
310 $moreheadjs.='var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
311 $moreheadjs.='</script>'."\n";
312 
313 llxHeader($moreheadcss.$moreheadjs,$langs->trans("ECMArea"),'','','','',$morejs,'',0,0);
314 
315 
316 // Add sections to manage
317 $rowspan=0;
318 $sectionauto=array();
319 if (! empty($conf->global->ECM_AUTO_TREE_ENABLED))
320 {
321  if (! empty($conf->product->enabled) || ! empty($conf->service->enabled)) { $langs->load("products"); $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'product', 'test'=>(! empty($conf->product->enabled) || ! empty($conf->service->enabled)), 'label'=>$langs->trans("ProductsAndServices"), 'desc'=>$langs->trans("ECMDocsByProducts")); }
322  if (! empty($conf->societe->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'company', 'test'=>$conf->societe->enabled, 'label'=>$langs->trans("ThirdParties"), 'desc'=>$langs->trans("ECMDocsByThirdParties")); }
323  if (! empty($conf->propal->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'propal', 'test'=>$conf->propal->enabled, 'label'=>$langs->trans("Proposals"), 'desc'=>$langs->trans("ECMDocsByProposals")); }
324  if (! empty($conf->contrat->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'contract','test'=>$conf->contrat->enabled, 'label'=>$langs->trans("Contracts"), 'desc'=>$langs->trans("ECMDocsByContracts")); }
325  if (! empty($conf->commande->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'order', 'test'=>$conf->commande->enabled, 'label'=>$langs->trans("CustomersOrders"), 'desc'=>$langs->trans("ECMDocsByOrders")); }
326  if (! empty($conf->facture->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'invoice', 'test'=>$conf->facture->enabled, 'label'=>$langs->trans("CustomersInvoices"), 'desc'=>$langs->trans("ECMDocsByInvoices")); }
327  if (! empty($conf->supplier_proposal->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'supplier_proposal', 'test'=>$conf->supplier_proposal->enabled, 'label'=>$langs->trans("SupplierProposals"), 'desc'=>$langs->trans("ECMDocsBySupplierProposals")); }
328  if (! empty($conf->fournisseur->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'order_supplier', 'test'=>$conf->fournisseur->enabled, 'label'=>$langs->trans("SuppliersOrders"), 'desc'=>$langs->trans("ECMDocsByOrders")); }
329  if (! empty($conf->fournisseur->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'invoice_supplier', 'test'=>$conf->fournisseur->enabled, 'label'=>$langs->trans("SuppliersInvoices"), 'desc'=>$langs->trans("ECMDocsByInvoices")); }
330  if (! empty($conf->tax->enabled)) { $langs->load("compta"); $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'tax', 'test'=>$conf->tax->enabled, 'label'=>$langs->trans("SocialContributions"), 'desc'=>$langs->trans("ECMDocsBySocialContributions")); }
331  if (! empty($conf->projet->enabled)) { $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'project', 'test'=>$conf->projet->enabled, 'label'=>$langs->trans("Projects"), 'desc'=>$langs->trans("ECMDocsByProjects")); }
332  if (! empty($conf->ficheinter->enabled)) { $langs->load("interventions"); $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'fichinter', 'test'=>$conf->ficheinter->enabled, 'label'=>$langs->trans("Interventions"), 'desc'=>$langs->trans("ECMDocsByInterventions")); }
333  if (! empty($conf->expensereport->enabled)) { $langs->load("trips"); $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'expensereport', 'test'=>$conf->expensereport->enabled, 'label'=>$langs->trans("ExpenseReports"), 'desc'=>$langs->trans("ECMDocsByExpenseReports")); }
334  $rowspan++; $sectionauto[]=array('level'=>1, 'module'=>'user', 'test'=>1, 'label'=>$langs->trans("Users"), 'desc'=>$langs->trans("ECMDocsByUsers"));
335 }
336 
337 $head = ecm_prepare_dasboard_head('');
338 dol_fiche_head($head, 'index_auto', $langs->trans("ECMArea").' - '.$langs->trans("ECMFileManager"), -1, '');
339 
340 
341 
342 // Confirm remove file (for non javascript users)
343 if ($action == 'delete' && empty($conf->use_javascript_ajax))
344 {
345  print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.$section.'&urlfile='.urlencode($_GET["urlfile"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile','','',1);
346 
347 }
348 
349 // Start container of all panels
350 ?>
351 <!-- Begin div id="containerlayout" -->
352 <div id="containerlayout">
353 <div id="ecm-layout-north" class="toolbar largebutton">
354 <?php
355 
356 // Start top panel, toolbar
357 print '<div class="inline-block toolbarbutton centpercent">';
358 
359 // Toolbar
360 $url=((! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS))?'#':($_SERVER["PHP_SELF"].'?action=refreshmanual'.($module?'&amp;module='.$module:'').($section?'&amp;section='.$section:'')));
361 print '<a href="'.$url.'" class="inline-block valignmiddle toolbarbutton" title="'.dol_escape_htmltag($langs->trans('Refresh')).'">';
362 print '<img id="refreshbutton" class="toolbarbutton" border="0" src="'.DOL_URL_ROOT.'/theme/common/view-refresh.png">';
363 print '</a>';
364 
365 print '</div>';
366 // End top panel, toolbar
367 
368 ?>
369 </div>
370 <div id="ecm-layout-west" class="inline-block">
371 <?php
372 // Start left area
373 
374 
375 // Confirmation de la suppression d'une ligne categorie
376 if ($action == 'delete_section')
377 {
378  print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.$section, $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection',$ecmdir->label), 'confirm_deletesection','','',1);
379 }
380 // End confirm
381 
382 
383 if (empty($action) || $action == 'file_manager' || preg_match('/refresh/i',$action) || $action == 'delete')
384 {
385  print '<table width="100%" class="liste noborderbottom">'."\n";
386 
387  print '<!-- Title for auto directories -->'."\n";
388  print '<tr class="liste_titre">'."\n";
389  print '<th class="liste_titre" align="left" colspan="6">';
390  print '&nbsp;'.$langs->trans("ECMSections");
391  print '</th></tr>';
392 
393  $showonrightsize='';
394  // Auto section
395  if (count($sectionauto))
396  {
397  $htmltooltip=$langs->trans("ECMAreaDesc2");
398 
399  $sectionauto=dol_sort_array($sectionauto,'label','ASC',true,false);
400 
401  print '<tr>';
402  print '<td colspan="6">';
403  print '<div id="filetreeauto" class="ecmfiletree"><ul class="ecmjqft">';
404 
405  $nbofentries=0;
406  $oldvallevel=0;
407  foreach ($sectionauto as $key => $val)
408  {
409  if (empty($val['test'])) continue; // If condition to show is ok
410 
411  $var=false;
412 
413  print '<li class="directory collapsed">';
414  if (! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS))
415  {
416  print '<a class="fmdirlia jqft ecmjqft" href="'.$_SERVER["PHP_SELF"].'?module='.$val['module'].'">';
417  print $val['label'];
418  print '</a>';
419  }
420  else
421  {
422  print '<a class="fmdirlia jqft ecmjqft" href="'.$_SERVER["PHP_SELF"].'?module='.$val['module'].'">';
423  print $val['label'];
424  print '</a>';
425  }
426 
427  print '<div class="ecmjqft">';
428  // Info
429  $htmltooltip='<b>'.$langs->trans("ECMSection").'</b>: '.$val['label'].'<br>';
430  $htmltooltip='<b>'.$langs->trans("Type").'</b>: '.$langs->trans("ECMSectionAuto").'<br>';
431  $htmltooltip.='<b>'.$langs->trans("ECMCreationUser").'</b>: '.$langs->trans("ECMTypeAuto").'<br>';
432  $htmltooltip.='<b>'.$langs->trans("Description").'</b>: '.$val['desc'];
433  print $form->textwithpicto('', $htmltooltip, 1, 'info');
434  print '</div>';
435  print '</li>';
436 
437  $nbofentries++;
438  }
439 
440  print '</ul></div></td></tr>';
441  }
442 
443  print "</table>";
444 }
445 
446 
447 // End left panel
448 ?>
449 </div>
450 <div id="ecm-layout-center" class="inline-block">
451 <div class="pane-in ecm-in-layout-center">
452 <div id="ecmfileview" class="ecmfileview">
453 <?php
454 // Start right panel
455 
456 
457 $mode='noajax';
458 $url=DOL_URL_ROOT.'/ecm/index_auto.php';
459 include_once DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirpreview.php';
460 
461 
462 // End right panel
463 ?>
464 </div>
465 </div>
466 
467 </div>
468 </div> <!-- End div id="containerlayout" -->
469 <?php
470 // End of page
471 
472 if (! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS)) {
473  include DOL_DOCUMENT_ROOT.'/ecm/tpl/enablefiletreeajax.tpl.php';
474 }
475 
476 
477 dol_fiche_end();
478 
479 llxFooter();
480 
481 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by second index function, which produces ascending (default) or descending output...
Class to manage Dolibarr users.
Definition: user.class.php:39
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:414
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_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dol_fiche_end($notab=0)
Show tab footer of a card.
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition: files.lib.php:35
$conf db user
Definition: repair.php:105
print
Draft customers invoices.
Definition: index.php:91
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
Class to manage ECM directories.
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $objcanvas=null)
Check permissions of a user to show a page and an object.