dolibarr  9.0.0
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 <http://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','alpha');
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  }
118  else
119  {
120  dol_syslog("No");
121  //print "No<br>\n";
122  }
123  }
124  else
125  {
126  dol_syslog("Parent is root");
127  $fk_parent=0; // Parent is root
128  }
129 
130  if ($fk_parent >= 0)
131  {
132  $ecmdirtmp->ref = 'NOTUSEDYET';
133  $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
134  $ecmdirtmp->description = '';
135  $ecmdirtmp->fk_parent = $fk_parent;
136 
137  $txt="We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
138  dol_syslog($txt);
139  //print $txt."<br>\n";
140  $id = $ecmdirtmp->create($user);
141  if ($id > 0)
142  {
143  $newdirsql=array('id'=>$id,
144  'id_mere'=>$ecmdirtmp->fk_parent,
145  'label'=>$ecmdirtmp->label,
146  'description'=>$ecmdirtmp->description,
147  'fullrelativename'=>$relativepathmissing);
148  $sqltree[]=$newdirsql; // We complete fulltree for following loops
149  //var_dump($sqltree);
150  $adirwascreated=1;
151  }
152  else
153  {
154  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
155  }
156  }
157  else {
158  $txt="Parent of ".$dirdesc['fullname']." not found";
159  dol_syslog($txt);
160  //print $txt."<br>\n";
161  }
162  }
163  }
164 
165  // Loop now on each sql tree to check if dir exists
166  foreach($sqltree as $dirdesc) // Loop on each sqltree to check dir is on disk
167  {
168  $dirtotest=$conf->$element->dir_output.'/'.$dirdesc['fullrelativename'];
169  if (! dol_is_dir($dirtotest))
170  {
171  $mesg.=$dirtotest." not found onto disk. We delete from database dir with id=".$dirdesc['id']."<br>\n";
172  $ecmdirtmp->id=$dirdesc['id'];
173  $ecmdirtmp->delete($user,'databaseonly');
174  //exit;
175  }
176  }
177 
178  $sql="UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
179  dol_syslog("sql = ".$sql);
180  $db->query($sql);
181  }
182 }
183 
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if(! defined('NOREQUIREMENU')) if(! function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1107
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:438
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
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:35
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
Class to manage ECM directories.