dolibarr 24.0.3
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.'/core/lib/functions.lib.php';
24require_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php';
25
33{
37 public static $FIELDS = array(
38 'fk_project'
39 );
40
44 public static $INTFIELDS = array(
45 'fk_soc',
46 'fk_actioncomm',
47 'fk_project',
48 'fk_invoice',
49 'status'
50 );
51
55 public $event_attendees;
56
60 public $table_element = 'eventorganization_conferenceorboothattendee';
61
65 public function __construct()
66 {
67 global $db;
68 $this->db = $db;
69 $this->event_attendees = new ConferenceOrBoothAttendee($this->db);
70 }
71
86 public function deleteById($id)
87 {
88 if ($id < 1 ) {
89 throw new RestException(400, 'No eventattendee with id<1 can exist');
90 }
91 $allowaccess = $this->_checkAccessRights('delete', 0);
92 if (!$allowaccess) {
93 throw new RestException(403, 'denied read access to Event attendees');
94 }
95
96 $result = $this->event_attendees->fetch($id, '');
97 if (!$result) {
98 throw new RestException(404, 'Event attendee with id '.$id.' not found');
99 }
100
101 if (!$this->event_attendees->delete(DolibarrApiAccess::$user)) {
102 throw new RestException(500, 'Error when delete event attendee : '.$this->event_attendees->error);
103 }
104
105 return array(
106 'success' => array(
107 'code' => 200,
108 'message' => 'event attendee deleted'
109 )
110 );
111 }
112
127 public function deleteByRef($ref)
128 {
129 $allowaccess = $this->_checkAccessRights('delete', 0);
130 if (!$allowaccess) {
131 throw new RestException(403, 'denied read access to Event attendees');
132 }
133
134 $result = $this->event_attendees->fetch(0, $ref);
135 if (!$result) {
136 throw new RestException(404, "Event attendee with ref ".$ref." not found");
137 }
138
139 if (!$this->event_attendees->delete(DolibarrApiAccess::$user)) {
140 throw new RestException(500, 'Error when delete event attendee : '.$this->event_attendees->error);
141 }
142
143 return array(
144 'success' => array(
145 'code' => 200,
146 'message' => 'event attendee deleted'
147 )
148 );
149 }
150
166 public function getById($id)
167 {
168 return $this->_fetch($id, '');
169 }
170
186 public function getByRef($ref)
187 {
188 return $this->_fetch(0, $ref);
189 }
190
213 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false, $loadlinkedobjects = 0)
214 {
215 $allowaccess = $this->_checkAccessRights('read', 0);
216 if (!$allowaccess) {
217 throw new RestException(403, 'denied read access to Event attendees');
218 }
219 // Fine-grained access is still checked later for each row on its fk_project
220 // entity stolen from api_setup.class.php
221 $entity = (int) DolibarrApiAccess::$user->entity;
222 $obj_ret = array();
223
224 $sql = "SELECT t.rowid";
225 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." AS t";
226 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet AS p ON t.fk_project = p.rowid";
227 if (isModEnabled('multicompany')) {
228 $sql .= ' WHERE p.entity = '.((int) $entity);
229 } else {
230 $sql .= ' WHERE 1 = 1';
231 }
232 // If user has no permission to see all projects, we restrict the list to projects he is allowed to see
233 // (public projects or projects he is a contact of), like api_projects::index() does,
234 // so unauthorized rows can't be probed with the sqlfilters parameter
235 if (!DolibarrApiAccess::$user->hasRight('project', 'all', 'read')) {
236 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
237 $projectstatic = new Project($this->db);
238 $projectsListId = $projectstatic->getProjectsAuthorizedForUser(DolibarrApiAccess::$user, 0, 1, 0);
239 $sql .= ' AND t.fk_project IN ('.$this->db->sanitize($projectsListId).')';
240 }
241
242
243 // Add sql filters
244 if ($sqlfilters) {
245 $errormessage = '';
246 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
247 if ($errormessage) {
248 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
249 }
250 }
251
252 //this query will return total orders with the filters given
253 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
254
255 $sql .= $this->db->order($sortfield, $sortorder);
256 if ($limit) {
257 if ($page < 0) {
258 $page = 0;
259 }
260 $offset = $limit * $page;
261
262 $sql .= $this->db->plimit($limit + 1, $offset);
263 }
264
265 dol_syslog(get_class($this)."::index", LOG_DEBUG);
266 $result = $this->db->query($sql);
267
268 if ($result) {
269 $num = $this->db->num_rows($result);
270 $min = min($num, ($limit <= 0 ? $num : $limit));
271 $i = 0;
272 while ($i < $min) {
273 $obj = $this->db->fetch_object($result);
274 $event_attendees_static = new ConferenceOrBoothAttendee($this->db);
275 if ($event_attendees_static->fetch($obj->rowid) > 0) {
276 try {
277 $rowallowaccess = $this->_checkAccessRights('read', $event_attendees_static->fk_project);
278 } catch (RestException $e) {
279 // No access to this row, we skip it so the response does not depend on rows we are not allowed to see
280 $rowallowaccess = false;
281 }
282 if ($rowallowaccess) {
283 if ($loadlinkedobjects) {
284 // retrieve linked objects
285 $event_attendees_static->fetchObjectLinked();
286 }
287 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($event_attendees_static), $properties);
288 }
289 }
290 $i++;
291 }
292 } else {
293 throw new RestException(503, 'Error when retrieve event attendee list : '.$this->db->lasterror());
294 }
295
296 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
297 if ($pagination_data) {
298 $totalsResult = $this->db->query($sqlTotals);
299 $total = $this->db->fetch_object($totalsResult)->total;
300
301 $tmp = $obj_ret;
302 $obj_ret = [];
303
304 $obj_ret['data'] = $tmp;
305 $obj_ret['pagination'] = [
306 'total' => (int) $total,
307 'page' => $page, //count starts from 0
308 'page_count' => ceil((int) $total / $limit),
309 'limit' => $limit
310 ];
311 }
312
313 return $obj_ret;
314 }
315
334 public function post($request_data = null)
335 {
336 $allowaccess = $this->_checkAccessRights('write', 0);
337 if (!$allowaccess) {
338 throw new RestException(403, 'denied create access to Event attendees');
339 }
340
341 // Check mandatory fields
342 $result = $this->_validate($request_data);
343
344 foreach ($request_data as $field => $value) {
345 if ($field === 'caller') {
346 // 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
347 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
348 continue;
349 }
350
351 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
352 }
353
354 if ($this->event_attendees->create(DolibarrApiAccess::$user) < 0) {
355 throw new RestException(500, "Error creating event attendee", array_merge(array($this->event_attendees->error), $this->event_attendees->errors));
356 }
357
358 return ((int) $this->event_attendees->id);
359 }
360
380 public function putById($id, $request_data = null)
381 {
382 if ($id < 1 ) {
383 throw new RestException(400, 'No eventattendee with id<1 can exist');
384 }
385 $allowaccess = $this->_checkAccessRights('write', 0);
386 if (!$allowaccess) {
387 throw new RestException(403, 'denied update access to Event attendees');
388 }
389
390 $result = $this->event_attendees->fetch($id, '');
391 if (!$result) {
392 throw new RestException(404, 'event attendee not found');
393 }
394
395 foreach ($request_data as $field => $value) {
396 if ($field == 'id') {
397 continue;
398 }
399 if ($field === 'caller') {
400 // 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
401 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
402 continue;
403 }
404
405 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
406 }
407
408 if ($this->event_attendees->update(DolibarrApiAccess::$user) > 0) {
409 return $this->_fetch($id, '');
410 } else {
411 throw new RestException(500, end($this->event_attendees->errors));
412 }
413 }
414
434 public function putByRef($ref, $request_data = null)
435 {
436 $allowaccess = $this->_checkAccessRights('write', 0);
437 if (!$allowaccess) {
438 throw new RestException(403, 'denied update access to Event attendees');
439 }
440
441 $result = $this->event_attendees->fetch(0, $ref);
442 if (!$result) {
443 throw new RestException(404, 'event attendee not found');
444 }
445
446 $newref = $ref;
447 foreach ($request_data as $field => $value) {
448 if ($field == 'id') {
449 continue;
450 }
451 if ($field == 'ref') {
452 $newref = $this->_checkValForAPI($field, $value, $this->event_attendees);
453 }
454 if ($field === 'caller') {
455 // 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
456 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
457 continue;
458 }
459
460 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
461 }
462
463 if ($this->event_attendees->update(DolibarrApiAccess::$user) > 0) {
464 return $this->_fetch(0, $newref);
465 } else {
466 throw new RestException(500, end($this->event_attendees->errors));
467 }
468 }
469
484 private function _fetch($id, $ref = '')
485 {
486 if ($id < 1 && empty($ref)) {
487 throw new RestException(400, 'No eventattendee with id<1 can exist');
488 }
489 if (empty($id) && empty($ref)) {
490 throw new RestException(400, 'No eventattendee can be found with no criteria');
491 }
492 $allowaccess = $this->_checkAccessRights('read', 0);
493 if (!$allowaccess) {
494 throw new RestException(403, 'denied read access to Event attendees');
495 }
496 // we first need to fetch the object so we can get the fk_project id and then check for access
497 $result = $this->event_attendees->fetch($id, $ref);
498 if (!$result) {
499 if ($id) {
500 throw new RestException(404, 'Event attendee with id '.((string) $id).' not found');
501 }
502 if ($ref) {
503 throw new RestException(404, 'Event attendee with ref '.$ref.' not found');
504 }
505 throw new RestException(404, 'Event attendee not found');
506 }
507 $project_id = $this->event_attendees->fk_project;
508 $allowaccess = $this->_checkAccessRights('read', $project_id);
509 if (!$allowaccess) {
510 throw new RestException(403, 'denied read access to Event attendees');
511 }
512
513 $this->event_attendees->fetchObjectLinked();
514
515 return $this->_cleanObjectDatas($this->event_attendees);
516 }
517
518 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
531 protected function _cleanObjectDatas($object)
532 {
533 // phpcs:enable
534 $object = parent::_cleanObjectDatas($object);
535
536 unset($object->array_languages);
537 unset($object->contacts_ids);
538 unset($object->canvas);
539 unset($object->contact_id);
540 unset($object->user);
541 unset($object->origin_type);
542 unset($object->origin_id);
543 unset($object->ref_ext);
544 unset($object->statut);
545 unset($object->civility_code);
546 unset($object->country_id);
547 unset($object->country_code);
548 unset($object->state_id);
549 unset($object->region_id);
550 unset($object->barcode_type);
551 unset($object->barcode_type_coder);
552 unset($object->mode_reglement_id);
553 unset($object->cond_reglement_id);
554 unset($object->demand_reason_id);
555 unset($object->transport_mode_id);
556 unset($object->shipping_method_id);
557 unset($object->shipping_method);
558 unset($object->fk_multicurrency);
559 unset($object->multicurrency_code);
560 unset($object->multicurrency_tx);
561 unset($object->multicurrency_total_ht);
562 unset($object->multicurrency_total_tva);
563 unset($object->multicurrency_total_ttc);
564 unset($object->multicurrency_total_localtax1);
565 unset($object->multicurrency_total_localtax2);
566 unset($object->fk_account);
567 unset($object->total_ht);
568 unset($object->total_tva);
569 unset($object->total_localtax1);
570 unset($object->total_localtax2);
571 unset($object->total_ttc);
572 unset($object->lines);
573 unset($object->actiontypecode);
574 unset($object->name);
575 unset($object->civility_id);
576 unset($object->user_creation_id);
577 unset($object->user_validation_id);
578 unset($object->user_closing_id);
579 unset($object->user_modification_id);
580 unset($object->totalpaid);
581 unset($object->product);
582 unset($object->cond_reglement_supplier_id);
583 unset($object->deposit_percent);
584 unset($object->retained_warranty_fk_cond_reglement);
585 unset($object->warehouse_id);
586 unset($object->target);
587 unset($object->extraparams);
588 unset($object->specimen);
589 unset($object->date_validation);
590 unset($object->date_modification);
591 unset($object->date_cloture);
592 unset($object->rowid);
593 unset($object->module);
594 unset($object->entity);
595 unset($object->paid);
596
597 return $object;
598 }
599
609 private function _validate($data)
610 {
611 $event_attendees = array();
612 foreach (EventAttendees::$FIELDS as $field) {
613 if (!isset($data[$field])) {
614 throw new RestException(400, $field." field missing");
615 }
616 $event_attendees[$field] = $data[$field];
617 }
618 return $event_attendees;
619 }
620
632 private function _checkAccessRights($accesstype, $project_id = 0)
633 {
634 // what kind of access management do we need?
635 $moduleaccess = false;
636 if (isModEnabled("eventorganization") && DolibarrApiAccess::$user->hasRight('project', $accesstype)) {
637 $moduleaccess = true;
638 }
639 $fullprojectaccess = false;
640 if (DolibarrApiAccess::$user->hasRight('project', 'all', $accesstype)) {
641 $fullprojectaccess = true;
642 }
643
644 if ($moduleaccess && $fullprojectaccess) {
645 return true;
646 } else {
647 $singleprojectaccess = false;
648 if (0 < $project_id) {
649 // we should also check project visibility and if set to assigned contacts it should be only those contacts.
650 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
651 $event_project = new Project($this->db);
652 $result = $event_project->fetch($project_id);
653 if (0 < $result) {
654 $public = $event_project->public;
655 if ( 1 == $public) {
656 $singleprojectaccess = true;
657 } else {
658 $userProjectAccessListId = $event_project->getProjectsAuthorizedForUser(DolibarrApiAccess::$user, 0, 0);
659 $project_title = $event_project->title;
660 if (in_array($project_title, $userProjectAccessListId)) {
661 $singleprojectaccess = true;
662 } else {
663 dol_syslog("project_title ".$project_title." is NOT in array from getProjectsAuthorizedForUser()", LOG_DEBUG);
664 return false;
665 }
666 }
667 } elseif (0 == $result) {
668 throw new RestException(500, 'Project id '.$project_id.' not found');
669 } else {
670 throw new RestException(500, 'Error during fetch project '.$project_id.': '.$this->db->lasterror());
671 }
672 } elseif ($moduleaccess && ($project_id == 0)) {
673 return true;
674 // because we assume that the caller will know to check for each fk_projekt
675 }
676 if ($moduleaccess && $singleprojectaccess) {
677 return true;
678 } elseif ($moduleaccess) {
679 throw new RestException(403, 'Event attendees access granted, but denied access to the project');
680 } elseif ($singleprojectaccess) {
681 throw new RestException(403, 'project access granted, but denied access to Event attendees');
682 } else {
683 throw new RestException(403, 'denied access both Event attendees and the project');
684 }
685 }
686 }
687}
$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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.