Skip to content
Snippets Groups Projects
Commit f4be2f07 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms Committed by Jan-Hendrik Willms
Browse files

add faculty and sub-institutes relationship to institutes route, fixes #3458

Closes #3458

Merge request studip/studip!2359
parent ca5f419f
No related branches found
No related tags found
No related merge requests found
......@@ -10,17 +10,45 @@ use JsonApi\Schemas\Institute as InstituteSchema;
class InstitutesIndex extends JsonApiController
{
protected $allowedIncludePaths = [
InstituteSchema::REL_FACULTY,
InstituteSchema::REL_STATUS_GROUPS,
InstituteSchema::REL_SUB_INSTITUTES,
];
protected $allowedFilteringParameters = ['is-faculty'];
protected $allowedPagingParameters = ['offset', 'limit'];
public function __invoke(Request $request, Response $response, $args)
{
list($offset, $limit) = $this->getOffsetAndLimit();
$institutes = \Institute::findBySql('1 ORDER BY Name LIMIT ? OFFSET ?', [$limit, $offset]);
$total = \Institute::countBySql();
[$offset, $limit] = $this->getOffsetAndLimit();
$filters = $this->getFilters();
if (!isset($filters['is-faculty'])) {
$condition = '1';
} elseif ($filters['is-faculty']) {
$condition = 'fakultaets_id = Institut_id';
} else {
$condition = 'fakultaets_id != Institut_id';
}
$institutes = \Institute::findBySql("{$condition} ORDER BY Name LIMIT ? OFFSET ?", [$limit, $offset]);
$total = \Institute::countBySql($condition);
return $this->getPaginatedContentResponse($institutes, $total);
}
private function getFilters()
{
$filtering = $this->getQueryParameters()->getFilteringParameters() ?? [];
$filters = [];
if (isset($filtering['is-faculty'])) {
$filters['is-faculty'] = (bool) $filtering['is-faculty'];
}
return $filters;
}
}
......@@ -15,7 +15,9 @@ use JsonApi\Schemas\Institute as InstituteSchema;
class InstitutesShow extends JsonApiController
{
protected $allowedIncludePaths = [
InstituteSchema::REL_FACULTY,
InstituteSchema::REL_STATUS_GROUPS,
InstituteSchema::REL_SUB_INSTITUTES,
];
/**
......
......@@ -10,47 +10,55 @@ class Institute extends SchemaProvider
const TYPE = 'institutes';
const REL_BLUBBER = 'blubber-threads';
const REL_FACULTY = 'faculty';
const REL_FILES = 'file-refs';
const REL_FOLDERS = 'folders';
const REL_STATUS_GROUPS = 'status-groups';
const REL_SUB_INSTITUTES = 'sub-institutes';
/**
* @param \Institute $institute
*/
public function getId($institute): ?string
{
return $institute->id;
}
/**
* @param \Institute $institute
*/
public function getAttributes($institute, ContextInterface $context): iterable
{
return [
'name' => (string) $institute['Name'],
'city' => $institute['Plz'],
'street' => $institute['Strasse'],
'phone' => $institute['telefon'],
'fax' => $institute['fax'],
'url' => $institute['url'],
'mkdate' => date('c', $institute['mkdate']),
'chdate' => date('c', $institute['chdate']),
'name' => (string) $institute->name,
'city' => $institute->plz,
'street' => $institute->strasse,
'phone' => $institute->telefon,
'fax' => $institute->fax,
'url' => $institute->url,
'is-faculty' => $institute->is_fak,
'mkdate' => date('c', $institute->mkdate),
'chdate' => date('c', $institute->chdate),
];
}
/**
* @param \Institute $resource
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getRelationships($resource, ContextInterface $context): iterable
{
$relationships = [];
$filesLink = $this->getRelationshipRelatedLink($resource, self::REL_FILES);
$relationships[self::REL_FILES] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $filesLink,
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_FILES),
],
];
$foldersLink = $this->getRelationshipRelatedLink($resource, self::REL_FOLDERS);
$relationships[self::REL_FOLDERS] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $foldersLink,
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_FOLDERS),
],
];
......@@ -66,6 +74,60 @@ class Institute extends SchemaProvider
$this->shouldInclude($context, self::REL_STATUS_GROUPS)
);
if (!$resource->is_fak) {
$relationships = $this->addFacultyRelationship(
$relationships,
$resource,
$this->shouldInclude($context, self::REL_FACULTY)
);
}
$relationships = $this->addSubInstitutesRelationship(
$relationships,
$resource,
$this->shouldInclude($context, self::REL_SUB_INSTITUTES)
);
return $relationships;
}
private function addFacultyRelationship(array $relationships, \Institute $resource, bool $includeData): array
{
$relation = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_FACULTY),
],
];
if ($includeData) {
$relation[self::RELATIONSHIP_DATA] = $resource->faculty;
} else {
$relation[self::RELATIONSHIP_DATA] = \Institute::build(['id' => $resource->faculty->id]);
}
$relationships[self::REL_FACULTY] = $relation;
return $relationships;
}
private function addSubInstitutesRelationship(array $relationships, \Institute $resource, bool $includeData): array
{
$relation = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_SUB_INSTITUTES),
],
];
if ($includeData) {
$relation[self::RELATIONSHIP_DATA] = $resource->sub_institutes;
} else {
$relation[self::RELATIONSHIP_DATA] = $resource->sub_institutes->map(function (\Institute $institute): \Institute {
return \Institute::build(['id' => $institute->id]);
});
}
$relationships[self::REL_SUB_INSTITUTES] = $relation;
return $relationships;
}
......@@ -79,11 +141,26 @@ class Institute extends SchemaProvider
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_STATUS_GROUPS),
]
];
if ($includeData) {
$related = $resource->status_groups;
$relation[self::RELATIONSHIP_DATA] = $related;
$relation[self::RELATIONSHIP_DATA] = $resource->status_groups;
}
return array_merge($relationships, [self::REL_STATUS_GROUPS => $relation]);
}
public function hasResourceMeta($resource): bool
{
return true;
}
/**
* @param \Institute $resource
*/
public function getResourceMeta($resource)
{
return [
'sub-institutes-count' => count($resource->sub_institutes),
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment