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