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