dolibarr  13.0.2
ecmdatabase.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
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 
23 if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); // Disables token renewal
24 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
25 if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
26 if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
27 
28 require '../../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
30 
31 $action = GETPOST('action', 'aZ09');
32 $element = GETPOST('element', 'alpha');
33 
34 /*
35  * View
36  */
37 
38 top_httphead();
39 
40 //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
41 
42 // Load original field value
43 if (isset($action) && !empty($action))
44 {
45  $error = 0;
46 
47  if ($action == 'build' && !empty($element))
48  {
49  require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
50 
51  $ecmdirstatic = new EcmDirectory($db);
52  $ecmdirtmp = new EcmDirectory($db);
53 
54  // This part of code is same than into file index.php for action refreshmanual TODO Remove duplicate
55  clearstatcache();
56 
57  $diroutputslash = str_replace('\\', '/', $conf->$element->dir_output);
58  $diroutputslash .= '/';
59 
60  // Scan directory tree on disk
61  $disktree = dol_dir_list($conf->$element->dir_output, 'directories', 1, '', array('^temp$'), '', '', 0);
62 
63  // Scan directory tree in database
64  $sqltree = $ecmdirstatic->get_full_arbo(0);
65 
66  $adirwascreated = 0;
67 
68  // Now we compare both trees to complete missing trees into database
69  //var_dump($disktree);
70  //var_dump($sqltree);
71  foreach ($disktree as $dirdesc) // Loop on tree onto disk
72  {
73  set_time_limit(0); // To force restarts the timeout counter from zero
74 
75  $dirisindatabase = 0;
76  foreach ($sqltree as $dirsqldesc)
77  {
78  if ($conf->$element->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname'])
79  {
80  $dirisindatabase = 1;
81  break;
82  }
83  }
84 
85  if (!$dirisindatabase)
86  {
87  $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
88  dol_syslog($txt);
89 
90  // We must first find the fk_parent of directory to create $dirdesc['fullname']
91  $fk_parent = -1;
92  $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
93  $relativepathtosearchparent = $relativepathmissing;
94  //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
95  if (preg_match('/\//', $relativepathtosearchparent))
96  //while (preg_match('/\//',$relativepathtosearchparent))
97  {
98  $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
99  $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
100  dol_syslog($txt);
101  //print $txt." -> ";
102  $parentdirisindatabase = 0;
103  foreach ($sqltree as $dirsqldesc)
104  {
105  if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent)
106  {
107  $parentdirisindatabase = $dirsqldesc['id'];
108  break;
109  }
110  }
111  if ($parentdirisindatabase > 0)
112  {
113  dol_syslog("Yes with id ".$parentdirisindatabase);
114  //print "Yes with id ".$parentdirisindatabase."<br>\n";
115  $fk_parent = $parentdirisindatabase;
116  //break; // We found parent, we can stop the while loop
117  } else {
118  dol_syslog("No");
119  //print "No<br>\n";
120  }
121  } else {
122  dol_syslog("Parent is root");
123  $fk_parent = 0; // Parent is root
124  }
125 
126  if ($fk_parent >= 0)
127  {
128  $ecmdirtmp->ref = 'NOTUSEDYET';
129  $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
130  $ecmdirtmp->description = '';
131  $ecmdirtmp->fk_parent = $fk_parent;
132 
133  $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
134  dol_syslog($txt);
135  //print $txt."<br>\n";
136  $id = $ecmdirtmp->create($user);
137  if ($id > 0)
138  {
139  $newdirsql = array('id'=>$id,
140  'id_mere'=>$ecmdirtmp->fk_parent,
141  'label'=>$ecmdirtmp->label,
142  'description'=>$ecmdirtmp->description,
143  'fullrelativename'=>$relativepathmissing);
144  $sqltree[] = $newdirsql; // We complete fulltree for following loops
145  //var_dump($sqltree);
146  $adirwascreated = 1;
147  } else {
148  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
149  }
150  } else {
151  $txt = "Parent of ".$dirdesc['fullname']." not found";
152  dol_syslog($txt);
153  //print $txt."<br>\n";
154  }
155  }
156  }
157 
158  // Loop now on each sql tree to check if dir exists
159  foreach ($sqltree as $dirdesc) // Loop on each sqltree to check dir is on disk
160  {
161  $dirtotest = $conf->$element->dir_output.'/'.$dirdesc['fullrelativename'];
162  if (!dol_is_dir($dirtotest))
163  {
164  $mesg .= $dirtotest." not found onto disk. We delete from database dir with id=".$dirdesc['id']."<br>\n";
165  $ecmdirtmp->id = $dirdesc['id'];
166  $ecmdirtmp->delete($user, 'databaseonly');
167  //exit;
168  }
169  }
170 
171  $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
172  dol_syslog("sql = ".$sql);
173  $db->query($sql);
174  }
175 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:432
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype= 'text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1214
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition: files.lib.php:36
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:60
Class to manage ECM directories.