diff --git a/module/VuFind/src/VuFind/Controller/CoverController.php b/module/VuFind/src/VuFind/Controller/CoverController.php index f23cd560cbbe5f033745eed78bcb93e47d525405..ff4c8b2ab387ff946962dd6f2cf5f238653b632f 100644 --- a/module/VuFind/src/VuFind/Controller/CoverController.php +++ b/module/VuFind/src/VuFind/Controller/CoverController.php @@ -106,6 +106,28 @@ class CoverController extends AbstractBase return $this->proxy; } + /** + * Convert image parameters into an array for use by the image loader. + * + * @return array + */ + protected function getImageParams() + { + $params = $this->params(); // shortcut for readability + return [ + // Legacy support for "isn" param which has been superseded by isbn: + 'isbn' => $params()->fromQuery('isbn') ?: $params()->fromQuery('isn'), + 'size' => $params()->fromQuery('size'), + 'type' => $params()->fromQuery('contenttype'), + 'title' => $params()->fromQuery('title'), + 'author' => $params()->fromQuery('author'), + 'callnumber' => $params()->fromQuery('callnumber'), + 'issn' => $params()->fromQuery('issn'), + 'oclc' => $params()->fromQuery('oclc'), + 'upc' => $params()->fromQuery('upc'), + ]; + } + /** * Send image data for display in the view * @@ -131,18 +153,7 @@ class CoverController extends AbstractBase } // Default case -- use image loader: - $this->getLoader()->loadImage( - // Legacy support for "isn" param which has been superseded by isbn: - $this->params()->fromQuery('isbn', $this->params()->fromQuery('isn')), - $this->params()->fromQuery('size'), - $this->params()->fromQuery('contenttype'), - $this->params()->fromQuery('title'), - $this->params()->fromQuery('author'), - $this->params()->fromQuery('callnumber'), - $this->params()->fromQuery('issn'), - $this->params()->fromQuery('oclc'), - $this->params()->fromQuery('upc') - ); + $this->getLoader()->loadImage($this->getImageParams()); return $this->displayImage(); } diff --git a/module/VuFind/src/VuFind/Cover/Loader.php b/module/VuFind/src/VuFind/Cover/Loader.php index 988ce2cc2e3e5f5cf48dd616c62f769389525c5d..f5c82f54a9a74ef21614f6ba33e7b62d1fdbd30e 100644 --- a/module/VuFind/src/VuFind/Cover/Loader.php +++ b/module/VuFind/src/VuFind/Cover/Loader.php @@ -162,34 +162,90 @@ class Loader extends \VuFind\ImageLoader ); } + /** + * Get default settings for loadImage(). + * + * @return array + */ + protected function getDefaultSettings() + { + return [ + 'isbn' => null, + 'size' => 'small', + 'type' => null, + 'title' => null, + 'author' => null, + 'callnumber' => null, + 'issn' => null, + 'oclc' => null, + 'upc' => null, + ]; + } + + /** + * Translate legacy function arguments into new-style array. + * + * @param array $args Function arguments + * + * @return array + */ + protected function getImageSettingsFromLegacyArgs($args) + { + return [ + 'isbn' => $args[0], + 'size' => $args[1], + 'type' => $args[2], + 'title' => $args[3], + 'author' => $args[4], + 'callnumber' => $args[5], + 'issn' => $args[6], + 'oclc' => $args[7], + 'upc' => $args[8], + ]; + } + + /** + * Support method for loadImage() -- sanitize and store some key values. + * + * @param array $settings Settings from loadImage (with missing defaults + * already filled in). + */ + protected function storeSanitizedSettings($settings) + { + $this->isbn = new ISBN($settings['isbn']); + if (!empty($settings['issn'])) { + $rawissn = preg_replace('/[^0-9X]/', '', strtoupper($settings['issn'])); + $this->issn = substr($rawissn, 0, 8); + } else { + $this->issn = null; + } + $this->oclc = $settings['oclc']; + $this->upc = $settings['upc']; + $this->type = preg_replace('/[^a-zA-Z]/', '', $settings['type']); + $this->size = $settings['size']; + } + /** * Load an image given an ISBN and/or content type. * - * @param string $isbn ISBN - * @param string $size Requested size - * @param string $type Content type - * @param string $title Title of book (for dynamic covers) - * @param string $author Author of the book (for dynamic covers) - * @param string $callnumber Callnumber (unique id for dynamic covers) - * @param string $issn ISSN - * @param string $oclc OCLC number - * @param string $upc UPC number + * @param array $settings Array of settings used to calculate a cover; may + * contain any or all of these keys: 'isbn' (ISBN), 'size' (requested size), + * 'type' (content type), 'title' (title of book, for dynamic covers), 'author' + * (author of book, for dynamic covers), 'callnumber' (unique ID, for dynamic + * covers), 'issn' (ISSN), 'oclc' (OCLC number), 'upc' (UPC number). * * @return void */ - public function loadImage($isbn = null, $size = 'small', $type = null, - $title = null, $author = null, $callnumber = null, $issn = null, - $oclc = null, $upc = null - ) { - // Sanitize parameters: - $this->isbn = new ISBN($isbn); - $this->issn = empty($issn) - ? null - : substr(preg_replace('/[^0-9X]/', '', strtoupper($issn)), 0, 8); - $this->oclc = $oclc; - $this->upc = $upc; - $this->type = preg_replace("/[^a-zA-Z]/", "", $type); - $this->size = $size; + public function loadImage($settings = []) + { + // Load settings from legacy function parameters if they are not passed + // in as an array: + $settings = is_array($settings) + ? array_merge($this->getDefaultSettings(), $settings) + : $this->getImageSettingsFromLegacyArgs(func_get_args()); + + // Store sanitized versions of some parameters for future reference: + $this->storeSanitizedSettings($settings); // Display a fail image unless our parameters pass inspection and we // are able to display an ISBN or content-type-based image. @@ -201,8 +257,9 @@ class Loader extends \VuFind\ImageLoader if (isset($this->config->Content->makeDynamicCovers) && false !== $this->config->Content->makeDynamicCovers ) { - $this->image = $this->getCoverGenerator() - ->generate($title, $author, $callnumber); + $this->image = $this->getCoverGenerator()->generate( + $settings['title'], $settings['author'], $settings['callnumber'] + ); $this->contentType = 'image/png'; } else { $this->loadUnavailable();