dolibarr 19.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-2020 Frédéric France <frederic.france@netlogic.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25use Luracast\Restler\RestException;
26
27require_once DOL_DOCUMENT_ROOT.'/zapier/class/hook.class.php';
28
29
36class Zapier extends DolibarrApi
37{
41 public static $FIELDS = array(
42 'url',
43 );
44
45
49 public $hook;
50
57 public function __construct()
58 {
59 global $db, $conf;
60 $this->db = $db;
61 $this->hook = new Hook($this->db);
62 }
63
75 public function get($id)
76 {
77 if (!DolibarrApiAccess::$user->rights->zapier->read) {
78 throw new RestException(401);
79 }
80
81 $result = $this->hook->fetch($id);
82 if (!$result) {
83 throw new RestException(404, 'Hook not found');
84 }
85
86 if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
87 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
88 }
89
90 return $this->_cleanObjectDatas($this->hook);
91 }
92
103 public function getModulesChoices()
104 {
105 if (!DolibarrApiAccess::$user->rights->zapier->read) {
106 throw new RestException(401);
107 }
108
109 $arraychoices = array(
110 'invoices' => 'Invoices',
111 'orders' => 'Orders',
112 'thirdparties' => 'Thirparties',
113 'contacts' => 'Contacts',
114 'users' => 'Users',
115 );
116 // $result = $this->hook->fetch($id);
117 // if (! $result ) {
118 // throw new RestException(404, 'Hook not found');
119 // }
120
121 // if (! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
122 // throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
123 // }
124
125 return $arraychoices;
126 }
127
145 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
146 {
147 global $db, $conf;
148
149 if (!DolibarrApiAccess::$user->rights->zapier->read) {
150 throw new RestException(401);
151 }
152
153 $obj_ret = array();
154
155 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
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->rights->societe->client->voir && !$socid) {
163 $search_sale = DolibarrApiAccess::$user->id;
164 }
165
166 $sql = "SELECT t.rowid";
167 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
168 // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
169 $sql .= ", sc.fk_soc, sc.fk_user";
170 }
171 $sql .= " FROM ".MAIN_DB_PREFIX."hook_mytable as t";
172
173 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
174 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
175 }
176 $sql .= " WHERE 1 = 1";
177
178 // Example of use $mode
179 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
180 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
181
182 $tmpobject = new Hook($this->db);
183 if ($tmpobject->ismultientitymanaged) {
184 $sql .= ' AND t.entity IN ('.getEntity('hook').')';
185 }
186 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
187 $sql .= " AND t.fk_soc = sc.fk_soc";
188 }
189 if ($restrictonsocid && $socid) {
190 $sql .= " AND t.fk_soc = ".((int) $socid);
191 }
192 if ($restrictonsocid && $search_sale > 0) {
193 // Join for the needed table to filter by sale
194 $sql .= " AND t.rowid = sc.fk_soc";
195 }
196 // Insert sale filter
197 if ($restrictonsocid && $search_sale > 0) {
198 $sql .= " AND sc.fk_user = ".((int) $search_sale);
199 }
200 if ($sqlfilters) {
201 $errormessage = '';
202 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
203 if ($errormessage) {
204 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
205 }
206 }
207
208 $sql .= $this->db->order($sortfield, $sortorder);
209 if ($limit) {
210 if ($page < 0) {
211 $page = 0;
212 }
213 $offset = $limit * $page;
214
215 $sql .= $this->db->plimit($limit + 1, $offset);
216 }
217
218 $result = $this->db->query($sql);
219 $i = 0;
220 if ($result) {
221 $num = $this->db->num_rows($result);
222 while ($i < $num) {
223 $obj = $this->db->fetch_object($result);
224 $hook_static = new Hook($this->db);
225 if ($hook_static->fetch($obj->rowid)) {
226 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($hook_static), $properties);
227 }
228 $i++;
229 }
230 } else {
231 throw new RestException(503, 'Error when retrieve hook list');
232 }
233
234 return $obj_ret;
235 }
236
245 public function post($request_data = null)
246 {
247 if (!DolibarrApiAccess::$user->rights->zapier->write) {
248 throw new RestException(401);
249 }
250
251 dol_syslog("API Zapier create hook receive : ".print_r($request_data, true), LOG_DEBUG);
252
253 // Check mandatory fields
254 $fields = array(
255 'url',
256 );
257 $result = $this->validate($request_data, $fields);
258
259 foreach ($request_data as $field => $value) {
260 if ($field === 'caller') {
261 // 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 whith the caller
262 $this->hook->context['caller'] = $request_data['caller'];
263 continue;
264 }
265
266 $this->hook->$field = $value;
267 }
268
269 $this->hook->fk_user = DolibarrApiAccess::$user->id;
270 // we create the hook into database
271 if (!$this->hook->create(DolibarrApiAccess::$user)) {
272 throw new RestException(500, "Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
273 }
274 return array(
275 'id' => $this->hook->id,
276 );
277 }
278
279 // /**
280 // * Update hook
281 // *
282 // * @param int $id Id of hook to update
283 // * @param array $request_data Datas
284 // * @return int
285 // *
286 // * @url PUT /hooks/{id}
287 // */
288 /*public function put($id, $request_data = null)
289 {
290 if (! DolibarrApiAccess::$user->rights->zapier->write) {
291 throw new RestException(401);
292 }
293
294 $result = $this->hook->fetch($id);
295 if( ! $result ) {
296 throw new RestException(404, 'Hook not found');
297 }
298
299 if( ! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
300 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
301 }
302
303 foreach($request_data as $field => $value) {
304 if ($field == 'id') {
305 continue;
306 }
307 $this->hook->$field = $value;
308 }
309
310 if ($this->hook->update($id, DolibarrApiAccess::$user) > 0) {
311 return $this->get($id);
312 } else {
313 throw new RestException(500, $this->hook->error);
314 }
315 }*/
316
325 public function delete($id)
326 {
327 if (!DolibarrApiAccess::$user->rights->zapier->delete) {
328 throw new RestException(401);
329 }
330
331 $result = $this->hook->fetch($id);
332 if (!$result) {
333 throw new RestException(404, 'Hook not found');
334 }
335
336 if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
337 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
338 }
339
340 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
341 throw new RestException(500, 'Error when deleting Hook : '.$this->hook->error);
342 }
343
344 return array(
345 'success' => array(
346 'code' => 200,
347 'message' => 'Hook deleted'
348 )
349 );
350 }
351
352 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
359 public function _cleanObjectDatas($object)
360 {
361 // phpcs:disable
362 $object = parent::_cleanObjectDatas($object);
363
364 return $object;
365 }
366
376 private function validate($data, $fields)
377 {
378 $hook = array();
379 foreach ($fields as $field) {
380 if (!isset($data[$field])) {
381 throw new RestException(400, $field." field missing");
382 }
383 $hook[$field] = $data[$field];
384 }
385 return $hook;
386 }
387}
Class for API REST v1.
Definition api.class.php:31
_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.
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
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.