dolibarr 21.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-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 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 $entity;
197
201 public $label;
202
206 public $url;
207
211 public $fk_user;
212
216 public $status;
217
221 public $fk_user_creat;
222
226 public $fk_user_modif;
227
231 public $import_key;
232
233
239 public function __construct(DoliDB $db)
240 {
241 global $conf, $langs, $user;
242
243 $this->db = $db;
244
245 $this->ismultientitymanaged = 0;
246 $this->isextrafieldmanaged = 1;
247
248 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
249 $this->fields['rowid']['visible'] = 0;
250 }
251 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
252 $this->fields['entity']['enabled'] = 0;
253 }
254
255 // Unset fields that are disabled
256 foreach ($this->fields as $key => $val) {
257 if (isset($val['enabled']) && empty($val['enabled'])) {
258 unset($this->fields[$key]);
259 }
260 }
261
262 // Translate some data of arrayofkeyval
263 foreach ($this->fields as $key => $val) {
264 if (is_array($this->fields['status']['arrayofkeyval'])) {
265 foreach ($this->fields['status']['arrayofkeyval'] as $key2 => $val2) {
266 $this->fields['status']['arrayofkeyval'][$key2] = $langs->trans($val2);
267 }
268 }
269 }
270 }
271
279 public function create(User $user, $notrigger = 0)
280 {
281 return $this->createCommon($user, $notrigger);
282 }
283
291 public function createFromClone(User $user, $fromid)
292 {
293 global $langs, $hookmanager, $extrafields;
294 $error = 0;
295
296 dol_syslog(__METHOD__, LOG_DEBUG);
297
298 $object = new self($this->db);
299
300 $this->db->begin();
301
302 // Load source object
303 $object->fetchCommon($fromid);
304 // Reset some properties
305 unset($object->id);
306 unset($object->fk_user_creat);
307 unset($object->import_key);
308
309 // Clear fields
310 $object->ref = "copy_of_".$object->ref;
311 $object->title = $langs->trans("CopyOf")." ".$object->title;
312 // ...
313 // Clear extrafields that are unique
314 if (is_array($object->array_options) && count($object->array_options) > 0) {
315 $extrafields->fetch_name_optionals_label($this->table_element);
316 foreach ($object->array_options as $key => $option) {
317 $shortkey = preg_replace('/options_/', '', $key);
318 if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
319 // var_dump($key);
320 // var_dump($clonedObj->array_options[$key]);
321 // exit;
322 unset($object->array_options[$key]);
323 }
324 }
325 }
326
327 // Create clone
328 $object->context['createfromclone'] = 'createfromclone';
329 $result = $object->createCommon($user);
330 if ($result < 0) {
331 $error++;
332 $this->error = $object->error;
333 $this->errors = $object->errors;
334 }
335
336 unset($object->context['createfromclone']);
337
338 // End
339 if (!$error) {
340 $this->db->commit();
341 return $object;
342 } else {
343 $this->db->rollback();
344 return -1;
345 }
346 }
347
355 public function fetch($id, $ref = null)
356 {
357 $result = $this->fetchCommon($id, $ref);
358
359 return $result;
360 }
361
374 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
375 {
376 global $conf;
377
378 dol_syslog(__METHOD__, LOG_DEBUG);
379
380 $records = array();
381
382 $sql = 'SELECT';
383 $sql .= ' t.rowid';
384 // TODO Get all fields
385 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
386 $sql .= ' WHERE t.entity = '.((int) $conf->entity);
387
388 // Manage filter
389 $errormessage = '';
390 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
391 if ($errormessage) {
392 $this->errors[] = $errormessage;
393 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
394 return -1;
395 }
396
397 if (!empty($sortfield)) {
398 $sql .= $this->db->order($sortfield, $sortorder);
399 }
400 if (!empty($limit)) {
401 $sql .= $this->db->plimit($limit, $offset);
402 }
403
404 $resql = $this->db->query($sql);
405 if ($resql) {
406 $num = $this->db->num_rows($resql);
407
408 while ($obj = $this->db->fetch_object($resql)) {
409 $record = new self($this->db);
410
411 $record->id = $obj->rowid;
412 // TODO Get other fields
413
414 //var_dump($record->id);
415 $records[$record->id] = $record;
416 }
417 $this->db->free($resql);
418
419 return $records;
420 } else {
421 $this->errors[] = 'Error '.$this->db->lasterror();
422 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
423
424 return -1;
425 }
426 }
427
435 public function update(User $user, $notrigger = 0)
436 {
437 return $this->updateCommon($user, $notrigger);
438 }
439
447 public function delete(User $user, $notrigger = 0)
448 {
449 return $this->deleteCommon($user, $notrigger);
450 //return $this->deleteCommon($user, $notrigger, 1);
451 }
452
463 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
464 {
465 global $db, $conf, $langs, $hookmanager, $action;
466 global $dolibarr_main_authentication, $dolibarr_main_demo;
467 global $menumanager;
468
469 if (!empty($conf->dol_no_mouse_hover)) {
470 // Force disable tooltips
471 $notooltip = 1;
472 }
473
474 $result = '';
475
476 $label = '<u>'.$langs->trans("Hook").'</u>';
477 $label .= '<br>';
478 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
479
480 $url = DOL_URL_ROOT.'/zapier/hook_card.php?id='.$this->id;
481
482 if ($option != 'nolink') {
483 // Add param to save lastsearch_values or not
484 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
485 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
486 $add_save_lastsearch_values = 1;
487 }
488 if ($add_save_lastsearch_values) {
489 $url .= '&save_lastsearch_values=1';
490 }
491 }
492
493 $linkclose = '';
494 if (empty($notooltip)) {
495 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
496 $label = $langs->trans("ShowMyObject");
497 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
498 }
499 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
500 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
501 } else {
502 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
503 }
504
505 $linkstart = '<a href="'.$url.'"';
506 $linkstart .= $linkclose.'>';
507 $linkend = '</a>';
508
509 $result .= $linkstart;
510 if ($withpicto) {
511 $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);
512 }
513 if ($withpicto != 2) {
514 $result .= $this->ref;
515 }
516 $result .= $linkend;
517 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
518
519 $hookmanager->initHooks(array('hookdao'));
520 $parameters = array(
521 'id' => $this->id,
522 'getnomurl' => &$result,
523 );
524 // Note that $action and $object may have been modified by some hooks
525 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);
526 if ($reshook > 0) {
527 $result = $hookmanager->resPrint;
528 } else {
529 $result .= $hookmanager->resPrint;
530 }
531
532 return $result;
533 }
534
546 public function getLibStatut($mode = 0)
547 {
548 return $this->LibStatut($this->status, $mode);
549 }
550
551 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
560 public function LibStatut($status, $mode = 0)
561 {
562 // phpcs:enable
563 global $langs;
564
565 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
566 global $langs;
567 //$langs->load("mymodule");
568 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
569 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
570 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
571 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
572 }
573
574 $statusType = 'status5';
575 if ($status == self::STATUS_VALIDATED) {
576 $statusType = 'status4';
577 }
578
579 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
580 }
581
588 public function info($id)
589 {
590 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
591 $sql .= ' fk_user_creat, fk_user_modif';
592 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
593 $sql .= ' WHERE t.rowid = '.((int) $id);
594 $result = $this->db->query($sql);
595 if ($result) {
596 if ($this->db->num_rows($result)) {
597 $obj = $this->db->fetch_object($result);
598
599 $this->id = $obj->rowid;
600
601 $this->user_creation_id = $obj->fk_user_creat;
602 $this->user_modification_id = $obj->fk_user_modif;
603 $this->date_creation = $this->db->jdate($obj->datec);
604 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
605 }
606
607 $this->db->free($result);
608 } else {
609 dol_print_error($this->db);
610 }
611 }
612
619 public function initAsSpecimen()
620 {
621 return $this->initAsSpecimenCommon();
622 }
623}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.
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:162
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
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.