dolibarr 23.0.3
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-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27
31class Hook extends CommonObject
32{
36 public $element = 'hook';
37
41 public $table_element = 'zapier_hook';
42
46 public $picto = 'hook@zapier';
47
48
49 const STATUS_DRAFT = 0;
50 const STATUS_VALIDATED = 1;
51 const STATUS_DISABLED = -1;
52
53
77 public $fields = array(
78 'rowid' => array(
79 'type' => 'integer',
80 'label' => 'TechnicalID',
81 'enabled' => 1,
82 'visible' => -2,
83 'noteditable' => 1,
84 'notnull' => 1,
85 'index' => 1,
86 'position' => 1,
87 'comment' => 'Id',
88 ),
89 'entity' => array(
90 'type' => 'integer',
91 'label' => 'Entity',
92 'enabled' => 1,
93 'visible' => 0,
94 'notnull' => 1,
95 'default' => '1',
96 'index' => 1,
97 'position' => 20,
98 ),
99 'fk_user' => array(
100 'type' => 'integer',
101 'label' => 'UserOwner',
102 'enabled' => 1,
103 'visible' => -2,
104 'notnull' => 1,
105 'position' => 510,
106 'foreignkey' => 'llx_user.rowid',
107 ),
108 'url' => array(
109 'type' => 'varchar(255)',
110 'label' => 'Url',
111 'enabled' => 1,
112 'visible' => 1,
113 'position' => 30,
114 'searchall' => 1,
115 'css' => 'minwidth200',
116 'help' => 'Hook url'
117 ),
118 'module' => array(
119 'type' => 'varchar(128)',
120 'label' => 'Module',
121 'enabled' => 1,
122 'visible' => 1,
123 'position' => 30,
124 'searchall' => 1,
125 'css' => 'minwidth200',
126 'help' => 'Hook module'
127 ),
128 'action' => array(
129 'type' => 'varchar(128)',
130 'label' => 'Action',
131 'enabled' => 1,
132 'visible' => 1,
133 'position' => 30,
134 'searchall' => 1,
135 'css' => 'minwidth200',
136 'help' => 'Hook action trigger'
137 ),
138 'event' => array(
139 'type' => 'varchar(255)',
140 'label' => 'Event',
141 'enabled' => 1,
142 'visible' => 1,
143 'position' => 30,
144 'searchall' => 1,
145 'css' => 'minwidth200',
146 'help' => 'Event',
147 'showoncombobox' => 1,
148 ),
149 'date_creation' => array(
150 'type' => 'datetime',
151 'label' => 'DateCreation',
152 'enabled' => 1,
153 'visible' => -2,
154 'notnull' => 1,
155 'position' => 500,
156 ),
157 'import_key' => array(
158 'type' => 'varchar(14)',
159 'label' => 'ImportId',
160 'enabled' => 1,
161 'visible' => -2,
162 'notnull' => -1,
163 'index' => 0,
164 'position' => 1000,
165 ),
166 'status' => array(
167 'type' => 'integer',
168 'label' => 'Status',
169 'enabled' => 1,
170 'visible' => 1,
171 'notnull' => 1,
172 'default' => '0',
173 'index' => 1,
174 'position' => 1000,
175 'arrayofkeyval' => array(
176 0 => 'Draft',
177 1 => 'Active',
178 -1 => 'Canceled',
179 ),
180 ),
181 );
182
186 public $rowid;
187
191 public $ref;
192
196 public $label;
197
201 public $url;
202
206 public $fk_user;
207
211 public $status;
212
216 public $fk_user_creat;
217
221 public $fk_user_modif;
222
226 public $import_key;
227
228
234 public function __construct(DoliDB $db)
235 {
236 global $langs;
237
238 $this->db = $db;
239
240 $this->ismultientitymanaged = 0;
241 $this->isextrafieldmanaged = 1;
242
243 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
244 $this->fields['rowid']['visible'] = 0;
245 }
246 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
247 $this->fields['entity']['enabled'] = 0;
248 }
249
250 // Unset fields that are disabled
251 foreach ($this->fields as $key => $val) {
252 if (isset($val['enabled']) && empty($val['enabled'])) {
253 unset($this->fields[$key]);
254 }
255 }
256
257 // Translate some data of arrayofkeyval
258 foreach ($this->fields as $key => $val) {
259 if (is_array($this->fields['status']['arrayofkeyval'])) {
260 foreach ($this->fields['status']['arrayofkeyval'] as $key2 => $val2) {
261 $this->fields['status']['arrayofkeyval'][$key2] = $langs->trans($val2);
262 }
263 }
264 }
265 }
266
274 public function create(User $user, $notrigger = 0)
275 {
276 return $this->createCommon($user, $notrigger);
277 }
278
286 public function createFromClone(User $user, $fromid)
287 {
288 global $langs, $extrafields;
289 $error = 0;
290
291 dol_syslog(__METHOD__, LOG_DEBUG);
292
293 $object = new self($this->db);
294
295 $this->db->begin();
296
297 // Load source object
298 $object->fetchCommon($fromid);
299 // Reset some properties
300 unset($object->id);
301 unset($object->fk_user_creat);
302 unset($object->import_key);
303
304 // Clear fields
305 $object->ref = "copy_of_".$object->ref;
306 $object->label = $langs->trans("CopyOf")." ".$object->label;
307 // ...
308 // Clear extrafields that are unique
309 if (is_array($object->array_options) && count($object->array_options) > 0) {
310 $extrafields->fetch_name_optionals_label($this->table_element);
311 foreach ($object->array_options as $key => $option) {
312 $shortkey = preg_replace('/options_/', '', $key);
313 if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
314 // var_dump($key);
315 // var_dump($clonedObj->array_options[$key]);
316 // exit;
317 unset($object->array_options[$key]);
318 }
319 }
320 }
321
322 // Create clone
323 $object->context['createfromclone'] = 'createfromclone';
324 $result = $object->createCommon($user);
325 if ($result < 0) {
326 $error++;
328 }
329
330 unset($object->context['createfromclone']);
331
332 // End
333 if (!$error) {
334 $this->db->commit();
335 return $object;
336 } else {
337 $this->db->rollback();
338 return -1;
339 }
340 }
341
349 public function fetch($id, $ref = null)
350 {
351 $result = $this->fetchCommon($id, $ref);
352
353 return $result;
354 }
355
368 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
369 {
370 global $conf;
371
372 dol_syslog(__METHOD__, LOG_DEBUG);
373
374 $records = array();
375
376 $sql = 'SELECT';
377 $sql .= ' t.rowid';
378 // TODO Get all fields
379 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
380 $sql .= ' WHERE t.entity = '.((int) $conf->entity);
381
382 // Manage filter
383 $errormessage = '';
384 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
385 if ($errormessage) {
386 $this->errors[] = $errormessage;
387 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
388 return -1;
389 }
390
391 if (!empty($sortfield)) {
392 $sql .= $this->db->order($sortfield, $sortorder);
393 }
394 if (!empty($limit)) {
395 $sql .= $this->db->plimit($limit, $offset);
396 }
397
398 $resql = $this->db->query($sql);
399 if ($resql) {
400 $num = $this->db->num_rows($resql);
401
402 while ($obj = $this->db->fetch_object($resql)) {
403 $record = new self($this->db);
404
405 $record->id = $obj->rowid;
406 // TODO Get other fields
407
408 //var_dump($record->id);
409 $records[$record->id] = $record;
410 }
411 $this->db->free($resql);
412
413 return $records;
414 } else {
415 $this->errors[] = 'Error '.$this->db->lasterror();
416 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
417
418 return -1;
419 }
420 }
421
429 public function update(User $user, $notrigger = 0)
430 {
431 return $this->updateCommon($user, $notrigger);
432 }
433
441 public function delete(User $user, $notrigger = 0)
442 {
443 return $this->deleteCommon($user, $notrigger);
444 //return $this->deleteCommon($user, $notrigger, 1);
445 }
446
457 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
458 {
459 global $db, $conf, $langs, $hookmanager, $action;
460 global $dolibarr_main_authentication, $dolibarr_main_demo;
461 global $menumanager;
462
463 if (!empty($conf->dol_no_mouse_hover)) {
464 // Force disable tooltips
465 $notooltip = 1;
466 }
467
468 $result = '';
469
470 $label = '<u>'.$langs->trans("Hook").'</u>';
471 $label .= '<br>';
472 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
473
474 $url = DOL_URL_ROOT.'/zapier/hook_card.php?id='.$this->id;
475
476 if ($option != 'nolink') {
477 // Add param to save lastsearch_values or not
478 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
479 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
480 $add_save_lastsearch_values = 1;
481 }
482 if ($add_save_lastsearch_values) {
483 $url .= '&save_lastsearch_values=1';
484 }
485 }
486
487 $linkclose = '';
488 if (empty($notooltip)) {
489 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
490 $label = $langs->trans("ShowMyObject");
491 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
492 }
493 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
494 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
495 } else {
496 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
497 }
498
499 $linkstart = '<a href="'.$url.'"';
500 $linkstart .= $linkclose.'>';
501 $linkend = '</a>';
502
503 $result .= $linkstart;
504 if ($withpicto) {
505 $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);
506 }
507 if ($withpicto != 2) {
508 $result .= $this->ref;
509 }
510 $result .= $linkend;
511 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
512
513 $hookmanager->initHooks(array('hookdao'));
514 $parameters = array(
515 'id' => $this->id,
516 'getnomurl' => &$result,
517 );
518 // Note that $action and $object may have been modified by some hooks
519 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);
520 if ($reshook > 0) {
521 $result = $hookmanager->resPrint;
522 } else {
523 $result .= $hookmanager->resPrint;
524 }
525
526 return $result;
527 }
528
540 public function getLibStatut($mode = 0)
541 {
542 return $this->LibStatut($this->status, $mode);
543 }
544
545 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
554 public function LibStatut($status, $mode = 0)
555 {
556 // phpcs:enable
557 global $langs;
558
559 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
560 global $langs;
561 //$langs->load("mymodule");
562 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
563 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
564 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
565 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
566 }
567
568 $statusType = 'status5';
569 if ($status == self::STATUS_VALIDATED) {
570 $statusType = 'status4';
571 }
572
573 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
574 }
575
582 public function info($id)
583 {
584 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
585 $sql .= ' fk_user_creat, fk_user_modif';
586 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
587 $sql .= ' WHERE t.rowid = '.((int) $id);
588 $result = $this->db->query($sql);
589 if ($result) {
590 if ($this->db->num_rows($result)) {
591 $obj = $this->db->fetch_object($result);
592
593 $this->id = $obj->rowid;
594
595 $this->user_creation_id = $obj->fk_user_creat;
596 $this->user_modification_id = $obj->fk_user_modif;
597 $this->date_creation = $this->db->jdate($obj->datec);
598 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
599 }
600
601 $this->db->free($result);
602 } else {
603 dol_print_error($this->db);
604 }
605 }
606
613 public function initAsSpecimen()
614 {
615 return $this->initAsSpecimenCommon();
616 }
617}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class of all other business classes (invoices, contracts, proposals, orders,...
setErrorsFromObject($object)
setErrorsFromObject
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.
info($id)
Load the info information in the object.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
__construct(DoliDB $db)
Constructor.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load list of objects in memory from the database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
fetch($id, $ref=null)
Load object in memory from the database.
getLibStatut($mode=0)
Return label of the status.
create(User $user, $notrigger=0)
Create object into database.
update(User $user, $notrigger=0)
Update object into database.
createFromClone(User $user, $fromid)
Clone an object into another one.
LibStatut($status, $mode=0)
Return the status.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
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 a 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.