Work on module template

This commit is contained in:
Laurent Destailleur 2017-06-02 23:41:18 +02:00
parent 66420d0e80
commit 111087dfdc
4 changed files with 113 additions and 28 deletions

View File

@ -24,7 +24,9 @@
// $massaction must be defined // $massaction must be defined
// $objectclass and $$objectlabel must be defined // $objectclass and $$objectlabel must be defined
// $uploaddir (example $conf->projet->dir_output . "/";) // $parameters, $object, $action must be defined for the hook.
// $uploaddir may be defined (example to $conf->projet->dir_output."/";)
// $toselect may be defined // $toselect may be defined
@ -36,8 +38,8 @@ if (empty($objectclass) || empty($uploaddir))
} }
// Mass actions. Controls on number of lines checked // Mass actions. Controls on number of lines checked.
$maxformassaction=1000; $maxformassaction=(empty($conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS)?1000:$conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS);
if (! empty($massaction) && count($toselect) < 1) if (! empty($massaction) && count($toselect) < 1)
{ {
$error++; $error++;
@ -565,6 +567,11 @@ if (! $error && $massaction == 'delete' && $permtodelete)
//var_dump($listofobjectthirdparties);exit; //var_dump($listofobjectthirdparties);exit;
} }
$parameters['toselect']=$toselect;
$parameters['uploaddir']=$uploaddir;
$reshook=$hookmanager->executeHooks('doMassActions',$parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');

View File

@ -140,6 +140,7 @@ class HookManager
'addStatisticLine', 'addStatisticLine',
'deleteFile', 'deleteFile',
'doActions', 'doActions',
'doMassActions',
'formCreateThirdpartyOptions', 'formCreateThirdpartyOptions',
'formObjectOptions', 'formObjectOptions',
'formattachOptions', 'formattachOptions',

View File

@ -546,7 +546,7 @@ class Form
$ret='<div class="centpercent center">'; $ret='<div class="centpercent center">';
$ret.='<select data-role="none" class="flat'.(empty($conf->use_javascript_ajax)?'':' hideobject').' massaction massactionselect" name="massaction"'.($disabled?' disabled="disabled"':'').'>'; $ret.='<select data-role="none" class="flat'.(empty($conf->use_javascript_ajax)?'':' hideobject').' massaction massactionselect" name="massaction"'.($disabled?' disabled="disabled"':'').'>';
// Complete list with data from external modules. THe module can use $_SERVER['PHP_SELF'] to know on which page we are, or use the $parameters['context'] completed by executeHooks. // Complete list with data from external modules. THe module can use $_SERVER['PHP_SELF'] to know on which page we are, or use the $parameters['currentcontext'] completed by executeHooks.
$parameters=array(); $parameters=array();
$reshook=$hookmanager->executeHooks('addMoreMassActions',$parameters); // Note that $action and $object may have been modified by hook $reshook=$hookmanager->executeHooks('addMoreMassActions',$parameters); // Note that $action and $object may have been modified by hook
if (empty($reshook)) if (empty($reshook))

View File

@ -62,24 +62,101 @@ class ActionsMyModule
*/ */
public function doActions($parameters, &$object, &$action, $hookmanager) public function doActions($parameters, &$object, &$action, $hookmanager)
{ {
global $conf, $user, $langs;
$error = 0; // Error counter $error = 0; // Error counter
$myvalue = 'test'; // A result value
/*
print_r($parameters); print_r($parameters);
echo "action: " . $action;
print_r($object); print_r($object);
echo "action: " . $action;
*/
if (in_array($parameters['currentcontext'], array('somecontext1','somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
if (in_array('somecontext', explode(':', $parameters['context']))) {
// do something only for the context 'somecontext'
} }
if (! $error) { if (! $error) {
$this->results = array('myreturn' => $myvalue); $this->results = array('myreturn' => 999);
$this->resprints = 'A text to show'; $this->resprints = 'A text to show';
return 0; // or return 1 to replace standard code return 0; // or return 1 to replace standard code
} else { } else {
$this->errors[] = 'Error message'; $this->errors[] = 'Error message';
return -1; return -1;
} }
} }
/**
* Overloading the doActions function : replacing the parent's function with the one below
*
* @param array() $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @param HookManager $hookmanager Hook manager propagated to allow calling another hook
* @return int < 0 on error, 0 on success, 1 to replace standard code
*/
public function doMassActions($parameters, &$object, &$action, $hookmanager)
{
global $conf, $user, $langs;
$error = 0; // Error counter
/*
print_r($parameters);
print_r($object);
echo "action: " . $action;
*/
if (in_array($parameters['currentcontext'], array('somecontext1','somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
foreach($parameters['toselect'] as $objectid)
{
// Do action on each object id
}
}
if (! $error) {
$this->results = array('myreturn' => 999);
$this->resprints = 'A text to show';
return 0; // or return 1 to replace standard code
} else {
$this->errors[] = 'Error message';
return -1;
}
}
/**
* Overloading the addMoreMassActions function : replacing the parent's function with the one below
*
* @param array() $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @param HookManager $hookmanager Hook manager propagated to allow calling another hook
* @return int < 0 on error, 0 on success, 1 to replace standard code
*/
public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
{
global $conf, $user, $langs;
$error = 0; // Error counter
if (in_array($parameters['currentcontext'], array('somecontext1','somecontext2'))) // do something only for the context 'somecontext'
{
$this->resprints = '<option value="0"'.($disabled?' disabled="disabled"':'').'>'.$langs->trans("MyModuleMassAction").'</option>';
}
if (! $error) {
return 0; // or return 1 to replace standard code
} else {
$this->errors[] = 'Error message';
return -1;
}
}
} }