dolibarr 22.0.5
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 $error = 0; // Error counter
106
107 /* print_r($parameters); print_r($object); echo "action: " . $action; */
108 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
109 // Do what you want here...
110 // 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.
111
112 if (!$error) {
113 $this->results = array('myreturn' => 999);
114 $this->resprints = 'A text to show';
115 return 0; // or return 1 to replace standard code
116 } else {
117 $this->errors[] = 'Error message';
118 return -1;
119 }
120 }
121
122 return 0;
123 }
124
125
135 public function doMassActions($parameters, &$object, &$action, $hookmanager)
136 {
137 global $conf, $user, $langs;
138
139 $error = 0; // Error counter
140
141 /* print_r($parameters); print_r($object); echo "action: " . $action; */
142 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
143 // @phan-suppress-next-line PhanPluginEmptyStatementForeachLoop
144 foreach ($parameters['toselect'] as $objectid) {
145 // Do action on each object id
146 }
147
148 if (!$error) {
149 $this->results = array('myreturn' => 999);
150 $this->resprints = 'A text to show';
151 return 0; // or return 1 to replace standard code
152 } else {
153 $this->errors[] = 'Error message';
154 return -1;
155 }
156 }
157
158 return 0;
159 }
160
161
171 public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
172 {
173 global $conf, $user, $langs;
174
175 $error = 0; // Error counter
176 $disabled = 1;
177
178 /* print_r($parameters); print_r($object); echo "action: " . $action; */
179 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
180 $this->resprints = '<option value="0"'.($disabled ? ' disabled="disabled"' : '').'>'.$langs->trans("MyModuleMassAction").'</option>';
181 }
182
183 if (!$error) {
184 return 0; // or return 1 to replace standard code
185 } else {
186 $this->errors[] = 'Error message';
187 return -1;
188 }
189 }
190
191
192
203 public function beforePDFCreation($parameters, &$object, &$action)
204 {
205 global $conf, $user, $langs;
206 global $hookmanager;
207
208 $outputlangs = $langs;
209
210 $ret = 0;
211 $deltemp = array();
212 dol_syslog(get_class($this).'::executeHooks action='.$action);
213
214 /* print_r($parameters); print_r($object); echo "action: " . $action; */
215 // @phan-suppress-next-line PhanPluginEmptyStatementIf
216 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
217 // do something only for the context 'somecontext1' or 'somecontext2'
218 }
219
220 return $ret;
221 }
222
233 public function afterPDFCreation($parameters, &$pdfhandler, &$action)
234 {
235 global $conf, $user, $langs;
236 global $hookmanager;
237
238 $outputlangs = $langs;
239
240 $ret = 0;
241 $deltemp = array();
242 dol_syslog(get_class($this).'::executeHooks action='.$action);
243
244 /* print_r($parameters); print_r($object); echo "action: " . $action; */
245 // @phan-suppress-next-line PhanPluginEmptyStatementIf
246 if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
247 // do something only for the context 'somecontext1' or 'somecontext2'
248 }
249
250 return $ret;
251 }
252
253
254
263 public function loadDataForCustomReports($parameters, &$action, $hookmanager)
264 {
265 global $langs;
266
267 $langs->load("mymodule@mymodule");
268
269 $this->results = array();
270
271 $head = array();
272 $h = 0;
273
274 if ($parameters['tabfamily'] == 'mymodule') {
275 $head[$h][0] = dol_buildpath('/module/index.php', 1);
276 $head[$h][1] = $langs->trans("Home");
277 $head[$h][2] = 'home';
278 $h++;
279
280 $this->results['title'] = $langs->trans("MyModule");
281 $this->results['picto'] = 'mymodule@mymodule';
282 }
283
284 $head[$h][0] = 'customreports.php?objecttype='.$parameters['objecttype'].(empty($parameters['tabfamily']) ? '' : '&tabfamily='.$parameters['tabfamily']);
285 $head[$h][1] = $langs->trans("CustomReports");
286 $head[$h][2] = 'customreports';
287
288 $this->results['head'] = $head;
289
290 $arrayoftypes = array();
291 //$arrayoftypes['mymodule_myobject'] = array('label' => 'MyObject', 'picto'=>'myobject@mymodule', 'ObjectClassName' => 'MyObject', 'enabled' => isModEnabled('mymodule'), 'ClassPath' => "/mymodule/class/myobject.class.php", 'langs'=>'mymodule@mymodule')
292
293 $this->results['arrayoftype'] = $arrayoftypes;
294
295 return 0;
296 }
297
298
299
311 public function restrictedArea($parameters, $object, &$action, $hookmanager)
312 {
313 global $user;
314
315 if ($parameters['features'] == 'myobject') {
316 if ($user->hasRight('mymodule', 'myobject', 'read')) {
317 $this->results['result'] = 1;
318 return 1;
319 } else {
320 $this->results['result'] = 0;
321 return 1;
322 }
323 }
324
325 return 0;
326 }
327
339 public function completeTabsHead(&$parameters, &$object, &$action, $hookmanager)
340 {
341 global $langs, $conf, $user;
342
343 if (!isset($parameters['object']->element)) {
344 return 0;
345 }
346 if ($parameters['mode'] == 'remove') {
347 // used to make some tabs removed
348 return 0;
349 } elseif ($parameters['mode'] == 'add') {
350 $langs->load('mymodule@mymodule');
351 // used when we want to add some tabs
352 $counter = count($parameters['head']);
353 $element = $parameters['object']->element;
354 $id = $parameters['object']->id;
355 // verifier le type d'onglet comme member_stats où ça ne doit pas apparaitre
356 // if (in_array($element, ['societe', 'member', 'contrat', 'fichinter', 'project', 'propal', 'commande', 'facture', 'order_supplier', 'invoice_supplier'])) {
357 if (in_array($element, ['context1', 'context2'])) {
358 $datacount = 0;
359
360 $parameters['head'][$counter][0] = dol_buildpath('/mymodule/mymodule_tab.php', 1) . '?id=' . $id . '&amp;module='.$element;
361 $parameters['head'][$counter][1] = $langs->trans('MyModuleTab');
362 if ($datacount > 0) {
363 $parameters['head'][$counter][1] .= '<span class="badge marginleftonlyshort">' . $datacount . '</span>';
364 }
365 $parameters['head'][$counter][2] = 'mymoduleemails';
366 $counter++;
367 }
368 if ($counter > 0 && (int) DOL_VERSION < 14) { // @phpstan-ignore-line
369 $this->results = $parameters['head'];
370 // return 1 to replace standard code
371 return 1;
372 } else {
373 // From V14 onwards, $parameters['head'] is modifiable by reference
374 return 0;
375 }
376 } else {
377 // Bad value for $parameters['mode']
378 return -1;
379 }
380 }
381
382
392 public function showLinkToObjectBlock($parameters, &$object, &$action, $hookmanager)
393 {
394 $myobject = new MyObject($object->db);
395 $this->results = array('myobject@mymodule' => array(
396 'enabled' => isModEnabled('mymodule'),
397 'perms' => 1,
398 'label' => 'LinkToMyObject',
399 'sql' => "SELECT t.rowid, t.ref, t.ref as 'name' FROM " . $this->db->prefix() . $myobject->table_element. " as t "),);
400
401 return 1;
402 }
403 /* Add other hook methods here... */
404}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
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.
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