dolibarr 21.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 *
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 if (!$error) {
116 $this->results = array('myreturn' => 999);
117 $this->resprints = 'A text to show';
118 return 0; // or return 1 to replace standard code
119 } else {
120 $this->errors[] = 'Error message';
121 return -1;
122 }
123 }
124
125 return 0;
126 }
127
128
138 public function doMassActions($parameters, &$object, &$action, $hookmanager)
139 {
140 global $conf, $user, $langs;
141
142 $error = 0; // Error counter
143
144 /* print_r($parameters); print_r($object); echo "action: " . $action; */
145 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
146 // @phan-suppress-next-line PhanPluginEmptyStatementForeachLoop
147 foreach ($parameters['toselect'] as $objectid) {
148 // Do action on each object id
149 }
150
151 if (!$error) {
152 $this->results = array('myreturn' => 999);
153 $this->resprints = 'A text to show';
154 return 0; // or return 1 to replace standard code
155 } else {
156 $this->errors[] = 'Error message';
157 return -1;
158 }
159 }
160
161 return 0;
162 }
163
164
174 public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
175 {
176 global $conf, $user, $langs;
177
178 $error = 0; // Error counter
179 $disabled = 1;
180
181 /* print_r($parameters); print_r($object); echo "action: " . $action; */
182 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
183 $this->resprints = '<option value="0"'.($disabled ? ' disabled="disabled"' : '').'>'.$langs->trans("MyModuleMassAction").'</option>';
184 }
185
186 if (!$error) {
187 return 0; // or return 1 to replace standard code
188 } else {
189 $this->errors[] = 'Error message';
190 return -1;
191 }
192 }
193
194
195
206 public function beforePDFCreation($parameters, &$object, &$action)
207 {
208 global $conf, $user, $langs;
209 global $hookmanager;
210
211 $outputlangs = $langs;
212
213 $ret = 0;
214 $deltemp = array();
215 dol_syslog(get_class($this).'::executeHooks action='.$action);
216
217 /* print_r($parameters); print_r($object); echo "action: " . $action; */
218 // @phan-suppress-next-line PhanPluginEmptyStatementIf
219 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
220 }
221
222 return $ret;
223 }
224
235 public function afterPDFCreation($parameters, &$pdfhandler, &$action)
236 {
237 global $conf, $user, $langs;
238 global $hookmanager;
239
240 $outputlangs = $langs;
241
242 $ret = 0;
243 $deltemp = array();
244 dol_syslog(get_class($this).'::executeHooks action='.$action);
245
246 /* print_r($parameters); print_r($object); echo "action: " . $action; */
247 // @phan-suppress-next-line PhanPluginEmptyStatementIf
248 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
249 // do something only for the context 'somecontext1' or 'somecontext2'
250 }
251
252 return $ret;
253 }
254
255
256
265 public function loadDataForCustomReports($parameters, &$action, $hookmanager)
266 {
267 global $langs;
268
269 $langs->load("mymodule@mymodule");
270
271 $this->results = array();
272
273 $head = array();
274 $h = 0;
275
276 if ($parameters['tabfamily'] == 'mymodule') {
277 $head[$h][0] = dol_buildpath('/module/index.php', 1);
278 $head[$h][1] = $langs->trans("Home");
279 $head[$h][2] = 'home';
280 $h++;
281
282 $this->results['title'] = $langs->trans("MyModule");
283 $this->results['picto'] = 'mymodule@mymodule';
284 }
285
286 $head[$h][0] = 'customreports.php?objecttype='.$parameters['objecttype'].(empty($parameters['tabfamily']) ? '' : '&tabfamily='.$parameters['tabfamily']);
287 $head[$h][1] = $langs->trans("CustomReports");
288 $head[$h][2] = 'customreports';
289
290 $this->results['head'] = $head;
291
292 $arrayoftypes = array();
293 //$arrayoftypes['mymodule_myobject'] = array('label' => 'MyObject', 'picto'=>'myobject@mymodule', 'ObjectClassName' => 'MyObject', 'enabled' => isModEnabled('mymodule'), 'ClassPath' => "/mymodule/class/myobject.class.php", 'langs'=>'mymodule@mymodule')
294
295 $this->results['arrayoftype'] = $arrayoftypes;
296
297 return 0;
298 }
299
300
301
312 public function restrictedArea($parameters, &$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 // verifier le type d'onglet comme member_stats où ça ne doit pas apparaitre
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 /* Add other hook methods here... */
384}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79