aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+github@gmail.com>2023-12-07 14:04:18 +0100
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2024-10-30 12:40:05 +0000
commit288f22cd7f789e6408c4fc8dcfac19627d0ff01b (patch)
treef38fd112175dd78dd3833036e97b4f7e42ff6f85
parent363c78feaae65f3dfaba40b39463e2f1156048d4 (diff)
initial commit, re #2799tic-2799
-rw-r--r--lib/classes/JsonApi/ComplexFilter.php110
-rw-r--r--lib/classes/JsonApi/ResourceBookingSchema.php110
-rw-r--r--lib/classes/JsonApi/ResourceCategorySchema.php48
-rw-r--r--lib/classes/JsonApi/RouteMap.php19
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourceBookingIndex.php269
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalIndex.php209
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalShow.php27
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourceBookingShow.php23
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourceCategoryIndex.php91
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourceCategoryShow.php27
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourceIndex.php99
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourcePropertyIndex.php0
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourcePropertyShow.php0
-rw-r--r--lib/classes/JsonApi/Routes/Resources/ResourceShow.php30
-rw-r--r--lib/classes/JsonApi/SchemaMap.php6
-rw-r--r--lib/classes/JsonApi/Schemas/ResourceBookingIntervalSchema.php91
-rw-r--r--lib/classes/JsonApi/Schemas/ResourcePropertySchema.php37
-rw-r--r--lib/classes/JsonApi/Schemas/ResourceSchema.php89
18 files changed, 1285 insertions, 0 deletions
diff --git a/lib/classes/JsonApi/ComplexFilter.php b/lib/classes/JsonApi/ComplexFilter.php
new file mode 100644
index 0000000..f0db0d7
--- /dev/null
+++ b/lib/classes/JsonApi/ComplexFilter.php
@@ -0,0 +1,110 @@
+<?php
+namespace JsonApi;
+
+/**
+ * @see https://www.jsonapi.net/usage/reading/filtering.html
+ */
+final class ComplexFilter
+{
+ private const OPERATIONS = [
+ 'equals' => [1, '%s = %s'],
+ 'lessThan' => [1, '%s < %s'],
+ 'lessOrEqual' => [1, '%s <= %s'],
+ 'greaterThan' => [1, '%s > %s'],
+ 'greaterOrEqual' => [1, '%s >= %s'],
+ 'contains' => [1, "%s LIKE CONCAT('%%', %s, '%%')"],
+ 'startsWith' => [1, "%s LIKE CONCAT(%s, '%%')"],
+ 'endsWith' => [1, "%s LIKE CONCAT('%%', %s)"],
+ 'any' => [-1, '%s IN (%s)'],
+ // 'has',
+ // 'not',
+ // 'or',
+ // 'and',
+ 'between' => [2, '%s BETWEEN %s AND %s'],
+ ];
+
+ public static function detect(string $input): bool
+ {
+ return preg_match(self::getRegexp(), $input);
+ }
+
+ public static function create(string $input): ComplexFilter
+ {
+ return new self($input);
+ }
+
+ public static function getRegexp(bool $withCaptureGroups = false): string
+ {
+ $quotedOperations = array_map(
+ function (string $operation): string {
+ return preg_quote($operation, '/');
+ },
+ array_keys(self::OPERATIONS)
+ );
+ $template = $withCaptureGroups
+ ? '/^(%s)\((\w+(?:,\w+)*)\)$/'
+ : '/^(?:%s)\(\w+(?:,\w+)*\)$/';
+
+ return sprintf($template, implode('|', $quotedOperations));
+ }
+
+ private $operation;
+ private $parameters;
+
+ private function __construct(string $input)
+ {
+ [$this->operation, $this->parameters] = $this->parse($input);
+ }
+
+ private function parse(string $input): array
+ {
+ preg_match(self::getRegexp(true), $input, $matches);
+
+ $operation = $matches[1];
+ $parameters = explode(',', $matches[2], self::OPERATIONS[$operation][0]);
+
+ return [$operation, $parameters];
+ }
+
+ public function apply(array &$conditions, array &$parameters, string $column, string $variable = null): void
+ {
+ if ($variable === null) {
+ $variable = ":${column}";
+ }
+
+ if (self::OPERATIONS[$this->operation][0] > 1) {
+ $params = array_combine(
+ array_map(
+ function ($index) use ($variable): string {
+ return "{$variable}{$index}";
+ },
+ array_keys($this->parameters)
+ ),
+ $this->parameters
+ );
+ $conditions[] = sprintf(
+ self::OPERATIONS[$this->operation][1],
+ $column,
+ ...array_keys($params)
+ );
+ $parameters = array_merge(
+ $parameters,
+ $params
+ );
+ } elseif (self::OPERATIONS[$this->operation][0] === 1) {
+ $conditions[] = sprintf(
+ self::OPERATIONS[$this->operation][1],
+ $column,
+ $variable
+ );
+ $parameters[$variable] = $this->parameters[0];
+ } elseif (self::OPERATIONS[$this->operation][0] === -1) {
+ $conditions[] = sprintf(
+ self::OPERATIONS[$this->operation][1],
+ $column,
+ $variable
+ );
+ $parameters[$variable] = $this->parameters;
+ }
+ }
+}
diff --git a/lib/classes/JsonApi/ResourceBookingSchema.php b/lib/classes/JsonApi/ResourceBookingSchema.php
new file mode 100644
index 0000000..6e9d124
--- /dev/null
+++ b/lib/classes/JsonApi/ResourceBookingSchema.php
@@ -0,0 +1,110 @@
+<?php
+namespace JsonApi\Schemas;
+
+use Neomerx\JsonApi\Contracts\Schema\BaseLinkInterface;
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use Neomerx\JsonApi\Schema\Link;
+use ResourceBooking;
+
+final class ResourceBookingSchema extends SchemaProvider
+{
+ const TYPE = 'resource-bookings';
+
+ const REL_ASSIGNED_USER = 'assigned-user';
+ const REL_BOOKING_USER = 'assigned-user';
+ const REL_COURSE_DATE = 'course-date';
+ const REL_INTERVALS = 'intervals';
+ const REL_RESOURCE = 'resource';
+
+ /**
+ * @param ResourceBooking $resource
+ */
+ public function getId($resource): ?string
+ {
+ return $resource->id;
+ }
+
+ /**
+ * @param ResourceBooking $resource
+ */
+ public function getAttributes($resource, ContextInterface $context): iterable
+ {
+ $stringOrNull = function ($item): ?string {
+ return trim($item) ? (string) $item : null;
+ };
+
+ return [
+ 'booking-type' => (int) $resource->booking_type,
+ 'description' => $stringOrNull($resource->description),
+ 'internal-comment' => $stringOrNull($resource->internal_comment),
+
+ 'begin' => date('c', $resource->begin),
+ 'end' => date('c', $resource->end),
+ 'preparation-time' => (int)$resource->preparation_time,
+
+ 'repeat-end' => $resource->repeat_end ? date('c', $resource->repeat_end) : null,
+ 'repeat-quantity' => isset($resource->repeat_quantity) ? (int)$resource->repeat_quantity : null,
+ 'repetition-interval' => (string)$resource->repetition_interval,
+
+ 'mkdate' => date('c', $resource->mkdate),
+ 'chdate' => date('c', $resource->chdate),
+ ];
+ }
+
+ /**
+ * @param ResourceBooking $resource
+ */
+ public function getRelationships($resource, ContextInterface $context): iterable
+ {
+ $relationships = [];
+
+ $isPrimary = $context->getPosition()->getLevel() === 0;
+ if ($isPrimary) {
+ $relationships = $this->getIntervalsRelationship(
+ $relationships,
+ $resource,
+ $this->shouldInclude($context, self::REL_INTERVALS)
+ );
+ $relationships = $this->getResourceRelationship(
+ $relationships,
+ $resource->resource,
+ $this->shouldInclude($context, self::REL_RESOURCE)
+ );
+ // TODO: More relations
+ }
+
+ return $relationships;
+ }
+
+ private function getIntervalsRelationship(
+ array $relationships,
+ \ResourceBooking $booking,
+ bool $includeData
+ ): array
+ {
+ $relationships[self::REL_INTERVALS] = [
+ self::RELATIONSHIP_LINKS => [
+ Link::RELATED => $this->getRelationshipRelatedLink($booking, self::REL_INTERVALS),
+ ],
+ self::RELATIONSHIP_DATA => $booking->time_intervals->map(function (\ResourceBookingInterval $interval) use ($includeData) {
+ return $includeData ? $interval : \ResourceBookingInterval::build(['id' => $interval->id]);
+ }),
+ ];
+
+ return $relationships;
+ }
+
+ private function getResourceRelationship(
+ array $relationships,
+ \Resource $resource,
+ bool $includeData
+ ): array {
+ $relationships[self::REL_RESOURCE] = [
+ self::RELATIONSHIP_LINKS => [
+ Link::RELATED => $this->createLinkToResource($resource),
+ ],
+ self::RELATIONSHIP_DATA => $includeData ? $resource : \Resource::build(['id' => $resource->id]),
+ ];
+ return $relationships;
+ }
+}
diff --git a/lib/classes/JsonApi/ResourceCategorySchema.php b/lib/classes/JsonApi/ResourceCategorySchema.php
new file mode 100644
index 0000000..3e9b995
--- /dev/null
+++ b/lib/classes/JsonApi/ResourceCategorySchema.php
@@ -0,0 +1,48 @@
+<?php
+namespace JsonApi\Schemas;
+
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use ResourceCategory;
+
+final class ResourceCategorySchema extends SchemaProvider
+{
+ const TYPE = 'resource_categories';
+
+ /**
+ * @param ResourceCategory $resource
+ */
+ public function getId($resource): ?string
+ {
+ return $resource->id;
+ }
+
+ /**
+ * @param ResourceCategory $resource
+ */
+ public function getAttributes($resource, ContextInterface $context): iterable
+ {
+ return [
+ 'name' => (string) $resource->name,
+ 'description' => (string) $resource->description,
+ 'system' => (bool) $resource->system,
+ 'class_name' => (string) $resource->class_name,
+
+ 'mkdate' => date('c', $resource->mkdate),
+ 'chdate' => date('c', $resource->chdate),
+ ];
+ }
+
+ /**
+ * @param ResourceCategory $resource
+ */
+ public function getRelationships($resource, ContextInterface $context): iterable
+ {
+ if ($context->getPosition()->getLevel() > 0) {
+ return [];
+ };
+
+ $relationships = [];
+
+ return $relationships;
+ }
+}
diff --git a/lib/classes/JsonApi/RouteMap.php b/lib/classes/JsonApi/RouteMap.php
index 8c6037a..b05086b 100644
--- a/lib/classes/JsonApi/RouteMap.php
+++ b/lib/classes/JsonApi/RouteMap.php
@@ -161,6 +161,7 @@ class RouteMap
}
$this->addUnauthenticatedTreeRoutes($group);
+ $this->addUnauthenticatedResourcesRoutes($group);
}
private function getAuthenticator(): callable
@@ -664,4 +665,22 @@ class RouteMap
{
$group->map(['GET', 'PATCH', 'POST', 'DELETE'], $url, $handler);
}
+
+ private function addUnauthenticatedResourcesRoutes(RouteCollectorProxy $group): void
+ {
+ $group->get('/resources', Routes\Resources\ResourceIndex::class);
+ $group->get('/resources/{id}', Routes\Resources\ResourceShow::class);
+
+ $group->get('/resource-bookings', Routes\Resources\ResourceBookingIndex::class);
+ $group->get('/resource-bookings/{id}', Routes\Resources\ResourceShow::class);
+ $group->get('/resources/{id}/bookings', Routes\Resources\ResourceBookingIndex::class)->setName('bookings-of-resource');
+
+ $group->get('/resource-booking-intervals', Routes\Resources\ResourceBookingIntervalIndex::class);
+ $group->get('/resource-booking-intervals/{id}', Routes\Resources\ResourceBookingIntervalShow::class);
+ $group->get('/resources/{id}/intervals', Routes\Resources\ResourceBookingIntervalIndex::class)->setName('intervals-of-resource');
+ $group->get('/resource-bookings/{id}/intervals', Routes\Resources\ResourceBookingIntervalIndex::class)->setName('intervals-of-booking');
+
+ $group->get('/resource-categories', Routes\Resources\ResourceCategoryIndex::class);
+ $group->get('/resource-categories/{id}', Routes\Resources\ResourceCategoryShow::class);
+ }
}
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceBookingIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIndex.php
new file mode 100644
index 0000000..9d2b880
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIndex.php
@@ -0,0 +1,269 @@
+<?php
+namespace JsonApi\Routes\Resources;
+
+use CourseDate;
+use JsonApi\ComplexFilter;
+use JsonApi\Schemas\ResourceBookingSchema;
+use JsonApi\Errors\BadRequestException;
+use JsonApi\Errors\RecordNotFoundException;
+use JsonApi\JsonApiController;
+use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface;
+use Neomerx\JsonApi\Exceptions\JsonApiException;
+use Neomerx\JsonApi\Schema\ErrorCollection;
+use Psr\Http\Message\{
+ RequestInterface as Request,
+ ResponseInterface as Response
+};
+use Resource;
+use ResourceBooking;
+use Slim\Routing\RouteContext;
+use User;
+
+final class ResourceBookingIndex extends JsonApiController
+{
+ protected $allowedPagingParameters = ['offset', 'limit'];
+ protected $allowedIncludePaths = [
+ ResourceBookingSchema::REL_INTERVALS,
+ ResourceBookingSchema::REL_RESOURCE,
+ ];
+ protected $allowedSortFields = [
+ 'begin',
+ 'end',
+ 'mkdate',
+ ];
+ protected $allowedFilteringParameters = [
+ 'assigned-course-date-id',
+ 'assigned-user-id',
+ 'begin',
+ 'booking-type',
+ 'booking-user-id',
+ 'end',
+ 'range-id',
+ 'resource-id',
+ ];
+
+ public function __invoke(Request $request, Response $response, array $args): Response
+ {
+ $filters = $this->getFilters();
+ $order = $this->getOrder();
+ [$offset, $limit] = $this->getOffsetAndLimit();
+
+ $routeName = RouteContext::fromRequest($request)->getRoute()->getName();
+ if ($routeName === 'bookings-of-resource') {
+ if (isset($filters['resource-id'])) {
+ throw new BadRequestException('You may not use the resource-id filter for this route.');
+ }
+
+ if (!Resource::exists($args['id'])) {
+ throw new RecordNotFoundException("No resource found with id {$args['id']}.");
+ }
+
+ $filters['resource-id'] = $args['id'];
+ }
+
+ [$condition, $parameters] = $this->getConditionAndParameters($filters);
+
+ $total = ResourceBooking::countBySql($condition, $parameters);
+ $bookings = ResourceBooking::findBySQL(
+ "{$condition} {$order} LIMIT {$offset}, {$limit}",
+ $parameters
+ );
+
+ return $this->getPaginatedContentResponse($bookings, $total);
+ }
+
+ private function getFilters(): array
+ {
+ $filters = iterator_to_array($this->getQueryParameters()->getFilters());
+ $errors = new ErrorCollection();
+
+ if (array_key_exists('assigned-course-date-id', $filters)) {
+ if (!CourseDate::exists($filters['assigned-course-date-id'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ sprintf(
+ 'Filter assigned-course-date-id links to an unknown course date with id %s.',
+ $filters['assigned-course-date-id']
+ )
+ );
+ }
+ }
+
+ if (array_key_exists('assigned-user-id', $filters)) {
+ if (!User::exists($filters['assigned-user-id'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ sprintf(
+ 'Filter assigned-user-id links to an unknown user with id %s.',
+ $filters['assigned-user-id']
+ )
+ );
+ }
+ }
+
+ if (array_key_exists('begin', $filters)) {
+ if (ComplexFilter::detect($filters['begin'])) {
+ $filters['begin'] = ComplexFilter::create($filters['begin']);
+ } elseif (!is_numeric($filters['begin'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ 'Filter begin must be numeric.'
+ );
+ } else {
+ $filters['begin'] = (int) $filters['begin'];
+ }
+ }
+
+ if (array_key_exists('booking-type', $filters)) {
+ if (!is_numeric($filters['booking-type'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ 'Filter booking-type must be numeric.'
+ );
+ } else {
+ $filters['booking-type'] = (int) $filters['booking-type'];
+ }
+ }
+
+ if (array_key_exists('booking-user-id', $filters)) {
+ if (!User::exists($filters['booking-user-id'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ sprintf(
+ 'Filter booking-user-id links to an unknown user with id %s.',
+ $filters['booking-user-id']
+ )
+ );
+ }
+ }
+
+ if (array_key_exists('end', $filters)) {
+ if (ComplexFilter::detect($filters['end'])) {
+ $filters['end'] = ComplexFilter::create($filters['end']);
+ } elseif (!is_numeric($filters['end'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ 'Filter end must be numeric.'
+ );
+ } else {
+ $filters['end'] = (int) $filters['end'];
+ }
+ }
+
+ if (array_key_exists('range-id', $filters)) {
+ if (
+ !CourseDate::exists($filters['range-id'])
+ && !User::exists($filters['range-id'])
+ ) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ sprintf(
+ 'Filter range-id links to an unknown course date or user with id %s.',
+ $filters['range-id']
+ )
+ );
+ }
+ }
+
+ if (array_key_exists('resource-id', $filters)) {
+ if (!Resource::exists($filters['resource-id'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ sprintf(
+ 'Filter resource-id links to an unknown resource with id %s.',
+ $filters['resource-id']
+ )
+ );
+ }
+ }
+
+ if (count($errors) > 0) {
+ throw new JsonApiException($errors, JsonApiException::HTTP_CODE_BAD_REQUEST);
+ }
+
+ return $filters;
+ }
+
+ private function getOrder(): string
+ {
+ $result = [];
+ foreach ($this->getQueryParameters()->getSorts() as $column => $ascending) {
+ if ($ascending) {
+ $result[] = $column;
+ } else {
+ $result[] = "{$column} DESC";
+ }
+ }
+
+ return count($result) > 0 ? 'ORDER BY ' . implode(', ', $result) : '';
+ }
+
+ private function getConditionAndParameters(array $filters): array
+ {
+ $conditions = [];
+ $joins = [];
+ $parameters = [];
+
+ if (array_key_exists('assigned-course-date-id', $filters)) {
+ $conditions[] = 'range_id = :assigned_course_date_id';
+ $parameters[':assigned_course_date_id'] = $filters['assigned-course-date-id'];
+ }
+
+ if (array_key_exists('assigned-user-id', $filters)) {
+ $conditions[] = 'range_id = :assigned_user_id';
+ $parameters[':assigned_user_id'] = $filters['assigned-user-id'];
+ }
+
+ if (array_key_exists('begin', $filters)) {
+ if ($filters['begin'] instanceof ComplexFilter) {
+ $filters['begin']->apply($conditions, $parameters, 'begin');
+ } else {
+ $conditions[] = 'begin = :begin';
+ $parameters[':begin'] = $filters['begin'];
+ }
+ }
+
+ if (array_key_exists('booking-type', $filters)) {
+ $conditions[] = 'booking_type = :booking_type';
+ $parameters[':booking_type'] = $filters['booking-type'];
+ }
+
+ if (array_key_exists('booking-user-id', $filters)) {
+ $conditions[] = 'booking_user_id = :booking_user_id';
+ $parameters[':booking_user_id'] = $filters['booking-user-id'];
+ }
+
+ if (array_key_exists('end', $filters)) {
+ if ($filters['end'] instanceof ComplexFilter) {
+ $filters['end']->apply($conditions, $parameters, 'end');
+ } else {
+ $conditions[] = 'end = :end';
+ $parameters[':end'] = $filters['end'];
+ }
+ }
+
+ if (array_key_exists('range-id', $filters)) {
+ $conditions[] = 'range_id = :range_id';
+ $parameters[':range_id'] = $filters['range_id'];
+ }
+
+ if (array_key_exists('resource-id', $filters)) {
+ $conditions[] = 'resource_id = :resource_id';
+ $parameters[':resource_id'] = $filters['resource-id'];
+ }
+
+ $condition = implode(' ', $joins);
+ if ($condition) {
+ $condition .= ' WHERE ';
+ }
+ $condition .= '(' . implode(') AND (', $conditions ?: [1]) . ')';
+
+ return [$condition, $parameters];
+ }
+
+
+ private function decomposeFilter(string $key, $value)
+ {
+
+ }
+}
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalIndex.php
new file mode 100644
index 0000000..578e1fe
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalIndex.php
@@ -0,0 +1,209 @@
+<?php
+namespace JsonApi\Routes\Resources;
+
+use JsonApi\ComplexFilter;
+use JsonApi\Schemas\ResourceBookingIntervalSchema;
+use JsonApi\Errors\BadRequestException;
+use JsonApi\Errors\RecordNotFoundException;
+use JsonApi\JsonApiController;
+use Neomerx\JsonApi\Contracts\Http\Query\BaseQueryParserInterface;
+use Neomerx\JsonApi\Exceptions\JsonApiException;
+use Neomerx\JsonApi\Schema\ErrorCollection;
+use Psr\Http\Message\RequestInterface as Request;
+use Psr\Http\Message\ResponseInterface as Response;
+use Resource;
+use ResourceBooking;
+use ResourceBookingInterval;
+use Slim\Routing\RouteContext;
+
+final class ResourceBookingIntervalIndex extends JsonApiController
+{
+ protected $allowedPagingParameters = ['offset', 'limit'];
+ protected $allowedIncludePaths = [
+ ResourceBookingIntervalSchema::REL_BOOKING,
+ ResourceBookingIntervalSchema::REL_RESOURCE,
+ ];
+ protected $allowedSortFields = [
+ 'begin',
+ 'end',
+ 'mkdate',
+ ];
+ protected $allowedFilteringParameters = [
+ 'begin',
+ 'booking-id',
+ 'end',
+ 'takes-place',
+ 'resource-id',
+ ];
+ protected $allowedFieldSetTypes = [
+ ResourceBookingIntervalSchema::TYPE => ['begin', 'end', 'takes-place', 'mkdate', 'chdate'],
+ ];
+
+ public function __invoke(Request $request, Response $response, array $args): Response
+ {
+ $filters = $this->getFilters();
+ $order = $this->getOrder();
+ [$offset, $limit] = $this->getOffsetAndLimit();
+
+ $routeName = RouteContext::fromRequest($request)->getRoute()->getName();
+ if ($routeName === 'intervals-of-booking') {
+ if (isset($filters['booking-id'])) {
+ throw new BadRequestException('You may not use the booking-id filter for this route.');
+ }
+
+ if (!ResourceBooking::exists($args['id'])) {
+ throw new RecordNotFoundException("No resource booking found with id {$args['id']}.");
+ }
+
+ $filters['booking-id'] = $args['id'];
+ } elseif ($routeName === 'intervals-of-resource') {
+ if (isset($filters['resource-id'])) {
+ throw new BadRequestException('You may not use the resource-id filter for this route.');
+ }
+
+ if (!Resource::exists($args['id'])) {
+ throw new RecordNotFoundException("No resource found with id {$args['id']}.");
+ }
+
+ $filters['resource-id'] = $args['id'];
+ }
+
+ [$condition, $parameters] = $this->getConditionAndParameters($filters);
+
+ $total = ResourceBookingInterval::countBySql($condition, $parameters);
+ $bookings = ResourceBookingInterval::findBySQL(
+ "{$condition} {$order} LIMIT {$offset}, {$limit}",
+ $parameters
+ );
+
+ return $this->getPaginatedContentResponse($bookings, $total);
+ }
+
+ private function getFilters(): array
+ {
+ $filters = iterator_to_array($this->getQueryParameters()->getFilters());
+ $errors = new ErrorCollection();
+
+ if (array_key_exists('begin', $filters)) {
+ if (ComplexFilter::detect($filters['begin'])) {
+ $filters['begin'] = ComplexFilter::create($filters['begin']);
+ } elseif (!is_numeric($filters['begin'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ 'Filter begin must be numeric.'
+ );
+ } else {
+ $filters['begin'] = (int)$filters['begin'];
+ }
+ }
+
+ if (array_key_exists('booking-id', $filters)) {
+ if (!ResourceBooking::exists($filters['booking-id'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ sprintf(
+ 'Filter booking-id links to an unknown resource booking with id %s.',
+ $filters['booking-id']
+ )
+ );
+ }
+ }
+
+ if (array_key_exists('end', $filters)) {
+ if (ComplexFilter::detect($filters['end'])) {
+ $filters['end'] = ComplexFilter::create($filters['end']);
+ } elseif (!is_numeric($filters['end'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ 'Filter end must be numeric.'
+ );
+ } else {
+ $filters['end'] = (int)$filters['end'];
+ }
+ }
+
+ if (array_key_exists('takes-place', $filters)) {
+ $filters['takes-place'] = (bool) $filters['takes-place'];
+ }
+
+ if (array_key_exists('resource-id', $filters)) {
+ if (!Resource::exists($filters['resource-id'])) {
+ $errors->addQueryParameterError(
+ BaseQueryParserInterface::PARAM_FILTER,
+ sprintf(
+ 'Filter resource-id links to an unknown resource with id %s.',
+ $filters['resource-id']
+ )
+ );
+ }
+ }
+
+ if (count($errors) > 0) {
+ throw new JsonApiException($errors, JsonApiException::HTTP_CODE_BAD_REQUEST);
+ }
+
+ return $filters;
+ }
+
+ private function getOrder(): string
+ {
+ $result = [];
+ foreach ($this->getQueryParameters()->getSorts() as $column => $ascending) {
+ if ($ascending) {
+ $result[] = $column;
+ } else {
+ $result[] = "{$column} DESC";
+ }
+ }
+
+ return count($result) > 0 ? 'ORDER BY ' . implode(', ', $result) : '';
+ }
+
+ private function getConditionAndParameters(array $filters): array
+ {
+ $conditions = [];
+ $joins = [];
+ $parameters = [];
+
+ if (array_key_exists('begin', $filters)) {
+ if ($filters['begin'] instanceof ComplexFilter) {
+ $filters['begin']->apply($conditions, $parameters, 'begin');
+ } else {
+ $conditions[] = 'begin = :begin';
+ $parameters[':begin'] = $filters['begin'];
+ }
+ }
+
+ if (array_key_exists('booking-id', $filters)) {
+ $conditions[] = 'booking_id = :booking_id';
+ $parameters[':booking_id'] = $filters['booking-id'];
+ }
+
+ if (array_key_exists('end', $filters)) {
+ if ($filters['end'] instanceof ComplexFilter) {
+ $filters['end']->apply($conditions, $parameters, 'end');
+ } else {
+ $conditions[] = 'end = :end';
+ $parameters[':end'] = $filters['end'];
+ }
+ }
+
+ if (array_key_exists('takes-place', $filters)) {
+ $conditions[] = 'takes_place = :takes_place';
+ $parameters[':takes_place'] = (int) $filters['takes-place'];
+ }
+
+ if (array_key_exists('resource-id', $filters)) {
+ $conditions[] = 'resource_id = :resource_id';
+ $parameters[':resource_id'] = $filters['resource-id'];
+ }
+
+ $condition = implode(' ', $joins);
+ if ($condition) {
+ $condition .= ' WHERE ';
+ }
+ $condition .= '(' . implode(') AND (', $conditions ?: [1]) . ')';
+
+ return [$condition, $parameters];
+ }
+}
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalShow.php b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalShow.php
new file mode 100644
index 0000000..b561b42
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourceBookingIntervalShow.php
@@ -0,0 +1,27 @@
+<?php
+namespace JsonApi\Routes\Resources;
+
+use JsonApi\Schemas\ResourceBookingIntervalSchema;
+use JsonApi\Errors\RecordNotFoundException;
+use JsonApi\JsonApiController;
+use Psr\Http\Message\RequestInterface as Request;
+use Psr\Http\Message\ResponseInterface as Response;
+use ResourceBookingInterval;
+
+final class ResourceBookingIntervalShow extends JsonApiController
+{
+ protected $allowedIncludePaths = [
+ ResourceBookingIntervalSchema::REL_BOOKING,
+ ResourceBookingIntervalSchema::REL_RESOURCE,
+ ];
+
+ public function __invoke(Request $request, Response $response, array $args): Response
+ {
+ $interval = ResourceBookingInterval::find($args['id']);
+ if (!$interval) {
+ throw new RecordNotFoundException("No booking interval with the id {$args['id']}");
+ }
+
+ return $this->getContentResponse($interval);
+ }
+}
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceBookingShow.php b/lib/classes/JsonApi/Routes/Resources/ResourceBookingShow.php
new file mode 100644
index 0000000..f5f0dc8
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourceBookingShow.php
@@ -0,0 +1,23 @@
+<?php
+namespace JsonApi\Routes\Resources;
+
+use JsonApi\Errors\RecordNotFoundException;
+use JsonApi\JsonApiController;
+use Psr\Http\Message\{
+ RequestInterface as Request,
+ ResponseInterface as Response
+};
+use ResourceBooking;
+
+final class ResourceBookingShow extends JsonApiController
+{
+ public function __invoke(Request $request, Response $response, array $args): Response
+ {
+ $booking = ResourceBooking::find($args['id']);
+ if (!$booking) {
+ throw new RecordNotFoundException("No booking with the id {$args['id']}");
+ }
+
+ return $this->getContentResponse($booking);
+ }
+}
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceCategoryIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourceCategoryIndex.php
new file mode 100644
index 0000000..4eed857
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourceCategoryIndex.php
@@ -0,0 +1,91 @@
+<?php
+namespace JsonApi\Routes\Resources;
+
+use JsonApi\Errors\BadRequestException;
+use JsonApi\JsonApiController;
+use Psr\Http\Message\{
+ RequestInterface as Request,
+ ResponseInterface as Response
+};
+
+final class ResourceCategoryIndex extends JsonApiController
+{
+ protected $allowedFilteringParameters = ['class_name', 'system'];
+ protected $allowedPagingParameters = ['offset', 'limit'];
+
+ public function __invoke(Request $request, Response $response, array $args): Response
+ {
+ [$offset, $limit] = $this->getOffsetAndLimit();
+ [$condition, $parameters] = $this->getConditionAndParameters(
+ $this->getFilters()
+ );
+
+ $total = \ResourceCategory::countBySql($condition, $parameters);
+ $resources = \ResourceCategory::findBySQL(
+ "{$condition} LIMIT {$offset}, {$limit}",
+ $parameters
+ );
+
+ return $this->getPaginatedContentResponse($resources, $total);
+ }
+
+ private function getFilters()
+ {
+ $filters = [];
+
+ $filtering = $this->getQueryParameters()->getFilteringParameters() ?? [];
+
+ if (array_key_exists('class_name', $filtering)) {
+ if (empty($filtering['class_name'])) {
+ throw new BadRequestException('Class name filter must be not be empty.');
+ }
+
+ $filters['class_name'] = $filtering['class_name'];
+ }
+
+ if (array_key_exists('system', $filtering)) {
+ $filters['system'] = (bool) $filtering['system'];
+ }
+
+ return $filters;
+ }
+
+ private function getConditionAndParameters(array $filters): array
+ {
+ $joins = [];
+ $conditions = [];
+ $parameters = [];
+
+ if (array_key_exists('class_name', $filters)) {
+ $conditions[] = '`class_name` = :class';
+ $parameters[':class'] = $filters['class'];
+ }
+
+ if (array_key_exists('system', $filters)) {
+ $conditions[] = '`system` = :system';
+ $parameters[':system'] = (int) $filters['system'];
+ }
+
+ // Build condition
+ $condition = implode(' ', $joins);
+ if ($condition) {
+ $condition .= ' WHERE ';
+ }
+
+ if (count($conditions) === 0) {
+ $conditions[] = '1';
+ }
+
+ $condition .= implode(' AND ', array_map(
+ function ($condition): string {
+ return "({$condition})";
+ },
+ $conditions
+ ));
+
+ return [
+ $condition,
+ $parameters,
+ ];
+ }
+}
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceCategoryShow.php b/lib/classes/JsonApi/Routes/Resources/ResourceCategoryShow.php
new file mode 100644
index 0000000..f73d06e
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourceCategoryShow.php
@@ -0,0 +1,27 @@
+<?php
+namespace JsonApi\Routes\Resources;
+
+use JsonApi\Errors\BadRequestException;
+use JsonApi\Errors\RecordNotFoundException;
+use JsonApi\JsonApiController;
+use Psr\Http\Message\{
+ RequestInterface as Request,
+ ResponseInterface as Response
+};
+
+final class ResourceCategoryShow extends JsonApiController
+{
+ public function __invoke(Request $request, Response $response, array $args): Response
+ {
+ if (empty($args['id'])) {
+ throw new BadRequestException('Id must not be empty.');
+ }
+
+ $resource = \ResourceCategory::find($args['id']);
+ if ($resource === null) {
+ throw new RecordNotFoundException("No resource category found with id {$args['id']}");
+ }
+
+ return $this->getContentResponse($resource);
+ }
+}
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourceIndex.php
new file mode 100644
index 0000000..07f5b12
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourceIndex.php
@@ -0,0 +1,99 @@
+<?php
+namespace JsonApi\Routes\Resources;
+
+use JsonApi\Schemas\ResourceSchema;
+use JsonApi\Errors\BadRequestException;
+use JsonApi\JsonApiController;
+use Psr\Http\Message\{
+ RequestInterface as Request,
+ ResponseInterface as Response
+};
+
+final class ResourceIndex extends JsonApiController
+{
+ protected $allowedFilteringParameters = ['level', 'class'];
+ protected $allowedIncludePaths = [ResourceSchema::REL_CATEGORY];
+ protected $allowedPagingParameters = ['offset', 'limit'];
+
+ public function __invoke(Request $request, Response $response, array $args): Response
+ {
+ [$offset, $limit] = $this->getOffsetAndLimit();
+ [$condition, $parameters] = $this->getConditionAndParameters(
+ $this->getFilters()
+ );
+
+ $total = \Resource::countBySql($condition, $parameters);
+ $resources = \Resource::findBySQL(
+ "{$condition} LIMIT {$offset}, {$limit}",
+ $parameters
+ );
+
+ return $this->getPaginatedContentResponse($resources, $total);
+ }
+
+ private function getFilters()
+ {
+ $filters = [];
+
+ $filtering = $this->getQueryParameters()->getFilteringParameters() ?? [];
+
+ if (array_key_exists('level', $filtering)) {
+ if (!ctype_digit($filtering['level'])) {
+ throw new BadRequestException('Level filter must be an int.');
+ }
+
+ $filters['level'] = (int) $filtering['level'];
+ }
+
+ if (array_key_exists('class', $filtering)) {
+ if (empty($filtering['class'])) {
+ throw new BadRequestException('Class filter must be not be empty.');
+ }
+
+ $filters['class'] = $filtering['class'];
+ }
+
+ return $filters;
+ }
+
+ private function getConditionAndParameters(array $filters): array
+ {
+ $joins = [];
+ $conditions = [];
+ $parameters = [];
+
+ if (array_key_exists('level', $filters)) {
+ $conditions[] = '`resources`.`level` = :level';
+ $parameters[':level'] = $filters['level'];
+ }
+
+ if (array_key_exists('class', $filters)) {
+ $joins[] = 'JOIN `resource_categories`
+ ON `resources`.`category_id` = `resource_categories`.`id`';
+ $conditions[] = '`resource_categories`.`class_name` = :class';
+ $parameters[':class'] = $filters['class'];
+ }
+
+ // Build condition
+ $condition = implode(' ', $joins);
+ if ($condition) {
+ $condition .= ' WHERE ';
+ }
+
+ if (count($conditions) === 0) {
+ $conditions[] = '1';
+ }
+
+ $condition .= implode(' AND ', array_map(
+ function ($condition): string {
+ return "({$condition})";
+ },
+ $conditions
+ ));
+
+ return [
+ $condition,
+ $parameters,
+ ];
+ }
+}
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourcePropertyIndex.php b/lib/classes/JsonApi/Routes/Resources/ResourcePropertyIndex.php
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourcePropertyIndex.php
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourcePropertyShow.php b/lib/classes/JsonApi/Routes/Resources/ResourcePropertyShow.php
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourcePropertyShow.php
diff --git a/lib/classes/JsonApi/Routes/Resources/ResourceShow.php b/lib/classes/JsonApi/Routes/Resources/ResourceShow.php
new file mode 100644
index 0000000..19dceb9
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/Resources/ResourceShow.php
@@ -0,0 +1,30 @@
+<?php
+namespace JsonApi\Routes\Resources;
+
+use JsonApi\Schemas\ResourceSchema;
+use JsonApi\Errors\BadRequestException;
+use JsonApi\Errors\RecordNotFoundException;
+use JsonApi\JsonApiController;
+use Psr\Http\Message\{
+ RequestInterface as Request,
+ ResponseInterface as Response
+};
+
+final class ResourceShow extends JsonApiController
+{
+ protected $allowedIncludePaths = [ResourceSchema::REL_CATEGORY];
+
+ public function __invoke(Request $request, Response $response, array $args): Response
+ {
+ if (empty($args['id'])) {
+ throw new BadRequestException('Id must not be empty.');
+ }
+
+ $resource = \Resource::find($args['id']);
+ if ($resource === null) {
+ throw new RecordNotFoundException("No resource found with id {$args['id']}");
+ }
+
+ return $this->getContentResponse($resource);
+ }
+}
diff --git a/lib/classes/JsonApi/SchemaMap.php b/lib/classes/JsonApi/SchemaMap.php
index a5c1213..56db7d0 100644
--- a/lib/classes/JsonApi/SchemaMap.php
+++ b/lib/classes/JsonApi/SchemaMap.php
@@ -60,6 +60,12 @@ class SchemaMap
\FileRef::class => Schemas\FileRef::class,
\FolderType::class => Schemas\Folder::class,
+ \Resource::class => Schemas\ResourceSchema::class,
+ \ResourceBooking::class => Schemas\ResourceBookingSchema::class,
+ \ResourceBookingInterval::class => Schemas\ResourceBookingIntervalSchema::class,
+ \ResourceCategory::class => Schemas\ResourceCategorySchema::class,
+ \ResourceProperty::class => Schemas\ResourcePropertySchema::class,
+
\Courseware\Block::class => Schemas\Courseware\Block::class,
\Courseware\BlockComment::class => Schemas\Courseware\BlockComment::class,
\Courseware\BlockFeedback::class => Schemas\Courseware\BlockFeedback::class,
diff --git a/lib/classes/JsonApi/Schemas/ResourceBookingIntervalSchema.php b/lib/classes/JsonApi/Schemas/ResourceBookingIntervalSchema.php
new file mode 100644
index 0000000..888d3cd
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/ResourceBookingIntervalSchema.php
@@ -0,0 +1,91 @@
+<?php
+namespace JsonApi\Schemas;
+
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use Neomerx\JsonApi\Schema\Link;
+use ResourceBookingInterval;
+
+final class ResourceBookingIntervalSchema extends SchemaProvider
+{
+ const TYPE = 'resource-booking-intervals';
+
+ const REL_BOOKING = 'booking';
+ const REL_RESOURCE = 'resource';
+
+ /**
+ * @param ResourceBookingInterval $resource
+ */
+ public function getId($resource): ?string
+ {
+ return $resource->id;
+ }
+
+ /**
+ * @param ResourceBookingInterval $resource
+ */
+ public function getAttributes($resource, ContextInterface $context): iterable
+ {
+ return [
+ 'begin' => date('c', $resource->begin),
+ 'end' => date('c', $resource->end),
+
+ 'takes-place' => (bool) $resource->takes_place,
+
+ 'mkdate' => date('c', $resource->mkdate),
+ 'chdate' => date('c', $resource->chdate),
+ ];
+ }
+
+ /**
+ * @param ResourceBookingInterval $resource
+ */
+ public function getRelationships($resource, ContextInterface $context): iterable
+ {
+ $relationships = [];
+
+ $isPrimary = $context->getPosition()->getLevel() === 0;
+ if ($isPrimary) {
+ $relationships = $this->getBookingRelationship(
+ $relationships,
+ $resource->booking,
+ $this->shouldInclude($context, self::REL_BOOKING)
+ );
+ $relationships = $this->getResourceRelationship(
+ $relationships,
+ $resource->resource,
+ $this->shouldInclude($context, self::REL_RESOURCE)
+ );
+ }
+
+ return $relationships;
+ }
+
+ private function getBookingRelationship(
+ array $relationships,
+ \ResourceBooking $booking,
+ bool $includeData
+ ): array {
+ $relationships[self::REL_BOOKING] = [
+ self::RELATIONSHIP_LINKS => [
+ Link::RELATED => $this->createLinkToResource($booking),
+ ],
+ self::RELATIONSHIP_DATA => $includeData ? $booking : \ResourceBooking::build(['id' => $booking->id]),
+ ];
+
+ return $relationships;
+ }
+
+ private function getResourceRelationship(
+ array $relationships,
+ \Resource $resource,
+ bool $includeData
+ ): array {
+ $relationships[self::REL_RESOURCE] = [
+ self::RELATIONSHIP_LINKS => [
+ Link::RELATED => $this->createLinkToResource($resource),
+ ],
+ self::RELATIONSHIP_DATA => $includeData ? $resource : \Resource::build(['id' => $resource->id]),
+ ];
+ return $relationships;
+ }
+}
diff --git a/lib/classes/JsonApi/Schemas/ResourcePropertySchema.php b/lib/classes/JsonApi/Schemas/ResourcePropertySchema.php
new file mode 100644
index 0000000..d9caf30
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/ResourcePropertySchema.php
@@ -0,0 +1,37 @@
+<?php
+namespace JsonApi\Schemas;
+
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use ResourceProperty;
+
+final class ResourcePropertySchema extends SchemaProvider
+{
+ /**
+ * @param ResourceProperty $resource
+ */
+ public function getId($resource): ?string
+ {
+ return $resource->id;
+ }
+
+ /**
+ * @param ResourceProperty $resource
+ */
+ public function getAttributes($resource, ContextInterface $context): iterable
+ {
+ return [
+ 'state' => $resource->state,
+
+ 'mkdate' => date('c', $resource->mkdate),
+ 'chdate' => date('c', $resource->chdate),
+ ];
+ }
+
+ /**
+ * @param ResourceProperty $resource
+ */
+ public function getRelationships($resource, ContextInterface $context): iterable
+ {
+ return [];
+ }
+}
diff --git a/lib/classes/JsonApi/Schemas/ResourceSchema.php b/lib/classes/JsonApi/Schemas/ResourceSchema.php
new file mode 100644
index 0000000..bcf89db
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/ResourceSchema.php
@@ -0,0 +1,89 @@
+<?php
+namespace JsonApi\Schemas;
+
+use Neomerx\JsonApi\Contracts\Schema\BaseLinkInterface;
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use Resource;
+
+final class ResourceSchema extends SchemaProvider
+{
+ const TYPE = 'resources';
+
+ const REL_CATEGORY = 'category';
+
+ /**
+ * @param Resource $resource
+ */
+ public function getId($resource): ?string
+ {
+ return $resource->id;
+ }
+
+ /**
+ * @param Resource $resource
+ */
+ public function getAttributes($resource, ContextInterface $context): iterable
+ {
+ return [
+ 'level' => (int) $resource->level,
+ 'name' => (string) $resource->name,
+ 'description' => (string) $resource->description,
+ 'requestable' => (bool) $resource->requestable,
+ 'lockable' => (bool) $resource->lockable,
+ 'sort_position' => (int) $resource->sort_position,
+
+ 'mkdate' => date('c', $resource->mkdate),
+ 'chdate' => date('c', $resource->chdate),
+ ];
+ }
+
+ /**
+ * @param Resource $resource
+ */
+ public function hasResourceMeta($resource): bool
+ {
+ return true;
+ }
+
+ /**
+ * @param Resource $resource
+ */
+ public function getResourceMeta($resource)
+ {
+ return [
+ 'class' => $resource->class_name,
+ ];
+ }
+
+ /**
+ * @param Resource $resource
+ */
+ public function getRelationships($resource, ContextInterface $context): iterable
+ {
+ if ($context->getPosition()->getLevel() > 0) {
+ return [];
+ };
+
+ $relationships = [];
+
+ $relationships = $this->getCategoryRelationship(
+ $relationships,
+ $resource,
+ $this->shouldInclude($context, self::REL_CATEGORY)
+ );
+
+ return $relationships;
+ }
+
+ private function getCategoryRelationship(array $relationships, $resource, bool $shouldInclude)
+ {
+ $relationships[self::REL_CATEGORY] = [
+ self::RELATIONSHIP_LINKS => [
+ BaseLinkInterface::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_CATEGORY),
+ ],
+ self::RELATIONSHIP_DATA => $shouldInclude ? $resource->category : \ResourceCategory::build(['id' => $resource->category_id]),
+ ];
+
+ return $relationships;
+ }
+}