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