dolibarr  16.0.5
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 Frédéric France <frederic.france@netlogic.fr>
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
24 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25 
29 class Hook extends CommonObject
30 {
34  public $element = 'hook';
35 
39  public $table_element = 'zapier_hook';
40 
44  public $ismultientitymanaged = 0;
45 
49  public $isextrafieldmanaged = 1;
50 
54  public $picto = 'hook@zapier';
55 
56 
57  const STATUS_DRAFT = 0;
58  const STATUS_VALIDATED = 1;
59  const STATUS_DISABLED = -1;
60 
61 
85  public $fields = array(
86  'rowid' => array(
87  'type' => 'integer',
88  'label' => 'TechnicalID',
89  'enabled' => 1,
90  'visible' => -2,
91  'noteditable' => 1,
92  'notnull' => 1,
93  'index' => 1,
94  'position' => 1,
95  'comment' => 'Id',
96  ),
97  'entity' => array(
98  'type' => 'integer',
99  'label' => 'Entity',
100  'enabled' => 1,
101  'visible' => 0,
102  'notnull' => 1,
103  'default' => 1,
104  'index' => 1,
105  'position' => 20,
106  ),
107  'fk_user' => array(
108  'type' => 'integer',
109  'label' => 'UserOwner',
110  'enabled' => 1,
111  'visible' => -2,
112  'notnull' => 1,
113  'position' => 510,
114  'foreignkey' => 'llx_user.rowid',
115  ),
116  'url' => array(
117  'type' => 'varchar(255)',
118  'label' => 'Url',
119  'enabled' => 1,
120  'visible' => 1,
121  'position' => 30,
122  'searchall' => 1,
123  'css' => 'minwidth200',
124  'help' => 'Hook url'
125  ),
126  'module' => array(
127  'type' => 'varchar(128)',
128  'label' => 'Module',
129  'enabled' => 1,
130  'visible' => 1,
131  'position' => 30,
132  'searchall' => 1,
133  'css' => 'minwidth200',
134  'help' => 'Hook module'
135  ),
136  'action' => array(
137  'type' => 'varchar(128)',
138  'label' => 'Action',
139  'enabled' => 1,
140  'visible' => 1,
141  'position' => 30,
142  'searchall' => 1,
143  'css' => 'minwidth200',
144  'help' => 'Hook action trigger'
145  ),
146  'event' => array(
147  'type' => 'varchar(255)',
148  'label' => 'Event',
149  'enabled' => 1,
150  'visible' => 1,
151  'position' => 30,
152  'searchall' => 1,
153  'css' => 'minwidth200',
154  'help' => 'Event',
155  'showoncombobox' => 1,
156  ),
157  'date_creation' => array(
158  'type' => 'datetime',
159  'label' => 'DateCreation',
160  'enabled' => 1,
161  'visible' => -2,
162  'notnull' => 1,
163  'position' => 500,
164  ),
165  'import_key' => array(
166  'type' => 'varchar(14)',
167  'label' => 'ImportId',
168  'enabled' => 1,
169  'visible' => -2,
170  'notnull' => -1,
171  'index' => 0,
172  'position' => 1000,
173  ),
174  'status' => array(
175  'type' => 'integer',
176  'label' => 'Status',
177  'enabled' => 1,
178  'visible' => 1,
179  'notnull' => 1,
180  'default' => 0,
181  'index' => 1,
182  'position' => 1000,
183  'arrayofkeyval' => array(
184  0 => 'Draft',
185  1 => 'Active',
186  -1 => 'Canceled',
187  ),
188  ),
189  );
190 
194  public $rowid;
195 
199  public $ref;
200 
204  public $entity;
205 
209  public $label;
210 
214  public $url;
215 
219  public $fk_user;
220 
224  public $status;
225 
229  public $date_creation;
230 
234  public $tms;
235 
239  public $fk_user_creat;
240 
244  public $fk_user_modif;
245 
249  public $import_key;
250 
251 
257  public function __construct(DoliDB $db)
258  {
259  global $conf, $langs, $user;
260 
261  $this->db = $db;
262 
263  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
264  $this->fields['rowid']['visible'] = 0;
265  }
266  if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
267  $this->fields['entity']['enabled'] = 0;
268  }
269 
270  // Unset fields that are disabled
271  foreach ($this->fields as $key => $val) {
272  if (isset($val['enabled']) && empty($val['enabled'])) {
273  unset($this->fields[$key]);
274  }
275  }
276 
277  // Translate some data of arrayofkeyval
278  foreach ($this->fields as $key => $val) {
279  if (is_array($this->fields['status']['arrayofkeyval'])) {
280  foreach ($this->fields['status']['arrayofkeyval'] as $key2 => $val2) {
281  $this->fields['status']['arrayofkeyval'][$key2] = $langs->trans($val2);
282  }
283  }
284  }
285  }
286 
294  public function create(User $user, $notrigger = false)
295  {
296  return $this->createCommon($user, $notrigger);
297  }
298 
306  public function createFromClone(User $user, $fromid)
307  {
308  global $langs, $hookmanager, $extrafields;
309  $error = 0;
310 
311  dol_syslog(__METHOD__, LOG_DEBUG);
312 
313  $object = new self($this->db);
314 
315  $this->db->begin();
316 
317  // Load source object
318  $object->fetchCommon($fromid);
319  // Reset some properties
320  unset($object->id);
321  unset($object->fk_user_creat);
322  unset($object->import_key);
323 
324  // Clear fields
325  $object->ref = "copy_of_".$object->ref;
326  $object->title = $langs->trans("CopyOf")." ".$object->title;
327  // ...
328  // Clear extrafields that are unique
329  if (is_array($object->array_options) && count($object->array_options) > 0) {
330  $extrafields->fetch_name_optionals_label($this->table_element);
331  foreach ($object->array_options as $key => $option) {
332  $shortkey = preg_replace('/options_/', '', $key);
333  if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
334  // var_dump($key);
335  // var_dump($clonedObj->array_options[$key]);
336  // exit;
337  unset($object->array_options[$key]);
338  }
339  }
340  }
341 
342  // Create clone
343  $object->context['createfromclone'] = 'createfromclone';
344  $result = $object->createCommon($user);
345  if ($result < 0) {
346  $error++;
347  $this->error = $object->error;
348  $this->errors = $object->errors;
349  }
350 
351  unset($object->context['createfromclone']);
352 
353  // End
354  if (!$error) {
355  $this->db->commit();
356  return $object;
357  } else {
358  $this->db->rollback();
359  return -1;
360  }
361  }
362 
370  public function fetch($id, $ref = null)
371  {
372  $result = $this->fetchCommon($id, $ref);
373  if ($result > 0 && !empty($this->table_element_line)) {
374  //$this->fetchLines();
375  }
376  return $result;
377  }
378 
384  /*public function fetchLines()
385  {
386  $this->lines=array();
387 
388  // Load lines with object MyObjectLine
389 
390  return count($this->lines)?1:0;
391  }*/
392 
404  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
405  {
406  global $conf;
407 
408  dol_syslog(__METHOD__, LOG_DEBUG);
409 
410  $records = array();
411 
412  $sql = 'SELECT';
413  $sql .= ' t.rowid';
414  // TODO Get all fields
415  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
416  $sql .= ' WHERE t.entity = '.((int) $conf->entity);
417  // Manage filter
418  $sqlwhere = array();
419  if (count($filter) > 0) {
420  foreach ($filter as $key => $value) {
421  if ($key == 't.rowid') {
422  $sqlwhere[] = $key." = ".((int) $value);
423  } elseif (strpos($key, 'date') !== false) {
424  $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
425  } elseif ($key == 'customsql') {
426  $sqlwhere[] = $value;
427  } else {
428  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
429  }
430  }
431  }
432  if (count($sqlwhere) > 0) {
433  $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
434  }
435 
436  if (!empty($sortfield)) {
437  $sql .= $this->db->order($sortfield, $sortorder);
438  }
439  if (!empty($limit)) {
440  $sql .= $this->db->plimit($limit, $offset);
441  }
442 
443  $resql = $this->db->query($sql);
444  if ($resql) {
445  $num = $this->db->num_rows($resql);
446 
447  while ($obj = $this->db->fetch_object($resql)) {
448  $record = new self($this->db);
449 
450  $record->id = $obj->rowid;
451  // TODO Get other fields
452 
453  //var_dump($record->id);
454  $records[$record->id] = $record;
455  }
456  $this->db->free($resql);
457 
458  return $records;
459  } else {
460  $this->errors[] = 'Error '.$this->db->lasterror();
461  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
462 
463  return -1;
464  }
465  }
466 
474  public function update(User $user, $notrigger = false)
475  {
476  return $this->updateCommon($user, $notrigger);
477  }
478 
486  public function delete(User $user, $notrigger = false)
487  {
488  return $this->deleteCommon($user, $notrigger);
489  //return $this->deleteCommon($user, $notrigger, 1);
490  }
491 
502  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
503  {
504  global $db, $conf, $langs, $hookmanager, $action;
505  global $dolibarr_main_authentication, $dolibarr_main_demo;
506  global $menumanager;
507 
508  if (!empty($conf->dol_no_mouse_hover)) {
509  // Force disable tooltips
510  $notooltip = 1;
511  }
512 
513  $result = '';
514 
515  $label = '<u>'.$langs->trans("Hook").'</u>';
516  $label .= '<br>';
517  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
518 
519  $url = DOL_URL_ROOT.'/zapier/hook_card.php?id='.$this->id;
520 
521  if ($option != 'nolink') {
522  // Add param to save lastsearch_values or not
523  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
524  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
525  $add_save_lastsearch_values = 1;
526  }
527  if ($add_save_lastsearch_values) {
528  $url .= '&save_lastsearch_values=1';
529  }
530  }
531 
532  $linkclose = '';
533  if (empty($notooltip)) {
534  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
535  $label = $langs->trans("ShowMyObject");
536  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
537  }
538  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
539  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
540  } else {
541  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
542  }
543 
544  $linkstart = '<a href="'.$url.'"';
545  $linkstart .= $linkclose.'>';
546  $linkend = '</a>';
547 
548  $result .= $linkstart;
549  if ($withpicto) {
550  $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);
551  }
552  if ($withpicto != 2) {
553  $result .= $this->ref;
554  }
555  $result .= $linkend;
556  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
557 
558  $hookmanager->initHooks(array('hookdao'));
559  $parameters = array(
560  'id' => $this->id,
561  'getnomurl' => &$result,
562  );
563  // Note that $action and $object may have been modified by some hooks
564  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);
565  if ($reshook > 0) {
566  $result = $hookmanager->resPrint;
567  } else {
568  $result .= $hookmanager->resPrint;
569  }
570 
571  return $result;
572  }
573 
585  public function getLibStatut($mode = 0)
586  {
587  return $this->LibStatut($this->status, $mode);
588  }
589 
590  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
599  public function LibStatut($status, $mode = 0)
600  {
601  // phpcs:enable
602  global $langs;
603 
604  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
605  global $langs;
606  //$langs->load("mymodule");
607  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
608  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
609  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Disabled');
610  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
611  }
612 
613  $statusType = 'status5';
614  if ($status == self::STATUS_VALIDATED) {
615  $statusType = 'status4';
616  }
617 
618  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
619  }
620 
627  public function info($id)
628  {
629  $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
630  $sql .= ' fk_user_creat, fk_user_modif';
631  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
632  $sql .= ' WHERE t.rowid = '.((int) $id);
633  $result = $this->db->query($sql);
634  if ($result) {
635  if ($this->db->num_rows($result)) {
636  $obj = $this->db->fetch_object($result);
637  $this->id = $obj->rowid;
638 
639 
640  $this->user_creation_id = $obj->fk_user_creat;
641  $this->user_modification_id = $obj->fk_user_modif;
642  $this->date_creation = $this->db->jdate($obj->datec);
643  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
644  }
645 
646  $this->db->free($result);
647  } else {
648  dol_print_error($this->db);
649  }
650  }
651 
658  public function initAsSpecimen()
659  {
660  $this->initAsSpecimenCommon();
661  }
662 
663 
670  public function doScheduledJob()
671  {
672  global $conf, $langs;
673 
674  //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
675 
676  $error = 0;
677  $this->output = '';
678  $this->error = '';
679 
680  dol_syslog(__METHOD__, LOG_DEBUG);
681 
682  $now = dol_now();
683 
684  $this->db->begin();
685 
686  // ...
687 
688  $this->db->commit();
689 
690  return $error;
691  }
692 }
693 
697 /*
698 class MyObjectLine
699 {
700  // @var int ID
701  public $id;
702  // @var mixed Sample line property 1
703  public $prop1;
704  // @var mixed Sample line property 2
705  public $prop2;
706 }
707 */
CommonObject\deleteCommon
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
Definition: commonobject.class.php:9406
db
$conf db
API class for accounts.
Definition: inc.php:41
CommonObject\fetchCommon
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
Definition: commonobject.class.php:9202
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
Hook\LibStatut
LibStatut($status, $mode=0)
Return the status.
Definition: hook.class.php:599
Hook\fetchAll
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object lines in memory from the database.
Definition: hook.class.php:404
Hook\getLibStatut
getLibStatut($mode=0)
Return label of the status.
Definition: hook.class.php:585
CommonObject\initAsSpecimenCommon
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: commonobject.class.php:9733
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
Hook\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
Definition: hook.class.php:502
Hook\info
info($id)
Load the info information in the object.
Definition: hook.class.php:627
CommonObject\createCommon
createCommon(User $user, $notrigger=false)
Create object into database.
Definition: commonobject.class.php:9035
Hook\update
update(User $user, $notrigger=false)
Update object into database.
Definition: hook.class.php:474
Hook\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: hook.class.php:658
Hook
Class for Hook.
Definition: hook.class.php:29
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Hook\create
create(User $user, $notrigger=false)
Create object into database.
Definition: hook.class.php:294
CommonObject\updateCommon
updateCommon(User $user, $notrigger=false)
Update object into database.
Definition: commonobject.class.php:9308
Hook\__construct
__construct(DoliDB $db)
Constructor.
Definition: hook.class.php:257
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Hook\fetch
fetch($id, $ref=null)
Load object in memory from the database.
Definition: hook.class.php:370
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Hook\createFromClone
createFromClone(User $user, $fromid)
Clone an object into another one.
Definition: hook.class.php:306
Hook\doScheduledJob
doScheduledJob()
Action executed by scheduler CAN BE A CRON TASK.
Definition: hook.class.php:670