dolibarr 24.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$error = 0;
54
55
56/*
57 * Actions
58 */
59
60// None
61
62
63/*
64 * View
65 */
66
68
69//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
70
71// Load original field value
72if (isset($action) && !empty($action)) {
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 to get list of all directories
86 $disktree = dol_dir_list($conf->$element->dir_output, 'directories', 1, '', array('^temp$'), '', 0, 0);
87 //dol_syslog("Found ".count($disktree)." directories on disk");
88
89 // Scan directory tree in database
90 $sqltree = $ecmdirstatic->get_full_arbo(0);
91 //dol_syslog("Found ".count($sqltree)." directories in database");
92
93 $adirwascreated = 0;
94
95 // Now we compare both trees to complete missing trees into database
96 //var_dump($disktree);
97 //var_dump($sqltree);
98 foreach ($disktree as $dirdesc) { // Loop on tree onto disk
99 set_time_limit(0); // To force restarts the timeout counter from zero
100
101 $dirisindatabase = 0;
102 foreach ($sqltree as $dirsqldesc) {
103 if ($conf->$element->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
104 $dirisindatabase = 1;
105 break;
106 }
107 }
108
109 if (!$dirisindatabase) {
110 $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into table ecm_directories, so we add it";
111 dol_syslog($txt);
112
113 // We must first find the fk_parent of directory to create $dirdesc['fullname']
114 $fk_parent = -1;
115 $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
116 $relativepathtosearchparent = $relativepathmissing;
117 //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
118 if (preg_match('/\//', $relativepathtosearchparent)) {
119 //while (preg_match('/\//',$relativepathtosearchparent))
120 $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
121 $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
122 dol_syslog($txt);
123 //print $txt." -> ";
124 $parentdirisindatabase = 0;
125 foreach ($sqltree as $dirsqldesc) {
126 if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
127 $parentdirisindatabase = $dirsqldesc['id'];
128 break;
129 }
130 }
131 if ($parentdirisindatabase > 0) {
132 dol_syslog("Yes with id ".$parentdirisindatabase);
133 //print "Yes with id ".$parentdirisindatabase."<br>\n";
134 $fk_parent = $parentdirisindatabase;
135 //break; // We found parent, we can stop the while loop
136 } else {
137 dol_syslog("No");
138 //print "No<br>\n";
139 }
140 } else {
141 dol_syslog("Parent is root");
142 $fk_parent = 0; // Parent is root
143 }
144
145 if ($fk_parent >= 0) {
146 $ecmdirtmp->ref = 'NOTUSEDYET';
147 $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
148 $ecmdirtmp->description = '';
149 $ecmdirtmp->fk_parent = $fk_parent;
150
151 $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
152 dol_syslog($txt);
153 //print $txt."<br>\n";
154 $id = $ecmdirtmp->create($user);
155 if ($id > 0) {
156 $newdirsql = array('id'=>$id,
157 'id_mere'=>$ecmdirtmp->fk_parent,
158 'label'=>$ecmdirtmp->label,
159 'description'=>$ecmdirtmp->description,
160 'fullrelativename'=>$relativepathmissing);
161 $sqltree[] = $newdirsql; // We complete fulltree for following loops
162 //var_dump($sqltree);
163 $adirwascreated = 1;
164 } else {
165 dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
166 }
167 } else {
168 $txt = "Parent of ".$dirdesc['fullname']." not found";
169 dol_syslog($txt);
170 //print $txt."<br>\n";
171 }
172 }
173 }
174
175 // Loop now on each sql tree to check if dir exists
176 foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
177 $dirtotest = $conf->$element->dir_output.'/'.$dirdesc['fullrelativename'];
178 if (!dol_is_dir($dirtotest)) {
179 dol_syslog($dirtotest." not found onto disk. We delete from database dir with id=".$dirdesc['id']);
180 $ecmdirtmp->id = $dirdesc['id'];
181 $ecmdirtmp->delete($user, 'databaseonly');
182 //exit;
183 }
184 }
185
186 dol_syslog("Nb of directories added into database = ".$adirwascreated);
187
188 $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
189 $db->query($sql);
190 }
191}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class to manage ECM directories.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition files.lib.php:39
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:64
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.