dolibarr 19.0.3
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
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
22
23
31{
35 public static $FIELDS = array(
36 );
37
41 public $actioncomm;
42
43
47 public function __construct()
48 {
49 global $db, $conf;
50 $this->db = $db;
51 $this->actioncomm = new ActionComm($this->db);
52 }
53
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 of this owner id. 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
106 public function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = '', $sqlfilters = '', $properties = '')
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 (!isModEnabled('societe')) {
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 (isModEnabled("societe")) {
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 LEFT JOIN ".MAIN_DB_PREFIX."actioncomm_extrafields AS ef ON (ef.fk_object = t.id)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
138 if (isModEnabled("societe")) {
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 (isModEnabled("societe")) {
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 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
163 if ($errormessage) {
164 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
165 }
166 }
167
168 $sql .= $this->db->order($sortfield, $sortorder);
169 if ($limit) {
170 if ($page < 0) {
171 $page = 0;
172 }
173 $offset = $limit * $page;
174
175 $sql .= $this->db->plimit($limit + 1, $offset);
176 }
177
178 $result = $this->db->query($sql);
179
180 if ($result) {
181 $i = 0;
182 $num = $this->db->num_rows($result);
183 $min = min($num, ($limit <= 0 ? $num : $limit));
184 while ($i < $min) {
185 $obj = $this->db->fetch_object($result);
186 $actioncomm_static = new ActionComm($this->db);
187 if ($actioncomm_static->fetch($obj->rowid)) {
188 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($actioncomm_static), $properties);
189 }
190 $i++;
191 }
192 } else {
193 throw new RestException(503, 'Error when retrieve Agenda Event list : '.$this->db->lasterror());
194 }
195
196 return $obj_ret;
197 }
198
205 public function post($request_data = null)
206 {
207 if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
208 throw new RestException(401, "Insufficient rights to create your Agenda Event");
209 }
210 if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
211 throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
212 }
213
214 // Check mandatory fields
215 $result = $this->_validate($request_data);
216
217 foreach ($request_data as $field => $value) {
218 if ($field === 'caller') {
219 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
220 $this->actioncomm->context['caller'] = $request_data['caller'];
221 continue;
222 }
223
224 $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
225 }
226 /*if (isset($request_data["lines"])) {
227 $lines = array();
228 foreach ($request_data["lines"] as $line) {
229 array_push($lines, (object) $line);
230 }
231 $this->expensereport->lines = $lines;
232 }*/
233
234 if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
235 throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
236 }
237
238 return $this->actioncomm->id;
239 }
240
241
250 public function put($id, $request_data = null)
251 {
252 if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
253 throw new RestException(401, "Insufficient rights to create your Agenda Event");
254 }
255 if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
256 throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
257 }
258
259 $result = $this->actioncomm->fetch($id);
260 if ($result) {
261 $this->actioncomm->fetch_optionals();
262 $this->actioncomm->fetch_userassigned();
263 $this->actioncomm->oldcopy = clone $this->actioncomm;
264 }
265 if (!$result) {
266 throw new RestException(404, 'actioncomm not found');
267 }
268
269 if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
270 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
271 }
272 foreach ($request_data as $field => $value) {
273 if ($field == 'id') {
274 continue;
275 }
276 if ($field === 'caller') {
277 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
278 $this->actioncomm->context['caller'] = $request_data['caller'];
279 continue;
280 }
281
282 $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
283 }
284
285 if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
286 return $this->get($id);
287 }
288
289 return false;
290 }
291
299 public function delete($id)
300 {
301 if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'delete')) {
302 throw new RestException(401, "Insufficient rights to delete your Agenda Event");
303 }
304
305 $result = $this->actioncomm->fetch($id);
306 if ($result) {
307 $this->actioncomm->fetch_optionals();
308 $this->actioncomm->fetch_userassigned();
309 $this->actioncomm->oldcopy = clone $this->actioncomm;
310 }
311
312 if (!DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
313 throw new RestException(401, "Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.' Your id is '.DolibarrApiAccess::$user->id);
314 }
315
316 if (!$result) {
317 throw new RestException(404, 'Agenda Event not found');
318 }
319
320 if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
321 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
322 }
323
324 if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
325 throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
326 }
327
328 return array(
329 'success' => array(
330 'code' => 200,
331 'message' => 'Agenda Event deleted'
332 )
333 );
334 }
335
343 private function _validate($data)
344 {
345 $event = array();
346 foreach (AgendaEvents::$FIELDS as $field) {
347 if (!isset($data[$field])) {
348 throw new RestException(400, "$field field missing");
349 }
350 $event[$field] = $data[$field];
351 }
352 return $event;
353 }
354
355 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
362 protected function _cleanObjectDatas($object)
363 {
364 // phpcs:enable
365 $object = parent::_cleanObjectDatas($object);
366
367 unset($object->note); // alreaydy into note_private
368 unset($object->usermod);
369 unset($object->libelle);
370 unset($object->context);
371 unset($object->canvas);
372 unset($object->contact);
373 unset($object->contact_id);
374 unset($object->thirdparty);
375 unset($object->user);
376 unset($object->origin);
377 unset($object->origin_id);
378 unset($object->ref_ext);
379 unset($object->statut);
380 unset($object->state_code);
381 unset($object->state_id);
382 unset($object->state);
383 unset($object->region);
384 unset($object->region_code);
385 unset($object->country);
386 unset($object->country_id);
387 unset($object->country_code);
388 unset($object->barcode_type);
389 unset($object->barcode_type_code);
390 unset($object->barcode_type_label);
391 unset($object->barcode_type_coder);
392 unset($object->mode_reglement_id);
393 unset($object->cond_reglement_id);
394 unset($object->cond_reglement);
395 unset($object->fk_delivery_address);
396 unset($object->shipping_method_id);
397 unset($object->fk_account);
398 unset($object->total_ht);
399 unset($object->total_tva);
400 unset($object->total_localtax1);
401 unset($object->total_localtax2);
402 unset($object->total_ttc);
403 unset($object->fk_incoterms);
404 unset($object->label_incoterms);
405 unset($object->location_incoterms);
406 unset($object->name);
407 unset($object->lastname);
408 unset($object->firstname);
409 unset($object->civility_id);
410 unset($object->contact);
411 unset($object->societe);
412 unset($object->demand_reason_id);
413 unset($object->transport_mode_id);
414 unset($object->region_id);
415 unset($object->actions);
416 unset($object->lines);
417
418 return $object;
419 }
420}
Class to manage agenda events (actions)
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
index($sortfield="t.id", $sortorder='ASC', $limit=100, $page=0, $user_ids='', $sqlfilters='', $properties='')
List Agenda Events.
__construct()
Constructor.
put($id, $request_data=null)
Update Agenda Event general fields.
post($request_data=null)
Create Agenda Event object.
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:85
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria