Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
Added support or building endpoints that allow items to retrieve thei…
Browse files Browse the repository at this point in the history
…r children and grandchildren items. Also added item scoping to the polymorphic item fetch that allow items to rpolymorphically retrieve single child and grandchild items.
  • Loading branch information
nickbart committed Dec 25, 2012
1 parent 4cb038d commit 02029ee
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions Server/Library/SectionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ abstract class Plex_Server_Library_SectionAbstract extends Plex_Server_Library
*/
const ENDPOINT_SEARCH = 'search';

/**
* Endping for listing the child items of a parent or grandparent item.
*/
const ENDPOINT_CHILDREN = 'children';

/**
* Parameter for searching movies.
*/
Expand Down Expand Up @@ -290,6 +295,26 @@ protected function buildSearchEndpoint($type, $query)
return $this->buildEndpoint($endpoint);
}

/**
* Builds an endpoint for an item to retrieve its children and grandchildren
* items.
*
* @uses Plex_Server_Library::ENDPOINT_METADATA
* @uses Plex_Server_Library_SectionAbstract::ENDPOINT_CHILDREN
* @uses Plex_Server_Library_ItemAbstract::getRatingKey()
*
* @return string The requested children endpoint.
*/
protected function buildChildrenEndpoint()
{
return sprintf(
'%s/%d/%s',
Plex_Server_Library::ENDPOINT_METADATA,
$this->getRatingKey(),
self::ENDPOINT_CHILDREN
);
}

/**
* Generic method allowing a child class to retrieve all items for its
* section.
Expand Down Expand Up @@ -518,7 +543,7 @@ protected function getItemsByYear($year)
*
* @return Plex_Server_Library_ItemAbstract The request Plex library item.
*/
protected function getPolymorphicItem($polymorphicData)
protected function getPolymorphicItem($polymorphicData, $scopedToItem = FALSE)
{
if (is_int($polymorphicData)) {
// If we have an integer then we can assume we have a rating key.
Expand Down Expand Up @@ -553,14 +578,27 @@ protected function getPolymorphicItem($polymorphicData)
// If we don't have a rating key or a key then we just assume we're
// doing an exact title match.

// If we are scoped to item it means an item is trying to retrieve
// children or grandchildren. This has two implications. We can't
// search at this level, so we have to "get" then loop/match/return.
// It also means that to get the calling function we have to change
// the depth as there is an extra function inbetween.
$depth = 2;
$functionType = 'search';

if ($scopedToItem) {
$depth = 3;
$functionType = 'get';
}

// Find the item type.
$itemType = $this->functionToType(
$this->getCallingFunction()
$this->getCallingFunction($depth)
);

// Find the search method and make sure it exists in the search
// class.
$searchMethod = sprintf('search%ss', ucfirst($itemType));
$searchMethod = sprintf('%s%ss', $functionType, ucfirst($itemType));

if (method_exists($this, $searchMethod)) {
foreach ($this->{$searchMethod}($polymorphicData) as $item) {
Expand Down

0 comments on commit 02029ee

Please sign in to comment.