New: add ajax refresh method and code more clear

This commit is contained in:
Regis Houssin 2012-09-07 13:22:37 +02:00
parent 928aae7f79
commit 2ee6f33f72
3 changed files with 257 additions and 54 deletions

View File

@ -0,0 +1,170 @@
<?php
/* Copyright (C) 2012 Regis Houssin <regis@dolibarr.fr>
*
* 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 2 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');
/*
* 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')
{
require DOL_DOCUMENT_ROOT . '/ecm/class/ecmdirectory.class.php';
$ecmdirstatic = new EcmDirectory($db);
clearstatcache();
$diroutputslash=str_replace('\\', '/', $conf->ecm->dir_output);
$diroutputslash.='/';
// Scan directory tree on disk
$disktree=dol_dir_list($conf->ecm->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->ecm->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);
}
}
?>

View File

@ -24,6 +24,7 @@
*/
if (! defined('REQUIRE_JQUERY_LAYOUT')) define('REQUIRE_JQUERY_LAYOUT','1');
if (! defined('REQUIRE_JQUERY_BLOCKUI')) define('REQUIRE_JQUERY_BLOCKUI', 1);
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
@ -430,8 +431,9 @@ else
print '<img class="toolbarbutton" border="0" src="'.DOL_URL_ROOT.'/theme/common/folder-new.png">';
print '</a>';
}
print '<a href="'.$_SERVER["PHP_SELF"].'?action=refreshmanual'.($module?'&amp;module='.$module:'').($section?'&amp;section='.$section:'').'" class="toolbarbutton" title="'.dol_escape_htmltag($langs->trans('Refresh')).'">';
print '<img class="toolbarbutton" border="0" src="'.DOL_URL_ROOT.'/theme/common/view-refresh.png">';
$url=((! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS))?'#':($_SERVER["PHP_SELF"].'?action=refreshmanual'.($module?'&amp;module='.$module:'').($section?'&amp;section='.$section:'')));
print '<a href="'.$url.'" class="toolbarbutton" title="'.dol_escape_htmltag($langs->trans('Refresh')).'">';
print '<img id="refreshbutton" class="toolbarbutton" border="0" src="'.DOL_URL_ROOT.'/theme/common/view-refresh.png">';
print '</a>';
print '</div>';
@ -565,57 +567,7 @@ if (empty($action) || $action == 'file_manager' || preg_match('/refresh/i',$acti
print '<tr><td colspan="6" style="padding-left: 20px">';
// Show filemanager tree
print '<div id="filetree" class="ecmfiletree">';
print '</div>';
$openeddir='/';
?>
<script type="text/javascript">
function loadandshowpreview(filedirname,section)
{
//alert('filedirname='+filedirname);
jQuery('#ecmfileview').empty();
url='<?php echo dol_buildpath('/core/ajax/ajaxdirpreview.php',1); ?>?action=preview&module=ecm&section='+section+'&file='+urlencode(filedirname);
jQuery.get(url, function(data) {
//alert('Load of url '+url+' was performed : '+data);
pos=data.indexOf("TYPE=directory",0);
//alert(pos);
if ((pos > 0) && (pos < 20))
{
filediractive=filedirname; // Save current dirname
filetypeactive='directory';
}
else
{
filediractive=filedirname; // Save current dirname
filetypeactive='file';
}
jQuery('#ecmfileview').append(data);
});
}
jQuery(document).ready( function() {
jQuery('#filetree').fileTree({ root: '<?php print dol_escape_js($openeddir); ?>',
// Called if we click on a file (not a dir)
script: '<?php echo DOL_URL_ROOT.'/core/ajax/ajaxdirtree.php?modulepart=ecm&openeddir='.urlencode($openeddir); ?>',
folderEvent: 'click',
multiFolder: false },
// Called if we click on a file (not a dir)
function(file) {
jQuery("#mesg").hide();
loadandshowpreview(file,0);
}
);
});
</script>
<?php
print '<div id="filetree" class="ecmfiletree"></div>';
if ($action == 'deletefile') print $form->formconfirm('eeeee', $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 'deletefile');
@ -815,7 +767,7 @@ include_once DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirpreview.php';
// To attach new file
if (empty($conf->global->MAIN_ECM_DISABLE_JS) || ! empty($section))
if ((! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS)) || ! empty($section))
{
$formfile=new FormFile($db);
$formfile->form_attach_new_file(DOL_URL_ROOT.'/ecm/index.php', 'none', 0, ($section?$section:-1), $user->rights->ecm->upload, 48);
@ -832,6 +784,9 @@ else print '&nbsp;';
<?php
// End of page
if (! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS)) {
include 'tpl/builddatabase.tpl.php';
}
llxFooter();

View File

@ -0,0 +1,78 @@
<?php
/* Copyright (C) 2012 Regis Houssin <regis@dolibarr.fr>
*
* 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 2 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/>.
*
*/
$openeddir='/';
?>
<!-- BEGIN PHP TEMPLATE FOR JQUERY -->
<script type="text/javascript">
$(document).ready( function() {
$('#filetree').fileTree({ root: '<?php print dol_escape_js($openeddir); ?>',
// Called if we click on a file (not a dir)
script: '<?php echo DOL_URL_ROOT.'/core/ajax/ajaxdirtree.php?modulepart=ecm&openeddir='.urlencode($openeddir); ?>',
folderEvent: 'click',
multiFolder: false },
// Called if we click on a file (not a dir)
function(file) {
$("#mesg").hide();
loadandshowpreview(file,0);
}
);
$('#refreshbutton').click( function() {
ecmBuildDatabase();
});
});
function loadandshowpreview(filedirname,section)
{
//alert('filedirname='+filedirname);
$('#ecmfileview').empty();
var url = '<?php echo dol_buildpath('/core/ajax/ajaxdirpreview.php',1); ?>?action=preview&module=ecm&section='+section+'&file='+urlencode(filedirname);
$.get(url, function(data) {
//alert('Load of url '+url+' was performed : '+data);
pos=data.indexOf("TYPE=directory",0);
//alert(pos);
if ((pos > 0) && (pos < 20))
{
filediractive=filedirname; // Save current dirname
filetypeactive='directory';
}
else
{
filediractive=filedirname; // Save current dirname
filetypeactive='file';
}
$('#ecmfileview').append(data);
});
}
ecmBuildDatabase = function() {
$.pleaseBePatient("<?php echo $langs->trans('PleaseBePatient'); ?>");
$.getJSON( "<?php echo DOL_URL_ROOT . '/ecm/ajax/ecmdatabase.php'; ?>", {
action: "build"
},
function(response) {
$.unblockUI();
//location.href="<?php echo $_SERVER['PHP_SELF']; ?>";
});
};
</script>
<!-- END PHP TEMPLATE FOR JQUERY -->