dolibarr  16.0.5
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 <http://www.gnu.org/licenses/>.
17  */
18 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/zapier/class/hook.class.php';
22 
35 class ZapierApi extends DolibarrApi
36 {
40  public static $FIELDS = array(
41  'url',
42  );
43 
44 
48  public $hook;
49 
56  public function __construct()
57  {
58  global $db, $conf;
59  $this->db = $db;
60  $this->hook = new Hook($this->db);
61  }
62 
74  public function get($id)
75  {
76  if (!DolibarrApiAccess::$user->rights->zapier->read) {
77  throw new RestException(401);
78  }
79 
80  $result = $this->hook->fetch($id);
81  if (!$result) {
82  throw new RestException(404, 'Hook not found');
83  }
84 
85  if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
86  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
87  }
88 
89  return $this->_cleanObjectDatas($this->hook);
90  }
91 
102  public function getModulesChoices()
103  {
104  if (!DolibarrApiAccess::$user->rights->zapier->read) {
105  throw new RestException(401);
106  }
107 
108  $arraychoices = array(
109  'invoices' => 'Invoices',
110  'orders' => 'Orders',
111  'thirdparties' => 'Thirparties',
112  'contacts' => 'Contacts',
113  'users' => 'Users',
114  );
115  // $result = $this->hook->fetch($id);
116  // if (! $result ) {
117  // throw new RestException(404, 'Hook not found');
118  // }
119 
120  // if (! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
121  // throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
122  // }
123 
124  return $arraychoices;
125  }
126 
143  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
144  {
145  global $db, $conf;
146 
147  if (!DolibarrApiAccess::$user->rights->zapier->read) {
148  throw new RestException(401);
149  }
150 
151  $obj_ret = array();
152 
153  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
154 
155  // Set to 1 if there is a field socid in table of object
156  $restrictonsocid = 0;
157 
158  // If the internal user must only see his customers, force searching by him
159  $search_sale = 0;
160  if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
161  $search_sale = DolibarrApiAccess::$user->id;
162  }
163 
164  $sql = "SELECT t.rowid";
165  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
166  // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
167  $sql .= ", sc.fk_soc, sc.fk_user";
168  }
169  $sql .= " FROM ".MAIN_DB_PREFIX."hook_mytable as t";
170 
171  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
172  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
173  }
174  $sql .= " WHERE 1 = 1";
175 
176  // Example of use $mode
177  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
178  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
179 
180  $tmpobject = new Hook($this->db);
181  if ($tmpobject->ismultientitymanaged) {
182  $sql .= ' AND t.entity IN ('.getEntity('hook').')';
183  }
184  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
185  $sql .= " AND t.fk_soc = sc.fk_soc";
186  }
187  if ($restrictonsocid && $socid) {
188  $sql .= " AND t.fk_soc = ".((int) $socid);
189  }
190  if ($restrictonsocid && $search_sale > 0) {
191  // Join for the needed table to filter by sale
192  $sql .= " AND t.rowid = sc.fk_soc";
193  }
194  // Insert sale filter
195  if ($restrictonsocid && $search_sale > 0) {
196  $sql .= " AND sc.fk_user = ".((int) $search_sale);
197  }
198  if ($sqlfilters) {
199  $errormessage = '';
200  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
201  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
202  }
203  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
204  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
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  // on crée le hook dans la base
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  /*unset($object->note);
358  unset($object->address);
359  unset($object->barcode_type);
360  unset($object->barcode_type_code);
361  unset($object->barcode_type_label);
362  unset($object->barcode_type_coder);*/
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 }
db
$conf db
API class for accounts.
Definition: inc.php:41
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
ZapierApi\post
post($request_data=null)
Create hook object.
Definition: api_zapier.class.php:246
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
ZapierApi\getModulesChoices
getModulesChoices()
Get list of possibles choices for module.
Definition: api_zapier.class.php:102
Hook
Class for Hook.
Definition: hook.class.php:29
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
ZapierApi\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List hooks.
Definition: api_zapier.class.php:143
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
ZapierApi\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_zapier.class.php:352
ZapierApi\__construct
__construct()
Constructor.
Definition: api_zapier.class.php:56
ZapierApi\validate
validate($data, $fields)
Validate fields before create or update object.
Definition: api_zapier.class.php:376
ZapierApi
Definition: api_zapier.class.php:35