dolibarr 21.0.0-beta
ecmdatabase.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24if (!defined('NOTOKENRENEWAL')) {
25 define('NOTOKENRENEWAL', '1'); // Disables token renewal
26}
27if (!defined('NOREQUIREMENU')) {
28 define('NOREQUIREMENU', '1');
29}
30if (!defined('NOREQUIREAJAX')) {
31 define('NOREQUIREAJAX', '1');
32}
33if (!defined('NOREQUIRESOC')) {
34 define('NOREQUIRESOC', '1');
35}
36
37// Load Dolibarr environment
38require '../../main.inc.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
48$action = GETPOST('action', 'aZ09');
49$element = GETPOST('element', 'alpha');
50
51$permissiontoread = $user->hasRight('ecm', 'read');
52
53
54/*
55 * Actions
56 */
57
58// None
59
60
61/*
62 * View
63 */
64
66
67//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
68
69// Load original field value
70if (isset($action) && !empty($action)) {
71 $error = 0;
72
73 if ($action == 'build' && !empty($element) && $permissiontoread) {
74 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
75
76 $ecmdirstatic = new EcmDirectory($db);
77 $ecmdirtmp = new EcmDirectory($db);
78
79 // This part of code is same than into file index.php for action refreshmanual TODO Remove duplicate
80 clearstatcache();
81
82 $diroutputslash = str_replace('\\', '/', $conf->$element->dir_output);
83 $diroutputslash .= '/';
84
85 // Scan directory tree on disk
86 $disktree = dol_dir_list($conf->$element->dir_output, 'directories', 1, '', array('^temp$'), '', 0, 0);
87
88 // Scan directory tree in database
89 $sqltree = $ecmdirstatic->get_full_arbo(0);
90
91 $adirwascreated = 0;
92
93 // Now we compare both trees to complete missing trees into database
94 //var_dump($disktree);
95 //var_dump($sqltree);
96 foreach ($disktree as $dirdesc) { // Loop on tree onto disk
97 set_time_limit(0); // To force restarts the timeout counter from zero
98
99 $dirisindatabase = 0;
100 foreach ($sqltree as $dirsqldesc) {
101 if ($conf->$element->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
102 $dirisindatabase = 1;
103 break;
104 }
105 }
106
107 if (!$dirisindatabase) {
108 $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into table ecm_directories, so we add it";
109 dol_syslog($txt);
110
111 // We must first find the fk_parent of directory to create $dirdesc['fullname']
112 $fk_parent = -1;
113 $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
114 $relativepathtosearchparent = $relativepathmissing;
115 //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
116 if (preg_match('/\//', $relativepathtosearchparent)) {
117 //while (preg_match('/\//',$relativepathtosearchparent))
118 $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
119 $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
120 dol_syslog($txt);
121 //print $txt." -> ";
122 $parentdirisindatabase = 0;
123 foreach ($sqltree as $dirsqldesc) {
124 if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
125 $parentdirisindatabase = $dirsqldesc['id'];
126 break;
127 }
128 }
129 if ($parentdirisindatabase > 0) {
130 dol_syslog("Yes with id ".$parentdirisindatabase);
131 //print "Yes with id ".$parentdirisindatabase."<br>\n";
132 $fk_parent = $parentdirisindatabase;
133 //break; // We found parent, we can stop the while loop
134 } else {
135 dol_syslog("No");
136 //print "No<br>\n";
137 }
138 } else {
139 dol_syslog("Parent is root");
140 $fk_parent = 0; // Parent is root
141 }
142
143 if ($fk_parent >= 0) {
144 $ecmdirtmp->ref = 'NOTUSEDYET';
145 $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
146 $ecmdirtmp->description = '';
147 $ecmdirtmp->fk_parent = $fk_parent;
148
149 $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
150 dol_syslog($txt);
151 //print $txt."<br>\n";
152 $id = $ecmdirtmp->create($user);
153 if ($id > 0) {
154 $newdirsql = array('id'=>$id,
155 'id_mere'=>$ecmdirtmp->fk_parent,
156 'label'=>$ecmdirtmp->label,
157 'description'=>$ecmdirtmp->description,
158 'fullrelativename'=>$relativepathmissing);
159 $sqltree[] = $newdirsql; // We complete fulltree for following loops
160 //var_dump($sqltree);
161 $adirwascreated = 1;
162 } else {
163 dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
164 }
165 } else {
166 $txt = "Parent of ".$dirdesc['fullname']." not found";
167 dol_syslog($txt);
168 //print $txt."<br>\n";
169 }
170 }
171 }
172
173 // Loop now on each sql tree to check if dir exists
174 foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
175 $dirtotest = $conf->$element->dir_output.'/'.$dirdesc['fullrelativename'];
176 if (!dol_is_dir($dirtotest)) {
177 dol_syslog($dirtotest." not found onto disk. We delete from database dir with id=".$dirdesc['id']);
178 $ecmdirtmp->id = $dirdesc['id'];
179 $ecmdirtmp->delete($user, 'databaseonly');
180 //exit;
181 }
182 }
183
184 dol_syslog("Nb of directories added into database = ".$adirwascreated);
185
186 $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
187 $db->query($sql);
188 }
189}
$id
Definition account.php:48
Class to manage ECM directories.
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition files.lib.php:38
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
dol_is_dir($folder)
Test if filename is a directory.
GETPOST($paramname, $check='alphanohtml', $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='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79