209 lines
6.5 KiB
PHP
209 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Api\V1;
|
|
|
|
use App\Libraries\EventAccess;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
|
|
class Events extends ResourceController
|
|
{
|
|
protected $helpers = ['text'];
|
|
|
|
protected function eventAccess(): EventAccess
|
|
{
|
|
return new EventAccess();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$db = \Config\Database::connect();
|
|
$comId = (int) $this->request->com_id;
|
|
$access = $this->eventAccess();
|
|
|
|
$eventIds = $access->getAuthorizedEventIds($comId);
|
|
|
|
if (empty($eventIds)) {
|
|
return $this->respond([
|
|
'events' => [],
|
|
'access_mode' => $access->hasV2Access($comId) ? 'v2' : 'legacy',
|
|
]);
|
|
}
|
|
|
|
$startDate = $this->request->getGet('start_date');
|
|
$endDate = $this->request->getGet('end_date');
|
|
$updatedSince = $this->request->getGet('updated_since');
|
|
|
|
$builder = $db->table('api_v1_events')
|
|
->select('eve_id, eve_nom_fr, eve_lieu_fr, eve_lieu_en, eve_date_debut')
|
|
->whereIn('eve_id', $eventIds);
|
|
|
|
if ($startDate && $endDate) {
|
|
$builder->where('eve_date_debut >=', $startDate)
|
|
->where('eve_date_debut <=', $endDate);
|
|
}
|
|
|
|
if ($updatedSince) {
|
|
$updatedSince = str_replace('T',' ', $updatedSince);
|
|
$updatedSince = str_replace('Z','', $updatedSince);
|
|
|
|
$builder->where('last_update >', $updatedSince);
|
|
}
|
|
|
|
$events = $builder->get()->getResultArray();
|
|
|
|
foreach ($events as &$event) {
|
|
$categories = $db->table('api_v1_categories')
|
|
->where('eve_id', $event['eve_id'])
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$event['categories'] = $categories;
|
|
}
|
|
|
|
unset($event);
|
|
|
|
if ($access->hasV2Access($comId)) {
|
|
$events = array_values(array_filter($events, function ($e) use ($access, $comId) {
|
|
return $access->hasPermission($comId, (int) $e['eve_id'], 'events.list');
|
|
}));
|
|
}
|
|
|
|
$events = clean_text_fields($events);
|
|
|
|
return $this->respond([
|
|
'events' => $events,
|
|
'total' => count($events),
|
|
'access_mode' => $access->hasV2Access($comId) ? 'v2' : 'legacy',
|
|
]);
|
|
}
|
|
|
|
public function show($id = null)
|
|
{
|
|
$db = \Config\Database::connect();
|
|
$comId = (int) $this->request->com_id;
|
|
$access = $this->eventAccess();
|
|
$id = (string) $id;
|
|
|
|
if (!$access->assertEventPermission($comId, (int) $id, 'events.view')) {
|
|
return $this->respond([
|
|
'error' => 'event_not_allowed',
|
|
'requested_event' => $id,
|
|
'access_mode' => $access->hasV2Access($comId) ? 'v2' : 'legacy',
|
|
], 403);
|
|
}
|
|
|
|
$event = $db->table('api_v1_events')
|
|
->select('eve_id, eve_nom_fr, eve_lieu_fr, eve_lieu_en, eve_date_debut')
|
|
->where('eve_id', $id)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$categories = $db->table('api_v1_categories')
|
|
->where('eve_id', $id)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$event['categories'] = $categories;
|
|
$event = clean_text_fields($event);
|
|
|
|
return $this->respond([
|
|
'event' => $event,
|
|
'access_mode' => $access->hasV2Access($comId) ? 'v2' : 'legacy',
|
|
]);
|
|
}
|
|
|
|
public function categories($event_id)
|
|
{
|
|
$db = \Config\Database::connect();
|
|
$comId = (int) $this->request->com_id;
|
|
$access = $this->eventAccess();
|
|
|
|
if (!$access->assertEventPermission($comId, (int) $event_id, 'events.view')) {
|
|
return $this->respond(['error' => 'event_not_allowed'], 403);
|
|
}
|
|
|
|
$rows = $db->table('api_v1_categories')
|
|
->where('eve_id', $event_id)
|
|
->get()
|
|
->getResultArray();
|
|
$rows = clean_text_fields($rows);
|
|
|
|
return $this->response->setJSON([
|
|
'event_id' => (int)$event_id,
|
|
'categories' => $rows,
|
|
]);
|
|
}
|
|
|
|
public function registrations($event_id)
|
|
{
|
|
$db = \Config\Database::connect();
|
|
$comId = (int) $this->request->com_id;
|
|
$access = $this->eventAccess();
|
|
|
|
if (!$access->assertEventPermission($comId, (int) $event_id, 'registrations.view')) {
|
|
return $this->respond([
|
|
'error' => 'event_not_allowed',
|
|
'requested_event' => $event_id,
|
|
'access_mode' => $access->hasV2Access($comId) ? 'v2' : 'legacy',
|
|
], 403);
|
|
}
|
|
|
|
$updatedSince = $this->request->getGet('updated_since');
|
|
|
|
$builder = $db->table('api_v1_registrations')
|
|
->where('eve_id', $event_id);
|
|
|
|
if ($updatedSince) {
|
|
$updatedSince = str_replace('T',' ', $updatedSince);
|
|
$updatedSince = str_replace('Z','', $updatedSince);
|
|
|
|
$builder->where('ach_maj >', $updatedSince);
|
|
}
|
|
|
|
$rows = $builder
|
|
->orderBy('no_commande')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$rows = clean_text_fields($rows);
|
|
|
|
$registrations = [];
|
|
|
|
foreach ($rows as $row) {
|
|
$key = $row['pec_id'].'_'.$row['par_id'];
|
|
|
|
if (!isset($registrations[$key])) {
|
|
$registrations[$key] = $row;
|
|
$registrations[$key]['custom_questions'] = [];
|
|
|
|
unset(
|
|
$registrations[$key]['que_id'],
|
|
$registrations[$key]['que_question_fr'],
|
|
$registrations[$key]['que_question_en'],
|
|
$registrations[$key]['que_choix_fr'],
|
|
$registrations[$key]['que_choix_en'],
|
|
$registrations[$key]['que_variable']
|
|
);
|
|
}
|
|
|
|
if (!empty($row['que_id'])) {
|
|
$registrations[$key]['custom_questions'][] = [
|
|
'que_id' => $row['que_id'],
|
|
'que_question_fr' => $row['que_question_fr'],
|
|
'que_question_en' => $row['que_question_en'],
|
|
'que_choix_fr' => $row['que_choix_fr'],
|
|
'que_choix_en' => $row['que_choix_en'],
|
|
];
|
|
}
|
|
}
|
|
|
|
$registrations = array_values($registrations);
|
|
|
|
return $this->respond([
|
|
'registrations' => $registrations,
|
|
'total' => count($registrations),
|
|
'access_mode' => $access->hasV2Access($comId) ? 'v2' : 'legacy',
|
|
]);
|
|
}
|
|
}
|