dolibarr  x.y.z
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 
25 use Luracast\Restler\RestException;
26 
27 require_once DOL_DOCUMENT_ROOT.'/zapier/class/hook.class.php';
28 
29 
36 class 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  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
202  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
203  }
204  $regexstring = '\‍(([^:\'\‍(\‍)]+:[^:\'\‍(\‍)]+:[^\‍(\‍)]+)\‍)';
205  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
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->_cleanObjectDatas($hook_static);
227  }
228  $i++;
229  }
230  } else {
231  throw new RestException(503, 'Error when retrieve hook list');
232  }
233  if (!count($obj_ret)) {
234  throw new RestException(404, 'No hook found');
235  }
236  return $obj_ret;
237  }
238 
247  public function post($request_data = null)
248  {
249  if (!DolibarrApiAccess::$user->rights->zapier->write) {
250  throw new RestException(401);
251  }
252 
253  // Check mandatory fields
254  $fields = array(
255  'url',
256  );
257  dol_syslog("API Zapier create hook receive : ".print_r($request_data, true), LOG_DEBUG);
258  $result = $this->validate($request_data, $fields);
259 
260  foreach ($request_data as $field => $value) {
261  $this->hook->$field = $value;
262  }
263  $this->hook->fk_user = DolibarrApiAccess::$user->id;
264  // on crée le hook dans la base
265  if (!$this->hook->create(DolibarrApiAccess::$user)) {
266  throw new RestException(500, "Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
267  }
268  return array(
269  'id' => $this->hook->id,
270  );
271  }
272 
273  // /**
274  // * Update hook
275  // *
276  // * @param int $id Id of hook to update
277  // * @param array $request_data Datas
278  // * @return int
279  // *
280  // * @url PUT /hooks/{id}
281  // */
282  /*public function put($id, $request_data = null)
283  {
284  if (! DolibarrApiAccess::$user->rights->zapier->write) {
285  throw new RestException(401);
286  }
287 
288  $result = $this->hook->fetch($id);
289  if( ! $result ) {
290  throw new RestException(404, 'Hook not found');
291  }
292 
293  if( ! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
294  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
295  }
296 
297  foreach($request_data as $field => $value) {
298  if ($field == 'id') {
299  continue;
300  }
301  $this->hook->$field = $value;
302  }
303 
304  if ($this->hook->update($id, DolibarrApiAccess::$user) > 0) {
305  return $this->get($id);
306  } else {
307  throw new RestException(500, $this->hook->error);
308  }
309  }*/
310 
319  public function delete($id)
320  {
321  if (!DolibarrApiAccess::$user->rights->zapier->delete) {
322  throw new RestException(401);
323  }
324 
325  $result = $this->hook->fetch($id);
326  if (!$result) {
327  throw new RestException(404, 'Hook not found');
328  }
329 
330  if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
331  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
332  }
333 
334  if (!$this->hook->delete(DolibarrApiAccess::$user)) {
335  throw new RestException(500, 'Error when deleting Hook : '.$this->hook->error);
336  }
337 
338  return array(
339  'success' => array(
340  'code' => 200,
341  'message' => 'Hook deleted'
342  )
343  );
344  }
345 
346  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
353  public function _cleanObjectDatas($object)
354  {
355  // phpcs:disable
356  $object = parent::_cleanObjectDatas($object);
357 
358  /*unset($object->note);
359  unset($object->address);
360  unset($object->barcode_type);
361  unset($object->barcode_type_code);
362  unset($object->barcode_type_label);
363  unset($object->barcode_type_coder);*/
364 
365  return $object;
366  }
367 
377  private function validate($data, $fields)
378  {
379  $hook = array();
380  foreach ($fields as $field) {
381  if (!isset($data[$field])) {
382  throw new RestException(400, $field." field missing");
383  }
384  $hook[$field] = $data[$field];
385  }
386  return $hook;
387  }
388 }
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.
Definition: api.class.php:283
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Class for Hook.
Definition: hook.class.php:30
_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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41