dolibarr 25.0.0-alpha
api_eventattendees.class.php
1<?php
2/*
3/* Copyright (C) 2025 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
4 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
23require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php';
24
32{
36 public static $FIELDS = array(
37 'fk_project'
38 );
39
43 public static $INTFIELDS = array(
44 'fk_soc',
45 'fk_actioncomm',
46 'fk_project',
47 'fk_invoice',
48 'status'
49 );
50
54 public $event_attendees;
55
59 public $table_element = 'eventorganization_conferenceorboothattendee';
60
64 public function __construct()
65 {
66 global $db;
67 $this->db = $db;
68 $this->event_attendees = new ConferenceOrBoothAttendee($this->db);
69 }
70
85 public function deleteById($id)
86 {
87 if ($id < 1 ) {
88 throw new RestException(400, 'No eventattendee with id<1 can exist');
89 }
90 $allowaccess = $this->_checkAccessRights('delete', 0);
91 if (!$allowaccess) {
92 throw new RestException(403, 'denied read access to Event attendees');
93 }
94
95 $result = $this->event_attendees->fetch($id, '');
96 if (!$result) {
97 throw new RestException(404, 'Event attendee with id '.$id.' not found');
98 }
99
100 if (!$this->event_attendees->delete(DolibarrApiAccess::$user)) {
101 throw new RestException(500, 'Error when delete event attendee : '.$this->event_attendees->errorsToString());
102 }
103
104 return array(
105 'success' => array(
106 'code' => 200,
107 'message' => 'event attendee deleted'
108 )
109 );
110 }
111
126 public function deleteByRef($ref)
127 {
128 $allowaccess = $this->_checkAccessRights('delete', 0);
129 if (!$allowaccess) {
130 throw new RestException(403, 'denied read access to Event attendees');
131 }
132
133 $result = $this->event_attendees->fetch(0, $ref);
134 if (!$result) {
135 throw new RestException(404, "Event attendee with ref ".$ref." not found");
136 }
137
138 if (!$this->event_attendees->delete(DolibarrApiAccess::$user)) {
139 throw new RestException(500, 'Error when delete event attendee : '.$this->event_attendees->errorsToString());
140 }
141
142 return array(
143 'success' => array(
144 'code' => 200,
145 'message' => 'event attendee deleted'
146 )
147 );
148 }
149
165 public function getById($id)
166 {
167 return $this->_fetch($id, '');
168 }
169
185 public function getByRef($ref)
186 {
187 return $this->_fetch(0, $ref);
188 }
189
212 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false, $loadlinkedobjects = 0)
213 {
214 $allowaccess = $this->_checkAccessRights('read', 0);
215 if (!$allowaccess) {
216 throw new RestException(403, 'denied read access to Event attendees');
217 }
218 // Fine-grained access is still checked later for each row on its fk_project
219 // entity stolen from api_setup.class.php
220 $entity = (int) DolibarrApiAccess::$user->entity;
221 $obj_ret = array();
222
223 $sql = "SELECT t.rowid";
224 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." AS t";
225 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet AS p ON t.fk_project = p.rowid";
226 if (isModEnabled('multicompany')) {
227 $sql .= ' WHERE p.entity = '.((int) $entity);
228 } else {
229 $sql .= ' WHERE 1 = 1';
230 }
231 // If user has no permission to see all projects, we restrict the list to projects he is allowed to see
232 // (public projects or projects he is a contact of), like api_projects::index() does,
233 // so unauthorized rows can't be probed with the sqlfilters parameter
234 if (!DolibarrApiAccess::$user->hasRight('project', 'all', 'read')) {
235 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
236 $projectstatic = new Project($this->db);
237 $projectsListId = $projectstatic->getProjectsAuthorizedForUser(DolibarrApiAccess::$user, 0, 1, 0);
238 $sql .= ' AND t.fk_project IN ('.$this->db->sanitize($projectsListId).')';
239 }
240
241
242 // Add sql filters
243 if ($sqlfilters) {
244 $errormessage = '';
245 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
246 if ($errormessage) {
247 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
248 }
249 }
250
251 //this query will return total orders with the filters given
252 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
253
254 $sql .= $this->db->order($sortfield, $sortorder);
255 if ($limit) {
256 if ($page < 0) {
257 $page = 0;
258 }
259 $offset = $limit * $page;
260
261 $sql .= $this->db->plimit($limit + 1, $offset);
262 }
263
264 dol_syslog(get_class($this)."::index", LOG_DEBUG);
265 $result = $this->db->query($sql);
266
267 if ($result) {
268 $num = $this->db->num_rows($result);
269 $min = min($num, ($limit <= 0 ? $num : $limit));
270 $i = 0;
271 while ($i < $min) {
272 $obj = $this->db->fetch_object($result);
273 $event_attendees_static = new ConferenceOrBoothAttendee($this->db);
274 if ($event_attendees_static->fetch($obj->rowid) > 0) {
275 try {
276 $rowallowaccess = $this->_checkAccessRights('read', $event_attendees_static->fk_project);
277 } catch (RestException $e) {
278 // No access to this row, we skip it so the response does not depend on rows we are not allowed to see
279 $rowallowaccess = false;
280 }
281 if ($rowallowaccess) {
282 if ($loadlinkedobjects) {
283 // retrieve linked objects
284 $event_attendees_static->fetchObjectLinked();
285 }
286 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($event_attendees_static), $properties);
287 }
288 }
289 $i++;
290 }
291 } else {
292 throw new RestException(503, 'Error when retrieve event attendee list : '.$this->db->lasterror());
293 }
294
295 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
296 if ($pagination_data) {
297 $totalsResult = $this->db->query($sqlTotals);
298 $total = $this->db->fetch_object($totalsResult)->total;
299
300 $tmp = $obj_ret;
301 $obj_ret = [];
302
303 $obj_ret['data'] = $tmp;
304 $obj_ret['pagination'] = [
305 'total' => (int) $total,
306 'page' => $page, //count starts from 0
307 'page_count' => ceil((int) $total / $limit),
308 'limit' => $limit
309 ];
310 }
311
312 return $obj_ret;
313 }
314
333 public function post($request_data = null)
334 {
335 $allowaccess = $this->_checkAccessRights('write', 0);
336 if (!$allowaccess) {
337 throw new RestException(403, 'denied create access to Event attendees');
338 }
339
340 // Check mandatory fields
341 $result = $this->_validate($request_data);
342
343 foreach ($request_data as $field => $value) {
344 if ($field === 'caller') {
345 // 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 with the caller
346 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
347 continue;
348 }
349
350 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
351 }
352
353 if ($this->event_attendees->create(DolibarrApiAccess::$user) < 0) {
354 throw new RestException(500, "Error creating event attendee", array_merge(array($this->event_attendees->error), $this->event_attendees->errors));
355 }
356
357 return ((int) $this->event_attendees->id);
358 }
359
379 public function putById($id, $request_data = null)
380 {
381 if ($id < 1 ) {
382 throw new RestException(400, 'No eventattendee with id<1 can exist');
383 }
384 $allowaccess = $this->_checkAccessRights('write', 0);
385 if (!$allowaccess) {
386 throw new RestException(403, 'denied update access to Event attendees');
387 }
388
389 $result = $this->event_attendees->fetch($id, '');
390 if (!$result) {
391 throw new RestException(404, 'event attendee not found');
392 }
393
394 foreach ($request_data as $field => $value) {
395 if ($field == 'id') {
396 continue;
397 }
398 if ($field === 'caller') {
399 // 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 with the caller
400 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
401 continue;
402 }
403
404 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
405 }
406
407 if ($this->event_attendees->update(DolibarrApiAccess::$user) > 0) {
408 return $this->_fetch($id, '');
409 } else {
410 throw new RestException(500, end($this->event_attendees->errors));
411 }
412 }
413
433 public function putByRef($ref, $request_data = null)
434 {
435 $allowaccess = $this->_checkAccessRights('write', 0);
436 if (!$allowaccess) {
437 throw new RestException(403, 'denied update access to Event attendees');
438 }
439
440 $result = $this->event_attendees->fetch(0, $ref);
441 if (!$result) {
442 throw new RestException(404, 'event attendee not found');
443 }
444
445 $newref = $ref;
446 foreach ($request_data as $field => $value) {
447 if ($field == 'id') {
448 continue;
449 }
450 if ($field == 'ref') {
451 $newref = $this->_checkValForAPI($field, $value, $this->event_attendees);
452 }
453 if ($field === 'caller') {
454 // 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 with the caller
455 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
456 continue;
457 }
458
459 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
460 }
461
462 if ($this->event_attendees->update(DolibarrApiAccess::$user) > 0) {
463 return $this->_fetch(0, $newref);
464 } else {
465 throw new RestException(500, end($this->event_attendees->errors));
466 }
467 }
468
483 private function _fetch($id, $ref = '')
484 {
485 if ($id < 1 && empty($ref)) {
486 throw new RestException(400, 'No eventattendee with id<1 can exist');
487 }
488 if (empty($id) && empty($ref)) {
489 throw new RestException(400, 'No eventattendee can be found with no criteria');
490 }
491 $allowaccess = $this->_checkAccessRights('read', 0);
492 if (!$allowaccess) {
493 throw new RestException(403, 'denied read access to Event attendees');
494 }
495 // we first need to fetch the object so we can get the fk_project id and then check for access
496 $result = $this->event_attendees->fetch($id, $ref);
497 if (!$result) {
498 if ($id) {
499 throw new RestException(404, 'Event attendee with id '.((string) $id).' not found');
500 }
501 if ($ref) {
502 throw new RestException(404, 'Event attendee with ref '.$ref.' not found');
503 }
504 throw new RestException(404, 'Event attendee not found');
505 }
506 $project_id = $this->event_attendees->fk_project;
507 $allowaccess = $this->_checkAccessRights('read', $project_id);
508 if (!$allowaccess) {
509 throw new RestException(403, 'denied read access to Event attendees');
510 }
511
512 $this->event_attendees->fetchObjectLinked();
513
514 return $this->_cleanObjectDatas($this->event_attendees);
515 }
516
517 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
530 protected function _cleanObjectDatas($object)
531 {
532 // phpcs:enable
533 $object = parent::_cleanObjectDatas($object);
534
535 unset($object->array_languages);
536 unset($object->contacts_ids);
537 unset($object->canvas);
538 unset($object->contact_id);
539 unset($object->user);
540 unset($object->origin_type);
541 unset($object->origin_id);
542 unset($object->ref_ext);
543 unset($object->statut);
544 unset($object->civility_code);
545 unset($object->country_id);
546 unset($object->country_code);
547 unset($object->state_id);
548 unset($object->region_id);
549 unset($object->barcode_type);
550 unset($object->barcode_type_coder);
551 unset($object->mode_reglement_id);
552 unset($object->cond_reglement_id);
553 unset($object->demand_reason_id);
554 unset($object->transport_mode_id);
555 unset($object->shipping_method_id);
556 unset($object->shipping_method);
557 unset($object->fk_multicurrency);
558 unset($object->multicurrency_code);
559 unset($object->multicurrency_tx);
560 unset($object->multicurrency_total_ht);
561 unset($object->multicurrency_total_tva);
562 unset($object->multicurrency_total_ttc);
563 unset($object->multicurrency_total_localtax1);
564 unset($object->multicurrency_total_localtax2);
565 unset($object->fk_account);
566 unset($object->total_ht);
567 unset($object->total_tva);
568 unset($object->total_localtax1);
569 unset($object->total_localtax2);
570 unset($object->total_ttc);
571 unset($object->lines);
572 unset($object->actiontypecode);
573 unset($object->name);
574 unset($object->civility_id);
575 unset($object->user_creation_id);
576 unset($object->user_validation_id);
577 unset($object->user_closing_id);
578 unset($object->user_modification_id);
579 unset($object->totalpaid);
580 unset($object->product);
581 unset($object->cond_reglement_supplier_id);
582 unset($object->deposit_percent);
583 unset($object->retained_warranty_fk_cond_reglement);
584 unset($object->warehouse_id);
585 unset($object->target);
586 unset($object->extraparams);
587 unset($object->specimen);
588 unset($object->date_validation);
589 unset($object->date_modification);
590 unset($object->date_cloture);
591 unset($object->rowid);
592 unset($object->module);
593 unset($object->entity);
594 unset($object->paid);
595
596 return $object;
597 }
598
608 private function _validate($data)
609 {
610 $event_attendees = array();
611 foreach (EventAttendees::$FIELDS as $field) {
612 if (!isset($data[$field])) {
613 throw new RestException(400, $field." field missing");
614 }
615 $event_attendees[$field] = $data[$field];
616 }
617 return $event_attendees;
618 }
619
631 private function _checkAccessRights($accesstype, $project_id = 0)
632 {
633 // what kind of access management do we need?
634 $moduleaccess = false;
635 if (isModEnabled("eventorganization") && DolibarrApiAccess::$user->hasRight('project', $accesstype)) {
636 $moduleaccess = true;
637 }
638 $fullprojectaccess = false;
639 if (DolibarrApiAccess::$user->hasRight('project', 'all', $accesstype)) {
640 $fullprojectaccess = true;
641 }
642
643 if ($moduleaccess && $fullprojectaccess) {
644 return true;
645 } else {
646 $singleprojectaccess = false;
647 if (0 < $project_id) {
648 // we should also check project visibility and if set to assigned contacts it should be only those contacts.
649 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
650 $event_project = new Project($this->db);
651 $result = $event_project->fetch($project_id);
652 if (0 < $result) {
653 $public = $event_project->public;
654 if ( 1 == $public) {
655 $singleprojectaccess = true;
656 } else {
657 $userProjectAccessListId = $event_project->getProjectsAuthorizedForUser(DolibarrApiAccess::$user, 0, 0);
658 $project_title = $event_project->title;
659 if (in_array($project_title, $userProjectAccessListId)) {
660 $singleprojectaccess = true;
661 } else {
662 dol_syslog("project_title ".$project_title." is NOT in array from getProjectsAuthorizedForUser()", LOG_DEBUG);
663 return false;
664 }
665 }
666 } elseif (0 == $result) {
667 throw new RestException(500, 'Project id '.$project_id.' not found');
668 } else {
669 throw new RestException(500, 'Error during fetch project '.$project_id.': '.$this->db->lasterror());
670 }
671 } elseif ($moduleaccess && ($project_id == 0)) {
672 return true;
673 // because we assume that the caller will know to check for each fk_projekt
674 }
675 if ($moduleaccess && $singleprojectaccess) {
676 return true;
677 } elseif ($moduleaccess) {
678 throw new RestException(403, 'Event attendees access granted, but denied access to the project');
679 } elseif ($singleprojectaccess) {
680 throw new RestException(403, 'project access granted, but denied access to Event attendees');
681 } else {
682 throw new RestException(403, 'denied access both Event attendees and the project');
683 }
684 }
685 }
686}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for ConferenceOrBoothAttendee.
Class for API REST v1.
Definition api.class.php:35
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_validate($data)
Validate fields before create or update object.
putByRef($ref, $request_data=null)
Update an event attendee.
_checkAccessRights($accesstype, $project_id=0)
function to check for access rights - should probably have 1.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List Event attendees.
getById($id)
Get properties of a event attendee by id.
deleteByRef($ref)
Delete an event attendee.
_fetch($id, $ref='')
Get properties of an event attendee.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
getByRef($ref)
Get properties of an event attendee by ref.
__construct()
Constructor of the class.
putById($id, $request_data=null)
Update an event attendee.
deleteById($id)
Delete an event attendee.
post($request_data=null)
Create an event attendee.
Class to manage projects.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.