Files
Jonathan Miller d257afa23e feat: events/volunteers/memberships/grants API controller
NpoEventsController: event listing + registration + check-in, volunteer
hours logging, membership summary, grant pipeline with reports due.
Permission checks on admin-only endpoints (npo.grants, core.manage).
2026-06-13 08:57:23 -05:00

104 lines
3.1 KiB
PHP

<?php
namespace Moko\Component\MokoSuiteNpo\Api\Controller;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\Database\DatabaseInterface;
/**
* Events + Volunteers + Memberships API.
*/
class NpoEventsController extends BaseController
{
private function requireAuth(string $action = 'core.manage'): void
{
$user = Factory::getApplication()->getIdentity();
if (!$user || $user->guest || (!$user->authorise('core.admin') && !$user->authorise($action, 'com_mokosuitenpo'))) {
http_response_code(403);
echo json_encode(['error' => 'Access denied.']);
Factory::getApplication()->close();
}
}
public function listEvents(): void
{
$events = \Moko\Plugin\System\MokoSuiteNpo\Helper\EventHelper::getUpcomingEvents(50);
$this->sendJson($events);
}
public function registerForEvent(): void
{
$input = Factory::getApplication()->getInput();
$regId = \Moko\Plugin\System\MokoSuiteNpo\Helper\EventHelper::register(
$input->getInt('event_id', 0), [
'contact_id' => $input->getInt('contact_id', 0),
'name' => $input->getString('name', ''),
'email' => $input->getString('email', ''),
'phone' => $input->getString('phone', ''),
'tickets' => $input->getInt('tickets', 1),
'amount' => $input->getFloat('amount', 0),
'dietary' => $input->getString('dietary', ''),
]
);
$this->sendJson(['id' => $regId, 'message' => 'Registered.']);
}
public function checkIn(): void
{
$this->requireAuth('core.manage');
$regId = Factory::getApplication()->getInput()->getInt('registration_id', 0);
\Moko\Plugin\System\MokoSuiteNpo\Helper\EventHelper::checkIn($regId);
$this->sendJson(['message' => 'Checked in.']);
}
public function listVolunteers(): void
{
$this->requireAuth('core.manage');
$stats = \Moko\Plugin\System\MokoSuiteNpo\Helper\VolunteerHelper::getVolunteerStats();
$this->sendJson($stats);
}
public function logVolunteerHours(): void
{
$this->requireAuth('core.manage');
$input = Factory::getApplication()->getInput();
$logId = \Moko\Plugin\System\MokoSuiteNpo\Helper\VolunteerHelper::logHours(
$input->getInt('volunteer_id', 0),
$input->getString('activity', ''),
$input->getFloat('hours', 0),
$input->getString('date', date('Y-m-d')),
$input->getString('notes', '')
);
$this->sendJson(['id' => $logId, 'message' => 'Hours logged.']);
}
public function membershipSummary(): void
{
$this->requireAuth('core.manage');
$summary = \Moko\Plugin\System\MokoSuiteNpo\Helper\MembershipHelper::getSummary();
$this->sendJson($summary);
}
public function grantPipeline(): void
{
$this->requireAuth('npo.grants');
$pipeline = \Moko\Plugin\System\MokoSuiteNpo\Helper\GrantHelper::getPipelineSummary();
$reports = \Moko\Plugin\System\MokoSuiteNpo\Helper\GrantHelper::getReportsDue();
$this->sendJson(['pipeline' => $pipeline, 'reports_due' => $reports]);
}
private function sendJson(mixed $data): void
{
$app = Factory::getApplication();
$app->getDocument()->setMimeEncoding('application/json');
echo json_encode(['data' => $data], JSON_THROW_ON_ERROR);
$app->close();
}
}