diff --git a/Module.php b/Module.php index 1962e4d4..057900f7 100644 --- a/Module.php +++ b/Module.php @@ -442,46 +442,57 @@ public function handleSiteSettingsFilters(Event $event) */ public function addHeaders(Event $event) { - // The search field is added via a js hack, because the admin layout - // doesn't use a partial or a trigger for the sidebar. + // The admin search field is added via a js hack, because the admin + // layout doesn't use a partial or a trigger for the sidebar. + $view = $event->getTarget(); $status = $view->status(); - if ($status->isAdminRequest()) { - $searchMainPage = $view->setting('search_main_page'); - if (!$searchMainPage) { - return; - } - $assetUrl = $view->plugin('assetUrl'); - $view->headLink()->appendStylesheet($assetUrl('css/search-admin-search.css', 'Search')); - $view->headScript() - ->appendScript(sprintf('var searchUrl = %s;', json_encode($searchMainPage, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE))) - ->appendFile($assetUrl('js/search-admin-search.js', 'Search'), 'text/javascript', ['defer' => 'defer']); - $searchPage = $view->api()->searchOne('search_pages', ['path' => $searchMainPage])->getContent(); - } elseif ($status->isSiteRequest()) { + if ($status->isSiteRequest()) { $params = $view->params()->fromRoute(); - if ($params['controller'] = \Search\Controller\IndexController::class) { + if ($params['controller'] === \Search\Controller\IndexController::class) { $searchPage = @$params['id']; } else { $searchPage = $view->siteSetting('search_main_page'); } - if (!$searchPage) { - return; - } - $searchPage = $view->api()->searchOne('search_pages', ['id' => $searchPage])->getContent(); + } elseif ($status->isAdminRequest()) { + $searchPage = $view->setting('search_main_page'); } else { return; } + if (!$searchPage) { + return; + } + /** @var \Search\Api\Representation\SearchPageRepresentation $searchPage */ - if (!$searchPage || !($formAdapter = $searchPage->formAdapter())) { + $searchPage = $view->api()->searchOne('search_pages', ['id' => $searchPage])->getContent(); + if (!$searchPage) { + return; + } + + if ($status->isAdminRequest()) { + $basePath = $view->plugin('basePath'); + $assetUrl = $view->plugin('assetUrl'); + $view->headLink() + ->appendStylesheet($assetUrl('css/search-admin-search.css', 'Search')); + $view->headScript() + ->appendScript(sprintf('var searchUrl = %s;', json_encode($basePath('admin/' . $searchPage->path()), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE))) + ->appendFile($assetUrl('js/search-admin-search.js', 'Search'), 'text/javascript', ['defer' => 'defer']); + } + + $formAdapter = $searchPage->formAdapter(); + if (!$formAdapter) { return; } + $partialHeaders = $formAdapter->getFormPartialHeaders(); - if ($partialHeaders) { - // No echo: it's just a preload. - $view->partial($partialHeaders); + if (!$partialHeaders) { + return; } + + // No echo: it should just be a preload. + $view->partial($partialHeaders); } protected function installResources() diff --git a/config/module.config.php b/config/module.config.php index 1908bf88..c8b98c57 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -223,7 +223,7 @@ ], 'search' => [ 'settings' => [ - 'search_main_page' => '', + 'search_main_page' => null, 'search_pages' => [], 'search_api_page' => '', 'search_batch_size' => 100, diff --git a/config/module.ini b/config/module.ini index d2966cd8..e0ba2922 100644 --- a/config/module.ini +++ b/config/module.ini @@ -8,5 +8,5 @@ author_link = "https://github.com/Daniel-KM" module_link = "https://github.com/Daniel-KM/Omeka-S-module-Search" support_link = "https://github.com/Daniel-KM/Omeka-S-module-Search/issues" configurable = false -version = "3.5.12.1" +version = "3.5.12.2" omeka_version_constraint = "^1.3.0 || ^2.0.0" diff --git a/data/scripts/upgrade.php b/data/scripts/upgrade.php index a7f1444f..28a89afc 100644 --- a/data/scripts/upgrade.php +++ b/data/scripts/upgrade.php @@ -193,3 +193,18 @@ $siteSettings->set('search_pages', $searchPages); } } + +if (version_compare($oldVersion, '3.5.12.2', '<')) { + $mainSearchPage = $settings->get('search_main_page'); + if ($mainSearchPage) { + $mainSearchPage = basename($mainSearchPage); + // The api for search_pages is not available during upgrade. + $sql = <<fetchColumn($sql, ['search_page' => $mainSearchPage], 0); + $settings->set('search_main_page', $id ? (string) $id : null); + } +} diff --git a/src/Form/SettingsFieldset.php b/src/Form/SettingsFieldset.php index 66fa4a41..437c53e5 100644 --- a/src/Form/SettingsFieldset.php +++ b/src/Form/SettingsFieldset.php @@ -2,7 +2,6 @@ namespace Search\Form; use Omeka\View\Helper\Api; -use Zend\View\Helper\BasePath; use Zend\Form\Element; use Zend\Form\Fieldset; @@ -13,31 +12,19 @@ class SettingsFieldset extends Fieldset */ protected $api; - /** - * @var BasePath - */ - protected $basePath; - protected $label = 'Search (admin board)'; // @translate public function init() { - $api = $this->getApi(); - $basePath = $this->getBasePath(); - $adminBasePath = $basePath('admin/'); - /** @var \Search\Api\Representation\SearchPageRepresentation[] $pages */ - $pages = $api->search('search_pages')->getContent(); + $pages = $this->api->search('search_pages')->getContent(); $valueOptions = []; - $pageOptions = []; - $apiOptions = []; foreach ($pages as $page) { - $label = sprintf('%s (/%s)', $page->name(), $page->path()); - $valueOptions[$adminBasePath . $page->path()] = $label; - $pageOptions[$page->id()] = $label; + $labelSearchPage = sprintf('%s (/%s)', $page->name(), $page->path()); + $valueOptions[$page->id()] = $labelSearchPage; if ($page->formAdapter() instanceof \Search\FormAdapter\ApiFormAdapter) { - $apiOptions[$page->id()] = $label; + $apiOptions[$page->id()] = $labelSearchPage; } } @@ -60,7 +47,7 @@ public function init() 'type' => Element\MultiCheckbox::class, 'options' => [ 'label' => 'Available search pages', // @translate - 'value_options' => $pageOptions, + 'value_options' => $valueOptions, ], 'attributes' => [ 'id' => 'search_pages', @@ -103,29 +90,4 @@ public function setApi(Api $api) $this->api = $api; return $this; } - - /** - * @return \Omeka\View\Helper\Api - */ - public function getApi() - { - return $this->api; - } - - /** - * @param BasePath $basePath - */ - public function setBasePath(BasePath $basePath) - { - $this->basePath = $basePath; - return $this; - } - - /** - * @return \Zend\View\Helper\BasePath - */ - public function getBasePath() - { - return $this->basePath; - } } diff --git a/src/Form/SiteSettingsFieldset.php b/src/Form/SiteSettingsFieldset.php index 914ff1ce..baf73bae 100644 --- a/src/Form/SiteSettingsFieldset.php +++ b/src/Form/SiteSettingsFieldset.php @@ -20,10 +20,8 @@ class SiteSettingsFieldset extends Fieldset public function init() { - $api = $this->getApi(); - /** @var \Search\Api\Representation\SearchPageRepresentation[] $pages */ - $pages = $api->search('search_pages')->getContent(); + $pages = $this->api->search('search_pages')->getContent(); $valueOptions = []; foreach ($pages as $page) { @@ -77,12 +75,4 @@ public function setApi(Api $api) $this->api = $api; return $this; } - - /** - * @return \Omeka\View\Helper\Api - */ - public function getApi() - { - return $this->api; - } } diff --git a/src/Service/Form/SettingsFieldsetFactory.php b/src/Service/Form/SettingsFieldsetFactory.php index 80e11134..86626341 100644 --- a/src/Service/Form/SettingsFieldsetFactory.php +++ b/src/Service/Form/SettingsFieldsetFactory.php @@ -12,7 +12,6 @@ public function __invoke(ContainerInterface $services, $requestedName, array $op $fieldset = new SettingsFieldset(null, $options); $viewHelpers = $services->get('ViewHelperManager'); $fieldset->setApi($viewHelpers->get('api')); - $fieldset->setBasePath($viewHelpers->get('basePath')); return $fieldset; } } diff --git a/src/View/Helper/SearchForm.php b/src/View/Helper/SearchForm.php index 229e7356..995f2e7a 100644 --- a/src/View/Helper/SearchForm.php +++ b/src/View/Helper/SearchForm.php @@ -50,10 +50,21 @@ protected function initSearchForm(SearchPageRepresentation $searchPage = null, $ $view = $this->getView(); $isAdmin = $view->status()->isAdminRequest(); if (empty($searchPage)) { - $searchPageId = $isAdmin - ? $view->setting('search_main_page') - : $view->siteSetting('search_main_page'); - /** @var \Search\Api\Representation\SearchPageRepresentation $searchPage */ + // If it is on a search page route, use the id, else use the setting. + $params = $view->params()->fromRoute(); + $setting = $isAdmin + ? $view->getHelperPluginManager()->get('setting') + : $view->getHelperPluginManager()->get('siteSetting'); + if ($params['controller'] === 'Search\Controller\IndexController') { + $searchPageId = $params['id']; + // Check if this search page is allowed. + if (!in_array($searchPageId, $setting('search_pages'))) { + $searchPageId = 0; + } + } + if (empty($searchPageId)) { + $searchPageId = $setting('search_main_page'); + } $this->searchPage = $view->api()->searchOne('search_pages', ['id' => (int) $searchPageId])->getContent(); } else { $this->searchPage = $searchPage;