dolibarr 18.0.6
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{
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 (!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->_cleanObjectDatas($actioncomm_static);
189 }
190 $i++;
191 }
192 } else {
193 throw new RestException(503, 'Error when retrieve Agenda Event list : '.$this->db->lasterror());
194 }
195 if (!count($obj_ret)) {
196 throw new RestException(404, 'No Agenda Event found');
197 }
198 return $obj_ret;
199 }
200
207 public function post($request_data = null)
208 {
209 if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
210 throw new RestException(401, "Insufficient rights to create your Agenda Event");
211 }
212 if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
213 throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
214 }
215
216 // Check mandatory fields
217 $result = $this->_validate($request_data);
218
219 foreach ($request_data as $field => $value) {
220 $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
221 }
222 /*if (isset($request_data["lines"])) {
223 $lines = array();
224 foreach ($request_data["lines"] as $line) {
225 array_push($lines, (object) $line);
226 }
227 $this->expensereport->lines = $lines;
228 }*/
229
230 if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
231 throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
232 }
233
234 return $this->actioncomm->id;
235 }
236
237
246 public function put($id, $request_data = null)
247 {
248 if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
249 throw new RestException(401, "Insufficient rights to create your Agenda Event");
250 }
251 if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
252 throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
253 }
254
255 $result = $this->actioncomm->fetch($id);
256 if ($result) {
257 $this->actioncomm->fetch_optionals();
258 $this->actioncomm->fetch_userassigned();
259 $this->actioncomm->oldcopy = clone $this->actioncomm;
260 }
261 if (!$result) {
262 throw new RestException(404, 'actioncomm not found');
263 }
264
265 if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
266 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
267 }
268 foreach ($request_data as $field => $value) {
269 if ($field == 'id') {
270 continue;
271 }
272
273 $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
274 }
275
276 if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
277 return $this->get($id);
278 }
279
280 return false;
281 }
282
290 public function delete($id)
291 {
292 if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'delete')) {
293 throw new RestException(401, "Insufficient rights to delete your Agenda Event");
294 }
295
296 $result = $this->actioncomm->fetch($id);
297 if ($result) {
298 $this->actioncomm->fetch_optionals();
299 $this->actioncomm->fetch_userassigned();
300 $this->actioncomm->oldcopy = clone $this->actioncomm;
301 }
302
303 if (!DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
304 throw new RestException(401, "Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.' Your id is '.DolibarrApiAccess::$user->id);
305 }
306
307 if (!$result) {
308 throw new RestException(404, 'Agenda Event not found');
309 }
310
311 if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
312 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
313 }
314
315 if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
316 throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
317 }
318
319 return array(
320 'success' => array(
321 'code' => 200,
322 'message' => 'Agenda Event deleted'
323 )
324 );
325 }
326
334 private function _validate($data)
335 {
336 $event = array();
337 foreach (AgendaEvents::$FIELDS as $field) {
338 if (!isset($data[$field])) {
339 throw new RestException(400, "$field field missing");
340 }
341 $event[$field] = $data[$field];
342 }
343 return $event;
344 }
345
346 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
353 protected function _cleanObjectDatas($object)
354 {
355 // phpcs:enable
356 $object = parent::_cleanObjectDatas($object);
357
358 unset($object->note); // alreaydy into note_private
359 unset($object->usermod);
360 unset($object->libelle);
361 unset($object->context);
362 unset($object->canvas);
363 unset($object->contact);
364 unset($object->contact_id);
365 unset($object->thirdparty);
366 unset($object->user);
367 unset($object->origin);
368 unset($object->origin_id);
369 unset($object->ref_ext);
370 unset($object->statut);
371 unset($object->state_code);
372 unset($object->state_id);
373 unset($object->state);
374 unset($object->region);
375 unset($object->region_code);
376 unset($object->country);
377 unset($object->country_id);
378 unset($object->country_code);
379 unset($object->barcode_type);
380 unset($object->barcode_type_code);
381 unset($object->barcode_type_label);
382 unset($object->barcode_type_coder);
383 unset($object->mode_reglement_id);
384 unset($object->cond_reglement_id);
385 unset($object->cond_reglement);
386 unset($object->fk_delivery_address);
387 unset($object->shipping_method_id);
388 unset($object->fk_account);
389 unset($object->total_ht);
390 unset($object->total_tva);
391 unset($object->total_localtax1);
392 unset($object->total_localtax2);
393 unset($object->total_ttc);
394 unset($object->fk_incoterms);
395 unset($object->label_incoterms);
396 unset($object->location_incoterms);
397 unset($object->name);
398 unset($object->lastname);
399 unset($object->firstname);
400 unset($object->civility_id);
401 unset($object->contact);
402 unset($object->societe);
403 unset($object->demand_reason_id);
404 unset($object->transport_mode_id);
405 unset($object->region_id);
406 unset($object->actions);
407 unset($object->lines);
408 unset($object->modelpdf);
409
410 return $object;
411 }
412}
Class to manage agenda events (actions)
_cleanObjectDatas($object)
Clean sensible object datas.
index($sortfield="t.id", $sortorder='ASC', $limit=100, $page=0, $user_ids=0, $sqlfilters='')
List Agenda Events.
_validate($data)
Validate fields before create or update object.
__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
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:86
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria