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