dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
17  */
18 
20 
21  require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
22 
30 {
31 
35  static $FIELDS = array(
36  );
37 
41  public $actioncomm;
42 
43 
47  function __construct()
48  {
49  global $db, $conf;
50  $this->db = $db;
51  $this->actioncomm = new ActionComm($this->db);
52  }
53 
64  function get($id)
65  {
66  if (! DolibarrApiAccess::$user->rights->agenda->myactions->read) {
67  throw new RestException(401, "Insuffisant rights to read an event");
68  }
69 
70  $result = $this->actioncomm->fetch($id);
71  if ( ! $result ) {
72  throw new RestException(404, 'Agenda Events not found');
73  }
74 
75  if (! DolibarrApiAccess::$user->rights->agenda->allactions->read && $this->actioncomm->ownerid != DolibarrApiAccess::$user->id) {
76  throw new RestException(401, "Insuffisant rights to read event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
77  }
78 
79  if ( ! DolibarrApi::_checkAccessToResource('agenda',$this->actioncomm->id,'actioncomm','','fk_soc','id')) {
80  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
81  }
82 
83  $result = $this->actioncomm->fetch_optionals();
84 
85  $this->actioncomm->fetchObjectLinked();
86  return $this->_cleanObjectDatas($this->actioncomm);
87  }
88 
102  function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '')
103  {
104  global $db, $conf;
105 
106  $obj_ret = array();
107 
108  if (! DolibarrApiAccess::$user->rights->agenda->myactions->read) {
109  throw new RestException(401, "Insuffisant rights to read events");
110  }
111 
112  // case of external user
113  $socid = 0;
114  if (! empty(DolibarrApiAccess::$user->socid)) $socid = DolibarrApiAccess::$user->socid;
115 
116  // If the internal user must only see his customers, force searching by him
117  $search_sale = 0;
118  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
119  if (empty($conf->societe->enabled)) $search_sale = 0; // If module thirdparty not enabled, sale representative is something that does not exists
120 
121  $sql = "SELECT t.id as rowid";
122  if (! empty($conf->societe->enabled))
123  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)
124  $sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as t";
125  if (! empty($conf->societe->enabled))
126  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
127  $sql.= ' WHERE t.entity IN ('.getEntity('agenda').')';
128  if (! empty($conf->societe->enabled))
129  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
130  if ($user_ids) $sql.=" AND t.fk_user_action IN (".$user_ids.")";
131  if ($socid > 0) $sql.= " AND t.fk_soc = ".$socid;
132  // Insert sale filter
133  if ($search_sale > 0)
134  {
135  $sql .= " AND sc.fk_user = ".$search_sale;
136  }
137  // Add sql filters
138  if ($sqlfilters)
139  {
140  if (! DolibarrApi::_checkFilters($sqlfilters))
141  {
142  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
143  }
144  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
145  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
146  }
147 
148  $sql.= $db->order($sortfield, $sortorder);
149  if ($limit) {
150  if ($page < 0)
151  {
152  $page = 0;
153  }
154  $offset = $limit * $page;
155 
156  $sql.= $db->plimit($limit + 1, $offset);
157  }
158 
159  $result = $db->query($sql);
160 
161  if ($result)
162  {
163  $i=0;
164  $num = $db->num_rows($result);
165  $min = min($num, ($limit <= 0 ? $num : $limit));
166  while ($i < $min)
167  {
168  $obj = $db->fetch_object($result);
169  $actioncomm_static = new ActionComm($db);
170  if ($actioncomm_static->fetch($obj->rowid)) {
171  $obj_ret[] = $this->_cleanObjectDatas($actioncomm_static);
172  }
173  $i++;
174  }
175  }
176  else {
177  throw new RestException(503, 'Error when retrieve Agenda Event list : '.$db->lasterror());
178  }
179  if ( ! count($obj_ret)) {
180  throw new RestException(404, 'No Agenda Event found');
181  }
182  return $obj_ret;
183  }
184 
191  function post($request_data = null)
192  {
193  if (! DolibarrApiAccess::$user->rights->agenda->myactions->create) {
194  throw new RestException(401, "Insuffisant rights to create your Agenda Event");
195  }
196  if (! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
197  throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
198  }
199 
200  // Check mandatory fields
201  $result = $this->_validate($request_data);
202 
203  foreach($request_data as $field => $value) {
204  $this->actioncomm->$field = $value;
205  }
206  /*if (isset($request_data["lines"])) {
207  $lines = array();
208  foreach ($request_data["lines"] as $line) {
209  array_push($lines, (object) $line);
210  }
211  $this->expensereport->lines = $lines;
212  }*/
213  if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
214  throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
215  }
216 
217  return $this->actioncomm->id;
218  }
219 
220 
229  /*
230  function put($id, $request_data = null)
231  {
232  if (! DolibarrApiAccess::$user->rights->agenda->myactions->create) {
233  throw new RestException(401, "Insuffisant rights to create your Agenda Event");
234  }
235  if (! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
236  throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
237  }
238 
239  $result = $this->actioncomm->fetch($id);
240  if ( ! $result ) {
241  throw new RestException(404, 'actioncomm not found');
242  }
243 
244  if ( ! DolibarrApi::_checkAccessToResource('actioncomm',$this->actioncomm->id)) {
245  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
246  }
247  foreach($request_data as $field => $value) {
248  if ($field == 'id') continue;
249  $this->actioncomm->$field = $value;
250  }
251 
252  if ($this->actioncomm->update($id, DolibarrApiAccess::$user,1,'','','update'))
253  return $this->get($id);
254 
255  return false;
256  }
257  */
258 
266  function delete($id)
267  {
268  if(! DolibarrApiAccess::$user->rights->agenda->myactions->delete) {
269  throw new RestException(401, "Insuffisant rights to delete your Agenda Event");
270  }
271 
272  $result = $this->actioncomm->fetch($id);
273 
274  if(! DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
275  throw new RestException(401, "Insuffisant rights to delete an Agenda Event of owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
276  }
277 
278  if( ! $result ) {
279  throw new RestException(404, 'Agenda Event not found');
280  }
281 
282  if( ! DolibarrApi::_checkAccessToResource('actioncomm',$this->actioncomm->id)) {
283  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
284  }
285 
286  if( ! $this->actioncomm->delete(DolibarrApiAccess::$user)) {
287  throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
288  }
289 
290  return array(
291  'success' => array(
292  'code' => 200,
293  'message' => 'Agenda Event deleted'
294  )
295  );
296  }
297 
305  function _validate($data)
306  {
307  $event = array();
308  foreach (AgendaEvents::$FIELDS as $field) {
309  if (!isset($data[$field]))
310  throw new RestException(400, "$field field missing");
311  $event[$field] = $data[$field];
312  }
313  return $event;
314  }
315 
322  function _cleanObjectDatas($object)
323  {
324 
325  $object = parent::_cleanObjectDatas($object);
326 
327  unset($object->usermod);
328  unset($object->libelle);
329  unset($object->context);
330  unset($object->canvas);
331  unset($object->contact);
332  unset($object->contact_id);
333  unset($object->thirdparty);
334  unset($object->user);
335  unset($object->origin);
336  unset($object->origin_id);
337  unset($object->ref_ext);
338  unset($object->statut);
339  unset($object->country);
340  unset($object->country_id);
341  unset($object->country_code);
342  unset($object->barcode_type);
343  unset($object->barcode_type_code);
344  unset($object->barcode_type_label);
345  unset($object->barcode_type_coder);
346  unset($object->mode_reglement_id);
347  unset($object->cond_reglement_id);
348  unset($object->cond_reglement);
349  unset($object->fk_delivery_address);
350  unset($object->shipping_method_id);
351  unset($object->fk_account);
352  unset($object->total_ht);
353  unset($object->total_tva);
354  unset($object->total_localtax1);
355  unset($object->total_localtax2);
356  unset($object->total_ttc);
357  unset($object->fk_incoterms);
358  unset($object->libelle_incoterms);
359  unset($object->location_incoterms);
360  unset($object->name);
361  unset($object->lastname);
362  unset($object->firstname);
363  unset($object->civility_id);
364  unset($object->contact);
365  unset($object->societe);
366 
367  return $object;
368  }
369 }
_validate($data)
Validate fields before create or update object.
Class to manage agenda events (actions)
_cleanObjectDatas($object)
Clean sensible object datas.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
post($request_data=null)
Create Agenda Event object.
Class for API REST v1.
Definition: api.class.php:29
__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:225
index($sortfield="t.id", $sortorder='ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters='')
List Agenda Events.