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

@ -551,83 +551,115 @@ class FormFile
* @param string $url Full url to use for click links ('' = autodetect)
* @return int <0 if KO, nb of files shown if OK
*/
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 $bc;
global $sortfield, $sortorder, $maxheightmini;
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 $bc, $hookmanager;
global $sortfield, $sortorder, $maxheightmini;
// Show list of existing files
if (empty($useinecm)) print_titre($title?$title:$langs->trans("AttachedFiles"));
if (empty($url)) $url=$_SERVER["PHP_SELF"];
print '<table width="100%" class="'.($useinecm?'nobordernopadding':'liste').'">';
print '<tr class="liste_titre">';
print_liste_field_titre($langs->trans("Documents2"),$url,"name","",$param,'align="left"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Size"),$url,"size","",$param,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Date"),$url,"date","",$param,'align="center"',$sortfield,$sortorder);
if (empty($useinecm)) print_liste_field_titre('',$url,"","",$param,'align="center"');
print_liste_field_titre('','','');
print '</tr>';
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'));
$nboffiles=count($filearray);
$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 ($nboffiles > 0) include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
if (isset($reshook) && $reshook != '') // 0:not deleted, 1:deleted, null or '' for bypass
{
return $reshook;
}
else
{
// Show list of existing files
if (empty($useinecm)) print_titre($title?$title:$langs->trans("AttachedFiles"));
if (empty($url)) $url=$_SERVER["PHP_SELF"];
print '<table width="100%" class="'.($useinecm?'nobordernopadding':'liste').'">';
print '<tr class="liste_titre">';
print_liste_field_titre($langs->trans("Documents2"),$url,"name","",$param,'align="left"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Size"),$url,"size","",$param,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Date"),$url,"date","",$param,'align="center"',$sortfield,$sortorder);
if (empty($useinecm)) print_liste_field_titre('',$url,"","",$param,'align="center"');
print_liste_field_titre('','','');
print '</tr>';
$var=true;
foreach($filearray as $key => $file) // filearray must be only files here
{
if ($file['name'] != '.'
&& $file['name'] != '..'
&& ! preg_match('/\.meta$/i',$file['name']))
{
// Define relative path used to store the file
if (empty($relativepath))
$relativepath=(! empty($object->ref)?dol_sanitizeFileName($object->ref):'').'/';
$nboffiles=count($filearray);
$var=!$var;
print '<tr '.$bc[$var].'>';
print '<td>';
//print "XX".$file['name']; //$file['name'] must be utf8
print '<a href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
if ($forcedownload) print '&attachment=1';
if (! empty($object->entity)) print '&entity='.$object->entity;
print '&file='.urlencode($relativepath.$file['name']).'">';
print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' ';
print dol_trunc($file['name'],$maxlength,'middle');
print '</a>';
print "</td>\n";
print '<td align="right">'.dol_print_size($file['size'],1,1).'</td>';
print '<td align="center">'.dol_print_date($file['date'],"dayhour").'</td>';
// Preview
if (empty($useinecm))
{
print '<td align="center">';
$tmp=explode('.',$file['name']);
$minifile=$tmp[0].'_mini.'.$tmp[1];
if (image_format_supported($file['name']) > 0) print '<img border="0" height="'.$maxheightmini.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&file='.urlencode($relativepath.'thumbs/'.$minifile).'" title="">';
else print '&nbsp;';
print '</td>';
}
// Delete or view link
print '<td align="right">';
if ($useinecm) print '<a href="'.DOL_URL_ROOT.'/ecm/docfile.php?urlfile='.urlencode($file['name']).$param.'" class="editfilelink" rel="'.urlencode($file['name']).'">'.img_view().'</a> &nbsp; ';
if ($permtodelete) print '<a href="'.(($useinecm && ! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS))?'#':$url.'?action=delete'.(isset($object->id)?'&id='.$object->id:'').'&urlfile='.urlencode($file['name']).$param).'" class="deletefilelink" rel="'.urlencode($file['name']).'">'.img_delete().'</a>';
else print '&nbsp;';
print "</td>";
print "</tr>\n";
}
}
if ($nboffiles == 0)
{
print '<tr '.$bc[$var].'><td colspan="'.(empty($useinecm)?'5':'4').'">';
if (empty($textifempty)) print $langs->trans("NoFileFound");
else print $textifempty;
print '</td></tr>';
}
print "</table>";
if ($nboffiles > 0) include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
return $nboffiles;
}
$var=true;
foreach($filearray as $key => $file) // filearray must be only files here
{
if ($file['name'] != '.'
&& $file['name'] != '..'
&& ! preg_match('/\.meta$/i',$file['name']))
{
// Define relative path used to store the file
if (empty($relativepath))
$relativepath=(! empty($object->ref)?dol_sanitizeFileName($object->ref):'').'/';
$var=!$var;
print '<tr '.$bc[$var].'>';
print '<td>';
//print "XX".$file['name']; //$file['name'] must be utf8
print '<a href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart;
if ($forcedownload) print '&attachment=1';
if (! empty($object->entity)) print '&entity='.$object->entity;
print '&file='.urlencode($relativepath.$file['name']).'">';
print img_mime($file['name'],$file['name'].' ('.dol_print_size($file['size'],0,0).')').' ';
print dol_trunc($file['name'],$maxlength,'middle');
print '</a>';
print "</td>\n";
print '<td align="right">'.dol_print_size($file['size'],1,1).'</td>';
print '<td align="center">'.dol_print_date($file['date'],"dayhour").'</td>';
// Preview
if (empty($useinecm))
{
print '<td align="center">';
$tmp=explode('.',$file['name']);
$minifile=$tmp[0].'_mini.'.$tmp[1];
if (image_format_supported($file['name']) > 0) print '<img border="0" height="'.$maxheightmini.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&file='.urlencode($relativepath.'thumbs/'.$minifile).'" title="">';
else print '&nbsp;';
print '</td>';
}
// Delete or view link
print '<td align="right">';
if ($useinecm) print '<a href="'.DOL_URL_ROOT.'/ecm/docfile.php?urlfile='.urlencode($file['name']).$param.'" class="editfilelink" rel="'.urlencode($file['name']).'">'.img_view().'</a> &nbsp; ';
if ($permtodelete) print '<a href="'.(($useinecm && ! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS))?'#':$url.'?action=delete'.(isset($object->id)?'&id='.$object->id:'').'&urlfile='.urlencode($file['name']).$param).'" class="deletefilelink" rel="'.urlencode($file['name']).'">'.img_delete().'</a>';
else print '&nbsp;';
print "</td>";
print "</tr>\n";
}
}
if ($nboffiles == 0)
{
print '<tr '.$bc[$var].'><td colspan="'.(empty($useinecm)?'5':'4').'">';
if (empty($textifempty)) print $langs->trans("NoFileFound");
else print $textifempty;
print '</td></tr>';
}
print "</table>";
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)
{
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));
@ -83,7 +84,7 @@ function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefil
'loaddate' => $loaddate,
'loadsize' => $loadsize
);
$reshook=$hookmanager->executeHooks('getNodesList', $parameters);
$reshook=$hookmanager->executeHooks('getNodesList', $parameters, $object);
// $reshook may contain returns stacked by other modules
// $reshook is always empty with an array for can not lose returns stacked with other modules
@ -764,61 +765,91 @@ function dolMoveUploadedFile($src_file, $dest_file, $allowoverwrite, $notrigger=
* @param int $disableglob Disable usage of glob like *
* @param int $nophperrors Disable all PHP output errors
* @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
*/
function dol_delete_file($file,$disableglob=0,$nophperrors=0,$notrigger=0,$object=null)
{
global $db, $conf, $user, $langs;
global $hookmanager;
$langs->load("other");
$langs->load("errors");
$error=0;
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'));
//print "x".$file." ".$disableglob;
$ok=true;
$file_osencoded=dol_osencode($file); // New filename encoded in OS filesystem encoding charset
if (empty($disableglob) && ! empty($file_osencoded))
{
foreach (glob($file_osencoded) as $filename)
{
if ($nophperrors) $ok=@unlink($filename); // The unlink encapsulated by dolibarr
else $ok=unlink($filename); // The unlink encapsulated by dolibarr
if ($ok)
{
dol_syslog("Removed file ".$filename, LOG_DEBUG);
if (! $notrigger)
{
if (! is_object($object)) $object=(object) 'dummy';
$object->src_file=$file;
$parameters=array(
'file' => $file,
'disableglob'=> $disableglob,
'nophperrors' => $nophperrors,
'notrigger' => $notrigger
);
$reshook=$hookmanager->executeHooks('deleteFile', $parameters, $object);
// TODO Replace trigger by a hook. Triggers must be used for business events only.
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($db);
$result=$interface->run_triggers('FILE_DELETE',$object,$user,$langs,$conf);
if ($result < 0) { $error++; $errors=$interface->errors; }
// Fin appel triggers
}
}
else {
dol_syslog("Failed to remove file ".$filename, LOG_WARNING);
}
}
}
else
{
if ($nophperrors) $ok=@unlink($file_osencoded); // The unlink encapsulated by dolibarr
else $ok=unlink($file_osencoded); // The unlink encapsulated by dolibarr
if ($ok) {
dol_syslog("Removed file ".$file_osencoded, LOG_DEBUG);
}
else {
dol_syslog("Failed to remove file ".$file_osencoded, LOG_WARNING);
}
}
return $ok;
if (isset($reshook) && $reshook != '') // 0:not deleted, 1:deleted, null or '' for bypass
{
return $reshook;
}
else
{
$error=0;
//print "x".$file." ".$disableglob;
$ok=true;
$file_osencoded=dol_osencode($file); // New filename encoded in OS filesystem encoding charset
if (empty($disableglob) && ! empty($file_osencoded))
{
foreach (glob($file_osencoded) as $filename)
{
if ($nophperrors) $ok=@unlink($filename); // The unlink encapsulated by dolibarr
else $ok=unlink($filename); // The unlink encapsulated by dolibarr
if ($ok)
{
dol_syslog("Removed file ".$filename, LOG_DEBUG);
if (! $notrigger)
{
if (! is_object($object)) $object=(object) 'dummy';
$object->src_file=$file;
// TODO Replace trigger by a hook. Triggers must be used for business events only.
// REGIS just after of hook testing
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($db);
$result=$interface->run_triggers('FILE_DELETE',$object,$user,$langs,$conf);
if ($result < 0) {
$error++; $errors=$interface->errors;
}
// Fin appel triggers
}
}
else {
dol_syslog("Failed to remove file ".$filename, LOG_WARNING);
}
}
}
else
{
if ($nophperrors) $ok=@unlink($file_osencoded); // The unlink encapsulated by dolibarr
else $ok=unlink($file_osencoded); // The unlink encapsulated by dolibarr
if ($ok) {
dol_syslog("Removed file ".$file_osencoded, LOG_DEBUG);
}
else {
dol_syslog("Failed to remove file ".$file_osencoded, LOG_WARNING);
}
}
return $ok;
}
}
/**