dolibarr  16.0.5
api_agendaevents.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
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 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
22 
23 
31 {
32 
36  static $FIELDS = array(
37  );
38 
42  public $actioncomm;
43 
44 
48  public function __construct()
49  {
50  global $db, $conf;
51  $this->db = $db;
52  $this->actioncomm = new ActionComm($this->db);
53  }
54 
65  public function get($id)
66  {
67  if (!DolibarrApiAccess::$user->rights->agenda->myactions->read) {
68  throw new RestException(401, "Insufficient rights to read an event");
69  }
70  if ($id === 0) {
71  $result = $this->actioncomm->initAsSpecimen();
72  } else {
73  $result = $this->actioncomm->fetch($id);
74  if ($result) {
75  $this->actioncomm->fetch_optionals();
76  $this->actioncomm->fetchObjectLinked();
77  }
78  }
79  if (!$result) {
80  throw new RestException(404, 'Agenda Events not found');
81  }
82 
83  if (!DolibarrApiAccess::$user->rights->agenda->allactions->read && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) {
84  throw new RestException(401, "Insufficient rights to read event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
85  }
86 
87  if (!DolibarrApi::_checkAccessToResource('agenda', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
88  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
89  }
90  return $this->_cleanObjectDatas($this->actioncomm);
91  }
92 
106  public function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '')
107  {
108  global $db, $conf;
109 
110  $obj_ret = array();
111 
112  if (!DolibarrApiAccess::$user->rights->agenda->myactions->read) {
113  throw new RestException(401, "Insufficient rights to read events");
114  }
115 
116  // case of external user
117  $socid = 0;
118  if (!empty(DolibarrApiAccess::$user->socid)) {
119  $socid = DolibarrApiAccess::$user->socid;
120  }
121 
122  // If the internal user must only see his customers, force searching by him
123  $search_sale = 0;
124  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
125  $search_sale = DolibarrApiAccess::$user->id;
126  }
127  if (empty($conf->societe->enabled)) {
128  $search_sale = 0; // If module thirdparty not enabled, sale representative is something that does not exists
129  }
130 
131  $sql = "SELECT t.id as rowid";
132  if (!empty($conf->societe->enabled)) {
133  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
134  $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
135  }
136  }
137  $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as t";
138  if (!empty($conf->societe->enabled)) {
139  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
140  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
141  }
142  }
143  $sql .= ' WHERE t.entity IN ('.getEntity('agenda').')';
144  if (!empty($conf->societe->enabled)) {
145  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
146  $sql .= " AND t.fk_soc = sc.fk_soc";
147  }
148  }
149  if ($user_ids) {
150  $sql .= " AND t.fk_user_action IN (".$this->db->sanitize($user_ids).")";
151  }
152  if ($socid > 0) {
153  $sql .= " AND t.fk_soc = ".((int) $socid);
154  }
155  // Insert sale filter
156  if ($search_sale > 0) {
157  $sql .= " AND sc.fk_user = ".((int) $search_sale);
158  }
159  // Add sql filters
160  if ($sqlfilters) {
161  $errormessage = '';
162  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
163  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
164  }
165  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
166  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
167  }
168 
169  $sql .= $this->db->order($sortfield, $sortorder);
170  if ($limit) {
171  if ($page < 0) {
172  $page = 0;
173  }
174  $offset = $limit * $page;
175 
176  $sql .= $this->db->plimit($limit + 1, $offset);
177  }
178 
179  $result = $this->db->query($sql);
180 
181  if ($result) {
182  $i = 0;
183  $num = $this->db->num_rows($result);
184  $min = min($num, ($limit <= 0 ? $num : $limit));
185  while ($i < $min) {
186  $obj = $this->db->fetch_object($result);
187  $actioncomm_static = new ActionComm($this->db);
188  if ($actioncomm_static->fetch($obj->rowid)) {
189  $obj_ret[] = $this->_cleanObjectDatas($actioncomm_static);
190  }
191  $i++;
192  }
193  } else {
194  throw new RestException(503, 'Error when retrieve Agenda Event list : '.$this->db->lasterror());
195  }
196  if (!count($obj_ret)) {
197  throw new RestException(404, 'No Agenda Event found');
198  }
199  return $obj_ret;
200  }
201 
208  public function post($request_data = null)
209  {
210  if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
211  throw new RestException(401, "Insufficient rights to create your Agenda Event");
212  }
213  if (!DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
214  throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
215  }
216 
217  // Check mandatory fields
218  $result = $this->_validate($request_data);
219 
220  foreach ($request_data as $field => $value) {
221  $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
222  }
223  /*if (isset($request_data["lines"])) {
224  $lines = array();
225  foreach ($request_data["lines"] as $line) {
226  array_push($lines, (object) $line);
227  }
228  $this->expensereport->lines = $lines;
229  }*/
230 
231  if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
232  throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
233  }
234 
235  return $this->actioncomm->id;
236  }
237 
238 
247  public function put($id, $request_data = null)
248  {
249  if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
250  throw new RestException(401, "Insufficient rights to create your Agenda Event");
251  }
252  if (!DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
253  throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
254  }
255 
256  $result = $this->actioncomm->fetch($id);
257  if ($result) {
258  $this->actioncomm->fetch_optionals();
259  $this->actioncomm->fetch_userassigned();
260  $this->actioncomm->oldcopy = clone $this->actioncomm;
261  }
262  if (!$result) {
263  throw new RestException(404, 'actioncomm not found');
264  }
265 
266  if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
267  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
268  }
269  foreach ($request_data as $field => $value) {
270  if ($field == 'id') {
271  continue;
272  }
273 
274  $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
275  }
276 
277  if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
278  return $this->get($id);
279  }
280 
281  return false;
282  }
283 
291  public function delete($id)
292  {
293  if (!DolibarrApiAccess::$user->rights->agenda->myactions->delete) {
294  throw new RestException(401, "Insufficient rights to delete your Agenda Event");
295  }
296 
297  $result = $this->actioncomm->fetch($id);
298  if ($result) {
299  $this->actioncomm->fetch_optionals();
300  $this->actioncomm->fetch_userassigned();
301  $this->actioncomm->oldcopy = clone $this->actioncomm;
302  }
303 
304  if (!DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
305  throw new RestException(401, "Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.' Your id is '.DolibarrApiAccess::$user->id);
306  }
307 
308  if (!$result) {
309  throw new RestException(404, 'Agenda Event not found');
310  }
311 
312  if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
313  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
314  }
315 
316  if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
317  throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
318  }
319 
320  return array(
321  'success' => array(
322  'code' => 200,
323  'message' => 'Agenda Event deleted'
324  )
325  );
326  }
327 
335  private function _validate($data)
336  {
337  $event = array();
338  foreach (AgendaEvents::$FIELDS as $field) {
339  if (!isset($data[$field])) {
340  throw new RestException(400, "$field field missing");
341  }
342  $event[$field] = $data[$field];
343  }
344  return $event;
345  }
346 
347  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
354  protected function _cleanObjectDatas($object)
355  {
356  // phpcs:enable
357  $object = parent::_cleanObjectDatas($object);
358 
359  unset($object->note); // alreaydy into note_private
360  unset($object->usermod);
361  unset($object->libelle);
362  unset($object->context);
363  unset($object->canvas);
364  unset($object->contact);
365  unset($object->contact_id);
366  unset($object->thirdparty);
367  unset($object->user);
368  unset($object->origin);
369  unset($object->origin_id);
370  unset($object->ref_ext);
371  unset($object->statut);
372  unset($object->state_code);
373  unset($object->state_id);
374  unset($object->state);
375  unset($object->region);
376  unset($object->region_code);
377  unset($object->country);
378  unset($object->country_id);
379  unset($object->country_code);
380  unset($object->barcode_type);
381  unset($object->barcode_type_code);
382  unset($object->barcode_type_label);
383  unset($object->barcode_type_coder);
384  unset($object->mode_reglement_id);
385  unset($object->cond_reglement_id);
386  unset($object->cond_reglement);
387  unset($object->fk_delivery_address);
388  unset($object->shipping_method_id);
389  unset($object->fk_account);
390  unset($object->total_ht);
391  unset($object->total_tva);
392  unset($object->total_localtax1);
393  unset($object->total_localtax2);
394  unset($object->total_ttc);
395  unset($object->fk_incoterms);
396  unset($object->label_incoterms);
397  unset($object->location_incoterms);
398  unset($object->name);
399  unset($object->lastname);
400  unset($object->firstname);
401  unset($object->civility_id);
402  unset($object->contact);
403  unset($object->societe);
404  unset($object->demand_reason_id);
405  unset($object->transport_mode_id);
406  unset($object->region_id);
407  unset($object->actions);
408  unset($object->lines);
409  unset($object->modelpdf);
410 
411  return $object;
412  }
413 }
db
$conf db
API class for accounts.
Definition: inc.php:41
ActionComm
Class to manage agenda events (actions)
Definition: actioncomm.class.php:38
AgendaEvents\__construct
__construct()
Constructor.
Definition: api_agendaevents.class.php:48
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
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
AgendaEvents\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_agendaevents.class.php:335
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
AgendaEvents\index
index($sortfield="t.id", $sortorder='ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters='')
List Agenda Events.
Definition: api_agendaevents.class.php:106
AgendaEvents\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_agendaevents.class.php:354
DolibarrApi\_checkValForAPI
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition: api.class.php:86
AgendaEvents\put
put($id, $request_data=null)
Update Agenda Event general fields.
Definition: api_agendaevents.class.php:247
AgendaEvents
Definition: api_agendaevents.class.php:30
AgendaEvents\post
post($request_data=null)
Create Agenda Event object.
Definition: api_agendaevents.class.php:208