diff --git a/config/vufind/config.ini b/config/vufind/config.ini index 6e172c934ae0e652d2eaffea489ddec5029c690b..ec31e0cf18215fa5a331ddb6e6146ad376c187ca 100644 --- a/config/vufind/config.ini +++ b/config/vufind/config.ini @@ -522,6 +522,15 @@ noCoverAvailableImage = images/noCover2.gif ; You can select from Syndetics or SyndeticsPlus ;excerpts = Syndetics:MySyndeticsId,SyndeticsPlus:MySyndeticsId +; This setting can be used to hide review/excerpt tabs on the record page when +; no content is available from the providers. By default it is turned off. You +; can turn it on for all relevant tabs by setting it to true, or you can turn +; it on for a comma-separated list of values (e.g. "reviews" or "excerpts" or +; "reviews,excerpts") for selective activation. Note that hiding empty tabs will +; make your record pages slower, since it will require extra communication with +; content providers. +;hide_if_empty = reviews,excerpts + ; You can select from Syndetics or SyndeticsPlus ;authorNotes = Syndetics:MySyndeticsId,SyndeticsPlus:MySyndeticsId diff --git a/module/VuFind/src/VuFind/RecordTab/AbstractContent.php b/module/VuFind/src/VuFind/RecordTab/AbstractContent.php index 7e04633df7fde73db973653b2d0d98e50c02a5b5..298fbc1105428042ded868fde83d1cd6245749cc 100644 --- a/module/VuFind/src/VuFind/RecordTab/AbstractContent.php +++ b/module/VuFind/src/VuFind/RecordTab/AbstractContent.php @@ -26,6 +26,7 @@ * @link http://vufind.org/wiki/vufind2:record_tabs Wiki */ namespace VuFind\RecordTab; +use VuFind\Content\Loader; /** * Reviews tab @@ -41,18 +42,35 @@ abstract class AbstractContent extends AbstractBase /** * Content loader * - * @var \VuFind\Content\Loader + * @var Loader */ protected $loader; + /** + * Should we hide the tab if no content is found? + * + * @var bool + */ + protected $hideIfEmpty; + + /** + * Cache for results. + * + * @var array + */ + protected $results = null; + /** * Constructor * - * @param \VuFind\Content\Loader $loader Content loader (omit to disable) + * @param Loader $loader Content loader (null to disable) + * @param bool $hideIfEmpty Should we hide the tab if no content is found? + * (Note that turning this on has performance implications). */ - public function __construct(\VuFind\Content\Loader $loader = null) + public function __construct(Loader $loader = null, $hideIfEmpty = false) { $this->loader = $loader; + $this->hideIfEmpty = $hideIfEmpty; } /** @@ -62,11 +80,14 @@ abstract class AbstractContent extends AbstractBase */ public function isActive() { - if (null === $this->loader) { - return false; - } - $isbn = $this->getRecordDriver()->tryMethod('getCleanISBN'); - return !empty($isbn); + // Depending on the "hide if empty" setting, we either want to check if + // the API results are empty (an expensive operation) or if we have an + // ISBN that can be used to retrieve API results (much less expensive, + // but also less effective at consistently suppressing empty tabs). + $check = $this->hideIfEmpty + ? $this->getContent() + : $this->getRecordDriver()->tryMethod('getCleanISBN'); + return !(null === $this->loader || empty($check)); } /** @@ -76,8 +97,11 @@ abstract class AbstractContent extends AbstractBase */ public function getContent() { - $isbn = $this->getRecordDriver()->tryMethod('getCleanISBN'); - return (null === $this->loader || empty($isbn)) - ? array() : $this->loader->loadByIsbn($isbn); + if (null === $this->results) { + $isbn = $this->getRecordDriver()->tryMethod('getCleanISBN'); + $this->results = (null === $this->loader || empty($isbn)) + ? array() : $this->loader->loadByIsbn($isbn); + } + return $this->results; } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/RecordTab/Factory.php b/module/VuFind/src/VuFind/RecordTab/Factory.php index 7173fd43f0e4c678f336885a8a77fe2a3a9765b8..c82f0ad0e8065b63a982a0514e2082a64ddd6761 100644 --- a/module/VuFind/src/VuFind/RecordTab/Factory.php +++ b/module/VuFind/src/VuFind/RecordTab/Factory.php @@ -86,7 +86,32 @@ class Factory } else { $loader = null; } - return new Excerpt($loader); + return new Excerpt($loader, static::getHideSetting($config, 'excerpts')); + } + + /** + * Support method for construction of AbstractContent objects -- should we + * hide this tab if it is empty? + * + * @param \Zend\Config\Config $config VuFind configuration + * @param string $tab Name of tab to check config for + * + * @return bool + */ + protected static function getHideSetting(\Zend\Config\Config $config, $tab) + { + $setting = isset($config->Content->hide_if_empty) + ? $config->Content->hide_if_empty : false; + if ($setting === true || $setting === false + || $setting === 1 || $setting === 0 + ) { + return (bool)$setting; + } + if ($setting === 'true' || $setting === '1') { + return true; + } + $hide = array_map('trim', array_map('strtolower', explode(',', $setting))); + return in_array(strtolower($tab), $hide); } /** @@ -157,7 +182,7 @@ class Factory } else { $loader = null; } - return new Reviews($loader); + return new Reviews($loader, static::getHideSetting($config, 'reviews')); } /**