Skip to content

Commit

Permalink
Fixed search in admin.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Mar 15, 2020
1 parent 9a0fc14 commit a523bca
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 84 deletions.
57 changes: 34 additions & 23 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
],
'search' => [
'settings' => [
'search_main_page' => '',
'search_main_page' => null,
'search_pages' => [],
'search_api_page' => '',
'search_batch_size' => 100,
Expand Down
2 changes: 1 addition & 1 deletion config/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 15 additions & 0 deletions data/scripts/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<SQL
SELECT id
FROM search_page
WHERE path = :search_page;
SQL;
$id = $connection->fetchColumn($sql, ['search_page' => $mainSearchPage], 0);
$settings->set('search_main_page', $id ? (string) $id : null);
}
}
48 changes: 5 additions & 43 deletions src/Form/SettingsFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}

Expand All @@ -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',
Expand Down Expand Up @@ -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;
}
}
12 changes: 1 addition & 11 deletions src/Form/SiteSettingsFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion src/Service/Form/SettingsFieldsetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 15 additions & 4 deletions src/View/Helper/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a523bca

Please sign in to comment.