From f6a9941fe02c954559a9cf2d4a7c8fafd01bad6e Mon Sep 17 00:00:00 2001 From: Nick Bartkowiak Date: Mon, 24 Dec 2012 14:01:42 -0600 Subject: [PATCH] Added a polymorphic function for retrieving a single Plex library item by rating key, key, or by exact title match. --- Server/Library/SectionAbstract.php | 77 +++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/Server/Library/SectionAbstract.php b/Server/Library/SectionAbstract.php index c39f5c2..3a195ad 100644 --- a/Server/Library/SectionAbstract.php +++ b/Server/Library/SectionAbstract.php @@ -1,4 +1,4 @@ -getItems( + sprintf( + '%s/%d', + Plex_Server_Library::ENDPOINT_METADATA, + $polymorphicData + ) + ) + ); + } else if (strpos($polymorphicData, Plex_Server_Library::ENDPOINT_METADATA) + != FALSE) { + // If the single item endpoint appears in the polymorphic data then + // is assumed we are dealing with a key, which is already a valid + // endpoint. + + // A key will contain the library endpoint, which will be added back + // by our getItems method, so we strip it out here. The buildUrl + // method will handle stripping out and double slashes caused by + // this. + $endpoint = str_replace( + Plex_Server_Library::ENDPOINT_LIBRARY, + '', + $polymorphicData + ); + + return reset($this->getItems($endpoint)); + } else { + + // If we don't have a rating key or a key then we just assume we're + // doing an exact title match. + + // Find the item type. + $itemType = $this->functionToType( + $this->getCallingFunction() + ); + + // Find the search method and make sure it exists in the search + // class. + $searchMethod = sprintf('search%ss', ucfirst($itemType)); + + if (method_exists($this, $searchMethod)) { + foreach ($this->{$searchMethod}($polymorphicData) as $item) { + if ($item->getTitle() === $polymorphicData) { + return $item; + } + } + } + + // Tried to do an exact title match and came up empty. + return FALSE; + } + } + /** * Returns a list of collections for the child class's section. We use * makeCall directly here, because we want to return just the raw array of