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