2
0
forked from Wavyzz/dolibarr
Files
dolibarr-fork/htdocs/ecm/ajax/ecmdatabase.php
Laurent Destailleur 8d206fdfb0 Prepare move to other licence. For the moment all answers for licence
upgrade were not yet received. So we prepare for GPL by uniformizing
licence text keys to GPL-3+. Will move later to AGPL if all answers are
positive.
2013-01-16 15:36:08 +01:00

172 lines
5.3 KiB
PHP

<?php
/* Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/ecm/ajax/ecmdatabases.php
* \brief File to build ecm database
*/
if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
//if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
require '../../main.inc.php';
require DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
$action = GETPOST('action','alpha');
$element = GETPOST('element', 'alpha');
/*
* View
*/
top_httphead();
//print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
// Load original field value
if (isset($action) && ! empty($action))
{
$error=0;
if ($action == 'build' && ! empty($element))
{
require DOL_DOCUMENT_ROOT . '/ecm/class/ecmdirectory.class.php';
$ecmdirstatic = new EcmDirectory($db);
clearstatcache();
$diroutputslash=str_replace('\\', '/', $conf->$element->dir_output);
$diroutputslash.='/';
// Scan directory tree on disk
$disktree=dol_dir_list($conf->$element->dir_output,'directories',1,'',array('^temp$'),'','',0);
// Scan directory tree in database
$sqltree=$ecmdirstatic->get_full_arbo(0);
$adirwascreated=0;
// Now we compare both trees to complete missing trees into database
//var_dump($disktree);
//var_dump($sqltree);
foreach($disktree as $dirdesc) // Loop on tree onto disk
{
set_time_limit(0); // To force restarts the timeout counter from zero
$dirisindatabase=0;
foreach($sqltree as $dirsqldesc)
{
if ($conf->$element->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname'])
{
$dirisindatabase=1;
break;
}
}
if (! $dirisindatabase)
{
$txt="Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
dol_syslog($txt);
// We must first find the fk_parent of directory to create $dirdesc['fullname']
$fk_parent=-1;
$relativepathmissing=str_replace($diroutputslash,'',$dirdesc['fullname']);
$relativepathtosearchparent=$relativepathmissing;
//dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
if (preg_match('/\//',$relativepathtosearchparent))
//while (preg_match('/\//',$relativepathtosearchparent))
{
$relativepathtosearchparent=preg_replace('/\/[^\/]*$/','',$relativepathtosearchparent);
$txt="Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
dol_syslog($txt);
//print $txt." -> ";
$parentdirisindatabase=0;
foreach($sqltree as $dirsqldesc)
{
if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent)
{
$parentdirisindatabase=$dirsqldesc['id'];
break;
}
}
if ($parentdirisindatabase > 0)
{
dol_syslog("Yes with id ".$parentdirisindatabase);
//print "Yes with id ".$parentdirisindatabase."<br>\n";
$fk_parent=$parentdirisindatabase;
//break; // We found parent, we can stop the while loop
}
else
{
dol_syslog("No");
//print "No<br>\n";
}
}
else
{
dol_syslog("Parent is root");
$fk_parent=0; // Parent is root
}
if ($fk_parent >= 0)
{
$ecmdirtmp=new EcmDirectory($db);
$ecmdirtmp->ref = 'NOTUSEDYET';
$ecmdirtmp->label = dol_basename($dirdesc['fullname']);
$ecmdirtmp->description = '';
$ecmdirtmp->fk_parent = $fk_parent;
$txt="We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
dol_syslog($txt);
//print $txt."<br>\n";
$id = $ecmdirtmp->create($user);
if ($id > 0)
{
$newdirsql=array('id'=>$id,
'id_mere'=>$ecmdirtmp->fk_parent,
'label'=>$ecmdirtmp->label,
'description'=>$ecmdirtmp->description,
'fullrelativename'=>$relativepathmissing);
$sqltree[]=$newdirsql; // We complete fulltree for following loops
//var_dump($sqltree);
$adirwascreated=1;
}
else
{
dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
}
}
else {
$txt="Parent of ".$dirdesc['fullname']." not found";
dol_syslog($txt);
//print $txt."<br>\n";
}
}
}
$sql="UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
$db->query($sql);
}
}
?>