dolibarr 18.0.6
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
144 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
145 {
146 global $db, $conf;
147
148 if (!DolibarrApiAccess::$user->rights->zapier->read) {
149 throw new RestException(401);
150 }
151
152 $obj_ret = array();
153
154 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
155
156 // Set to 1 if there is a field socid in table of object
157 $restrictonsocid = 0;
158
159 // If the internal user must only see his customers, force searching by him
160 $search_sale = 0;
161 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
162 $search_sale = DolibarrApiAccess::$user->id;
163 }
164
165 $sql = "SELECT t.rowid";
166 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
167 // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
168 $sql .= ", sc.fk_soc, sc.fk_user";
169 }
170 $sql .= " FROM ".MAIN_DB_PREFIX."hook_mytable as t";
171
172 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
173 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
174 }
175 $sql .= " WHERE 1 = 1";
176
177 // Example of use $mode
178 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
179 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
180
181 $tmpobject = new Hook($this->db);
182 if ($tmpobject->ismultientitymanaged) {
183 $sql .= ' AND t.entity IN ('.getEntity('hook').')';
184 }
185 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
186 $sql .= " AND t.fk_soc = sc.fk_soc";
187 }
188 if ($restrictonsocid && $socid) {
189 $sql .= " AND t.fk_soc = ".((int) $socid);
190 }
191 if ($restrictonsocid && $search_sale > 0) {
192 // Join for the needed table to filter by sale
193 $sql .= " AND t.rowid = sc.fk_soc";
194 }
195 // Insert sale filter
196 if ($restrictonsocid && $search_sale > 0) {
197 $sql .= " AND sc.fk_user = ".((int) $search_sale);
198 }
199 if ($sqlfilters) {
200 $errormessage = '';
201 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
202 if ($errormessage) {
203 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
204 }
205 }
206
207 $sql .= $this->db->order($sortfield, $sortorder);
208 if ($limit) {
209 if ($page < 0) {
210 $page = 0;
211 }
212 $offset = $limit * $page;
213
214 $sql .= $this->db->plimit($limit + 1, $offset);
215 }
216
217 $result = $this->db->query($sql);
218 $i = 0;
219 if ($result) {
220 $num = $this->db->num_rows($result);
221 while ($i < $num) {
222 $obj = $this->db->fetch_object($result);
223 $hook_static = new Hook($this->db);
224 if ($hook_static->fetch($obj->rowid)) {
225 $obj_ret[] = $this->_cleanObjectDatas($hook_static);
226 }
227 $i++;
228 }
229 } else {
230 throw new RestException(503, 'Error when retrieve hook list');
231 }
232 if (!count($obj_ret)) {
233 throw new RestException(404, 'No hook found');
234 }
235 return $obj_ret;
236 }
237
246 public function post($request_data = null)
247 {
248 if (!DolibarrApiAccess::$user->rights->zapier->write) {
249 throw new RestException(401);
250 }
251
252 // Check mandatory fields
253 $fields = array(
254 'url',
255 );
256 dol_syslog("API Zapier create hook receive : ".print_r($request_data, true), LOG_DEBUG);
257 $result = $this->validate($request_data, $fields);
258
259 foreach ($request_data as $field => $value) {
260 $this->hook->$field = $value;
261 }
262 $this->hook->fk_user = DolibarrApiAccess::$user->id;
263 // we create the hook into database
264 if (!$this->hook->create(DolibarrApiAccess::$user)) {
265 throw new RestException(500, "Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
266 }
267 return array(
268 'id' => $this->hook->id,
269 );
270 }
271
272 // /**
273 // * Update hook
274 // *
275 // * @param int $id Id of hook to update
276 // * @param array $request_data Datas
277 // * @return int
278 // *
279 // * @url PUT /hooks/{id}
280 // */
281 /*public function put($id, $request_data = null)
282 {
283 if (! DolibarrApiAccess::$user->rights->zapier->write) {
284 throw new RestException(401);
285 }
286
287 $result = $this->hook->fetch($id);
288 if( ! $result ) {
289 throw new RestException(404, 'Hook not found');
290 }
291
292 if( ! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
293 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
294 }
295
296 foreach($request_data as $field => $value) {
297 if ($field == 'id') {
298 continue;
299 }
300 $this->hook->$field = $value;
301 }
302
303 if ($this->hook->update($id, DolibarrApiAccess::$user) > 0) {
304 return $this->get($id);
305 } else {
306 throw new RestException(500, $this->hook->error);
307 }
308 }*/
309
318 public function delete($id)
319 {
320 if (!DolibarrApiAccess::$user->rights->zapier->delete) {
321 throw new RestException(401);
322 }
323
324 $result = $this->hook->fetch($id);
325 if (!$result) {
326 throw new RestException(404, 'Hook not found');
327 }
328
329 if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
330 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
331 }
332
333 if (!$this->hook->delete(DolibarrApiAccess::$user)) {
334 throw new RestException(500, 'Error when deleting Hook : '.$this->hook->error);
335 }
336
337 return array(
338 'success' => array(
339 'code' => 200,
340 'message' => 'Hook deleted'
341 )
342 );
343 }
344
345 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
352 public function _cleanObjectDatas($object)
353 {
354 // phpcs:disable
355 $object = parent::_cleanObjectDatas($object);
356
357 return $object;
358 }
359
369 private function validate($data, $fields)
370 {
371 $hook = array();
372 foreach ($fields as $field) {
373 if (!isset($data[$field])) {
374 throw new RestException(400, $field." field missing");
375 }
376 $hook[$field] = $data[$field];
377 }
378 return $hook;
379 }
380}
Class for API REST v1.
Definition api.class.php:31
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='')
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.