Clean code. Show how to use TRIGGER_PREFIX in modulebuilder template.

This commit is contained in:
ldestailleur 2025-09-04 11:30:24 +02:00
parent 8a1ec5634f
commit 3b668903b5
5 changed files with 26 additions and 13 deletions

View file

@ -26,14 +26,8 @@
// Load Dolibarr environment
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
require_once DOL_DOCUMENT_ROOT.'/bom/lib/bom.lib.php';
require_once DOL_DOCUMENT_ROOT.'/mrp/lib/mrp.lib.php';
/**
* The main.inc.php has been included so the following variable are now defined:
* @var Conf $conf
* @var DoliDB $db
* @var HookManager $hookmanager
@ -41,6 +35,12 @@ require_once DOL_DOCUMENT_ROOT.'/mrp/lib/mrp.lib.php';
* @var Translate $langs
* @var User $user
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
require_once DOL_DOCUMENT_ROOT.'/bom/lib/bom.lib.php';
require_once DOL_DOCUMENT_ROOT.'/mrp/lib/mrp.lib.php';
// Load translation files required by the page
$langs->loadLangs(array('mrp', 'other'));
@ -130,8 +130,7 @@ if (empty($reshook)) {
}
}
$triggermodname = 'BOM_MODIFY'; // Name of trigger action code to execute when we modify record
$triggermodname = $object->TRIGGER_PREFIX.'_MODIFY'; // Name of trigger action code to execute when we modify record. Used in actions_addupdatedelete.inc.php
// Actions cancel, add, update, delete or clone
include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';

View file

@ -50,6 +50,12 @@ class BOM extends CommonObject
*/
public $element = 'bom';
/**
* @var string Prefix to check for any trigger code of any business class to prevent bad value for trigger code.
* @see CommonTrigger::call_trigger()
*/
public $TRIGGER_PREFIX = 'BOM';
/**
* @var string Name of table without prefix where object is stored
*/
@ -65,6 +71,7 @@ class BOM extends CommonObject
*/
public $product;
const STATUS_DRAFT = 0;
const STATUS_VALIDATED = 1;
const STATUS_CANCELED = 9;

View file

@ -446,7 +446,7 @@ if ($action == "update_extras" && GETPOSTINT('id') > 0 && !empty($permissiontoed
setEventMessages($extrafields->error, $object->errors, 'errors');
$action = 'edit_extras';
} else {
$result = $object->updateExtraField($attribute, empty($triggermodname) ? '' : $triggermodname, $user);
$result = $object->updateExtraField($attribute, empty($triggermodname) ? '' : $triggermodname, $user); // TODO Remove $triggermodname to use $object::TRIGGER_PREFIX.'_MODIFY' instead
if ($result > 0) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
$action = 'view';

View file

@ -43,6 +43,12 @@ class MyObject extends CommonObject
*/
public $element = 'myobject';
/**
* @var string Prefix to check for any trigger code of any business class to prevent bad value for trigger code.
* @see CommonTrigger::call_trigger()
*/
public $TRIGGER_PREFIX = 'MYMODULE_MYOBJECT'; // Will be used to build trgiger keys 'MYMODULE_MYOBJECT_MODIFY', ...
/**
* @var string Name of table without prefix where object is stored. This is also the key used for extrafields management (so extrafields know the link to the parent table).
*/

View file

@ -199,7 +199,7 @@ if (empty($reshook)) {
}
}
$triggermodname = 'MYMODULE_MYOBJECT_MODIFY'; // Name of trigger action code to execute when we modify record
$triggermodname = $object::TRIGGER_PREFIX.'_MODIFY'; // Name of trigger action code to execute when we modify record. Used in actions_addupdatedelete.inc.php
// Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
@ -216,12 +216,15 @@ if (empty($reshook)) {
// Action to build doc
include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
// Other special actions
/*
if ($action == 'set_thirdparty' && $permissiontoadd) {
$object->setValueFrom('fk_soc', GETPOSTINT('fk_soc'), '', null, 'date', '', $user, $triggermodname);
}
if ($action == 'classin' && $permissiontoadd) {
$object->setProject(GETPOSTINT('projectid'));
}
*/
// Actions to send emails
$triggersendname = 'MYMODULE_MYOBJECT_SENTBYMAIL';
@ -232,14 +235,12 @@ if (empty($reshook)) {
/*
* View
*/
$form = new Form($db);
$formfile = new FormFile($db);
$formproject = new FormProjets($db);
$title = $langs->trans("MyObject")." - ".$langs->trans('Card');
//$title = $object->ref." - ".$langs->trans('Card');