dolibarr 22.0.5
api_zapier.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2019-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2025 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
26use Luracast\Restler\RestException;
27
28require_once DOL_DOCUMENT_ROOT.'/zapier/class/hook.class.php';
29
30
37class Zapier extends DolibarrApi
38{
42 public static $FIELDS = array(
43 'url',
44 );
45
46
50 public $hook;
51
57 public function __construct()
58 {
59 global $db;
60
61 $this->db = $db;
62 $this->hook = new Hook($this->db);
63 }
64
76 public function get($id)
77 {
78 if (!DolibarrApiAccess::$user->hasRight('zapier', 'read')) {
79 throw new RestException(403);
80 }
81
82 $result = $this->hook->fetch($id);
83 if (!$result) {
84 throw new RestException(404, 'Hook not found');
85 }
86
87 if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
88 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
89 }
90
91 return $this->_cleanObjectDatas($this->hook);
92 }
93
106 public function getModulesChoices()
107 {
108 if (!DolibarrApiAccess::$user->hasRight('zapier', 'read')) {
109 throw new RestException(403);
110 }
111
112 $arraychoices = array(
113 'invoices' => 'Invoices',
114 'orders' => 'Orders',
115 'thirdparties' => 'ThirdParties',
116 'contacts' => 'Contacts',
117 'users' => 'Users',
118 );
119 // $result = $this->hook->fetch($id);
120 // if (! $result ) {
121 // throw new RestException(404, 'Hook not found');
122 // }
123
124 // if (! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
125 // throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
126 // }
127
128 return $arraychoices;
129 }
130
150 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
151 {
152 if (!DolibarrApiAccess::$user->hasRight('zapier', 'read')) {
153 throw new RestException(403);
154 }
155
156 $obj_ret = array();
157
158 $socid = DolibarrApiAccess::$user->socid ?: 0;
159
160 // Set to 1 if there is a field socid in table of object
161 $restrictonsocid = 0;
162
163 // If the internal user must only see his customers, force searching by him
164 $search_sale = 0;
165 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
166 $search_sale = DolibarrApiAccess::$user->id;
167 }
168
169 $sql = "SELECT t.rowid";
170 $sql .= " FROM ".MAIN_DB_PREFIX."hook_mytable as t";
171 $sql .= " WHERE 1 = 1";
172 $tmpobject = new Hook($this->db);
173 if ($tmpobject->ismultientitymanaged) {
174 $sql .= ' AND t.entity IN ('.getEntity('hook').')';
175 }
176 if ($restrictonsocid && $socid) {
177 $sql .= " AND t.fk_soc = ".((int) $socid);
178 }
179 // Search on sale representative
180 if ($search_sale && $search_sale != '-1') {
181 if ($search_sale == -2) {
182 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
183 } elseif ($search_sale > 0) {
184 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
185 }
186 }
187 if ($sqlfilters) {
188 $errormessage = '';
189 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
190 if ($errormessage) {
191 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
192 }
193 }
194
195 $sql .= $this->db->order($sortfield, $sortorder);
196 if ($limit) {
197 if ($page < 0) {
198 $page = 0;
199 }
200 $offset = $limit * $page;
201
202 $sql .= $this->db->plimit($limit + 1, $offset);
203 }
204
205 $result = $this->db->query($sql);
206 $i = 0;
207 if ($result) {
208 $num = $this->db->num_rows($result);
209 while ($i < $num) {
210 $obj = $this->db->fetch_object($result);
211 $hook_static = new Hook($this->db);
212 if ($hook_static->fetch($obj->rowid)) {
213 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($hook_static), $properties);
214 }
215 $i++;
216 }
217 } else {
218 throw new RestException(503, 'Error when retrieve hook list');
219 }
220
221 return $obj_ret;
222 }
223
236 public function post($request_data = null)
237 {
238 if (!DolibarrApiAccess::$user->hasRight('zapier', 'write')) {
239 throw new RestException(403);
240 }
241
242 dol_syslog("API Zapier create hook receive : ".print_r($request_data, true), LOG_DEBUG);
243
244 // Check mandatory fields
245 $fields = array(
246 'url',
247 );
248 $request_data = $this->validate($request_data, $fields);
249
250 foreach ($request_data as $field => $value) {
251 if ($field === 'caller') {
252 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
253 $this->hook->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
254 continue;
255 }
256
257 $this->hook->$field = $this->_checkValForAPI($field, $value, $this->hook);
258 }
259
260 $this->hook->fk_user = DolibarrApiAccess::$user->id;
261 // we create the hook into database
262 if (!$this->hook->create(DolibarrApiAccess::$user)) {
263 throw new RestException(500, "Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
264 }
265 return array(
266 'id' => $this->hook->id,
267 );
268 }
269
280 public function delete($id)
281 {
282 if (!DolibarrApiAccess::$user->hasRight('zapier', 'delete')) {
283 throw new RestException(403);
284 }
285
286 $result = $this->hook->fetch($id);
287 if (!$result) {
288 throw new RestException(404, 'Hook not found');
289 }
290
291 if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
292 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
293 }
294
295 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
296 throw new RestException(500, 'Error when deleting Hook : '.$this->hook->error);
297 }
298
299 return array(
300 'success' => array(
301 'code' => 200,
302 'message' => 'Hook deleted'
303 )
304 );
305 }
306
307 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
314 public function _cleanObjectDatas($object)
315 {
316 // phpcs:disable
317 $object = parent::_cleanObjectDatas($object);
318
319 return $object;
320 }
321
331 private function validate($data, $fields)
332 {
333 $hook = array();
334 foreach ($fields as $field) {
335 if (!isset($data[$field])) {
336 throw new RestException(400, $field." field missing");
337 }
338 $hook[$field] = $data[$field];
339 }
340 return $hook;
341 }
342}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for API REST v1.
Definition api.class.php:33
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
Class for Hook.
_cleanObjectDatas($object)
Clean sensible object datas.
validate($data, $fields)
Validate fields before create or update object.
__construct()
Constructor.
post($request_data=null)
Create hook object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List hooks.
getModulesChoices()
Get list of possibles choices for module.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.