dolibarr  16.0.5
interface_99_modWebhook_WebhookTriggers.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2022 SuperAdmin <test@dolibarr.com>
3  *
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 <https://www.gnu.org/licenses/>.
16  */
17 
33 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/webhook/class/target.class.php';
35 
40 {
46  public function __construct($db)
47  {
48  $this->db = $db;
49 
50  $this->name = preg_replace('/^Interface/i', '', get_class($this));
51  $this->family = "demo";
52  $this->description = "Webhook triggers.";
53  // 'development', 'experimental', 'dolibarr' or version
54  $this->version = 'development';
55  $this->picto = 'webhook';
56  }
57 
63  public function getName()
64  {
65  return $this->name;
66  }
67 
73  public function getDesc()
74  {
75  return $this->description;
76  }
77 
78 
91  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
92  {
93  if (empty($conf->webhook) || empty($conf->webhook->enabled)) {
94  return 0; // If module is not enabled, we do nothing
95  }
96  require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
97 
98  // Or you can execute some code here
99  $nbPosts = 0;
100  $errors = 0;
101  $static_object = new Target($this->db);
102  $target_url = $static_object->fetchAll();
103  foreach ($target_url as $key => $tmpobject) {
104  $actionarray = explode(",", $tmpobject->trigger_codes);
105  if (is_array($actionarray) && in_array($action, $actionarray)) {
106  $jsonstr = '{"triggercode":'.json_encode($action).',"object":'.json_encode($object).'}';
107  $response = getURLContent($tmpobject->url, 'POST', $jsonstr);
108  if (empty($response['curl_error_no']) && $response['http_code'] >= 200 && $response['http_code'] < 300) {
109  $nbPosts ++;
110  } else {
111  $errors ++;
112  dol_syslog("Failed to get url with httpcode=".(!empty($response['http_code']) ? $response['http_code'] : "")." curl_error_no=".(!empty($response['curl_error_no']) ? $response['curl_error_no'] : ""), LOG_DEBUG);
113  }
114  }
115  }
116  if (!empty($errors)) {
117  return $errors * -1;
118  }
119  return $nbPosts;
120  }
121 }
db
$conf db
API class for accounts.
Definition: inc.php:41
InterfaceWebhookTriggers\getDesc
getDesc()
Trigger description.
Definition: interface_99_modWebhook_WebhookTriggers.class.php:73
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
Translate
Class to manage translations.
Definition: translate.class.php:30
InterfaceWebhookTriggers\getName
getName()
Trigger name.
Definition: interface_99_modWebhook_WebhookTriggers.class.php:63
name
$conf db name
Definition: repair.php:122
getURLContent
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).
Definition: geturl.lib.php:41
Conf
Class to stock current configuration.
Definition: conf.class.php:33
InterfaceWebhookTriggers
Class of triggers for Webhook module.
Definition: interface_99_modWebhook_WebhookTriggers.class.php:39
InterfaceWebhookTriggers\runTrigger
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr business event is done.
Definition: interface_99_modWebhook_WebhookTriggers.class.php:91
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Target
Class for Target.
Definition: target.class.php:33
InterfaceWebhookTriggers\__construct
__construct($db)
Constructor.
Definition: interface_99_modWebhook_WebhookTriggers.class.php:46
DolibarrTriggers
Class that all the triggers must extend.
Definition: dolibarrtriggers.class.php:21