dolibarr  13.0.2
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  'showoncombobox' => 1,
126  ),
127  'module' => array(
128  'type' => 'varchar(128)',
129  'label' => 'Module',
130  'enabled' => 1,
131  'visible' => 1,
132  'position' => 30,
133  'searchall' => 1,
134  'css' => 'minwidth200',
135  'help' => 'Hook module',
136  'showoncombobox' => 1,
137  ),
138  'action' => array(
139  'type' => 'varchar(128)',
140  'label' => 'Action',
141  'enabled' => 1,
142  'visible' => 1,
143  'position' => 30,
144  'searchall' => 1,
145  'css' => 'minwidth200',
146  'help' => 'Hook action trigger',
147  'showoncombobox' => 1,
148  ),
149  'event' => array(
150  'type' => 'varchar(255)',
151  'label' => 'Event',
152  'enabled' => 1,
153  'visible' => 1,
154  'position' => 30,
155  'searchall' => 1,
156  'css' => 'minwidth200',
157  'help' => 'Event',
158  'showoncombobox' => 1,
159  ),
160  'date_creation' => array(
161  'type' => 'datetime',
162  'label' => 'DateCreation',
163  'enabled' => 1,
164  'visible' => -2,
165  'notnull' => 1,
166  'position' => 500,
167  ),
168  'import_key' => array(
169  'type' => 'varchar(14)',
170  'label' => 'ImportId',
171  'enabled' => 1,
172  'visible' => -2,
173  'notnull' => -1,
174  'index' => 0,
175  'position' => 1000,
176  ),
177  'status' => array(
178  'type' => 'integer',
179  'label' => 'Status',
180  'enabled' => 1,
181  'visible' => 1,
182  'notnull' => 1,
183  'default' => 0,
184  'index' => 1,
185  'position' => 1000,
186  'arrayofkeyval' => array(
187  0 => 'Draft',
188  1 => 'Active',
189  -1 => 'Canceled',
190  ),
191  ),
192  );
193 
197  public $rowid;
198 
202  public $ref;
203 
207  public $entity;
208 
212  public $label;
213 
217  public $url;
218 
222  public $fk_user;
223 
227  public $status;
228 
232  public $date_creation;
233 
237  public $tms;
238 
242  public $fk_user_creat;
243 
247  public $fk_user_modif;
248 
252  public $import_key;
253 
254 
260  public function __construct(DoliDB $db)
261  {
262  global $conf, $langs, $user;
263 
264  $this->db = $db;
265 
266  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
267  $this->fields['rowid']['visible'] = 0;
268  }
269  if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
270  $this->fields['entity']['enabled'] = 0;
271  }
272 
273  // Unset fields that are disabled
274  foreach ($this->fields as $key => $val) {
275  if (isset($val['enabled']) && empty($val['enabled'])) {
276  unset($this->fields[$key]);
277  }
278  }
279 
280  // Translate some data of arrayofkeyval
281  foreach ($this->fields as $key => $val) {
282  if (is_array($this->fields['status']['arrayofkeyval'])) {
283  foreach ($this->fields['status']['arrayofkeyval'] as $key2 => $val2) {
284  $this->fields['status']['arrayofkeyval'][$key2] = $langs->trans($val2);
285  }
286  }
287  }
288  }
289 
297  public function create(User $user, $notrigger = false)
298  {
299  return $this->createCommon($user, $notrigger);
300  }
301 
309  public function createFromClone(User $user, $fromid)
310  {
311  global $langs, $hookmanager, $extrafields;
312  $error = 0;
313 
314  dol_syslog(__METHOD__, LOG_DEBUG);
315 
316  $object = new self($this->db);
317 
318  $this->db->begin();
319 
320  // Load source object
321  $object->fetchCommon($fromid);
322  // Reset some properties
323  unset($object->id);
324  unset($object->fk_user_creat);
325  unset($object->import_key);
326 
327  // Clear fields
328  $object->ref = "copy_of_".$object->ref;
329  $object->title = $langs->trans("CopyOf")." ".$object->title;
330  // ...
331  // Clear extrafields that are unique
332  if (is_array($object->array_options) && count($object->array_options) > 0) {
333  $extrafields->fetch_name_optionals_label($this->table_element);
334  foreach ($object->array_options as $key => $option) {
335  $shortkey = preg_replace('/options_/', '', $key);
336  if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
337  // var_dump($key);
338  // var_dump($clonedObj->array_options[$key]);
339  // exit;
340  unset($object->array_options[$key]);
341  }
342  }
343  }
344 
345  // Create clone
346  $object->context['createfromclone'] = 'createfromclone';
347  $result = $object->createCommon($user);
348  if ($result < 0) {
349  $error++;
350  $this->error = $object->error;
351  $this->errors = $object->errors;
352  }
353 
354  unset($object->context['createfromclone']);
355 
356  // End
357  if (!$error) {
358  $this->db->commit();
359  return $object;
360  } else {
361  $this->db->rollback();
362  return -1;
363  }
364  }
365 
373  public function fetch($id, $ref = null)
374  {
375  $result = $this->fetchCommon($id, $ref);
376  if ($result > 0 && !empty($this->table_element_line)) {
377  //$this->fetchLines();
378  }
379  return $result;
380  }
381 
387  /*public function fetchLines()
388  {
389  $this->lines=array();
390 
391  // Load lines with object MyObjectLine
392 
393  return count($this->lines)?1:0;
394  }*/
395 
407  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
408  {
409  global $conf;
410 
411  dol_syslog(__METHOD__, LOG_DEBUG);
412 
413  $records = array();
414 
415  $sql = 'SELECT';
416  $sql .= ' t.rowid';
417  // TODO Get all fields
418  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
419  $sql .= ' WHERE t.entity = '.$conf->entity;
420  // Manage filter
421  $sqlwhere = array();
422  if (count($filter) > 0) {
423  foreach ($filter as $key => $value) {
424  if ($key == 't.rowid') {
425  $sqlwhere[] = $key.'='.$value;
426  } elseif (strpos($key, 'date') !== false) {
427  $sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
428  } elseif ($key == 'customsql') {
429  $sqlwhere[] = $value;
430  } else {
431  $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
432  }
433  }
434  }
435  if (count($sqlwhere) > 0) {
436  $sql .= ' AND ('.implode(' '.$filtermode.' ', $sqlwhere).')';
437  }
438 
439  if (!empty($sortfield)) {
440  $sql .= $this->db->order($sortfield, $sortorder);
441  }
442  if (!empty($limit)) {
443  $sql .= ' '.$this->db->plimit($limit, $offset);
444  }
445 
446  $resql = $this->db->query($sql);
447  if ($resql) {
448  $num = $this->db->num_rows($resql);
449 
450  while ($obj = $this->db->fetch_object($resql)) {
451  $record = new self($this->db);
452 
453  $record->id = $obj->rowid;
454  // TODO Get other fields
455 
456  //var_dump($record->id);
457  $records[$record->id] = $record;
458  }
459  $this->db->free($resql);
460 
461  return $records;
462  } else {
463  $this->errors[] = 'Error '.$this->db->lasterror();
464  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
465 
466  return -1;
467  }
468  }
469 
477  public function update(User $user, $notrigger = false)
478  {
479  return $this->updateCommon($user, $notrigger);
480  }
481 
489  public function delete(User $user, $notrigger = false)
490  {
491  return $this->deleteCommon($user, $notrigger);
492  //return $this->deleteCommon($user, $notrigger, 1);
493  }
494 
505  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
506  {
507  global $db, $conf, $langs, $hookmanager, $action;
508  global $dolibarr_main_authentication, $dolibarr_main_demo;
509  global $menumanager;
510 
511  if (!empty($conf->dol_no_mouse_hover)) {
512  // Force disable tooltips
513  $notooltip = 1;
514  }
515 
516  $result = '';
517 
518  $label = '<u>'.$langs->trans("Hook").'</u>';
519  $label .= '<br>';
520  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
521 
522  $url = dol_buildpath('/zapier/hook_card.php', 1).'?id='.$this->id;
523 
524  if ($option != 'nolink') {
525  // Add param to save lastsearch_values or not
526  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
527  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
528  $add_save_lastsearch_values = 1;
529  }
530  if ($add_save_lastsearch_values) {
531  $url .= '&save_lastsearch_values=1';
532  }
533  }
534 
535  $linkclose = '';
536  if (empty($notooltip)) {
537  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
538  $label = $langs->trans("ShowMyObject");
539  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
540  }
541  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
542  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
543 
544  /*
545  $hookmanager->initHooks(array('hookdao'));
546  $parameters=array('id'=>$this->id);
547  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
548  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
549  */
550  } else {
551  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
552  }
553 
554  $linkstart = '<a href="'.$url.'"';
555  $linkstart .= $linkclose.'>';
556  $linkend = '</a>';
557 
558  $result .= $linkstart;
559  if ($withpicto) {
560  $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);
561  }
562  if ($withpicto != 2) {
563  $result .= $this->ref;
564  }
565  $result .= $linkend;
566  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
567 
568  $hookmanager->initHooks(array('hookdao'));
569  $parameters = array(
570  'id' => $this->id,
571  'getnomurl' => $result,
572  );
573  // Note that $action and $object may have been modified by some hooks
574  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);
575  if ($reshook > 0) {
576  $result = $hookmanager->resPrint;
577  } else {
578  $result .= $hookmanager->resPrint;
579  }
580 
581  return $result;
582  }
583 
595  public function getLibStatut($mode = 0)
596  {
597  return $this->LibStatut($this->status, $mode);
598  }
599 
600  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
609  public function LibStatut($status, $mode = 0)
610  {
611  // phpcs:enable
612  global $langs;
613 
614  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
615  global $langs;
616  //$langs->load("mymodule");
617  $this->labelStatus[self::STATUS_DRAFT] = $langs->trans('Disabled');
618  $this->labelStatus[self::STATUS_VALIDATED] = $langs->trans('Enabled');
619  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->trans('Disabled');
620  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->trans('Enabled');
621  }
622 
623  $statusType = 'status5';
624  if ($status == self::STATUS_VALIDATED) $statusType = 'status4';
625 
626  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
627  }
628 
635  public function info($id)
636  {
637  $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
638  $sql .= ' fk_user_creat, fk_user_modif';
639  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
640  $sql .= ' WHERE t.rowid = '.$id;
641  $result = $this->db->query($sql);
642  if ($result) {
643  if ($this->db->num_rows($result)) {
644  $obj = $this->db->fetch_object($result);
645  $this->id = $obj->rowid;
646  if ($obj->fk_user_author) {
647  $cuser = new User($this->db);
648  $cuser->fetch($obj->fk_user_author);
649  $this->user_creation = $cuser;
650  }
651 
652  if ($obj->fk_user_valid) {
653  $vuser = new User($this->db);
654  $vuser->fetch($obj->fk_user_valid);
655  $this->user_validation = $vuser;
656  }
657 
658  if ($obj->fk_user_cloture) {
659  $cluser = new User($this->db);
660  $cluser->fetch($obj->fk_user_cloture);
661  $this->user_cloture = $cluser;
662  }
663 
664  $this->date_creation = $this->db->jdate($obj->datec);
665  $this->date_modification = $this->db->jdate($obj->datem);
666  $this->date_validation = $this->db->jdate($obj->datev);
667  }
668 
669  $this->db->free($result);
670  } else {
671  dol_print_error($this->db);
672  }
673  }
674 
681  public function initAsSpecimen()
682  {
683  $this->initAsSpecimenCommon();
684  }
685 
686 
693  public function doScheduledJob()
694  {
695  global $conf, $langs;
696 
697  //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
698 
699  $error = 0;
700  $this->output = '';
701  $this->error = '';
702 
703  dol_syslog(__METHOD__, LOG_DEBUG);
704 
705  $now = dol_now();
706 
707  $this->db->begin();
708 
709  // ...
710 
711  $this->db->commit();
712 
713  return $error;
714  }
715 }
716 
720 /*
721 class MyObjectLine
722 {
723  // @var int ID
724  public $id;
725  // @var mixed Sample line property 1
726  public $prop1;
727  // @var mixed Sample line property 2
728  public $prop2;
729 }
730 */
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
foreach($object->fields as $key=> $val) if(is_array($extrafields->attributes[$object->table_element]['label'])&&count($extrafields->attributes[$object->table_element]['label']) > 0) $object fields
info($id)
Load the info information in the object.
Definition: hook.class.php:635
dol_now($mode= 'auto')
Return date for now.
Class for Hook.
Definition: hook.class.php:29
Class to manage Dolibarr users.
Definition: user.class.php:44
LibStatut($status, $mode=0)
Return the status.
Definition: hook.class.php:609
Class to manage Dolibarr database access.
getLibStatut($mode=0)
Return label of the status.
Definition: hook.class.php:595
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
createCommon(User $user, $notrigger=false)
Create object into database.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
$conf db
API class for accounts.
Definition: inc.php:54
update(User $user, $notrigger=false)
Update object into database.
Definition: hook.class.php:477
fetchAll($sortorder= '', $sortfield= '', $limit=0, $offset=0, array $filter=array(), $filtermode= 'AND')
Load object lines in memory from the database.
Definition: hook.class.php:407
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: hook.class.php:681
__construct(DoliDB $db)
Constructor.
Definition: hook.class.php:260
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
create(User $user, $notrigger=false)
Create object into database.
Definition: hook.class.php:297
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
updateCommon(User $user, $notrigger=false)
Update object into database.
print $_SERVER["PHP_SELF"]
Edit parameters.
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:505
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
createFromClone(User $user, $fromid)
Clone an object into another one.
Definition: hook.class.php:309
fetch($id, $ref=null)
Load object in memory from the database.
Definition: hook.class.php:373
doScheduledJob()
Action executed by scheduler CAN BE A CRON TASK.
Definition: hook.class.php:693
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
fetchCommon($id, $ref=null, $morewhere= '')
Load object in memory from the database.