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