dolibarr  13.0.2
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  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  $arraychoices = array(
108  'invoices' => 'Invoices',
109  'orders' => 'Orders',
110  'thirdparties' => 'Thirparties',
111  'contacts' => 'Contacts',
112  'users' => 'Users',
113  );
114  // $result = $this->hook->fetch($id);
115  // if (! $result ) {
116  // throw new RestException(404, 'Hook not found');
117  // }
118 
119  // if (! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
120  // throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
121  // }
122 
123  return $arraychoices;
124  }
125 
142  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
143  {
144  global $db, $conf;
145 
146  $obj_ret = array();
147 
148  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
149 
150  // Set to 1 if there is a field socid in table of object
151  $restrictonsocid = 0;
152 
153  // If the internal user must only see his customers, force searching by him
154  $search_sale = 0;
155  if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
156  $search_sale = DolibarrApiAccess::$user->id;
157  }
158 
159  $sql = "SELECT t.rowid";
160  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
161  // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
162  $sql .= ", sc.fk_soc, sc.fk_user";
163  }
164  $sql .= " FROM ".MAIN_DB_PREFIX."hook_mytable as t";
165 
166  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
167  $sql .= " WHERE 1 = 1";
168 
169  // Example of use $mode
170  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
171  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
172 
173  $tmpobject = new Hook($this->db);
174  if ($tmpobject->ismultientitymanaged) {
175  $sql .= ' AND t.entity IN ('.getEntity('hook').')';
176  }
177  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
178  $sql .= " AND t.fk_soc = sc.fk_soc";
179  }
180  if ($restrictonsocid && $socid) {
181  $sql .= " AND t.fk_soc = ".$socid;
182  }
183  if ($restrictonsocid && $search_sale > 0) {
184  // Join for the needed table to filter by sale
185  $sql .= " AND t.rowid = sc.fk_soc";
186  }
187  // Insert sale filter
188  if ($restrictonsocid && $search_sale > 0) {
189  $sql .= " AND sc.fk_user = ".$search_sale;
190  }
191  if ($sqlfilters) {
192  if (!DolibarrApi::_checkFilters($sqlfilters)) {
193  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
194  }
195  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
196  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
197  }
198 
199  $sql .= $this->db->order($sortfield, $sortorder);
200  if ($limit) {
201  if ($page < 0) {
202  $page = 0;
203  }
204  $offset = $limit * $page;
205 
206  $sql .= $this->db->plimit($limit + 1, $offset);
207  }
208 
209  $result = $this->db->query($sql);
210  $i = 0;
211  if ($result) {
212  $num = $this->db->num_rows($result);
213  while ($i < $num) {
214  $obj = $this->db->fetch_object($result);
215  $hook_static = new Hook($this->db);
216  if ($hook_static->fetch($obj->rowid)) {
217  $obj_ret[] = $this->_cleanObjectDatas($hook_static);
218  }
219  $i++;
220  }
221  } else {
222  throw new RestException(503, 'Error when retrieve hook list');
223  }
224  if (!count($obj_ret)) {
225  throw new RestException(404, 'No hook found');
226  }
227  return $obj_ret;
228  }
229 
238  public function post($request_data = null)
239  {
240  if (!DolibarrApiAccess::$user->rights->zapier->write) {
241  throw new RestException(401);
242  }
243  // Check mandatory fields
244  $fields = array(
245  'url',
246  );
247  dol_syslog("API Zapier create hook receive : ".print_r($request_data, true), LOG_DEBUG);
248  $result = $this->validate($request_data, $fields);
249 
250  foreach ($request_data as $field => $value) {
251  $this->hook->$field = $value;
252  }
253  $this->hook->fk_user = DolibarrApiAccess::$user->id;
254  // on crée le hook dans la base
255  if (!$this->hook->create(DolibarrApiAccess::$user)) {
256  throw new RestException(500, "Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
257  }
258  return array(
259  'id' => $this->hook->id,
260  );
261  }
262 
263  // /**
264  // * Update hook
265  // *
266  // * @param int $id Id of hook to update
267  // * @param array $request_data Datas
268  // * @return int
269  // *
270  // * @url PUT /hooks/{id}
271  // */
272  /*public function put($id, $request_data = null)
273  {
274  if (! DolibarrApiAccess::$user->rights->zapier->write) {
275  throw new RestException(401);
276  }
277 
278  $result = $this->hook->fetch($id);
279  if( ! $result ) {
280  throw new RestException(404, 'Hook not found');
281  }
282 
283  if( ! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
284  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
285  }
286 
287  foreach($request_data as $field => $value) {
288  if ($field == 'id') {
289  continue;
290  }
291  $this->hook->$field = $value;
292  }
293 
294  if ($this->hook->update($id, DolibarrApiAccess::$user) > 0) {
295  return $this->get($id);
296  } else {
297  throw new RestException(500, $this->hook->error);
298  }
299  }*/
300 
309  public function delete($id)
310  {
311  if (!DolibarrApiAccess::$user->rights->zapier->delete) {
312  throw new RestException(401);
313  }
314  $result = $this->hook->fetch($id);
315  if (!$result) {
316  throw new RestException(404, 'Hook not found');
317  }
318 
319  if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
320  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
321  }
322 
323  if (!$this->hook->delete(DolibarrApiAccess::$user)) {
324  throw new RestException(500, 'Error when deleting Hook : '.$this->hook->error);
325  }
326 
327  return array(
328  'success' => array(
329  'code' => 200,
330  'message' => 'Hook deleted'
331  )
332  );
333  }
334 
335  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
342  public function _cleanObjectDatas($object)
343  {
344  // phpcs:disable
345  $object = parent::_cleanObjectDatas($object);
346 
347  /*unset($object->note);
348  unset($object->address);
349  unset($object->barcode_type);
350  unset($object->barcode_type_code);
351  unset($object->barcode_type_label);
352  unset($object->barcode_type_coder);*/
353 
354  return $object;
355  }
356 
366  private function validate($data, $fields)
367  {
368  $hook = array();
369  foreach ($fields as $field) {
370  if (!isset($data[$field])) {
371  throw new RestException(400, $field." field missing");
372  }
373  $hook[$field] = $data[$field];
374  }
375  return $hook;
376  }
377 }
Class for Hook.
Definition: hook.class.php:29
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
getModulesChoices()
Get list of possibles choices for module.
post($request_data=null)
Create hook object.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
Definition: api.class.php:252
validate($data, $fields)
Validate fields before create or update object.
_cleanObjectDatas($object)
Clean sensible object datas.
__construct()
Constructor.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List hooks.