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->error);
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->error);
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 // access check delayed until we can do it for each row checking each 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
232
233 // Add sql filters
234 if ($sqlfilters) {
235 $errormessage = '';
236 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
237 if ($errormessage) {
238 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
239 }
240 }
241
242 //this query will return total orders with the filters given
243 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
244
245 $sql .= $this->db->order($sortfield, $sortorder);
246 if ($limit) {
247 if ($page < 0) {
248 $page = 0;
249 }
250 $offset = $limit * $page;
251
252 $sql .= $this->db->plimit($limit + 1, $offset);
253 }
254
255 dol_syslog(get_class($this)."::index", LOG_DEBUG);
256 $result = $this->db->query($sql);
257
258 if ($result) {
259 $num = $this->db->num_rows($result);
260 $min = min($num, ($limit <= 0 ? $num : $limit));
261 $i = 0;
262 $onerowaccessgranted = false;
263 while ($i < $min) {
264 $obj = $this->db->fetch_object($result);
265 $event_attendees_static = new ConferenceOrBoothAttendee($this->db);
266 if ($event_attendees_static->fetch($obj->rowid) > 0) {
267 $rowallowaccess = $this->_checkAccessRights('read', $event_attendees_static->fk_project);
268 if ($rowallowaccess) {
269 if ($loadlinkedobjects) {
270 // retrieve linked objects
271 $event_attendees_static->fetchObjectLinked();
272 }
273 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($event_attendees_static), $properties);
274 $onerowaccessgranted = $rowallowaccess;
275 }
276 }
277 $i++;
278 }
279 if (($num > 0) && !$onerowaccessgranted) {
280 throw new RestException(403, 'No access granted for even a single of the rows found');
281 }
282 } else {
283 throw new RestException(503, 'Error when retrieve event attendee list : '.$this->db->lasterror());
284 }
285
286 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
287 if ($pagination_data) {
288 $totalsResult = $this->db->query($sqlTotals);
289 $total = $this->db->fetch_object($totalsResult)->total;
290
291 $tmp = $obj_ret;
292 $obj_ret = [];
293
294 $obj_ret['data'] = $tmp;
295 $obj_ret['pagination'] = [
296 'total' => (int) $total,
297 'page' => $page, //count starts from 0
298 'page_count' => ceil((int) $total / $limit),
299 'limit' => $limit
300 ];
301 }
302
303 return $obj_ret;
304 }
305
324 public function post($request_data = null)
325 {
326 $allowaccess = $this->_checkAccessRights('write', 0);
327 if (!$allowaccess) {
328 throw new RestException(403, 'denied create access to Event attendees');
329 }
330
331 // Check mandatory fields
332 $result = $this->_validate($request_data);
333
334 foreach ($request_data as $field => $value) {
335 if ($field === 'caller') {
336 // 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
337 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
338 continue;
339 }
340
341 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
342 }
343
344 if ($this->event_attendees->create(DolibarrApiAccess::$user) < 0) {
345 throw new RestException(500, "Error creating event attendee", array_merge(array($this->event_attendees->error), $this->event_attendees->errors));
346 }
347
348 return ((int) $this->event_attendees->id);
349 }
350
370 public function putById($id, $request_data = null)
371 {
372 if ($id < 1 ) {
373 throw new RestException(400, 'No eventattendee with id<1 can exist');
374 }
375 $allowaccess = $this->_checkAccessRights('write', 0);
376 if (!$allowaccess) {
377 throw new RestException(403, 'denied update access to Event attendees');
378 }
379
380 $result = $this->event_attendees->fetch($id, '');
381 if (!$result) {
382 throw new RestException(404, 'event attendee not found');
383 }
384
385 foreach ($request_data as $field => $value) {
386 if ($field == 'id') {
387 continue;
388 }
389 if ($field === 'caller') {
390 // 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
391 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
392 continue;
393 }
394
395 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
396 }
397
398 if ($this->event_attendees->update(DolibarrApiAccess::$user) > 0) {
399 return $this->_fetch($id, '');
400 } else {
401 throw new RestException(500, end($this->event_attendees->errors));
402 }
403 }
404
424 public function putByRef($ref, $request_data = null)
425 {
426 $allowaccess = $this->_checkAccessRights('write', 0);
427 if (!$allowaccess) {
428 throw new RestException(403, 'denied update access to Event attendees');
429 }
430
431 $result = $this->event_attendees->fetch(0, $ref);
432 if (!$result) {
433 throw new RestException(404, 'event attendee not found');
434 }
435
436 $newref = $ref;
437 foreach ($request_data as $field => $value) {
438 if ($field == 'id') {
439 continue;
440 }
441 if ($field == 'ref') {
442 $newref = $this->_checkValForAPI($field, $value, $this->event_attendees);
443 }
444 if ($field === 'caller') {
445 // 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
446 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
447 continue;
448 }
449
450 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
451 }
452
453 if ($this->event_attendees->update(DolibarrApiAccess::$user) > 0) {
454 return $this->_fetch(0, $newref);
455 } else {
456 throw new RestException(500, end($this->event_attendees->errors));
457 }
458 }
459
474 private function _fetch($id, $ref = '')
475 {
476 if ($id < 1 && empty($ref)) {
477 throw new RestException(400, 'No eventattendee with id<1 can exist');
478 }
479 if (empty($id) && empty($ref)) {
480 throw new RestException(400, 'No eventattendee can be found with no criteria');
481 }
482 // we first need to fetch the object so we can get the fk_project id and then check for access
483 $result = $this->event_attendees->fetch($id, $ref);
484 if (!$result) {
485 if ($id) {
486 throw new RestException(404, 'Event attendee with id '.((string) $id).' not found');
487 }
488 if ($ref) {
489 throw new RestException(404, 'Event attendee with ref '.$ref.' not found');
490 }
491 throw new RestException(404, 'Event attendee not found');
492 }
493 $project_id = $this->event_attendees->fk_project;
494 $allowaccess = $this->_checkAccessRights('read', $project_id);
495 if (!$allowaccess) {
496 throw new RestException(403, 'denied read access to Event attendees');
497 }
498
499 $this->event_attendees->fetchObjectLinked();
500
501 return $this->_cleanObjectDatas($this->event_attendees);
502 }
503
504 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
517 protected function _cleanObjectDatas($object)
518 {
519 // phpcs:enable
520 $object = parent::_cleanObjectDatas($object);
521
522 unset($object->array_languages);
523 unset($object->contacts_ids);
524 unset($object->canvas);
525 unset($object->contact_id);
526 unset($object->user);
527 unset($object->origin_type);
528 unset($object->origin_id);
529 unset($object->ref_ext);
530 unset($object->statut);
531 unset($object->civility_code);
532 unset($object->country_id);
533 unset($object->country_code);
534 unset($object->state_id);
535 unset($object->region_id);
536 unset($object->barcode_type);
537 unset($object->barcode_type_coder);
538 unset($object->mode_reglement_id);
539 unset($object->cond_reglement_id);
540 unset($object->demand_reason_id);
541 unset($object->transport_mode_id);
542 unset($object->shipping_method_id);
543 unset($object->shipping_method);
544 unset($object->fk_multicurrency);
545 unset($object->multicurrency_code);
546 unset($object->multicurrency_tx);
547 unset($object->multicurrency_total_ht);
548 unset($object->multicurrency_total_tva);
549 unset($object->multicurrency_total_ttc);
550 unset($object->multicurrency_total_localtax1);
551 unset($object->multicurrency_total_localtax2);
552 unset($object->fk_account);
553 unset($object->total_ht);
554 unset($object->total_tva);
555 unset($object->total_localtax1);
556 unset($object->total_localtax2);
557 unset($object->total_ttc);
558 unset($object->lines);
559 unset($object->actiontypecode);
560 unset($object->name);
561 unset($object->civility_id);
562 unset($object->user_creation_id);
563 unset($object->user_validation_id);
564 unset($object->user_closing_id);
565 unset($object->user_modification_id);
566 unset($object->totalpaid);
567 unset($object->product);
568 unset($object->cond_reglement_supplier_id);
569 unset($object->deposit_percent);
570 unset($object->retained_warranty_fk_cond_reglement);
571 unset($object->warehouse_id);
572 unset($object->target);
573 unset($object->extraparams);
574 unset($object->specimen);
575 unset($object->date_validation);
576 unset($object->date_modification);
577 unset($object->date_cloture);
578 unset($object->rowid);
579 unset($object->module);
580 unset($object->entity);
581 unset($object->paid);
582
583 return $object;
584 }
585
595 private function _validate($data)
596 {
597 $event_attendees = array();
598 foreach (EventAttendees::$FIELDS as $field) {
599 if (!isset($data[$field])) {
600 throw new RestException(400, $field." field missing");
601 }
602 $event_attendees[$field] = $data[$field];
603 }
604 return $event_attendees;
605 }
606
618 private function _checkAccessRights($accesstype, $project_id = 0)
619 {
620 // what kind of access management do we need?
621 $moduleaccess = false;
622 if (isModEnabled("eventorganization") && DolibarrApiAccess::$user->hasRight('project', $accesstype)) {
623 $moduleaccess = true;
624 }
625 $fullprojectaccess = false;
626 if (DolibarrApiAccess::$user->hasRight('project', 'all', $accesstype)) {
627 $fullprojectaccess = true;
628 }
629
630 if ($moduleaccess && $fullprojectaccess) {
631 return true;
632 } else {
633 $singleprojectaccess = false;
634 if (0 < $project_id) {
635 // we should also check project visibility and if set to assigned contacts it should be only those contacts.
636 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
637 $event_project = new Project($this->db);
638 $result = $event_project->fetch($project_id);
639 if (0 < $result) {
640 $public = $event_project->public;
641 if ( 1 == $public) {
642 $singleprojectaccess = true;
643 } else {
644 $userProjectAccessListId = $event_project->getProjectsAuthorizedForUser(DolibarrApiAccess::$user, 0, 0);
645 $project_title = $event_project->title;
646 if (in_array($project_title, $userProjectAccessListId)) {
647 $singleprojectaccess = true;
648 } else {
649 dol_syslog("project_title ".$project_title." is NOT in array from getProjectsAuthorizedForUser()", LOG_DEBUG);
650 return false;
651 }
652 }
653 } elseif (0 == $result) {
654 throw new RestException(500, 'Project id '.$project_id.' not found');
655 } else {
656 throw new RestException(500, 'Error during fetch project '.$project_id.': '.$this->db->lasterror());
657 }
658 } elseif ($moduleaccess && ($project_id == 0)) {
659 return true;
660 // because we assume that the caller will know to check for each fk_projekt
661 }
662 if ($moduleaccess && $singleprojectaccess) {
663 return true;
664 } elseif ($moduleaccess) {
665 throw new RestException(403, 'Event attendees access granted, but denied access to the project');
666 } elseif ($singleprojectaccess) {
667 throw new RestException(403, 'project access granted, but denied access to Event attendees');
668 } else {
669 throw new RestException(403, 'denied access both Event attendees and the project');
670 }
671 }
672 }
673}
$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.