Skip to content
This repository has been archived by the owner on Jan 10, 2021. It is now read-only.

Commit

Permalink
FIX, correcting a number of issues introduced with SS4.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Glasl committed Dec 6, 2017
1 parent b6d587f commit cdd11a2
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The following is an example configuration, where `ElasticSearch` extends the abs
```yaml
nglasl\extensible\ExtensibleSearchPage:
custom_search_engines:
ElasticSearch: 'Elastic'
nglasl\extensible\ElasticSearch: 'Elastic'
```

### Search Form
Expand Down
11 changes: 8 additions & 3 deletions client/javascript/extensible-search-approval.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@
page.on('click', '#Form_EditForm_Suggestions input.approved', function(event) {

event.stopPropagation();
update($(this));

// Make sure the edit form doesn't detect changes.
// This is required to ensure the events are triggered in the correct order.

$('#Form_EditForm').removeClass('changed');
update($(this));
setTimeout(function() {

// Make sure the edit form doesn't detect changes.

$('form#Form_EditForm').removeClass('changed');
}, 0);
});

})(jQuery);
7 changes: 5 additions & 2 deletions src/controllers/ExtensibleSearchAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public function toggleSuggestionApproved($request) {

// Display an appropriate CMS notification.

$this->getResponse()->setStatusDescription($status);
$response = $this->getResponse();
$response->setStatusCode(204);
$response->setStatusDescription($status);
return $status;
}
else {
Expand All @@ -64,7 +66,8 @@ public function toggleSuggestionApproved($request) {

public function getSuggestions($request) {

if(Config::inst()->get(ExtensibleSearchSuggestion::class, 'enable_suggestions') && ($suggestions = $this->service->getSuggestions($request->getVar('term'), $request->getVar('page')))) {
if(Config::inst()->get(ExtensibleSearchSuggestion::class, 'enable_suggestions')) {
$suggestions = $this->service->getSuggestions($request->getVar('term'), $request->getVar('page'));

// Return the search suggestions as JSON.

Expand Down
23 changes: 14 additions & 9 deletions src/controllers/ExtensibleSearchPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public function index() {
// Instantiate the search engine specific templates.

if($engine !== 'Full-Text') {
$explode = explode('\\', $engine);
$explode = end($explode);
$templates = array_merge(array(
$engine,
"{$engine}Page"
$explode,
"{$explode}Page"
), $templates);
}

Expand Down Expand Up @@ -258,8 +260,9 @@ public function getSearchResults($data = null, $form = null) {
if(!isset($data['Search'])) {
$data['Search'] = '';
}
$search = $data['Search'];
$request = $this->getRequest();
$request->offsetSet('Search', $data['Search']);
$request->offsetSet('Search', $search);

// Determine whether the remaining search parameters have been passed through.

Expand Down Expand Up @@ -307,14 +310,16 @@ public function getSearchResults($data = null, $form = null) {

// Instantiate the search engine specific templates.

$explode = explode('\\', $engine);
$explode = end($explode);
$templates = array_merge(array(
"{$engine}_results",
"{$engine}Page_results",
"{$explode}_results",
"{$explode}Page_results",
'ExtensibleSearch_results',
'ExtensibleSearchPage_results',
'Page_results',
$engine,
"{$engine}Page"
$explode,
"{$explode}Page"
), $templates);
}

Expand Down Expand Up @@ -377,11 +382,11 @@ public function getSearchResults($data = null, $form = null) {

// Determine whether analytics are to be suppressed.

if($request->getVar('analytics') !== 'false') {
if($search && ($request->getVar('analytics') !== 'false')) {

// Update the search page specific analytics.

$this->service->logSearch($data['Search'], $count, microtime(true) - $time, $engine, $page->ID);
$this->service->logSearch($search, $count, microtime(true) - $time, $engine, $page->ID);
}

// Display the search form results.
Expand Down
20 changes: 7 additions & 13 deletions src/objects/ExtensibleSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ExtensibleSearch extends DataObject {
private static $default_sort = 'ID DESC';

private static $summary_fields = array(
'TimeSummary',
'Created.Nice',
'Term',
'TimeTakenSummary',
'Results',
Expand All @@ -41,28 +41,22 @@ class ExtensibleSearch extends DataObject {

private static $enable_analytics = true;

public function canView($member = null) {

return true;
}

public function fieldLabels($includerelations = true) {

return array(
'TimeSummary' => _t('EXTENSIBLE_SEARCH.TIME', 'Time'),
'Created.Nice' => _t('EXTENSIBLE_SEARCH.TIME', 'Time'),
'Term' => _t('EXTENSIBLE_SEARCH.SEARCH_TERM', 'Search Term'),
'TimeTakenSummary' => _t('EXTENSIBLE_SEARCH.TIME_TAKEN', 'Time Taken (s)'),
'Results' => _t('EXTENSIBLE_SEARCH.RESULTS', 'Results'),
'SearchEngineSummary' => _t('EXTENSIBLE_SEARCH.SEARCH_ENGINE', 'Search Engine')
);
}

/**
* Retrieve the log time for display purposes.
*
* @return string
*/

public function getTimeSummary() {

return $this->dbObject('Created')->Format('jS F Y, g:i:sa');
}

/**
* Retrieve the search time for display purposes.
*
Expand Down
9 changes: 1 addition & 8 deletions src/objects/ExtensibleSearchSuggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function getCMSFields() {

$fields = parent::getCMSFields();
$fields->removeByName('ExtensibleSearchPageID');
$fields->dataFieldByName('Approved')->setTitle(_t('EXTENSIBLE_SEARCH.APPROVED?', 'Approved?'));

// Make sure the search suggestions and frequency are read only.

Expand All @@ -112,14 +113,6 @@ public function getCMSFields() {
}
$fields->removeByName('Frequency');

// Update the approved flag positioning.

$fields->removeByName('Approved');
$fields->addFieldToTab('Root.Main', $approved = FieldGroup::create(
_t('EXTENSIBLE_SEARCH.APPROVED?', 'Approved?')
)->addExtraClass('approved wrapper'));
$approved->push($this->getApprovedField());

// Allow extension customisation.

$this->extend('updateExtensibleSearchSuggestionCMSFields', $fields);
Expand Down
61 changes: 37 additions & 24 deletions src/pages/ExtensibleSearchPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace nglasl\extensible;

use SilverStripe\CMS\Controllers\CMSPageHistoryController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_Base;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldExportButton;
Expand Down Expand Up @@ -88,27 +91,13 @@ public function requireDefaultRecords() {

// Determine whether pages should be created.

if(self::config()->create_default_pages) {

// The problem is that class name mapping happens after this, but we need it right now to query pages.

if(!SiteTree::get()->filter('ClassName', array(
ExtensibleSearchPage::class,
'ExtensibleSearchPage'
))->first()) {

// Instantiate an extensible search page.

$page = ExtensibleSearchPage::create();
$page->Title = 'Search Page';
$page->write();
DB::alteration_message('"Default" Extensible Search Page', 'created');
}
if(!self::config()->create_default_pages) {
return;
}

// This is required to support multiple sites.

else if(ClassInfo::exists(Multisites::class)) {
if(ClassInfo::exists(Multisites::class)) {
foreach(Site::get() as $site) {

// The problem is that class name mapping happens after this, but we need it right now to query pages.
Expand All @@ -131,6 +120,23 @@ public function requireDefaultRecords() {
}
}
}
else {

// The problem is that class name mapping happens after this, but we need it right now to query pages.

if(!SiteTree::get()->filter('ClassName', array(
ExtensibleSearchPage::class,
'ExtensibleSearchPage'
))->first()) {

// Instantiate an extensible search page.

$page = ExtensibleSearchPage::create();
$page->Title = 'Search Page';
$page->write();
DB::alteration_message('"Default" Extensible Search Page', 'created');
}
}
Versioned::set_stage($stage);
}

Expand Down Expand Up @@ -261,9 +267,13 @@ public function getCMSFields() {
), 'Title');
}

// The history view shouldn't show the following, as they're not versioned.

$pageHistory = Controller::has_curr() && (Controller::curr() instanceof CMSPageHistoryController);

// Determine whether analytics have been enabled.

if($configuration->get(ExtensibleSearch::class, 'enable_analytics')) {
if($configuration->get(ExtensibleSearch::class, 'enable_analytics') && !$pageHistory) {

// Instantiate the analytic summary.

Expand All @@ -283,8 +293,10 @@ public function getCMSFields() {

// Instantiate an export button.

$summaryConfiguration->addComponent($summaryExport = new GridFieldExportButton());
$summaryExport->setExportColumns($summaryDisplay);
if($this->getHistorySummary()->exists()) {
$summaryConfiguration->addComponent($summaryExport = new GridFieldExportButton());
$summaryExport->setExportColumns($summaryDisplay);
}

// Update the custom summary fields to be sortable.

Expand All @@ -305,7 +317,7 @@ public function getCMSFields() {
// Update the custom summary fields to be sortable.

$historyConfiguration->getComponentByType(GridFieldSortableHeader::class)->setFieldSorting(array(
'TimeSummary' => 'Created',
'Created.Nice' => 'Created',
'TimeTakenSummary' => 'Time',
'SearchEngineSummary' => 'SearchEngine'
));
Expand All @@ -314,7 +326,7 @@ public function getCMSFields() {
// Instantiate the archived collection of search analytics.

$archives = $this->Archives();
if($archives->exists()) {
if($archives->exists() && $archives->first()->canView()) {
$fields->findOrMakeTab('Root.SearchAnalytics.Archives', _t('EXTENSIBLE_SEARCH.ARCHIVES', 'Archives'));
$fields->addFieldToTab('Root.SearchAnalytics.Archives', GridField::create(
'Archives',
Expand All @@ -334,7 +346,7 @@ public function getCMSFields() {

// Determine whether suggestions have been enabled.

if($configuration->get(ExtensibleSearchSuggestion::class, 'enable_suggestions')) {
if($configuration->get(ExtensibleSearchSuggestion::class, 'enable_suggestions') && !$pageHistory) {

// Appropriately restrict the approval functionality.

Expand All @@ -350,7 +362,8 @@ public function getCMSFields() {
'Suggestions',
_t('EXTENSIBLE_SEARCH.SUGGESTIONS', 'Suggestions'),
$this->Suggestions(),
$suggestionsConfiguration = GridFieldConfig_RecordEditor::create()
$suggestionsConfiguration =
singleton(ExtensibleSearchSuggestion::class)->canEdit() ? GridFieldConfig_RecordEditor::create() : GridFieldConfig_Base::create()
)->setModelClass(ExtensibleSearchSuggestion::class));

// Update the custom summary fields to be sortable.
Expand Down
2 changes: 1 addition & 1 deletion src/services/ExtensibleSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function getSuggestions($term, $pageID, $limit = 5, $approved = true) {
return array_unique($suggestions->column('Term'));
}
}
return null;
return array();
}

}
3 changes: 3 additions & 0 deletions tests/UnitTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static function setUpBeforeClass() {
$config->merge(SiteTree::class, 'create_table_options', array(
'MySQLDatabase' => 'ENGINE=MyISAM'
));

// This extension throws errors when it has already been applied.

if(!SiteTree::has_extension(FulltextSearchable::class)) {
SiteTree::add_extension(FulltextSearchable::class . "('Title', 'MenuTitle', 'Content', 'MetaDescription')");
}
Expand Down

0 comments on commit cdd11a2

Please sign in to comment.