New: add hooks in dol_delete_file and list_of_documents methods

This commit is contained in:
Regis Houssin 2012-09-11 17:01:54 +02:00
parent c6c2fd2f39
commit f95d611f12
2 changed files with 179 additions and 116 deletions

View File

@ -554,9 +554,40 @@ class FormFile
function list_of_documents($filearray,$object,$modulepart,$param,$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0,$title='',$url='') function list_of_documents($filearray,$object,$modulepart,$param,$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0,$title='',$url='')
{ {
global $user, $conf, $langs; global $user, $conf, $langs;
global $bc; global $bc, $hookmanager;
global $sortfield, $sortorder, $maxheightmini; global $sortfield, $sortorder, $maxheightmini;
if (! is_object($hookmanager))
{
if (! class_exists('HookManager')) {
// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
require DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
$hookmanager=new HookManager($db);
}
}
$hookmanager->initHooks(array('formfile'));
$parameters=array(
'filearray' => $filearray,
'modulepart'=> $modulepart,
'param' => $param,
'forcedownload' => $forcedownload,
'relativepath' => $relativepath,
'permtodelete' => $permtodelete,
'useinecm' => $useinecm,
'textifempty' => $textifempty,
'maxlength' => $maxlength,
'title' => $title,
'url' => $url
);
$reshook=$hookmanager->executeHooks('showNodesList', $parameters, $object);
if (isset($reshook) && $reshook != '') // 0:not deleted, 1:deleted, null or '' for bypass
{
return $reshook;
}
else
{
// Show list of existing files // Show list of existing files
if (empty($useinecm)) print_titre($title?$title:$langs->trans("AttachedFiles")); if (empty($useinecm)) print_titre($title?$title:$langs->trans("AttachedFiles"));
if (empty($url)) $url=$_SERVER["PHP_SELF"]; if (empty($url)) $url=$_SERVER["PHP_SELF"];
@ -628,6 +659,7 @@ class FormFile
return $nboffiles; return $nboffiles;
} }
}
/** /**

View File

@ -52,6 +52,7 @@ function dol_basename($pathfile)
function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter="", $sortcriteria="name", $sortorder=SORT_ASC, $mode=0) function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter="", $sortcriteria="name", $sortorder=SORT_ASC, $mode=0)
{ {
global $db, $hookmanager; global $db, $hookmanager;
global $object;
dol_syslog("files.lib.php::dol_dir_list path=".$path." types=".$types." recursive=".$recursive." filter=".$filter." excludefilter=".json_encode($excludefilter)); dol_syslog("files.lib.php::dol_dir_list path=".$path." types=".$types." recursive=".$recursive." filter=".$filter." excludefilter=".json_encode($excludefilter));
@ -83,7 +84,7 @@ function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefil
'loaddate' => $loaddate, 'loaddate' => $loaddate,
'loadsize' => $loadsize 'loadsize' => $loadsize
); );
$reshook=$hookmanager->executeHooks('getNodesList', $parameters); $reshook=$hookmanager->executeHooks('getNodesList', $parameters, $object);
// $reshook may contain returns stacked by other modules // $reshook may contain returns stacked by other modules
// $reshook is always empty with an array for can not lose returns stacked with other modules // $reshook is always empty with an array for can not lose returns stacked with other modules
@ -764,16 +765,41 @@ function dolMoveUploadedFile($src_file, $dest_file, $allowoverwrite, $notrigger=
* @param int $disableglob Disable usage of glob like * * @param int $disableglob Disable usage of glob like *
* @param int $nophperrors Disable all PHP output errors * @param int $nophperrors Disable all PHP output errors
* @param int $notrigger Disable all triggers * @param int $notrigger Disable all triggers
* @param Object $object Object * @param object $object Current object in use
* @return boolean True if file is deleted, False if error * @return boolean True if file is deleted, False if error
*/ */
function dol_delete_file($file,$disableglob=0,$nophperrors=0,$notrigger=0,$object=null) function dol_delete_file($file,$disableglob=0,$nophperrors=0,$notrigger=0,$object=null)
{ {
global $db, $conf, $user, $langs; global $db, $conf, $user, $langs;
global $hookmanager;
$langs->load("other"); $langs->load("other");
$langs->load("errors"); $langs->load("errors");
if (! is_object($hookmanager))
{
if (! class_exists('HookManager')) {
// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
require DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
$hookmanager=new HookManager($db);
}
}
$hookmanager->initHooks(array('fileslib'));
$parameters=array(
'file' => $file,
'disableglob'=> $disableglob,
'nophperrors' => $nophperrors,
'notrigger' => $notrigger
);
$reshook=$hookmanager->executeHooks('deleteFile', $parameters, $object);
if (isset($reshook) && $reshook != '') // 0:not deleted, 1:deleted, null or '' for bypass
{
return $reshook;
}
else
{
$error=0; $error=0;
//print "x".$file." ".$disableglob; //print "x".$file." ".$disableglob;
@ -794,11 +820,14 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$notrigger=0,$objec
$object->src_file=$file; $object->src_file=$file;
// TODO Replace trigger by a hook. Triggers must be used for business events only. // TODO Replace trigger by a hook. Triggers must be used for business events only.
// REGIS just after of hook testing
// Appel des triggers // Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($db); $interface=new Interfaces($db);
$result=$interface->run_triggers('FILE_DELETE',$object,$user,$langs,$conf); $result=$interface->run_triggers('FILE_DELETE',$object,$user,$langs,$conf);
if ($result < 0) { $error++; $errors=$interface->errors; } if ($result < 0) {
$error++; $errors=$interface->errors;
}
// Fin appel triggers // Fin appel triggers
} }
} }
@ -818,8 +847,10 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$notrigger=0,$objec
dol_syslog("Failed to remove file ".$file_osencoded, LOG_WARNING); dol_syslog("Failed to remove file ".$file_osencoded, LOG_WARNING);
} }
} }
return $ok; return $ok;
} }
}
/** /**
* Remove a directory (not recursive, so content must be empty). * Remove a directory (not recursive, so content must be empty).