dolibarr 25.0.0-alpha
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-2026 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 if (is_object($langs)) {
259 foreach ($this->fields as $key => $val) {
260 if (is_array($this->fields['status']['arrayofkeyval'])) {
261 foreach ($this->fields['status']['arrayofkeyval'] as $key2 => $val2) {
262 $this->fields['status']['arrayofkeyval'][$key2] = $langs->trans($val2);
263 }
264 }
265 }
266 }
267 }
268
276 public function create(User $user, $notrigger = 0)
277 {
278 return $this->createCommon($user, $notrigger);
279 }
280
288 public function createFromClone(User $user, $fromid)
289 {
290 global $langs, $extrafields;
291 $error = 0;
292
293 dol_syslog(__METHOD__, LOG_DEBUG);
294
295 $object = new self($this->db);
296
297 $this->db->begin();
298
299 // Load source object
300 $object->fetchCommon($fromid);
301 // Reset some properties
302 unset($object->id);
303 unset($object->fk_user_creat);
304 unset($object->import_key);
305
306 // Clear fields
307 $object->ref = "copy_of_".$object->ref;
308 $object->label = $langs->trans("CopyOf")." ".$object->label;
309 // ...
310 // Clear extrafields that are unique
311 if (is_array($object->array_options) && count($object->array_options) > 0) {
312 $extrafields->fetch_name_optionals_label($this->table_element);
313 foreach ($object->array_options as $key => $option) {
314 $shortkey = preg_replace('/options_/', '', $key);
315 if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
316 // var_dump($key);
317 // var_dump($clonedObj->array_options[$key]);
318 // exit;
319 unset($object->array_options[$key]);
320 }
321 }
322 }
323
324 // Create clone
325 $object->context['createfromclone'] = 'createfromclone';
326 $result = $object->createCommon($user);
327 if ($result < 0) {
328 $error++;
330 }
331
332 unset($object->context['createfromclone']);
333
334 // End
335 if (!$error) {
336 $this->db->commit();
337 return $object;
338 } else {
339 $this->db->rollback();
340 return -1;
341 }
342 }
343
351 public function fetch($id, $ref = null)
352 {
353 $result = $this->fetchCommon($id, $ref);
354
355 return $result;
356 }
357
370 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
371 {
372 global $conf;
373
374 dol_syslog(__METHOD__, LOG_DEBUG);
375
376 $records = array();
377
378 $sql = 'SELECT';
379 $sql .= ' t.rowid';
380 // TODO Get all fields
381 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
382 $sql .= ' WHERE t.entity = '.((int) $conf->entity);
383
384 // Manage filter
385 $errormessage = '';
386 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
387 if ($errormessage) {
388 $this->errors[] = $errormessage;
389 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
390 return -1;
391 }
392
393 if (!empty($sortfield)) {
394 $sql .= $this->db->order($sortfield, $sortorder);
395 }
396 if (!empty($limit)) {
397 $sql .= $this->db->plimit($limit, $offset);
398 }
399
400 $resql = $this->db->query($sql);
401 if ($resql) {
402 $num = $this->db->num_rows($resql);
403
404 while ($obj = $this->db->fetch_object($resql)) {
405 $record = new self($this->db);
406
407 $record->id = $obj->rowid;
408 // TODO Get other fields
409
410 //var_dump($record->id);
411 $records[$record->id] = $record;
412 }
413 $this->db->free($resql);
414
415 return $records;
416 } else {
417 $this->errors[] = 'Error '.$this->db->lasterror();
418 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
419
420 return -1;
421 }
422 }
423
431 public function update(User $user, $notrigger = 0)
432 {
433 return $this->updateCommon($user, $notrigger);
434 }
435
443 public function delete(User $user, $notrigger = 0)
444 {
445 return $this->deleteCommon($user, $notrigger);
446 //return $this->deleteCommon($user, $notrigger, 1);
447 }
448
459 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
460 {
461 global $db, $conf, $langs, $hookmanager, $action;
462 global $dolibarr_main_authentication, $dolibarr_main_demo;
463 global $menumanager;
464
465 if (!empty($conf->dol_no_mouse_hover)) {
466 // Force disable tooltips
467 $notooltip = 1;
468 }
469
470 $result = '';
471
472 $label = '<u>'.$langs->trans("Hook").'</u>';
473 $label .= '<br>';
474 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
475
476 $url = DOL_URL_ROOT.'/zapier/hook_card.php?id='.$this->id;
477
478 if ($option != 'nolink') {
479 // Add param to save lastsearch_values or not
480 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
481 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
482 $add_save_lastsearch_values = 1;
483 }
484 if ($add_save_lastsearch_values) {
485 $url .= '&save_lastsearch_values=1';
486 }
487 }
488
489 $linkclose = '';
490 if (empty($notooltip)) {
491 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
492 $label = $langs->trans("ShowMyObject");
493 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
494 }
495 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
496 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
497 } else {
498 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
499 }
500
501 $linkstart = '<a href="'.$url.'"';
502 $linkstart .= $linkclose.'>';
503 $linkend = '</a>';
504
505 $result .= $linkstart;
506 if ($withpicto) {
507 $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);
508 }
509 if ($withpicto != 2) {
510 $result .= $this->ref;
511 }
512 $result .= $linkend;
513 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
514
515 $hookmanager->initHooks(array('hookdao'));
516 $parameters = array(
517 'id' => $this->id,
518 'getnomurl' => &$result,
519 );
520 // Note that $action and $object may have been modified by some hooks
521 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);
522 if ($reshook > 0) {
523 $result = $hookmanager->resPrint;
524 } else {
525 $result .= $hookmanager->resPrint;
526 }
527
528 return $result;
529 }
530
542 public function getLibStatut($mode = 0)
543 {
544 return $this->LibStatut($this->status, $mode);
545 }
546
547 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
556 public function LibStatut($status, $mode = 0)
557 {
558 // phpcs:enable
559 global $langs;
560
561 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
562 global $langs;
563 //$langs->load("mymodule");
564 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
565 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
566 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
567 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
568 }
569
570 $statusType = 'status5';
571 if ($status == self::STATUS_VALIDATED) {
572 $statusType = 'status4';
573 }
574
575 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
576 }
577
584 public function info($id)
585 {
586 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
587 $sql .= ' fk_user_creat, fk_user_modif';
588 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
589 $sql .= ' WHERE t.rowid = '.((int) $id);
590 $result = $this->db->query($sql);
591 if ($result) {
592 if ($this->db->num_rows($result)) {
593 $obj = $this->db->fetch_object($result);
594
595 $this->id = $obj->rowid;
596
597 $this->user_creation_id = $obj->fk_user_creat;
598 $this->user_modification_id = $obj->fk_user_modif;
599 $this->date_creation = $this->db->jdate($obj->datec);
600 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
601 }
602
603 $this->db->free($result);
604 } else {
605 dol_print_error($this->db);
606 }
607 }
608
615 public function initAsSpecimen()
616 {
617 return $this->initAsSpecimenCommon();
618 }
619}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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:169
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
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.