dolibarr  20.0.0-beta
hook.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2019-2024 Frédéric France <frederic.france@free.fr>
4  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26 
30 class Hook extends CommonObject
31 {
35  public $element = 'hook';
36 
40  public $table_element = 'zapier_hook';
41 
45  public $picto = 'hook@zapier';
46 
47 
48  const STATUS_DRAFT = 0;
49  const STATUS_VALIDATED = 1;
50  const STATUS_DISABLED = -1;
51 
52 
76  public $fields = array(
77  'rowid' => array(
78  'type' => 'integer',
79  'label' => 'TechnicalID',
80  'enabled' => 1,
81  'visible' => -2,
82  'noteditable' => 1,
83  'notnull' => 1,
84  'index' => 1,
85  'position' => 1,
86  'comment' => 'Id',
87  ),
88  'entity' => array(
89  'type' => 'integer',
90  'label' => 'Entity',
91  'enabled' => 1,
92  'visible' => 0,
93  'notnull' => 1,
94  'default' => '1',
95  'index' => 1,
96  'position' => 20,
97  ),
98  'fk_user' => array(
99  'type' => 'integer',
100  'label' => 'UserOwner',
101  'enabled' => 1,
102  'visible' => -2,
103  'notnull' => 1,
104  'position' => 510,
105  'foreignkey' => 'llx_user.rowid',
106  ),
107  'url' => array(
108  'type' => 'varchar(255)',
109  'label' => 'Url',
110  'enabled' => 1,
111  'visible' => 1,
112  'position' => 30,
113  'searchall' => 1,
114  'css' => 'minwidth200',
115  'help' => 'Hook url'
116  ),
117  'module' => array(
118  'type' => 'varchar(128)',
119  'label' => 'Module',
120  'enabled' => 1,
121  'visible' => 1,
122  'position' => 30,
123  'searchall' => 1,
124  'css' => 'minwidth200',
125  'help' => 'Hook module'
126  ),
127  'action' => array(
128  'type' => 'varchar(128)',
129  'label' => 'Action',
130  'enabled' => 1,
131  'visible' => 1,
132  'position' => 30,
133  'searchall' => 1,
134  'css' => 'minwidth200',
135  'help' => 'Hook action trigger'
136  ),
137  'event' => array(
138  'type' => 'varchar(255)',
139  'label' => 'Event',
140  'enabled' => 1,
141  'visible' => 1,
142  'position' => 30,
143  'searchall' => 1,
144  'css' => 'minwidth200',
145  'help' => 'Event',
146  'showoncombobox' => 1,
147  ),
148  'date_creation' => array(
149  'type' => 'datetime',
150  'label' => 'DateCreation',
151  'enabled' => 1,
152  'visible' => -2,
153  'notnull' => 1,
154  'position' => 500,
155  ),
156  'import_key' => array(
157  'type' => 'varchar(14)',
158  'label' => 'ImportId',
159  'enabled' => 1,
160  'visible' => -2,
161  'notnull' => -1,
162  'index' => 0,
163  'position' => 1000,
164  ),
165  'status' => array(
166  'type' => 'integer',
167  'label' => 'Status',
168  'enabled' => 1,
169  'visible' => 1,
170  'notnull' => 1,
171  'default' => '0',
172  'index' => 1,
173  'position' => 1000,
174  'arrayofkeyval' => array(
175  0 => 'Draft',
176  1 => 'Active',
177  -1 => 'Canceled',
178  ),
179  ),
180  );
181 
185  public $rowid;
186 
190  public $ref;
191 
195  public $entity;
196 
200  public $label;
201 
205  public $url;
206 
210  public $fk_user;
211 
215  public $status;
216 
220  public $date_creation;
221 
225  public $fk_user_creat;
226 
230  public $fk_user_modif;
231 
235  public $import_key;
236 
237 
243  public function __construct(DoliDB $db)
244  {
245  global $conf, $langs, $user;
246 
247  $this->db = $db;
248 
249  $this->ismultientitymanaged = 0;
250  $this->isextrafieldmanaged = 1;
251 
252  if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
253  $this->fields['rowid']['visible'] = 0;
254  }
255  if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
256  $this->fields['entity']['enabled'] = 0;
257  }
258 
259  // Unset fields that are disabled
260  foreach ($this->fields as $key => $val) {
261  if (isset($val['enabled']) && empty($val['enabled'])) {
262  unset($this->fields[$key]);
263  }
264  }
265 
266  // Translate some data of arrayofkeyval
267  foreach ($this->fields as $key => $val) {
268  if (is_array($this->fields['status']['arrayofkeyval'])) {
269  foreach ($this->fields['status']['arrayofkeyval'] as $key2 => $val2) {
270  $this->fields['status']['arrayofkeyval'][$key2] = $langs->trans($val2);
271  }
272  }
273  }
274  }
275 
283  public function create(User $user, $notrigger = 0)
284  {
285  return $this->createCommon($user, $notrigger);
286  }
287 
295  public function createFromClone(User $user, $fromid)
296  {
297  global $langs, $hookmanager, $extrafields;
298  $error = 0;
299 
300  dol_syslog(__METHOD__, LOG_DEBUG);
301 
302  $object = new self($this->db);
303 
304  $this->db->begin();
305 
306  // Load source object
307  $object->fetchCommon($fromid);
308  // Reset some properties
309  unset($object->id);
310  unset($object->fk_user_creat);
311  unset($object->import_key);
312 
313  // Clear fields
314  $object->ref = "copy_of_".$object->ref;
315  $object->title = $langs->trans("CopyOf")." ".$object->title;
316  // ...
317  // Clear extrafields that are unique
318  if (is_array($object->array_options) && count($object->array_options) > 0) {
319  $extrafields->fetch_name_optionals_label($this->table_element);
320  foreach ($object->array_options as $key => $option) {
321  $shortkey = preg_replace('/options_/', '', $key);
322  if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
323  // var_dump($key);
324  // var_dump($clonedObj->array_options[$key]);
325  // exit;
326  unset($object->array_options[$key]);
327  }
328  }
329  }
330 
331  // Create clone
332  $object->context['createfromclone'] = 'createfromclone';
333  $result = $object->createCommon($user);
334  if ($result < 0) {
335  $error++;
336  $this->error = $object->error;
337  $this->errors = $object->errors;
338  }
339 
340  unset($object->context['createfromclone']);
341 
342  // End
343  if (!$error) {
344  $this->db->commit();
345  return $object;
346  } else {
347  $this->db->rollback();
348  return -1;
349  }
350  }
351 
359  public function fetch($id, $ref = null)
360  {
361  $result = $this->fetchCommon($id, $ref);
362  if ($result > 0 && !empty($this->table_element_line)) {
363  //$this->fetchLines();
364  }
365  return $result;
366  }
367 
373  /*public function fetchLines()
374  {
375  $this->lines=array();
376 
377  // Load lines with object MyObjectLine
378 
379  return count($this->lines)?1:0;
380  }*/
381 
394  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
395  {
396  global $conf;
397 
398  dol_syslog(__METHOD__, LOG_DEBUG);
399 
400  $records = array();
401 
402  $sql = 'SELECT';
403  $sql .= ' t.rowid';
404  // TODO Get all fields
405  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
406  $sql .= ' WHERE t.entity = '.((int) $conf->entity);
407 
408  // Manage filter
409  $errormessage = '';
410  $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
411  if ($errormessage) {
412  $this->errors[] = $errormessage;
413  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
414  return -1;
415  }
416 
417  if (!empty($sortfield)) {
418  $sql .= $this->db->order($sortfield, $sortorder);
419  }
420  if (!empty($limit)) {
421  $sql .= $this->db->plimit($limit, $offset);
422  }
423 
424  $resql = $this->db->query($sql);
425  if ($resql) {
426  $num = $this->db->num_rows($resql);
427 
428  while ($obj = $this->db->fetch_object($resql)) {
429  $record = new self($this->db);
430 
431  $record->id = $obj->rowid;
432  // TODO Get other fields
433 
434  //var_dump($record->id);
435  $records[$record->id] = $record;
436  }
437  $this->db->free($resql);
438 
439  return $records;
440  } else {
441  $this->errors[] = 'Error '.$this->db->lasterror();
442  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
443 
444  return -1;
445  }
446  }
447 
455  public function update(User $user, $notrigger = 0)
456  {
457  return $this->updateCommon($user, $notrigger);
458  }
459 
467  public function delete(User $user, $notrigger = 0)
468  {
469  return $this->deleteCommon($user, $notrigger);
470  //return $this->deleteCommon($user, $notrigger, 1);
471  }
472 
483  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
484  {
485  global $db, $conf, $langs, $hookmanager, $action;
486  global $dolibarr_main_authentication, $dolibarr_main_demo;
487  global $menumanager;
488 
489  if (!empty($conf->dol_no_mouse_hover)) {
490  // Force disable tooltips
491  $notooltip = 1;
492  }
493 
494  $result = '';
495 
496  $label = '<u>'.$langs->trans("Hook").'</u>';
497  $label .= '<br>';
498  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
499 
500  $url = DOL_URL_ROOT.'/zapier/hook_card.php?id='.$this->id;
501 
502  if ($option != 'nolink') {
503  // Add param to save lastsearch_values or not
504  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
505  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
506  $add_save_lastsearch_values = 1;
507  }
508  if ($add_save_lastsearch_values) {
509  $url .= '&save_lastsearch_values=1';
510  }
511  }
512 
513  $linkclose = '';
514  if (empty($notooltip)) {
515  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
516  $label = $langs->trans("ShowMyObject");
517  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
518  }
519  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
520  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
521  } else {
522  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
523  }
524 
525  $linkstart = '<a href="'.$url.'"';
526  $linkstart .= $linkclose.'>';
527  $linkend = '</a>';
528 
529  $result .= $linkstart;
530  if ($withpicto) {
531  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
532  }
533  if ($withpicto != 2) {
534  $result .= $this->ref;
535  }
536  $result .= $linkend;
537  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
538 
539  $hookmanager->initHooks(array('hookdao'));
540  $parameters = array(
541  'id' => $this->id,
542  'getnomurl' => &$result,
543  );
544  // Note that $action and $object may have been modified by some hooks
545  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);
546  if ($reshook > 0) {
547  $result = $hookmanager->resPrint;
548  } else {
549  $result .= $hookmanager->resPrint;
550  }
551 
552  return $result;
553  }
554 
566  public function getLibStatut($mode = 0)
567  {
568  return $this->LibStatut($this->status, $mode);
569  }
570 
571  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
580  public function LibStatut($status, $mode = 0)
581  {
582  // phpcs:enable
583  global $langs;
584 
585  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
586  global $langs;
587  //$langs->load("mymodule");
588  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
589  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
590  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
591  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
592  }
593 
594  $statusType = 'status5';
595  if ($status == self::STATUS_VALIDATED) {
596  $statusType = 'status4';
597  }
598 
599  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
600  }
601 
608  public function info($id)
609  {
610  $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
611  $sql .= ' fk_user_creat, fk_user_modif';
612  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
613  $sql .= ' WHERE t.rowid = '.((int) $id);
614  $result = $this->db->query($sql);
615  if ($result) {
616  if ($this->db->num_rows($result)) {
617  $obj = $this->db->fetch_object($result);
618 
619  $this->id = $obj->rowid;
620 
621  $this->user_creation_id = $obj->fk_user_creat;
622  $this->user_modification_id = $obj->fk_user_modif;
623  $this->date_creation = $this->db->jdate($obj->datec);
624  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
625  }
626 
627  $this->db->free($result);
628  } else {
629  dol_print_error($this->db);
630  }
631  }
632 
639  public function initAsSpecimen()
640  {
641  return $this->initAsSpecimenCommon();
642  }
643 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition: security.php:607
Parent class of all other business classes (invoices, contracts, proposals, orders,...
createCommon(User $user, $notrigger=0)
Create object in the database.
updateCommon(User $user, $notrigger=0)
Update object into database.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class for Hook.
Definition: hook.class.php:31
info($id)
Load the info information in the object.
Definition: hook.class.php:608
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: hook.class.php:639
__construct(DoliDB $db)
Constructor.
Definition: hook.class.php:243
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load object lines in memory from the database.
Definition: hook.class.php:394
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
Definition: hook.class.php:483
fetch($id, $ref=null)
Load object in memory from the database.
Definition: hook.class.php:359
getLibStatut($mode=0)
Return label of the status.
Definition: hook.class.php:566
create(User $user, $notrigger=0)
Create object into database.
Definition: hook.class.php:283
update(User $user, $notrigger=0)
Update object into database.
Definition: hook.class.php:455
createFromClone(User $user, $fromid)
Clone an object into another one.
Definition: hook.class.php:295
LibStatut($status, $mode=0)
Return the status.
Definition: hook.class.php:580
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.