dolibarr  19.0.0-dev
actions_mymodule.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) ---Put here your own copyright and developer email---
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonhookactions.class.php';
27 
32 {
36  public $db;
37 
41  public $error = '';
42 
46  public $errors = array();
47 
48 
52  public $results = array();
53 
57  public $resprints;
58 
62  public $priority;
63 
64 
70  public function __construct($db)
71  {
72  $this->db = $db;
73  }
74 
75 
86  public function getNomUrl($parameters, &$object, &$action)
87  {
88  global $db, $langs, $conf, $user;
89  $this->resprints = '';
90  return 0;
91  }
92 
102  public function doActions($parameters, &$object, &$action, $hookmanager)
103  {
104  global $conf, $user, $langs;
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 call global vars like $fieldstosearchall to overwrite them, or update database depending on $action and $_POST values.
112  }
113 
114  if (!$error) {
115  $this->results = array('myreturn' => 999);
116  $this->resprints = 'A text to show';
117  return 0; // or return 1 to replace standard code
118  } else {
119  $this->errors[] = 'Error message';
120  return -1;
121  }
122  }
123 
124 
134  public function doMassActions($parameters, &$object, &$action, $hookmanager)
135  {
136  global $conf, $user, $langs;
137 
138  $error = 0; // Error counter
139 
140  /* print_r($parameters); print_r($object); echo "action: " . $action; */
141  if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
142  foreach ($parameters['toselect'] as $objectid) {
143  // Do action on each object id
144  }
145  }
146 
147  if (!$error) {
148  $this->results = array('myreturn' => 999);
149  $this->resprints = 'A text to show';
150  return 0; // or return 1 to replace standard code
151  } else {
152  $this->errors[] = 'Error message';
153  return -1;
154  }
155  }
156 
157 
167  public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
168  {
169  global $conf, $user, $langs;
170 
171  $error = 0; // Error counter
172  $disabled = 1;
173 
174  /* print_r($parameters); print_r($object); echo "action: " . $action; */
175  if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
176  $this->resprints = '<option value="0"'.($disabled ? ' disabled="disabled"' : '').'>'.$langs->trans("MyModuleMassAction").'</option>';
177  }
178 
179  if (!$error) {
180  return 0; // or return 1 to replace standard code
181  } else {
182  $this->errors[] = 'Error message';
183  return -1;
184  }
185  }
186 
187 
188 
199  public function beforePDFCreation($parameters, &$object, &$action)
200  {
201  global $conf, $user, $langs;
202  global $hookmanager;
203 
204  $outputlangs = $langs;
205 
206  $ret = 0; $deltemp = array();
207  dol_syslog(get_class($this).'::executeHooks action='.$action);
208 
209  /* print_r($parameters); print_r($object); echo "action: " . $action; */
210  if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
211  }
212 
213  return $ret;
214  }
215 
226  public function afterPDFCreation($parameters, &$pdfhandler, &$action)
227  {
228  global $conf, $user, $langs;
229  global $hookmanager;
230 
231  $outputlangs = $langs;
232 
233  $ret = 0; $deltemp = array();
234  dol_syslog(get_class($this).'::executeHooks action='.$action);
235 
236  /* print_r($parameters); print_r($object); echo "action: " . $action; */
237  if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
238  // do something only for the context 'somecontext1' or 'somecontext2'
239  }
240 
241  return $ret;
242  }
243 
244 
245 
254  public function loadDataForCustomReports($parameters, &$action, $hookmanager)
255  {
256  global $conf, $user, $langs;
257 
258  $langs->load("mymodule@mymodule");
259 
260  $this->results = array();
261 
262  $head = array();
263  $h = 0;
264 
265  if ($parameters['tabfamily'] == 'mymodule') {
266  $head[$h][0] = dol_buildpath('/module/index.php', 1);
267  $head[$h][1] = $langs->trans("Home");
268  $head[$h][2] = 'home';
269  $h++;
270 
271  $this->results['title'] = $langs->trans("MyModule");
272  $this->results['picto'] = 'mymodule@mymodule';
273  }
274 
275  $head[$h][0] = 'customreports.php?objecttype='.$parameters['objecttype'].(empty($parameters['tabfamily']) ? '' : '&tabfamily='.$parameters['tabfamily']);
276  $head[$h][1] = $langs->trans("CustomReports");
277  $head[$h][2] = 'customreports';
278 
279  $this->results['head'] = $head;
280 
281  return 1;
282  }
283 
284 
285 
296  public function restrictedArea($parameters, &$action, $hookmanager)
297  {
298  global $user;
299 
300  if ($parameters['features'] == 'myobject') {
301  if ($user->hasRight('mymodule', 'myobject', 'read')) {
302  $this->results['result'] = 1;
303  return 1;
304  } else {
305  $this->results['result'] = 0;
306  return 1;
307  }
308  }
309 
310  return 0;
311  }
312 
324  public function completeTabsHead(&$parameters, &$object, &$action, $hookmanager)
325  {
326  global $langs, $conf, $user;
327 
328  if (!isset($parameters['object']->element)) {
329  return 0;
330  }
331  if ($parameters['mode'] == 'remove') {
332  // used to make some tabs removed
333  return 0;
334  } elseif ($parameters['mode'] == 'add') {
335  $langs->load('mymodule@mymodule');
336  // used when we want to add some tabs
337  $counter = count($parameters['head']);
338  $element = $parameters['object']->element;
339  $id = $parameters['object']->id;
340  // verifier le type d'onglet comme member_stats où ça ne doit pas apparaitre
341  // if (in_array($element, ['societe', 'member', 'contrat', 'fichinter', 'project', 'propal', 'commande', 'facture', 'order_supplier', 'invoice_supplier'])) {
342  if (in_array($element, ['context1', 'context2'])) {
343  $datacount = 0;
344 
345  $parameters['head'][$counter][0] = dol_buildpath('/mymodule/mymodule_tab.php', 1) . '?id=' . $id . '&amp;module='.$element;
346  $parameters['head'][$counter][1] = $langs->trans('MyModuleTab');
347  if ($datacount > 0) {
348  $parameters['head'][$counter][1] .= '<span class="badge marginleftonlyshort">' . $datacount . '</span>';
349  }
350  $parameters['head'][$counter][2] = 'mymoduleemails';
351  $counter++;
352  }
353  if ($counter > 0 && (int) DOL_VERSION < 14) {
354  $this->results = $parameters['head'];
355  // return 1 to replace standard code
356  return 1;
357  } else {
358  // en V14 et + $parameters['head'] est modifiable par référence
359  return 0;
360  }
361  } else {
362  // Bad value for $parameters['mode']
363  return -1;
364  }
365  }
366 
367  /* Add here any other hooked methods... */
368 }
Class ActionsMyModule.
__construct($db)
Constructor.
doActions($parameters, &$object, &$action, $hookmanager)
Overloading the doActions function : replacing the parent's function with the one below.
loadDataForCustomReports($parameters, &$action, $hookmanager)
Overloading the loadDataForCustomReports function : returns data to complete the customreport tool.
doMassActions($parameters, &$object, &$action, $hookmanager)
Overloading the doMassActions function : replacing the parent's function with the one below.
restrictedArea($parameters, &$action, $hookmanager)
Overloading the restrictedArea function : check permission on an object.
afterPDFCreation($parameters, &$pdfhandler, &$action)
Execute action.
completeTabsHead(&$parameters, &$object, &$action, $hookmanager)
Execute action completeTabsHead.
beforePDFCreation($parameters, &$object, &$action)
Execute action.
addMoreMassActions($parameters, &$object, &$action, $hookmanager)
Overloading 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.