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 * 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
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
30class 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 $fk_user_creat;
221
225 public $fk_user_modif;
226
230 public $import_key;
231
232
238 public function __construct(DoliDB $db)
239 {
240 global $conf, $langs, $user;
241
242 $this->db = $db;
243
244 $this->ismultientitymanaged = 0;
245 $this->isextrafieldmanaged = 1;
246
247 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
248 $this->fields['rowid']['visible'] = 0;
249 }
250 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
251 $this->fields['entity']['enabled'] = 0;
252 }
253
254 // Unset fields that are disabled
255 foreach ($this->fields as $key => $val) {
256 if (isset($val['enabled']) && empty($val['enabled'])) {
257 unset($this->fields[$key]);
258 }
259 }
260
261 // Translate some data of arrayofkeyval
262 foreach ($this->fields as $key => $val) {
263 if (is_array($this->fields['status']['arrayofkeyval'])) {
264 foreach ($this->fields['status']['arrayofkeyval'] as $key2 => $val2) {
265 $this->fields['status']['arrayofkeyval'][$key2] = $langs->trans($val2);
266 }
267 }
268 }
269 }
270
278 public function create(User $user, $notrigger = 0)
279 {
280 return $this->createCommon($user, $notrigger);
281 }
282
290 public function createFromClone(User $user, $fromid)
291 {
292 global $langs, $hookmanager, $extrafields;
293 $error = 0;
294
295 dol_syslog(__METHOD__, LOG_DEBUG);
296
297 $object = new self($this->db);
298
299 $this->db->begin();
300
301 // Load source object
302 $object->fetchCommon($fromid);
303 // Reset some properties
304 unset($object->id);
305 unset($object->fk_user_creat);
306 unset($object->import_key);
307
308 // Clear fields
309 $object->ref = "copy_of_".$object->ref;
310 $object->title = $langs->trans("CopyOf")." ".$object->title;
311 // ...
312 // Clear extrafields that are unique
313 if (is_array($object->array_options) && count($object->array_options) > 0) {
314 $extrafields->fetch_name_optionals_label($this->table_element);
315 foreach ($object->array_options as $key => $option) {
316 $shortkey = preg_replace('/options_/', '', $key);
317 if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
318 // var_dump($key);
319 // var_dump($clonedObj->array_options[$key]);
320 // exit;
321 unset($object->array_options[$key]);
322 }
323 }
324 }
325
326 // Create clone
327 $object->context['createfromclone'] = 'createfromclone';
328 $result = $object->createCommon($user);
329 if ($result < 0) {
330 $error++;
331 $this->error = $object->error;
332 $this->errors = $object->errors;
333 }
334
335 unset($object->context['createfromclone']);
336
337 // End
338 if (!$error) {
339 $this->db->commit();
340 return $object;
341 } else {
342 $this->db->rollback();
343 return -1;
344 }
345 }
346
354 public function fetch($id, $ref = null)
355 {
356 $result = $this->fetchCommon($id, $ref);
357 if ($result > 0 && !empty($this->table_element_line)) {
358 //$this->fetchLines();
359 }
360 return $result;
361 }
362
368 /*public function fetchLines()
369 {
370 $this->lines=array();
371
372 // Load lines with object MyObjectLine
373
374 return count($this->lines)?1:0;
375 }*/
376
389 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
390 {
391 global $conf;
392
393 dol_syslog(__METHOD__, LOG_DEBUG);
394
395 $records = array();
396
397 $sql = 'SELECT';
398 $sql .= ' t.rowid';
399 // TODO Get all fields
400 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
401 $sql .= ' WHERE t.entity = '.((int) $conf->entity);
402
403 // Manage filter
404 $errormessage = '';
405 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
406 if ($errormessage) {
407 $this->errors[] = $errormessage;
408 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
409 return -1;
410 }
411
412 if (!empty($sortfield)) {
413 $sql .= $this->db->order($sortfield, $sortorder);
414 }
415 if (!empty($limit)) {
416 $sql .= $this->db->plimit($limit, $offset);
417 }
418
419 $resql = $this->db->query($sql);
420 if ($resql) {
421 $num = $this->db->num_rows($resql);
422
423 while ($obj = $this->db->fetch_object($resql)) {
424 $record = new self($this->db);
425
426 $record->id = $obj->rowid;
427 // TODO Get other fields
428
429 //var_dump($record->id);
430 $records[$record->id] = $record;
431 }
432 $this->db->free($resql);
433
434 return $records;
435 } else {
436 $this->errors[] = 'Error '.$this->db->lasterror();
437 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
438
439 return -1;
440 }
441 }
442
450 public function update(User $user, $notrigger = 0)
451 {
452 return $this->updateCommon($user, $notrigger);
453 }
454
462 public function delete(User $user, $notrigger = 0)
463 {
464 return $this->deleteCommon($user, $notrigger);
465 //return $this->deleteCommon($user, $notrigger, 1);
466 }
467
478 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
479 {
480 global $db, $conf, $langs, $hookmanager, $action;
481 global $dolibarr_main_authentication, $dolibarr_main_demo;
482 global $menumanager;
483
484 if (!empty($conf->dol_no_mouse_hover)) {
485 // Force disable tooltips
486 $notooltip = 1;
487 }
488
489 $result = '';
490
491 $label = '<u>'.$langs->trans("Hook").'</u>';
492 $label .= '<br>';
493 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
494
495 $url = DOL_URL_ROOT.'/zapier/hook_card.php?id='.$this->id;
496
497 if ($option != 'nolink') {
498 // Add param to save lastsearch_values or not
499 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
500 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
501 $add_save_lastsearch_values = 1;
502 }
503 if ($add_save_lastsearch_values) {
504 $url .= '&save_lastsearch_values=1';
505 }
506 }
507
508 $linkclose = '';
509 if (empty($notooltip)) {
510 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
511 $label = $langs->trans("ShowMyObject");
512 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
513 }
514 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
515 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
516 } else {
517 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
518 }
519
520 $linkstart = '<a href="'.$url.'"';
521 $linkstart .= $linkclose.'>';
522 $linkend = '</a>';
523
524 $result .= $linkstart;
525 if ($withpicto) {
526 $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);
527 }
528 if ($withpicto != 2) {
529 $result .= $this->ref;
530 }
531 $result .= $linkend;
532 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
533
534 $hookmanager->initHooks(array('hookdao'));
535 $parameters = array(
536 'id' => $this->id,
537 'getnomurl' => &$result,
538 );
539 // Note that $action and $object may have been modified by some hooks
540 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);
541 if ($reshook > 0) {
542 $result = $hookmanager->resPrint;
543 } else {
544 $result .= $hookmanager->resPrint;
545 }
546
547 return $result;
548 }
549
561 public function getLibStatut($mode = 0)
562 {
563 return $this->LibStatut($this->status, $mode);
564 }
565
566 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
575 public function LibStatut($status, $mode = 0)
576 {
577 // phpcs:enable
578 global $langs;
579
580 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
581 global $langs;
582 //$langs->load("mymodule");
583 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
584 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
585 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
586 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
587 }
588
589 $statusType = 'status5';
590 if ($status == self::STATUS_VALIDATED) {
591 $statusType = 'status4';
592 }
593
594 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
595 }
596
603 public function info($id)
604 {
605 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
606 $sql .= ' fk_user_creat, fk_user_modif';
607 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
608 $sql .= ' WHERE t.rowid = '.((int) $id);
609 $result = $this->db->query($sql);
610 if ($result) {
611 if ($this->db->num_rows($result)) {
612 $obj = $this->db->fetch_object($result);
613
614 $this->id = $obj->rowid;
615
616 $this->user_creation_id = $obj->fk_user_creat;
617 $this->user_modification_id = $obj->fk_user_modif;
618 $this->date_creation = $this->db->jdate($obj->datec);
619 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
620 }
621
622 $this->db->free($result);
623 } else {
624 dol_print_error($this->db);
625 }
626 }
627
634 public function initAsSpecimen()
635 {
636 return $this->initAsSpecimenCommon();
637 }
638}
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:640
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 object lines 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.
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.