dolibarr 21.0.0-alpha
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 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonhookactions.class.php';
28
33{
37 public $db;
38
42 public $error = '';
43
47 public $errors = array();
48
49
53 public $results = array();
54
58 public $resprints;
59
63 public $priority;
64
65
71 public function __construct($db)
72 {
73 $this->db = $db;
74 }
75
76
87 public function getNomUrl($parameters, &$object, &$action)
88 {
89 global $db, $langs, $conf, $user;
90 $this->resprints = '';
91 return 0;
92 }
93
103 public function doActions($parameters, &$object, &$action, $hookmanager)
104 {
105 global $conf, $user, $langs;
106
107 $error = 0; // Error counter
108
109 /* print_r($parameters); print_r($object); echo "action: " . $action; */
110 // @phan-suppress-next-line PhanPluginEmptyStatementIf
111 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
112 // Do what you want here...
113 // 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.
114 }
115
116 if (!$error) {
117 $this->results = array('myreturn' => 999);
118 $this->resprints = 'A text to show';
119 return 0; // or return 1 to replace standard code
120 } else {
121 $this->errors[] = 'Error message';
122 return -1;
123 }
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
150 if (!$error) {
151 $this->results = array('myreturn' => 999);
152 $this->resprints = 'A text to show';
153 return 0; // or return 1 to replace standard code
154 } else {
155 $this->errors[] = 'Error message';
156 return -1;
157 }
158 }
159
160
170 public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
171 {
172 global $conf, $user, $langs;
173
174 $error = 0; // Error counter
175 $disabled = 1;
176
177 /* print_r($parameters); print_r($object); echo "action: " . $action; */
178 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
179 $this->resprints = '<option value="0"'.($disabled ? ' disabled="disabled"' : '').'>'.$langs->trans("MyModuleMassAction").'</option>';
180 }
181
182 if (!$error) {
183 return 0; // or return 1 to replace standard code
184 } else {
185 $this->errors[] = 'Error message';
186 return -1;
187 }
188 }
189
190
191
202 public function beforePDFCreation($parameters, &$object, &$action)
203 {
204 global $conf, $user, $langs;
205 global $hookmanager;
206
207 $outputlangs = $langs;
208
209 $ret = 0;
210 $deltemp = array();
211 dol_syslog(get_class($this).'::executeHooks action='.$action);
212
213 /* print_r($parameters); print_r($object); echo "action: " . $action; */
214 // @phan-suppress-next-line PhanPluginEmptyStatementIf
215 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
216 }
217
218 return $ret;
219 }
220
231 public function afterPDFCreation($parameters, &$pdfhandler, &$action)
232 {
233 global $conf, $user, $langs;
234 global $hookmanager;
235
236 $outputlangs = $langs;
237
238 $ret = 0;
239 $deltemp = array();
240 dol_syslog(get_class($this).'::executeHooks action='.$action);
241
242 /* print_r($parameters); print_r($object); echo "action: " . $action; */
243 // @phan-suppress-next-line PhanPluginEmptyStatementIf
244 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
245 // do something only for the context 'somecontext1' or 'somecontext2'
246 }
247
248 return $ret;
249 }
250
251
252
261 public function loadDataForCustomReports($parameters, &$action, $hookmanager)
262 {
263 global $langs;
264
265 $langs->load("mymodule@mymodule");
266
267 $this->results = array();
268
269 $head = array();
270 $h = 0;
271
272 if ($parameters['tabfamily'] == 'mymodule') {
273 $head[$h][0] = dol_buildpath('/module/index.php', 1);
274 $head[$h][1] = $langs->trans("Home");
275 $head[$h][2] = 'home';
276 $h++;
277
278 $this->results['title'] = $langs->trans("MyModule");
279 $this->results['picto'] = 'mymodule@mymodule';
280 }
281
282 $head[$h][0] = 'customreports.php?objecttype='.$parameters['objecttype'].(empty($parameters['tabfamily']) ? '' : '&tabfamily='.$parameters['tabfamily']);
283 $head[$h][1] = $langs->trans("CustomReports");
284 $head[$h][2] = 'customreports';
285
286 $this->results['head'] = $head;
287
288 $arrayoftypes = array();
289 //$arrayoftypes['mymodule_myobject'] = array('label' => 'MyObject', 'picto'=>'myobject@mymodule', 'ObjectClassName' => 'MyObject', 'enabled' => isModEnabled('mymodule'), 'ClassPath' => "/mymodule/class/myobject.class.php", 'langs'=>'mymodule@mymodule')
290
291 $this->results['arrayoftype'] = $arrayoftypes;
292
293 return 0;
294 }
295
296
297
308 public function restrictedArea($parameters, &$action, $hookmanager)
309 {
310 global $user;
311
312 if ($parameters['features'] == 'myobject') {
313 if ($user->hasRight('mymodule', 'myobject', 'read')) {
314 $this->results['result'] = 1;
315 return 1;
316 } else {
317 $this->results['result'] = 0;
318 return 1;
319 }
320 }
321
322 return 0;
323 }
324
336 public function completeTabsHead(&$parameters, &$object, &$action, $hookmanager)
337 {
338 global $langs, $conf, $user;
339
340 if (!isset($parameters['object']->element)) {
341 return 0;
342 }
343 if ($parameters['mode'] == 'remove') {
344 // used to make some tabs removed
345 return 0;
346 } elseif ($parameters['mode'] == 'add') {
347 $langs->load('mymodule@mymodule');
348 // used when we want to add some tabs
349 $counter = count($parameters['head']);
350 $element = $parameters['object']->element;
351 $id = $parameters['object']->id;
352 // verifier le type d'onglet comme member_stats où ça ne doit pas apparaitre
353 // if (in_array($element, ['societe', 'member', 'contrat', 'fichinter', 'project', 'propal', 'commande', 'facture', 'order_supplier', 'invoice_supplier'])) {
354 if (in_array($element, ['context1', 'context2'])) {
355 $datacount = 0;
356
357 $parameters['head'][$counter][0] = dol_buildpath('/mymodule/mymodule_tab.php', 1) . '?id=' . $id . '&amp;module='.$element;
358 $parameters['head'][$counter][1] = $langs->trans('MyModuleTab');
359 if ($datacount > 0) {
360 $parameters['head'][$counter][1] .= '<span class="badge marginleftonlyshort">' . $datacount . '</span>';
361 }
362 $parameters['head'][$counter][2] = 'mymoduleemails';
363 $counter++;
364 }
365 if ($counter > 0 && (int) DOL_VERSION < 14) { // @phpstan-ignore-line
366 $this->results = $parameters['head'];
367 // return 1 to replace standard code
368 return 1;
369 } else {
370 // From V14 onwards, $parameters['head'] is modifiable by reference
371 return 0;
372 }
373 } else {
374 // Bad value for $parameters['mode']
375 return -1;
376 }
377 }
378
379 /* Add other hook methods here... */
380}
$id
Definition account.php:39
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.
restrictedArea($parameters, &$action, $hookmanager)
Overload the restrictedArea function : check permission on an object.
afterPDFCreation($parameters, &$pdfhandler, &$action)
Execute action after PDF (document) creation.
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.
Parent class of all other hook actions classes.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.