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

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

Closes #3458

Merge request studip/studip!2359
parent cfd5cc5f
No related branches found
No related tags found
No related merge requests found
...@@ -10,17 +10,45 @@ use JsonApi\Schemas\Institute as InstituteSchema; ...@@ -10,17 +10,45 @@ use JsonApi\Schemas\Institute as InstituteSchema;
class InstitutesIndex extends JsonApiController class InstitutesIndex extends JsonApiController
{ {
protected $allowedIncludePaths = [ protected $allowedIncludePaths = [
InstituteSchema::REL_FACULTY,
InstituteSchema::REL_STATUS_GROUPS, InstituteSchema::REL_STATUS_GROUPS,
InstituteSchema::REL_SUB_INSTITUTES,
]; ];
protected $allowedFilteringParameters = ['is-faculty'];
protected $allowedPagingParameters = ['offset', 'limit']; protected $allowedPagingParameters = ['offset', 'limit'];
public function __invoke(Request $request, Response $response, $args) public function __invoke(Request $request, Response $response, $args)
{ {
list($offset, $limit) = $this->getOffsetAndLimit(); [$offset, $limit] = $this->getOffsetAndLimit();
$institutes = \Institute::findBySql('1 ORDER BY Name LIMIT ? OFFSET ?', [$limit, $offset]);
$total = \Institute::countBySql(); $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); 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; ...@@ -15,7 +15,9 @@ use JsonApi\Schemas\Institute as InstituteSchema;
class InstitutesShow extends JsonApiController class InstitutesShow extends JsonApiController
{ {
protected $allowedIncludePaths = [ protected $allowedIncludePaths = [
InstituteSchema::REL_FACULTY,
InstituteSchema::REL_STATUS_GROUPS, InstituteSchema::REL_STATUS_GROUPS,
InstituteSchema::REL_SUB_INSTITUTES,
]; ];
/** /**
......
...@@ -10,48 +10,56 @@ class Institute extends SchemaProvider ...@@ -10,48 +10,56 @@ class Institute extends SchemaProvider
const TYPE = 'institutes'; const TYPE = 'institutes';
const REL_BLUBBER = 'blubber-threads'; const REL_BLUBBER = 'blubber-threads';
const REL_FACULTY = 'faculty';
const REL_FILES = 'file-refs'; const REL_FILES = 'file-refs';
const REL_FOLDERS = 'folders'; const REL_FOLDERS = 'folders';
const REL_MEMBERSHIPS = 'memberships'; const REL_MEMBERSHIPS = 'memberships';
const REL_STATUS_GROUPS = 'status-groups'; const REL_STATUS_GROUPS = 'status-groups';
const REL_SUB_INSTITUTES = 'sub-institutes';
/**
* @param \Institute $institute
*/
public function getId($institute): ?string public function getId($institute): ?string
{ {
return $institute->id; return $institute->id;
} }
/**
* @param \Institute $institute
*/
public function getAttributes($institute, ContextInterface $context): iterable public function getAttributes($institute, ContextInterface $context): iterable
{ {
return [ return [
'name' => (string) $institute['Name'], 'name' => (string) $institute->name,
'city' => $institute['Plz'], 'city' => $institute->plz,
'street' => $institute['Strasse'], 'street' => $institute->strasse,
'phone' => $institute['telefon'], 'phone' => $institute->telefon,
'fax' => $institute['fax'], 'fax' => $institute->fax,
'url' => $institute['url'], 'url' => $institute->url,
'mkdate' => date('c', $institute['mkdate']), 'is-faculty' => $institute->is_fak,
'chdate' => date('c', $institute['chdate']), 'mkdate' => date('c', $institute->mkdate),
'chdate' => date('c', $institute->chdate),
]; ];
} }
/** /**
* @param \Institute $resource
* @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/ */
public function getRelationships($resource, ContextInterface $context): iterable public function getRelationships($resource, ContextInterface $context): iterable
{ {
$relationships = []; $relationships = [];
$filesLink = $this->getRelationshipRelatedLink($resource, self::REL_FILES);
$relationships[self::REL_FILES] = [ $relationships[self::REL_FILES] = [
self::RELATIONSHIP_LINKS => [ 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] = [ $relationships[self::REL_FOLDERS] = [
self::RELATIONSHIP_LINKS => [ self::RELATIONSHIP_LINKS => [
Link::RELATED => $foldersLink, Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_FOLDERS),
], ],
]; ];
...@@ -73,6 +81,60 @@ class Institute extends SchemaProvider ...@@ -73,6 +81,60 @@ class Institute extends SchemaProvider
$this->shouldInclude($context, self::REL_STATUS_GROUPS) $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; return $relationships;
} }
...@@ -86,11 +148,26 @@ class Institute extends SchemaProvider ...@@ -86,11 +148,26 @@ class Institute extends SchemaProvider
Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_STATUS_GROUPS), Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_STATUS_GROUPS),
] ]
]; ];
if ($includeData) { if ($includeData) {
$related = $resource->status_groups; $relation[self::RELATIONSHIP_DATA] = $resource->status_groups;
$relation[self::RELATIONSHIP_DATA] = $related;
} }
return array_merge($relationships, [self::REL_STATUS_GROUPS => $relation]); 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