dolibarr 24.0.0-beta
actions_mymodule.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) ---Replace with your own copyright and developer email---
4 * Copyright (C) 2026 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
28require_once DOL_DOCUMENT_ROOT.'/core/class/commonhookactions.class.php';
29
34{
38 public $db;
39
43 public $error = '';
44
48 public $errors = array();
49
50
54 public $results = array();
55
59 public $resprints;
60
64 public $priority;
65
66
72 public function __construct($db)
73 {
74 $this->db = $db;
75 }
76
77
88 public function getNomUrl($parameters, &$object, &$action)
89 {
90 global $db, $langs, $conf, $user;
91 $this->resprints = '';
92 return 0;
93 }
94
104 public function doActions($parameters, &$object, &$action, $hookmanager)
105 {
106 $error = 0; // Error counter
107
108 /* print_r($parameters); print_r($object); echo "action: " . $action; */
109 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
110 // Do what you want here...
111 // You can for example load and use call global vars like $fieldstosearchall to overwrite them, or update the database depending on $action and GETPOST values.
112
113 if (!$error) {
114 $this->results = array('myreturn' => 999);
115 $this->resprints = 'A text to show';
116 return 0; // or return 1 to replace standard code
117 } else {
118 $this->errors[] = 'Error message';
119 return -1;
120 }
121 }
122
123 return 0;
124 }
125
126
136 public function doMassActions($parameters, &$object, &$action, $hookmanager)
137 {
138 global $conf, $user, $langs;
139
140 $error = 0; // Error counter
141
142 /* print_r($parameters); print_r($object); echo "action: " . $action; */
143 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
144 // @phan-suppress-next-line PhanPluginEmptyStatementForeachLoop
145 foreach ($parameters['toselect'] as $objectid) {
146 // Do action on each object id
147 }
148
149 if (!$error) {
150 $this->results = array('myreturn' => 999);
151 $this->resprints = 'A text to show';
152 return 0; // or return 1 to replace standard code
153 } else {
154 $this->errors[] = 'Error message';
155 return -1;
156 }
157 }
158
159 return 0;
160 }
161
162
172 public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
173 {
174 global $langs;
175
176 $error = 0; // Error counter
177 $disabled = 1;
178
179 /* print_r($parameters); print_r($object); echo "action: " . $action; */
180 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
181 $this->resprints = '<option value="0"'.($disabled ? ' disabled="disabled"' : '').'>'.$langs->trans("MyModuleMassAction").'</option>';
182 }
183
184 if (!$error) {
185 return 0; // or return 1 to replace standard code
186 } else {
187 $this->errors[] = 'Error message';
188 return -1;
189 }
190 }
191
192
193
204 public function beforePDFCreation($parameters, &$object, &$action)
205 {
206 global $conf, $user, $langs;
207 global $hookmanager;
208
209 $outputlangs = $langs;
210
211 $ret = 0;
212 $deltemp = array();
213 dol_syslog(get_class($this).'::executeHooks action='.$action);
214
215 /* print_r($parameters); print_r($object); echo "action: " . $action; */
216 // @phan-suppress-next-line PhanPluginEmptyStatementIf
217 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
218 // do something only for the context 'somecontext1' or 'somecontext2'
219 }
220
221 return $ret;
222 }
223
234 public function afterPDFCreation($parameters, &$pdfhandler, &$action)
235 {
236 global $conf, $user, $langs;
237 global $hookmanager;
238
239 $outputlangs = $langs;
240
241 $ret = 0;
242 $deltemp = array();
243 dol_syslog(get_class($this).'::executeHooks action='.$action);
244
245 /* print_r($parameters); print_r($object); echo "action: " . $action; */
246 // @phan-suppress-next-line PhanPluginEmptyStatementIf
247 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
248 // do something only for the context 'somecontext1' or 'somecontext2'
249 }
250
251 return $ret;
252 }
253
254
255
264 public function loadDataForCustomReports($parameters, &$action, $hookmanager)
265 {
266 global $langs;
267
268 $langs->load("mymodule@mymodule");
269
270 $this->results = array();
271
272 $head = array();
273 $h = 0;
274
275 if ($parameters['tabfamily'] == 'mymodule') {
276 $head[$h][0] = dol_buildpath('/module/index.php', 1);
277 $head[$h][1] = $langs->trans("Home");
278 $head[$h][2] = 'home';
279 $h++;
280
281 $this->results['title'] = $langs->trans("MyModule");
282 $this->results['picto'] = 'mymodule@mymodule';
283 }
284
285 $head[$h][0] = 'customreports.php?objecttype='.$parameters['objecttype'].(empty($parameters['tabfamily']) ? '' : '&tabfamily='.$parameters['tabfamily']);
286 $head[$h][1] = $langs->trans("CustomReports");
287 $head[$h][2] = 'customreports';
288
289 $this->results['head'] = $head;
290
291 $arrayoftypes = array();
292 //$arrayoftypes['mymodule_myobject'] = array('label' => 'MyObject', 'picto'=>'myobject@mymodule', 'ObjectClassName' => 'MyObject', 'enabled' => isModEnabled('mymodule'), 'ClassPath' => "/mymodule/class/myobject.class.php", 'langs'=>'mymodule@mymodule')
293
294 $this->results['arrayoftype'] = $arrayoftypes;
295
296 return 0;
297 }
298
299
300
312 public function restrictedArea($parameters, $object, &$action, $hookmanager)
313 {
314 global $user;
315
316 if ($parameters['features'] == 'myobject') {
317 if ($user->hasRight('mymodule', 'myobject', 'read')) {
318 $this->results['result'] = 1;
319 return 1;
320 } else {
321 $this->results['result'] = 0;
322 return 1;
323 }
324 }
325
326 return 0;
327 }
328
340 public function completeTabsHead(&$parameters, &$object, &$action, $hookmanager)
341 {
342 global $langs, $conf, $user;
343
344 if (!isset($parameters['object']->element)) {
345 return 0;
346 }
347 if ($parameters['mode'] == 'remove') {
348 // used to make some tabs removed
349 return 0;
350 } elseif ($parameters['mode'] == 'add') {
351 $langs->load('mymodule@mymodule');
352 // used when we want to add some tabs
353 $counter = count($parameters['head']);
354 $element = $parameters['object']->element;
355 $id = $parameters['object']->id;
356 // check the tab type like member_stats where it should not appear
357 // if (in_array($element, ['societe', 'member', 'contrat', 'fichinter', 'project', 'propal', 'commande', 'facture', 'order_supplier', 'invoice_supplier'])) {
358 if (in_array($element, ['context1', 'context2'])) {
359 $datacount = 0;
360
361 $parameters['head'][$counter][0] = dol_buildpath('/mymodule/mymodule_tab.php', 1) . '?id=' . $id . '&amp;module='.$element;
362 $parameters['head'][$counter][1] = $langs->trans('MyModuleTab');
363 if ($datacount > 0) {
364 $parameters['head'][$counter][1] .= '<span class="badge marginleftonlyshort">' . $datacount . '</span>';
365 }
366 $parameters['head'][$counter][2] = 'mymoduleemails';
367 $counter++;
368 }
369 if ($counter > 0 && (int) DOL_VERSION < 14) { // @phpstan-ignore-line
370 $this->results = $parameters['head'];
371 // return 1 to replace standard code
372 return 1;
373 } else {
374 // From V14 onwards, $parameters['head'] is modifiable by reference
375 return 0;
376 }
377 } else {
378 // Bad value for $parameters['mode']
379 return -1;
380 }
381 }
382
383
393 public function showLinkToObjectBlock($parameters, &$object, &$action, $hookmanager)
394 {
395 $myobject = new MyObject($object->db);
396 $this->results = array('myobject@mymodule' => array(
397 'enabled' => isModEnabled('mymodule'),
398 'perms' => 1,
399 'label' => 'LinkToMyObject',
400 'sql' => "SELECT t.rowid, t.ref, t.ref as 'name' FROM " . $this->db->prefix() . $myobject->table_element. " as t "),);
401
402 return 1;
403 }
404 /* Add other hook methods here... */
405}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class ActionsMyModule.
__construct($db)
Constructor.
doActions($parameters, &$object, &$action, $hookmanager)
Overload the doActions function : replacing the parent's function with the one below.
loadDataForCustomReports($parameters, &$action, $hookmanager)
Overload the loadDataForCustomReports function : returns data to complete the customreport tool.
doMassActions($parameters, &$object, &$action, $hookmanager)
Overload the doMassActions function : replacing the parent's function with the one below.
afterPDFCreation($parameters, &$pdfhandler, &$action)
Execute action after PDF (document) creation.
restrictedArea($parameters, $object, &$action, $hookmanager)
Overload the restrictedArea function : check permission on an object.
completeTabsHead(&$parameters, &$object, &$action, $hookmanager)
Execute action completeTabsHead.
beforePDFCreation($parameters, &$object, &$action)
Execute action before PDF (document) creation.
addMoreMassActions($parameters, &$object, &$action, $hookmanager)
Overload the addMoreMassActions function : replacing the parent's function with the one below.
getNomUrl($parameters, &$object, &$action)
Execute action.
showLinkToObjectBlock($parameters, &$object, &$action, $hookmanager)
Overload the showLinkToObjectBlock function : add or replace array of object linkable.
Parent class of all other hook actions classes.
Class for MyObject.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.