dolibarr 23.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 $allowaccess = $this->_checkAccessRights('delete', 0);
89 if (!$allowaccess) {
90 throw new RestException(403, 'denied read access to Event attendees');
91 }
92
93 $result = $this->event_attendees->fetch($id, '');
94 if (!$result) {
95 throw new RestException(404, 'Event attendee with id '.$id.' not found');
96 }
97
98 if (!$this->event_attendees->delete(DolibarrApiAccess::$user)) {
99 throw new RestException(500, 'Error when delete event attendee : '.$this->event_attendees->error);
100 }
101
102 return array(
103 'success' => array(
104 'code' => 200,
105 'message' => 'event attendee deleted'
106 )
107 );
108 }
109
124 public function deleteByRef($ref)
125 {
126 $allowaccess = $this->_checkAccessRights('delete', 0);
127 if (!$allowaccess) {
128 throw new RestException(403, 'denied read access to Event attendees');
129 }
130
131 $result = $this->event_attendees->fetch(0, $ref);
132 if (!$result) {
133 throw new RestException(404, "Event attendee with ref ".$ref." not found");
134 }
135
136 if (!$this->event_attendees->delete(DolibarrApiAccess::$user)) {
137 throw new RestException(500, 'Error when delete event attendee : '.$this->event_attendees->error);
138 }
139
140 return array(
141 'success' => array(
142 'code' => 200,
143 'message' => 'event attendee deleted'
144 )
145 );
146 }
147
163 public function getById($id)
164 {
165 return $this->_fetch($id, '');
166 }
167
183 public function getByRef($ref)
184 {
185 return $this->_fetch(0, $ref);
186 }
187
209 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
210 {
211 // $allowaccess = $this->_checkAccessRights('read', 0);
212 // if (!$allowaccess) {
213 // throw new RestException(403, 'denied read access to Event attendees');
214 // }
215 // access check delayed until we can do it for each row checking each fk_project
216 // entity stolen from api_setup.class.php
217 $entity = (int) DolibarrApiAccess::$user->entity;
218 $obj_ret = array();
219
220 $sql = "SELECT t.rowid";
221 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." AS t";
222 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet AS p ON t.fk_project = p.rowid";
223 if (isModEnabled('multicompany')) {
224 $sql .= ' WHERE p.entity = '.((int) $entity);
225 } else {
226 $sql .= ' WHERE 1 = 1';
227 }
228
229
230 // Add sql filters
231 if ($sqlfilters) {
232 $errormessage = '';
233 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
234 if ($errormessage) {
235 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
236 }
237 }
238
239 //this query will return total orders with the filters given
240 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
241
242 $sql .= $this->db->order($sortfield, $sortorder);
243 if ($limit) {
244 if ($page < 0) {
245 $page = 0;
246 }
247 $offset = $limit * $page;
248
249 $sql .= $this->db->plimit($limit + 1, $offset);
250 }
251
252 dol_syslog(get_class($this)."::index", LOG_DEBUG);
253 $result = $this->db->query($sql);
254
255 if ($result) {
256 $num = $this->db->num_rows($result);
257 $min = min($num, ($limit <= 0 ? $num : $limit));
258 $i = 0;
259 $onerowaccessgranted = false;
260 while ($i < $min) {
261 $obj = $this->db->fetch_object($result);
262 $event_attendees_static = new ConferenceOrBoothAttendee($this->db);
263 if ($event_attendees_static->fetch($obj->rowid, '') > 0) {
264 $rowallowaccess = $this->_checkAccessRights('read', $event_attendees_static->fk_project);
265 if ($rowallowaccess) {
266 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($event_attendees_static), $properties);
267 $onerowaccessgranted = $rowallowaccess;
268 }
269 }
270 $i++;
271 }
272 if (($num > 0) && !$onerowaccessgranted) {
273 throw new RestException(403, 'No access granted for even a single of the rows found');
274 }
275 } else {
276 throw new RestException(503, 'Error when retrieve event attendee list : '.$this->db->lasterror());
277 }
278
279 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
280 if ($pagination_data) {
281 $totalsResult = $this->db->query($sqlTotals);
282 $total = $this->db->fetch_object($totalsResult)->total;
283
284 $tmp = $obj_ret;
285 $obj_ret = [];
286
287 $obj_ret['data'] = $tmp;
288 $obj_ret['pagination'] = [
289 'total' => (int) $total,
290 'page' => $page, //count starts from 0
291 'page_count' => ceil((int) $total / $limit),
292 'limit' => $limit
293 ];
294 }
295
296 return $obj_ret;
297 }
298
317 public function post($request_data = null)
318 {
319 $allowaccess = $this->_checkAccessRights('write', 0);
320 if (!$allowaccess) {
321 throw new RestException(403, 'denied create access to Event attendees');
322 }
323
324 // Check mandatory fields
325 $result = $this->_validate($request_data);
326
327 foreach ($request_data as $field => $value) {
328 if ($field === 'caller') {
329 // 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
330 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
331 continue;
332 }
333
334 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
335 }
336
337 if ($this->event_attendees->create(DolibarrApiAccess::$user) < 0) {
338 throw new RestException(500, "Error creating event attendee", array_merge(array($this->event_attendees->error), $this->event_attendees->errors));
339 }
340
341 return ((int) $this->event_attendees->id);
342 }
343
363 public function putById($id, $request_data = null)
364 {
365 $allowaccess = $this->_checkAccessRights('write', 0);
366 if (!$allowaccess) {
367 throw new RestException(403, 'denied update access to Event attendees');
368 }
369
370 $result = $this->event_attendees->fetch($id, '');
371 if (!$result) {
372 throw new RestException(404, 'event attendee not found');
373 }
374
375 foreach ($request_data as $field => $value) {
376 if ($field == 'id') {
377 continue;
378 }
379 if ($field === 'caller') {
380 // 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
381 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
382 continue;
383 }
384
385 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
386 }
387
388 if ($this->event_attendees->update(DolibarrApiAccess::$user) > 0) {
389 return $this->_fetch($id, '');
390 } else {
391 throw new RestException(500, end($this->event_attendees->errors));
392 }
393 }
394
414 public function putByRef($ref, $request_data = null)
415 {
416 $allowaccess = $this->_checkAccessRights('write', 0);
417 if (!$allowaccess) {
418 throw new RestException(403, 'denied update access to Event attendees');
419 }
420
421 $result = $this->event_attendees->fetch(0, $ref);
422 if (!$result) {
423 throw new RestException(404, 'event attendee not found');
424 }
425
426 $newref = $ref;
427 foreach ($request_data as $field => $value) {
428 if ($field == 'id') {
429 continue;
430 }
431 if ($field == 'ref') {
432 $newref = $this->_checkValForAPI($field, $value, $this->event_attendees);
433 }
434 if ($field === 'caller') {
435 // 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
436 $this->event_attendees->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
437 continue;
438 }
439
440 $this->event_attendees->$field = $this->_checkValForAPI($field, $value, $this->event_attendees);
441 }
442
443 if ($this->event_attendees->update(DolibarrApiAccess::$user) > 0) {
444 return $this->_fetch(0, $newref);
445 } else {
446 throw new RestException(500, end($this->event_attendees->errors));
447 }
448 }
449
464 private function _fetch($id, $ref = '')
465 {
466 // we first need to fetch the object so we can get the fk_project id and then check for access
467 $result = $this->event_attendees->fetch($id, $ref);
468 if (!$result) {
469 if ($id) {
470 throw new RestException(404, 'Event attendee with id '.((string) $id).' not found');
471 }
472 if ($ref) {
473 throw new RestException(404, 'Event attendee with ref '.$ref.' not found');
474 }
475 throw new RestException(404, 'Event attendee not found');
476 }
477 $project_id = $this->event_attendees->fk_project;
478 $allowaccess = $this->_checkAccessRights('read', $project_id);
479 if (!$allowaccess) {
480 throw new RestException(403, 'denied read access to Event attendees');
481 }
482
483 return $this->_cleanObjectDatas($this->event_attendees);
484 }
485
486 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
499 protected function _cleanObjectDatas($object)
500 {
501 // phpcs:enable
502 $object = parent::_cleanObjectDatas($object);
503
504 unset($object->array_languages);
505 unset($object->contacts_ids);
506 unset($object->canvas);
507 unset($object->contact_id);
508 unset($object->user);
509 unset($object->origin_type);
510 unset($object->origin_id);
511 unset($object->ref_ext);
512 unset($object->statut);
513 unset($object->civility_code);
514 unset($object->country_id);
515 unset($object->country_code);
516 unset($object->state_id);
517 unset($object->region_id);
518 unset($object->barcode_type);
519 unset($object->barcode_type_coder);
520 unset($object->mode_reglement_id);
521 unset($object->cond_reglement_id);
522 unset($object->demand_reason_id);
523 unset($object->transport_mode_id);
524 unset($object->shipping_method_id);
525 unset($object->shipping_method);
526 unset($object->fk_multicurrency);
527 unset($object->multicurrency_code);
528 unset($object->multicurrency_tx);
529 unset($object->multicurrency_total_ht);
530 unset($object->multicurrency_total_tva);
531 unset($object->multicurrency_total_ttc);
532 unset($object->multicurrency_total_localtax1);
533 unset($object->multicurrency_total_localtax2);
534 unset($object->fk_account);
535 unset($object->total_ht);
536 unset($object->total_tva);
537 unset($object->total_localtax1);
538 unset($object->total_localtax2);
539 unset($object->total_ttc);
540 unset($object->lines);
541 unset($object->actiontypecode);
542 unset($object->name);
543 unset($object->civility_id);
544 unset($object->user_creation_id);
545 unset($object->user_validation_id);
546 unset($object->user_closing_id);
547 unset($object->user_modification_id);
548 unset($object->totalpaid);
549 unset($object->product);
550 unset($object->cond_reglement_supplier_id);
551 unset($object->deposit_percent);
552 unset($object->retained_warranty_fk_cond_reglement);
553 unset($object->warehouse_id);
554 unset($object->target);
555 unset($object->extraparams);
556 unset($object->specimen);
557 unset($object->date_validation);
558 unset($object->date_modification);
559 unset($object->date_cloture);
560 unset($object->rowid);
561 unset($object->module);
562 unset($object->entity);
563 unset($object->paid);
564
565 return $object;
566 }
567
577 private function _validate($data)
578 {
579 $event_attendees = array();
580 foreach (EventAttendees::$FIELDS as $field) {
581 if (!isset($data[$field])) {
582 throw new RestException(400, $field." field missing");
583 }
584 $event_attendees[$field] = $data[$field];
585 }
586 return $event_attendees;
587 }
588
600 private function _checkAccessRights($accesstype, $project_id = 0)
601 {
602 // what kind of access management do we need?
603 $moduleaccess = false;
604 if (isModEnabled("eventorganization") && DolibarrApiAccess::$user->hasRight('project', $accesstype)) {
605 $moduleaccess = true;
606 }
607 $fullprojectaccess = false;
608 if (DolibarrApiAccess::$user->hasRight('project', 'all', $accesstype)) {
609 $fullprojectaccess = true;
610 }
611
612 if ($moduleaccess && $fullprojectaccess) {
613 return true;
614 } else {
615 $singleprojectaccess = false;
616 if (0 < $project_id) {
617 // we should also check project visibility and if set to assigned contacts it should be only those contacts.
618 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
619 $event_project = new Project($this->db);
620 $result = $event_project->fetch($project_id);
621 if (0 < $result) {
622 $public = $event_project->public;
623 if ( 1 == $public) {
624 $singleprojectaccess = true;
625 } else {
626 $userProjectAccessListId = $event_project->getProjectsAuthorizedForUser(DolibarrApiAccess::$user, 0, 0);
627 $project_title = $event_project->title;
628 if (in_array($project_title, $userProjectAccessListId)) {
629 $singleprojectaccess = true;
630 } else {
631 dol_syslog("project_title ".$project_title." is NOT in array from getProjectsAuthorizedForUser()", LOG_DEBUG);
632 return false;
633 }
634 }
635 } elseif (0 == $result) {
636 throw new RestException(500, 'Project id '.$project_id.' not found');
637 } else {
638 throw new RestException(500, 'Error during fetch project '.$project_id.': '.$this->db->lasterror());
639 }
640 } elseif ($moduleaccess && ($project_id == 0)) {
641 return true;
642 // because we assume that the caller will know to check for each fk_projekt
643 }
644 if ($moduleaccess && $singleprojectaccess) {
645 return true;
646 } elseif ($moduleaccess) {
647 throw new RestException(403, 'Event attendees access granted, but denied access to the project');
648 } elseif ($singleprojectaccess) {
649 throw new RestException(403, 'project access granted, but denied access to Event attendees');
650 } else {
651 throw new RestException(403, 'denied access both Event attendees and the project');
652 }
653 }
654 }
655}
$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:33
_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.
Definition api.class.php:98
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List Event attendees.
_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.
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)
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.