dolibarr 21.0.0-alpha
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 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
58 public function __construct()
59 {
60 global $db;
61
62 $this->db = $db;
63 $this->hook = new Hook($this->db);
64 }
65
77 public function get($id)
78 {
79 if (!DolibarrApiAccess::$user->hasRight('zapier', 'read')) {
80 throw new RestException(403);
81 }
82
83 $result = $this->hook->fetch($id);
84 if (!$result) {
85 throw new RestException(404, 'Hook not found');
86 }
87
88 if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
89 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
90 }
91
92 return $this->_cleanObjectDatas($this->hook);
93 }
94
105 public function getModulesChoices()
106 {
107 if (!DolibarrApiAccess::$user->hasRight('zapier', 'read')) {
108 throw new RestException(403);
109 }
110
111 $arraychoices = array(
112 'invoices' => 'Invoices',
113 'orders' => 'Orders',
114 'thirdparties' => 'Thirparties',
115 'contacts' => 'Contacts',
116 'users' => 'Users',
117 );
118 // $result = $this->hook->fetch($id);
119 // if (! $result ) {
120 // throw new RestException(404, 'Hook not found');
121 // }
122
123 // if (! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
124 // throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
125 // }
126
127 return $arraychoices;
128 }
129
147 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
148 {
149 if (!DolibarrApiAccess::$user->hasRight('zapier', 'read')) {
150 throw new RestException(403);
151 }
152
153 $obj_ret = array();
154
155 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
156
157 // Set to 1 if there is a field socid in table of object
158 $restrictonsocid = 0;
159
160 // If the internal user must only see his customers, force searching by him
161 $search_sale = 0;
162 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
163 $search_sale = DolibarrApiAccess::$user->id;
164 }
165
166 $sql = "SELECT t.rowid";
167 $sql .= " FROM ".MAIN_DB_PREFIX."hook_mytable as t";
168 $sql .= " WHERE 1 = 1";
169 $tmpobject = new Hook($this->db);
170 if ($tmpobject->ismultientitymanaged) {
171 $sql .= ' AND t.entity IN ('.getEntity('hook').')';
172 }
173 if ($restrictonsocid && $socid) {
174 $sql .= " AND t.fk_soc = ".((int) $socid);
175 }
176 // Search on sale representative
177 if ($search_sale && $search_sale != '-1') {
178 if ($search_sale == -2) {
179 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
180 } elseif ($search_sale > 0) {
181 $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).")";
182 }
183 }
184 if ($sqlfilters) {
185 $errormessage = '';
186 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
187 if ($errormessage) {
188 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
189 }
190 }
191
192 $sql .= $this->db->order($sortfield, $sortorder);
193 if ($limit) {
194 if ($page < 0) {
195 $page = 0;
196 }
197 $offset = $limit * $page;
198
199 $sql .= $this->db->plimit($limit + 1, $offset);
200 }
201
202 $result = $this->db->query($sql);
203 $i = 0;
204 if ($result) {
205 $num = $this->db->num_rows($result);
206 while ($i < $num) {
207 $obj = $this->db->fetch_object($result);
208 $hook_static = new Hook($this->db);
209 if ($hook_static->fetch($obj->rowid)) {
210 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($hook_static), $properties);
211 }
212 $i++;
213 }
214 } else {
215 throw new RestException(503, 'Error when retrieve hook list');
216 }
217
218 return $obj_ret;
219 }
220
229 public function post($request_data = null)
230 {
231 if (!DolibarrApiAccess::$user->hasRight('zapier', 'write')) {
232 throw new RestException(403);
233 }
234
235 dol_syslog("API Zapier create hook receive : ".print_r($request_data, true), LOG_DEBUG);
236
237 // Check mandatory fields
238 $fields = array(
239 'url',
240 );
241 $result = $this->validate($request_data, $fields);
242
243 foreach ($request_data as $field => $value) {
244 if ($field === 'caller') {
245 // 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
246 $this->hook->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
247 continue;
248 }
249
250 $this->hook->$field = $this->_checkValForAPI($field, $value, $this->hook);
251 }
252
253 $this->hook->fk_user = DolibarrApiAccess::$user->id;
254 // we create the hook into database
255 if (!$this->hook->create(DolibarrApiAccess::$user)) {
256 throw new RestException(500, "Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
257 }
258 return array(
259 'id' => $this->hook->id,
260 );
261 }
262
271 public function delete($id)
272 {
273 if (!DolibarrApiAccess::$user->hasRight('zapier', 'delete')) {
274 throw new RestException(403);
275 }
276
277 $result = $this->hook->fetch($id);
278 if (!$result) {
279 throw new RestException(404, 'Hook not found');
280 }
281
282 if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
283 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
284 }
285
286 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
287 throw new RestException(500, 'Error when deleting Hook : '.$this->hook->error);
288 }
289
290 return array(
291 'success' => array(
292 'code' => 200,
293 'message' => 'Hook deleted'
294 )
295 );
296 }
297
298 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
305 public function _cleanObjectDatas($object)
306 {
307 // phpcs:disable
308 $object = parent::_cleanObjectDatas($object);
309
310 return $object;
311 }
312
322 private function validate($data, $fields)
323 {
324 $hook = array();
325 foreach ($fields as $field) {
326 if (!isset($data[$field])) {
327 throw new RestException(400, $field." field missing");
328 }
329 $hook[$field] = $data[$field];
330 }
331 return $hook;
332 }
333}
$id
Definition account.php:39
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class for API REST v1.
Definition api.class.php:30
_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:82
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.