dolibarr 23.0.3
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-2025 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 $sql = "SELECT t.rowid";
161 $sql .= " FROM ".MAIN_DB_PREFIX."zapier_hook as t";
162 $sql .= " WHERE 1 = 1";
163 $tmpobject = new Hook($this->db);
164 if ($tmpobject->ismultientitymanaged) {
165 $sql .= ' AND t.entity IN ('.getEntity('hook').')';
166 }
167 if ($sqlfilters) {
168 $errormessage = '';
169 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
170 if ($errormessage) {
171 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
172 }
173 }
174
175 $sql .= $this->db->order($sortfield, $sortorder);
176 if ($limit) {
177 if ($page < 0) {
178 $page = 0;
179 }
180 $offset = $limit * $page;
181
182 $sql .= $this->db->plimit($limit + 1, $offset);
183 }
184
185 $result = $this->db->query($sql);
186 $i = 0;
187 if ($result) {
188 $i = 0;
189 $num = $this->db->num_rows($result);
190 $min = min($num, ($limit <= 0 ? $num : $limit));
191 while ($i < $min) {
192 $obj = $this->db->fetch_object($result);
193 $hook_static = new Hook($this->db);
194 if ($hook_static->fetch($obj->rowid)) {
195 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($hook_static), $properties);
196 }
197 $i++;
198 }
199 } else {
200 throw new RestException(503, 'Error when retrieve hook list');
201 }
202
203 return $obj_ret;
204 }
205
218 public function post($request_data = null)
219 {
220 if (!DolibarrApiAccess::$user->hasRight('zapier', 'write')) {
221 throw new RestException(403);
222 }
223
224 dol_syslog("API Zapier create hook receive : ".print_r($request_data, true), LOG_DEBUG);
225
226 // Check mandatory fields
227 $fields = array(
228 'url',
229 );
230 $request_data = $this->validate($request_data, $fields);
231
232 foreach ($request_data as $field => $value) {
233 if ($field === 'caller') {
234 // 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
235 $this->hook->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
236 continue;
237 }
238
239 $this->hook->$field = $this->_checkValForAPI($field, $value, $this->hook);
240 }
241
242 $this->hook->fk_user = DolibarrApiAccess::$user->id;
243 // we create the hook into database
244 if (!$this->hook->create(DolibarrApiAccess::$user)) {
245 throw new RestException(500, "Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
246 }
247 return array(
248 'id' => $this->hook->id,
249 );
250 }
251
262 public function delete($id)
263 {
264 if (!DolibarrApiAccess::$user->hasRight('zapier', 'delete')) {
265 throw new RestException(403);
266 }
267
268 $result = $this->hook->fetch($id);
269 if (!$result) {
270 throw new RestException(404, 'Hook not found');
271 }
272
273 if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
274 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
275 }
276
277 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
278 throw new RestException(500, 'Error when deleting Hook : '.$this->hook->error);
279 }
280
281 return array(
282 'success' => array(
283 'code' => 200,
284 'message' => 'Hook deleted'
285 )
286 );
287 }
288
289 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
301 public function _cleanObjectDatas($object)
302 {
303 // phpcs:disable
304 $object = parent::_cleanObjectDatas($object);
305
306 return $object;
307 }
308
318 private function validate($data, $fields)
319 {
320 $hook = array();
321 foreach ($fields as $field) {
322 if (!isset($data[$field])) {
323 throw new RestException(400, $field." field missing");
324 }
325 $hook[$field] = $data[$field];
326 }
327 return $hook;
328 }
329}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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.