dolibarr  13.0.2
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 
64  public function get($id)
65  {
66  if (!DolibarrApiAccess::$user->rights->agenda->myactions->read) {
67  throw new RestException(401, "Insufficient rights to read an event");
68  }
69  if ($id === 0) {
70  $result = $this->actioncomm->initAsSpecimen();
71  } else {
72  $result = $this->actioncomm->fetch($id);
73  if ($result) {
74  $this->actioncomm->fetch_optionals();
75  $this->actioncomm->fetchObjectLinked();
76  }
77  }
78  if (!$result) {
79  throw new RestException(404, 'Agenda Events not found');
80  }
81 
82  if (!DolibarrApiAccess::$user->rights->agenda->allactions->read && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) {
83  throw new RestException(401, "Insufficient rights to read event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
84  }
85 
86  if (!DolibarrApi::_checkAccessToResource('agenda', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
87  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
88  }
89  return $this->_cleanObjectDatas($this->actioncomm);
90  }
91 
105  public function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '')
106  {
107  global $db, $conf;
108 
109  $obj_ret = array();
110 
111  if (!DolibarrApiAccess::$user->rights->agenda->myactions->read) {
112  throw new RestException(401, "Insufficient rights to read events");
113  }
114 
115  // case of external user
116  $socid = 0;
117  if (!empty(DolibarrApiAccess::$user->socid)) $socid = DolibarrApiAccess::$user->socid;
118 
119  // If the internal user must only see his customers, force searching by him
120  $search_sale = 0;
121  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
122  if (empty($conf->societe->enabled)) $search_sale = 0; // If module thirdparty not enabled, sale representative is something that does not exists
123 
124  $sql = "SELECT t.id as rowid";
125  if (!empty($conf->societe->enabled))
126  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $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)
127  $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as t";
128  if (!empty($conf->societe->enabled))
129  if ((!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
130  $sql .= ' WHERE t.entity IN ('.getEntity('agenda').')';
131  if (!empty($conf->societe->enabled))
132  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc";
133  if ($user_ids) $sql .= " AND t.fk_user_action IN (".$user_ids.")";
134  if ($socid > 0) $sql .= " AND t.fk_soc = ".$socid;
135  // Insert sale filter
136  if ($search_sale > 0)
137  {
138  $sql .= " AND sc.fk_user = ".$search_sale;
139  }
140  // Add sql filters
141  if ($sqlfilters)
142  {
143  if (!DolibarrApi::_checkFilters($sqlfilters))
144  {
145  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
146  }
147  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
148  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
149  }
150 
151  $sql .= $this->db->order($sortfield, $sortorder);
152  if ($limit) {
153  if ($page < 0)
154  {
155  $page = 0;
156  }
157  $offset = $limit * $page;
158 
159  $sql .= $this->db->plimit($limit + 1, $offset);
160  }
161 
162  $result = $this->db->query($sql);
163 
164  if ($result)
165  {
166  $i = 0;
167  $num = $this->db->num_rows($result);
168  $min = min($num, ($limit <= 0 ? $num : $limit));
169  while ($i < $min)
170  {
171  $obj = $this->db->fetch_object($result);
172  $actioncomm_static = new ActionComm($this->db);
173  if ($actioncomm_static->fetch($obj->rowid)) {
174  $obj_ret[] = $this->_cleanObjectDatas($actioncomm_static);
175  }
176  $i++;
177  }
178  } else {
179  throw new RestException(503, 'Error when retrieve Agenda Event list : '.$this->db->lasterror());
180  }
181  if (!count($obj_ret)) {
182  throw new RestException(404, 'No Agenda Event found');
183  }
184  return $obj_ret;
185  }
186 
193  public function post($request_data = null)
194  {
195  if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
196  throw new RestException(401, "Insufficient rights to create your Agenda Event");
197  }
198  if (!DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
199  throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
200  }
201 
202  // Check mandatory fields
203  $result = $this->_validate($request_data);
204 
205  foreach ($request_data as $field => $value) {
206  $this->actioncomm->$field = $value;
207  }
208  /*if (isset($request_data["lines"])) {
209  $lines = array();
210  foreach ($request_data["lines"] as $line) {
211  array_push($lines, (object) $line);
212  }
213  $this->expensereport->lines = $lines;
214  }*/
215  if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
216  throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
217  }
218 
219  return $this->actioncomm->id;
220  }
221 
222 
231  public function put($id, $request_data = null)
232  {
233  if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
234  throw new RestException(401, "Insufficient rights to create your Agenda Event");
235  }
236  if (!DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
237  throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
238  }
239 
240  $result = $this->actioncomm->fetch($id);
241  if ($result) {
242  $this->actioncomm->fetch_optionals();
243  $this->actioncomm->fetch_userassigned();
244  $this->actioncomm->oldcopy = clone $this->actioncomm;
245  }
246  if (!$result) {
247  throw new RestException(404, 'actioncomm not found');
248  }
249 
250  if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
251  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
252  }
253  foreach ($request_data as $field => $value) {
254  if ($field == 'id') continue;
255  $this->actioncomm->$field = $value;
256  }
257 
258  if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0)
259  return $this->get($id);
260 
261  return false;
262  }
263 
271  public function delete($id)
272  {
273  if (!DolibarrApiAccess::$user->rights->agenda->myactions->delete) {
274  throw new RestException(401, "Insufficient rights to delete your Agenda Event");
275  }
276 
277  $result = $this->actioncomm->fetch($id);
278  if ($result) {
279  $this->actioncomm->fetch_optionals();
280  $this->actioncomm->fetch_userassigned();
281  $this->actioncomm->oldcopy = clone $this->actioncomm;
282  }
283 
284  if (!DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
285  throw new RestException(401, "Insufficient rights to delete an Agenda Event of owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
286  }
287 
288  if (!$result) {
289  throw new RestException(404, 'Agenda Event not found');
290  }
291 
292  if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
293  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
294  }
295 
296  if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
297  throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
298  }
299 
300  return array(
301  'success' => array(
302  'code' => 200,
303  'message' => 'Agenda Event deleted'
304  )
305  );
306  }
307 
315  private function _validate($data)
316  {
317  $event = array();
318  foreach (AgendaEvents::$FIELDS as $field) {
319  if (!isset($data[$field]))
320  throw new RestException(400, "$field field missing");
321  $event[$field] = $data[$field];
322  }
323  return $event;
324  }
325 
326  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
333  protected function _cleanObjectDatas($object)
334  {
335  // phpcs:enable
336  $object = parent::_cleanObjectDatas($object);
337 
338  unset($object->note); // alreaydy into note_private
339  unset($object->usermod);
340  unset($object->libelle);
341  unset($object->context);
342  unset($object->canvas);
343  unset($object->contact);
344  unset($object->contact_id);
345  unset($object->thirdparty);
346  unset($object->user);
347  unset($object->origin);
348  unset($object->origin_id);
349  unset($object->ref_ext);
350  unset($object->statut);
351  unset($object->state_code);
352  unset($object->state_id);
353  unset($object->state);
354  unset($object->region);
355  unset($object->region_code);
356  unset($object->country);
357  unset($object->country_id);
358  unset($object->country_code);
359  unset($object->barcode_type);
360  unset($object->barcode_type_code);
361  unset($object->barcode_type_label);
362  unset($object->barcode_type_coder);
363  unset($object->mode_reglement_id);
364  unset($object->cond_reglement_id);
365  unset($object->cond_reglement);
366  unset($object->fk_delivery_address);
367  unset($object->shipping_method_id);
368  unset($object->fk_account);
369  unset($object->total_ht);
370  unset($object->total_tva);
371  unset($object->total_localtax1);
372  unset($object->total_localtax2);
373  unset($object->total_ttc);
374  unset($object->fk_incoterms);
375  unset($object->label_incoterms);
376  unset($object->location_incoterms);
377  unset($object->name);
378  unset($object->lastname);
379  unset($object->firstname);
380  unset($object->civility_id);
381  unset($object->contact);
382  unset($object->societe);
383 
384  unset($object->actions);
385  unset($object->lines);
386 
387  return $object;
388  }
389 }
_validate($data)
Validate fields before create or update object.
Class to manage agenda events (actions)
index($sortfield="t.id", $sortorder= 'ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters= '')
List Agenda Events.
_cleanObjectDatas($object)
Clean sensible object datas.
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
post($request_data=null)
Create Agenda Event object.
Class for API REST v1.
Definition: api.class.php:30
put($id, $request_data=null)
Update Agenda Event general fields.
__construct()
Constructor.
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