dolibarr  7.0.0-beta
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 
19  use Luracast\Restler\RestException;
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  $this->actioncomm->fetchObjectLinked();
84  return $this->_cleanObjectDatas($this->actioncomm);
85  }
86 
100  function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '') {
101  global $db, $conf;
102 
103  $obj_ret = array();
104 
105  if (! DolibarrApiAccess::$user->rights->agenda->myactions->read) {
106  throw new RestException(401, "Insuffisant rights to read events");
107  }
108 
109  // case of external user
110  $socid = 0;
111  if (! empty(DolibarrApiAccess::$user->socid)) $socid = DolibarrApiAccess::$user->socid;
112 
113  // If the internal user must only see his customers, force searching by him
114  $search_sale = 0;
115  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
116 
117  $sql = "SELECT t.id as rowid";
118  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)
119  $sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as t";
120  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
121  $sql.= ' WHERE t.entity IN ('.getEntity('agenda').')';
122  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
123  if ($user_ids) $sql.=" AND t.fk_user_action IN (".$user_ids.")";
124  if ($socid > 0) $sql.= " AND t.fk_soc = ".$socid;
125  // Insert sale filter
126  if ($search_sale > 0)
127  {
128  $sql .= " AND sc.fk_user = ".$search_sale;
129  }
130  // Add sql filters
131  if ($sqlfilters)
132  {
133  if (! DolibarrApi::_checkFilters($sqlfilters))
134  {
135  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
136  }
137  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
138  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
139  }
140 
141  $sql.= $db->order($sortfield, $sortorder);
142  if ($limit) {
143  if ($page < 0)
144  {
145  $page = 0;
146  }
147  $offset = $limit * $page;
148 
149  $sql.= $db->plimit($limit + 1, $offset);
150  }
151 
152  $result = $db->query($sql);
153 
154  if ($result)
155  {
156  $i=0;
157  $num = $db->num_rows($result);
158  $min = min($num, ($limit <= 0 ? $num : $limit));
159  while ($i < $min)
160  {
161  $obj = $db->fetch_object($result);
162  $actioncomm_static = new ActionComm($db);
163  if ($actioncomm_static->fetch($obj->rowid)) {
164  $obj_ret[] = $this->_cleanObjectDatas($actioncomm_static);
165  }
166  $i++;
167  }
168  }
169  else {
170  throw new RestException(503, 'Error when retrieve Agenda Event list : '.$db->lasterror());
171  }
172  if ( ! count($obj_ret)) {
173  throw new RestException(404, 'No Agenda Event found');
174  }
175  return $obj_ret;
176  }
177 
184  function post($request_data = NULL)
185  {
186  if (! DolibarrApiAccess::$user->rights->agenda->myactions->create) {
187  throw new RestException(401, "Insuffisant rights to create your Agenda Event");
188  }
189  if (! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
190  throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
191  }
192 
193  // Check mandatory fields
194  $result = $this->_validate($request_data);
195 
196  foreach($request_data as $field => $value) {
197  $this->actioncomm->$field = $value;
198  }
199  /*if (isset($request_data["lines"])) {
200  $lines = array();
201  foreach ($request_data["lines"] as $line) {
202  array_push($lines, (object) $line);
203  }
204  $this->expensereport->lines = $lines;
205  }*/
206  if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
207  throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
208  }
209 
210  return $this->actioncomm->id;
211  }
212 
213 
222  /*
223  function put($id, $request_data = NULL) {
224  if (! DolibarrApiAccess::$user->rights->agenda->myactions->create) {
225  throw new RestException(401, "Insuffisant rights to create your Agenda Event");
226  }
227  if (! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
228  throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
229  }
230 
231  $result = $this->expensereport->fetch($id);
232  if ( ! $result ) {
233  throw new RestException(404, 'expensereport not found');
234  }
235 
236  if ( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
237  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
238  }
239  foreach($request_data as $field => $value) {
240  if ($field == 'id') continue;
241  $this->expensereport->$field = $value;
242  }
243 
244  if ($this->expensereport->update($id, DolibarrApiAccess::$user,1,'','','update'))
245  return $this->get($id);
246 
247  return false;
248  }
249  */
250 
258  function delete($id)
259  {
260  if(! DolibarrApiAccess::$user->rights->agenda->myactions->delete) {
261  throw new RestException(401, "Insuffisant rights to delete your Agenda Event");
262  }
263 
264  $result = $this->actioncomm->fetch($id);
265 
266  if(! DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
267  throw new RestException(401, "Insuffisant rights to delete an Agenda Event of owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
268  }
269 
270  if( ! $result ) {
271  throw new RestException(404, 'Agenda Event not found');
272  }
273 
274  if( ! DolibarrApi::_checkAccessToResource('actioncomm',$this->actioncomm->id)) {
275  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
276  }
277 
278  if( ! $this->actioncomm->delete(DolibarrApiAccess::$user)) {
279  throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
280  }
281 
282  return array(
283  'success' => array(
284  'code' => 200,
285  'message' => 'Agenda Event deleted'
286  )
287  );
288 
289  }
290 
298  function _validate($data)
299  {
300  $event = array();
301  foreach (AgendaEvents::$FIELDS as $field) {
302  if (!isset($data[$field]))
303  throw new RestException(400, "$field field missing");
304  $event[$field] = $data[$field];
305 
306  }
307  return $event;
308  }
309 
316  function _cleanObjectDatas($object) {
317 
318  $object = parent::_cleanObjectDatas($object);
319 
320  unset($object->usermod);
321  unset($object->libelle);
322  unset($object->array_options);
323  unset($object->context);
324  unset($object->canvas);
325  unset($object->contact);
326  unset($object->contact_id);
327  unset($object->thirdparty);
328  unset($object->user);
329  unset($object->origin);
330  unset($object->origin_id);
331  unset($object->ref_ext);
332  unset($object->statut);
333  unset($object->country);
334  unset($object->country_id);
335  unset($object->country_code);
336  unset($object->barcode_type);
337  unset($object->barcode_type_code);
338  unset($object->barcode_type_label);
339  unset($object->barcode_type_coder);
340  unset($object->mode_reglement_id);
341  unset($object->cond_reglement_id);
342  unset($object->cond_reglement);
343  unset($object->fk_delivery_address);
344  unset($object->shipping_method_id);
345  unset($object->fk_account);
346  unset($object->total_ht);
347  unset($object->total_tva);
348  unset($object->total_localtax1);
349  unset($object->total_localtax2);
350  unset($object->total_ttc);
351  unset($object->fk_incoterms);
352  unset($object->libelle_incoterms);
353  unset($object->location_incoterms);
354  unset($object->name);
355  unset($object->lastname);
356  unset($object->firstname);
357  unset($object->civility_id);
358  unset($object->contact);
359  unset($object->societe);
360 
361  return $object;
362  }
363 }
_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.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
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:221
post($request_data=NULL)
Create Agenda Event object.