From d8ef24ff0ba6c3c9a50a28d8a8296d23f3cc9c6c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 5 Nov 2017 00:37:21 +0100 Subject: [PATCH] Clean code of the filemanager --- htdocs/core/ajax/ajaxdirtree.php | 448 +++++++++++++++------- htdocs/ecm/index.php | 181 +-------- htdocs/ecm/index_auto.php | 9 +- htdocs/ecm/tpl/enablefiletreeajax.tpl.php | 2 +- 4 files changed, 332 insertions(+), 308 deletions(-) diff --git a/htdocs/core/ajax/ajaxdirtree.php b/htdocs/core/ajax/ajaxdirtree.php index ea4bf830e21..2390e163239 100644 --- a/htdocs/core/ajax/ajaxdirtree.php +++ b/htdocs/core/ajax/ajaxdirtree.php @@ -23,25 +23,35 @@ // This script is called with a POST method. -// Directory to scan (full path) is inside POST['dir']. +// Directory to scan (full path) is inside POST['dir'] and encode by js escape() if ajax is used or encoded by urlencode if mode=noajax if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL',1); // Disables token renewal -//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); -$res=@include '../../main.inc.php'; -include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; -include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; -include_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php'; -include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; -include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php'; +if (! isset($mode) || $mode != 'noajax') // For ajax call +{ + $res=@include '../../main.inc.php'; + include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; + include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; + include_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php'; + include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; + include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php'; -$openeddir = GETPOST('openeddir'); -$modulepart= GETPOST('modulepart'); -$selecteddir = jsUnEscape(GETPOST('dir')); // relative path. We must decode using same encoding function used by javascript: escape() -if ($selecteddir != '/') $selecteddir = preg_replace('/\/$/','',$selecteddir); // We removed last '/' except if it is '/' + $openeddir = GETPOST('openeddir'); + $modulepart= GETPOST('modulepart'); + $selecteddir = jsUnEscape(GETPOST('dir')); // relative path. We must decode using same encoding function used by javascript: escape() + if ($selecteddir != '/') $selecteddir = preg_replace('/\/$/','',$selecteddir); // We removed last '/' except if it is '/' +} +else // For no ajax call +{ + $openeddir = GETPOST('openeddir'); + $modulepart= GETPOST('modulepart'); + $selecteddir = GETPOST('dir'); + if ($selecteddir != '/') $selecteddir = preg_replace('/\/$/','',$selecteddir); // We removed last '/' except if it is '/' + if (empty($url)) $url=DOL_URL_ROOT.'/ecm/index.php'; +} $langs->load("ecm"); @@ -76,7 +86,10 @@ if ($modulepart == 'medias') * View */ -top_httphead(); +if (! isset($mode) || $mode != 'noajax') +{ + top_httphead(); +} //print ''."\n"; $userstatic=new User($db); @@ -97,151 +110,320 @@ foreach($sqltree as $keycursor => $val) } } - -if (file_exists($fullpathselecteddir)) +if (! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS)) { - $files = @scandir($fullpathselecteddir); + if (file_exists($fullpathselecteddir)) + { + $files = @scandir($fullpathselecteddir); - if ($files) - { - natcasesort($files); - if ( count($files) > 2 ) /* The 2 accounts for . and .. */ - { - echo "\n"; - } - } - else print "PermissionDenied"; + } + } + else print "PermissionDenied"; + } + + // This ajax service is called only when a directory $selecteddir is opened but not when closed. + //print ''; } -// This ajax service is called only when a directory $selecteddir is opened but not when closed. -//print ''; -if (is_object($db)) $db->close(); +if (empty($conf->use_javascript_ajax) || ! empty($conf->global->MAIN_ECM_DISABLE_JS)) +{ + print ''; +} + + +// Close db if mode is not noajax +if ((! isset($mode) || $mode != 'noajax') && is_object($db)) $db->close(); diff --git a/htdocs/ecm/index.php b/htdocs/ecm/index.php index 77fb16f5afd..e9f3af7c255 100644 --- a/htdocs/ecm/index.php +++ b/htdocs/ecm/index.php @@ -14,13 +14,14 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * You can call this page with param module=medias to get a filemanager for medias. */ /** * \file htdocs/ecm/index.php * \ingroup ecm * \brief Main page for ECM section area - * \author Laurent Destailleur */ require '../main.inc.php'; @@ -31,14 +32,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php'; require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php'; // Load traductions files -$langs->load("ecm"); -$langs->load("companies"); -$langs->load("other"); -$langs->load("users"); -$langs->load("orders"); -$langs->load("propal"); -$langs->load("bills"); -$langs->load("contracts"); +$langs->loadLangs(array("ecm","companies","other","users","orders","propal","bills","contracts")); // Security check if ($user->societe_id) $socid=$user->societe_id; @@ -145,7 +139,6 @@ if ($action == 'confirm_deletefile') { // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections. //var_dump(GETPOST('urlfile'));exit; - $langs->load("other"); $upload_dir = $conf->ecm->dir_output.($relativepath?'/'.$relativepath:''); $file = $upload_dir . "/" . GETPOST('urlfile','alpha'); // Do not use urldecode here ($_GET and $_POST are already decoded by PHP). @@ -443,7 +436,7 @@ if (empty($action) || $action == 'file_manager' || preg_match('/refresh/i',$acti { print ''; - // Show filemanager tree + // Show filemanager tree (will be filled by call of ajax enablefiletreeajax.tpl.php that execute ajaxdirtree.php) print '
'; if ($action == 'deletefile') print $form->formconfirm('eeeee', $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 'deletefile'); @@ -453,164 +446,20 @@ if (empty($action) || $action == 'file_manager' || preg_match('/refresh/i',$acti else { print ''; + + if (empty($module)) $module='ecm'; + + $_POST['modulepart'] = $module; + $_POST['openeddir'] = GETPOST('openeddir'); + $_POST['dir'] = empty($_POST['dir'])?'/':$_POST['dir']; + + // Show filemanager tree (will be filled by direct include of ajaxdirtree.php in mode noajax, this will return all dir - all levels - to show) print '
'; - print '
    '; - // Load full tree - if (empty($sqltree)) $sqltree=$ecmdirstatic->get_full_arbo(0); // Slow + $mode='noajax'; + $url=DOL_URL_ROOT.'/ecm/index.php'; + include DOL_DOCUMENT_ROOT.'/core/ajax/ajaxdirtree.php'; - // ----- This section will show a tree from a fulltree array ----- - // $section must also be defined - // ---------------------------------------------------------------- - - // Define fullpathselected ( _x_y_z ) of $section parameter - $fullpathselected=''; - foreach($sqltree as $key => $val) - { - //print $val['id']."-".$section."
    "; - if ($val['id'] == $section) - { - $fullpathselected=$val['fullpath']; - break; - } - } - //print "fullpathselected=".$fullpathselected."
    "; - - // Update expandedsectionarray in session - $expandedsectionarray=array(); - if (isset($_SESSION['dol_ecmexpandedsectionarray'])) $expandedsectionarray=explode(',',$_SESSION['dol_ecmexpandedsectionarray']); - - if ($section && GETPOST('sectionexpand') == 'true') - { - // We add all sections that are parent of opened section - $pathtosection=explode('_',$fullpathselected); - foreach($pathtosection as $idcursor) - { - if ($idcursor && ! in_array($idcursor,$expandedsectionarray)) // Not already in array - { - $expandedsectionarray[]=$idcursor; - } - } - $_SESSION['dol_ecmexpandedsectionarray']=join(',',$expandedsectionarray); - } - if ($section && GETPOST('sectionexpand') == 'false') - { - // We removed all expanded sections that are child of the closed section - $oldexpandedsectionarray=$expandedsectionarray; - $expandedsectionarray=array(); // Reset - foreach($oldexpandedsectionarray as $sectioncursor) - { - // is_in_subtree(fulltree,sectionparent,sectionchild) - if ($sectioncursor && ! is_in_subtree($sqltree,$section,$sectioncursor)) $expandedsectionarray[]=$sectioncursor; - } - $_SESSION['dol_ecmexpandedsectionarray']=join(',',$expandedsectionarray); - } - //print $_SESSION['dol_ecmexpandedsectionarray'].'
    '; - - $nbofentries=0; - $oldvallevel=0; - $var=true; - foreach($sqltree as $key => $val) - { - $var=false; - - $ecmdirstatic->id=$val['id']; - $ecmdirstatic->ref=$val['label']; - - // Refresh cache - if (preg_match('/refresh/i',$action)) - { - $result=$ecmdirstatic->fetch($val['id']); - $ecmdirstatic->ref=$ecmdirstatic->label; - - $result=$ecmdirstatic->refreshcachenboffile(0); - $val['cachenbofdoc']=$result; - } - - //$fullpathparent=preg_replace('/(_[^_]+)$/i','',$val['fullpath']); - - // Define showline - $showline=0; - - // If directory is son of expanded directory, we show line - if (in_array($val['id_mere'],$expandedsectionarray)) $showline=4; - // If directory is brother of selected directory, we show line - elseif ($val['id'] != $section && $val['id_mere'] == $ecmdirstatic->motherof[$section]) $showline=3; - // If directory is parent of selected directory or is selected directory, we show line - elseif (preg_match('/'.$val['fullpath'].'_/i',$fullpathselected.'_')) $showline=2; - // If we are level one we show line - elseif ($val['level'] < 2) $showline=1; - - if ($showline) - { - if (in_array($val['id'],$expandedsectionarray)) $option='indexexpanded'; - else $option='indexnotexpanded'; - //print $option; - - print '\n"; - } - - $oldvallevel=$val['level']; - $nbofentries++; - } - - // If nothing to show - if ($nbofentries == 0) - { - print '\n"; - } - - print '
'; print '
'; print ''; } diff --git a/htdocs/ecm/index_auto.php b/htdocs/ecm/index_auto.php index 3fa2bc1dc7a..a0ceefd5014 100644 --- a/htdocs/ecm/index_auto.php +++ b/htdocs/ecm/index_auto.php @@ -31,14 +31,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php'; require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php'; // Load traductions files -$langs->load("ecm"); -$langs->load("companies"); -$langs->load("other"); -$langs->load("users"); -$langs->load("orders"); -$langs->load("propal"); -$langs->load("bills"); -$langs->load("contracts"); +$langs->loadLangs(array("ecm","companies","other","users","orders","propal","bills","contracts")); // Security check if ($user->societe_id) $socid=$user->societe_id; diff --git a/htdocs/ecm/tpl/enablefiletreeajax.tpl.php b/htdocs/ecm/tpl/enablefiletreeajax.tpl.php index 3f19e1d666f..7969ee6a458 100644 --- a/htdocs/ecm/tpl/enablefiletreeajax.tpl.php +++ b/htdocs/ecm/tpl/enablefiletreeajax.tpl.php @@ -33,7 +33,7 @@ $(document).ready(function() { $('#filetree').fileTree({ root: '', - // Ajax called if we click to expand a dir (not a file). Parameter of dir is provided as a POST parameter. + // Ajax called if we click to expand a dir (not a file). Parameter 'dir' is provided as a POST parameter by fileTree code. script: '', folderEvent: 'click', // 'dblclick' multiFolder: false },