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