@@ -11,120 +11,120 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Quicklink extends DataObject |
| 13 | 13 | { |
| 14 | - private static $db = [ |
|
| 15 | - 'Name' => 'Varchar(255)', |
|
| 16 | - 'ExternalLink' => 'Varchar(255)', |
|
| 17 | - 'SortOrder' => 'Int', |
|
| 18 | - ]; |
|
| 19 | - |
|
| 20 | - private static $has_one = [ |
|
| 21 | - 'Parent' => BaseHomePage::class, |
|
| 22 | - 'InternalLink' => SiteTree::class, |
|
| 23 | - ]; |
|
| 24 | - |
|
| 25 | - private static $summary_fields = [ |
|
| 26 | - 'Name' => 'Name', |
|
| 27 | - 'InternalLink.Title' => 'Internal Link', |
|
| 28 | - 'ExternalLink' => 'External Link', |
|
| 29 | - ]; |
|
| 30 | - |
|
| 31 | - private static $table_name = 'Quicklink'; |
|
| 32 | - |
|
| 33 | - public function fieldLabels($includerelations = true) |
|
| 34 | - { |
|
| 35 | - $labels = parent::fieldLabels($includerelations); |
|
| 36 | - $labels['Name'] = _t('Quicklink.NameLabel', 'Name'); |
|
| 37 | - $labels['ExternalLink'] = _t('Quicklink.ExternalLinkLabel', 'External Link'); |
|
| 38 | - $labels['SortOrder'] = _t('Quicklink.SortOrderLabel', 'Sort Order'); |
|
| 39 | - $labels['ParentID'] = _t('Quicklink.ParentRelationLabel', 'Parent'); |
|
| 40 | - $labels['InternalLinkID'] = _t('Quicklink.InternalLinkLabel', 'Internal Link'); |
|
| 41 | - |
|
| 42 | - return $labels; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function getLink() |
|
| 46 | - { |
|
| 47 | - if ($this->ExternalLink) { |
|
| 48 | - $url = parse_url($this->ExternalLink); |
|
| 49 | - |
|
| 50 | - // if no scheme set in the link, default to http |
|
| 51 | - if (!isset($url['scheme'])) { |
|
| 52 | - return 'http://' . $this->ExternalLink; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - return $this->ExternalLink; |
|
| 56 | - } elseif ($this->InternalLinkID) { |
|
| 57 | - return $this->InternalLink()->Link(); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function canCreate($member = null, $context = []) |
|
| 62 | - { |
|
| 63 | - return $this->Parent()->canCreate($member, $context); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function canEdit($member = null) |
|
| 67 | - { |
|
| 68 | - return $this->Parent()->canEdit($member); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function canDelete($member = null) |
|
| 72 | - { |
|
| 73 | - return $this->Parent()->canDelete($member); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function canView($member = null) |
|
| 77 | - { |
|
| 78 | - return $this->Parent()->canView($member); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function getCMSFields() |
|
| 82 | - { |
|
| 83 | - $fields = parent::getCMSFields(); |
|
| 84 | - |
|
| 85 | - $fields->removeByName('ParentID'); |
|
| 86 | - |
|
| 87 | - $externalLinkField = $fields->fieldByName('Root.Main.ExternalLink'); |
|
| 88 | - |
|
| 89 | - $fields->removeByName('ExternalLink'); |
|
| 90 | - $fields->removeByName('InternalLinkID'); |
|
| 91 | - $fields->removeByName('SortOrder'); |
|
| 92 | - $externalLinkField->addExtraClass('noBorder'); |
|
| 93 | - |
|
| 94 | - $fields->addFieldToTab('Root.Main', CompositeField::create( |
|
| 95 | - array( |
|
| 96 | - TreeDropdownField::create( |
|
| 97 | - 'InternalLinkID', |
|
| 98 | - $this->fieldLabel('InternalLinkID'), |
|
| 99 | - SiteTree::class |
|
| 100 | - ), |
|
| 101 | - $externalLinkField, |
|
| 102 | - $wrap = CompositeField::create( |
|
| 103 | - $extraLabel = LiteralField::create( |
|
| 104 | - 'NoteOverride', |
|
| 105 | - _t( |
|
| 106 | - __CLASS__ . '.Note', |
|
| 107 | - // @todo remove the HTML from this translation |
|
| 108 | - '<div class="message good notice">Note: If you specify an External Link, ' |
|
| 109 | - . 'the Internal Link will be ignored.</div>' |
|
| 110 | - ) |
|
| 111 | - ) |
|
| 112 | - ) |
|
| 113 | - ) |
|
| 114 | - )); |
|
| 115 | - $fields->insertBefore( |
|
| 116 | - LiteralField::create( |
|
| 117 | - 'Note', |
|
| 118 | - _t( |
|
| 119 | - __CLASS__ . '.Note2', |
|
| 120 | - // @todo remove the HTML from this translation |
|
| 121 | - '<p>Use this to specify a link to a page either on this site ' |
|
| 122 | - . '(Internal Link) or another site (External Link).</p>' |
|
| 123 | - ) |
|
| 124 | - ), |
|
| 125 | - 'Name' |
|
| 126 | - ); |
|
| 127 | - |
|
| 128 | - return $fields; |
|
| 129 | - } |
|
| 14 | + private static $db = [ |
|
| 15 | + 'Name' => 'Varchar(255)', |
|
| 16 | + 'ExternalLink' => 'Varchar(255)', |
|
| 17 | + 'SortOrder' => 'Int', |
|
| 18 | + ]; |
|
| 19 | + |
|
| 20 | + private static $has_one = [ |
|
| 21 | + 'Parent' => BaseHomePage::class, |
|
| 22 | + 'InternalLink' => SiteTree::class, |
|
| 23 | + ]; |
|
| 24 | + |
|
| 25 | + private static $summary_fields = [ |
|
| 26 | + 'Name' => 'Name', |
|
| 27 | + 'InternalLink.Title' => 'Internal Link', |
|
| 28 | + 'ExternalLink' => 'External Link', |
|
| 29 | + ]; |
|
| 30 | + |
|
| 31 | + private static $table_name = 'Quicklink'; |
|
| 32 | + |
|
| 33 | + public function fieldLabels($includerelations = true) |
|
| 34 | + { |
|
| 35 | + $labels = parent::fieldLabels($includerelations); |
|
| 36 | + $labels['Name'] = _t('Quicklink.NameLabel', 'Name'); |
|
| 37 | + $labels['ExternalLink'] = _t('Quicklink.ExternalLinkLabel', 'External Link'); |
|
| 38 | + $labels['SortOrder'] = _t('Quicklink.SortOrderLabel', 'Sort Order'); |
|
| 39 | + $labels['ParentID'] = _t('Quicklink.ParentRelationLabel', 'Parent'); |
|
| 40 | + $labels['InternalLinkID'] = _t('Quicklink.InternalLinkLabel', 'Internal Link'); |
|
| 41 | + |
|
| 42 | + return $labels; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function getLink() |
|
| 46 | + { |
|
| 47 | + if ($this->ExternalLink) { |
|
| 48 | + $url = parse_url($this->ExternalLink); |
|
| 49 | + |
|
| 50 | + // if no scheme set in the link, default to http |
|
| 51 | + if (!isset($url['scheme'])) { |
|
| 52 | + return 'http://' . $this->ExternalLink; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + return $this->ExternalLink; |
|
| 56 | + } elseif ($this->InternalLinkID) { |
|
| 57 | + return $this->InternalLink()->Link(); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function canCreate($member = null, $context = []) |
|
| 62 | + { |
|
| 63 | + return $this->Parent()->canCreate($member, $context); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function canEdit($member = null) |
|
| 67 | + { |
|
| 68 | + return $this->Parent()->canEdit($member); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function canDelete($member = null) |
|
| 72 | + { |
|
| 73 | + return $this->Parent()->canDelete($member); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function canView($member = null) |
|
| 77 | + { |
|
| 78 | + return $this->Parent()->canView($member); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function getCMSFields() |
|
| 82 | + { |
|
| 83 | + $fields = parent::getCMSFields(); |
|
| 84 | + |
|
| 85 | + $fields->removeByName('ParentID'); |
|
| 86 | + |
|
| 87 | + $externalLinkField = $fields->fieldByName('Root.Main.ExternalLink'); |
|
| 88 | + |
|
| 89 | + $fields->removeByName('ExternalLink'); |
|
| 90 | + $fields->removeByName('InternalLinkID'); |
|
| 91 | + $fields->removeByName('SortOrder'); |
|
| 92 | + $externalLinkField->addExtraClass('noBorder'); |
|
| 93 | + |
|
| 94 | + $fields->addFieldToTab('Root.Main', CompositeField::create( |
|
| 95 | + array( |
|
| 96 | + TreeDropdownField::create( |
|
| 97 | + 'InternalLinkID', |
|
| 98 | + $this->fieldLabel('InternalLinkID'), |
|
| 99 | + SiteTree::class |
|
| 100 | + ), |
|
| 101 | + $externalLinkField, |
|
| 102 | + $wrap = CompositeField::create( |
|
| 103 | + $extraLabel = LiteralField::create( |
|
| 104 | + 'NoteOverride', |
|
| 105 | + _t( |
|
| 106 | + __CLASS__ . '.Note', |
|
| 107 | + // @todo remove the HTML from this translation |
|
| 108 | + '<div class="message good notice">Note: If you specify an External Link, ' |
|
| 109 | + . 'the Internal Link will be ignored.</div>' |
|
| 110 | + ) |
|
| 111 | + ) |
|
| 112 | + ) |
|
| 113 | + ) |
|
| 114 | + )); |
|
| 115 | + $fields->insertBefore( |
|
| 116 | + LiteralField::create( |
|
| 117 | + 'Note', |
|
| 118 | + _t( |
|
| 119 | + __CLASS__ . '.Note2', |
|
| 120 | + // @todo remove the HTML from this translation |
|
| 121 | + '<p>Use this to specify a link to a page either on this site ' |
|
| 122 | + . '(Internal Link) or another site (External Link).</p>' |
|
| 123 | + ) |
|
| 124 | + ), |
|
| 125 | + 'Name' |
|
| 126 | + ); |
|
| 127 | + |
|
| 128 | + return $fields; |
|
| 129 | + } |
|
| 130 | 130 | } |
@@ -11,25 +11,25 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class CwpSearchPageController extends PageController |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Create the dummy search record for this page |
|
| 16 | - * |
|
| 17 | - * @return CwpSearchPage |
|
| 18 | - */ |
|
| 19 | - protected function generateSearchRecord() |
|
| 20 | - { |
|
| 21 | - $searchPage = CwpSearchPage::create(); |
|
| 22 | - $searchPage->URLSegment = 'search'; |
|
| 23 | - $searchPage->Title = _t('SearchForm.SearchResults', 'Search Results'); |
|
| 24 | - $searchPage->ID = -1; |
|
| 25 | - return $searchPage; |
|
| 26 | - } |
|
| 14 | + /** |
|
| 15 | + * Create the dummy search record for this page |
|
| 16 | + * |
|
| 17 | + * @return CwpSearchPage |
|
| 18 | + */ |
|
| 19 | + protected function generateSearchRecord() |
|
| 20 | + { |
|
| 21 | + $searchPage = CwpSearchPage::create(); |
|
| 22 | + $searchPage->URLSegment = 'search'; |
|
| 23 | + $searchPage->Title = _t('SearchForm.SearchResults', 'Search Results'); |
|
| 24 | + $searchPage->ID = -1; |
|
| 25 | + return $searchPage; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - public function __construct($dataRecord = null) |
|
| 29 | - { |
|
| 30 | - if (!$dataRecord) { |
|
| 31 | - $dataRecord = $this->generateSearchRecord(); |
|
| 32 | - } |
|
| 33 | - parent::__construct($dataRecord); |
|
| 34 | - } |
|
| 28 | + public function __construct($dataRecord = null) |
|
| 29 | + { |
|
| 30 | + if (!$dataRecord) { |
|
| 31 | + $dataRecord = $this->generateSearchRecord(); |
|
| 32 | + } |
|
| 33 | + parent::__construct($dataRecord); |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -16,138 +16,138 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class CwpSearchEngine |
| 18 | 18 | { |
| 19 | - use Configurable; |
|
| 20 | - use Extensible; |
|
| 21 | - use Injectable; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Default search options |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - * @config |
|
| 28 | - */ |
|
| 29 | - private static $search_options = [ |
|
| 30 | - 'hl' => 'true', |
|
| 31 | - ]; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Additional search options to send to search when spellcheck |
|
| 35 | - * is included |
|
| 36 | - * |
|
| 37 | - * @var array |
|
| 38 | - * @config |
|
| 39 | - */ |
|
| 40 | - private static $spellcheck_options = [ |
|
| 41 | - 'spellcheck' => 'true', |
|
| 42 | - 'spellcheck.collate' => 'true', |
|
| 43 | - // spellcheck.dictionary can also be configured to use '_spellcheck' |
|
| 44 | - // dictionary when indexing fields under the _spellcheckText column |
|
| 45 | - 'spellcheck.dictionary' => 'default', |
|
| 46 | - ]; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Build a SearchQuery for a new search |
|
| 50 | - * |
|
| 51 | - * @param string $keywords |
|
| 52 | - * @param array $classes |
|
| 53 | - * @return SearchQuery |
|
| 54 | - */ |
|
| 55 | - protected function getSearchQuery($keywords, $classes) |
|
| 56 | - { |
|
| 57 | - $query = new SearchQuery(); |
|
| 58 | - $query->classes = $classes; |
|
| 59 | - $query->search($keywords); |
|
| 60 | - $query->exclude('SiteTree_ShowInSearch', 0); |
|
| 61 | - $query->exclude('File_ShowInSearch', 0); |
|
| 62 | - |
|
| 63 | - // Artificially lower the amount of results to prevent too high resource usage. |
|
| 64 | - // on subsequent canView check loop. |
|
| 65 | - $query->limit(100); |
|
| 66 | - return $query; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Get solr search options for this query |
|
| 71 | - * |
|
| 72 | - * @param bool $spellcheck True if we should include spellcheck support |
|
| 73 | - * @return array |
|
| 74 | - */ |
|
| 75 | - protected function getSearchOptions($spellcheck) |
|
| 76 | - { |
|
| 77 | - $options = $this->config()->search_options; |
|
| 78 | - if ($spellcheck) { |
|
| 79 | - $options = array_merge($options, $this->config()->spellcheck_options); |
|
| 80 | - } |
|
| 81 | - return $options; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Get results for a search term |
|
| 86 | - * |
|
| 87 | - * @param string $keywords |
|
| 88 | - * @param array $classes |
|
| 89 | - * @param SolrIndex $searchIndex |
|
| 90 | - * @param int $limit Max number of results for this page |
|
| 91 | - * @param int $start Skip this number of records |
|
| 92 | - * @param bool $spellcheck True to enable spellcheck |
|
| 93 | - * @return CwpSearchResult |
|
| 94 | - */ |
|
| 95 | - protected function getResult($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $spellcheck = false) |
|
| 96 | - { |
|
| 97 | - // Prepare options |
|
| 98 | - $query = $this->getSearchQuery($keywords, $classes); |
|
| 99 | - $options = $this->getSearchOptions($spellcheck); |
|
| 100 | - |
|
| 101 | - // Get results |
|
| 102 | - $solrResult = $searchIndex->search( |
|
| 103 | - $query, |
|
| 104 | - $start, |
|
| 105 | - $limit, |
|
| 106 | - $options |
|
| 107 | - ); |
|
| 108 | - |
|
| 109 | - return CwpSearchResult::create($keywords, $solrResult); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get a CwpSearchResult for a given criterea |
|
| 114 | - * |
|
| 115 | - * @param string $keywords |
|
| 116 | - * @param array $classes |
|
| 117 | - * @param SolrIndex $searchIndex |
|
| 118 | - * @param int $limit Max number of results for this page |
|
| 119 | - * @param int $start Skip this number of records |
|
| 120 | - * @param bool $followSuggestions True to enable suggested searches to be returned immediately |
|
| 121 | - * @return CwpSearchResult|null |
|
| 122 | - */ |
|
| 123 | - public function search($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $followSuggestions = false) |
|
| 124 | - { |
|
| 125 | - if (empty($keywords)) { |
|
| 126 | - return null; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - try { |
|
| 130 | - // Begin search |
|
| 131 | - $result = $this->getResult($keywords, $classes, $searchIndex, $limit, $start, true); |
|
| 132 | - |
|
| 133 | - // Return results if we don't need to refine this any further |
|
| 134 | - if (!$followSuggestions || $result->hasResults() || !$result->getSuggestion()) { |
|
| 135 | - return $result; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Perform new search with the suggested terms |
|
| 139 | - $suggested = $result->getSuggestion(); |
|
| 140 | - $newResult = $this->getResult($suggested, $classes, $searchIndex, $limit, $start, false); |
|
| 141 | - $newResult->setOriginal($keywords); |
|
| 142 | - |
|
| 143 | - // Compare new results to the original query |
|
| 144 | - if ($newResult->hasResults()) { |
|
| 145 | - return $newResult; |
|
| 146 | - } else { |
|
| 147 | - return $result; |
|
| 148 | - } |
|
| 149 | - } catch (Exception $e) { |
|
| 150 | - Injector::inst()->get(LoggerInterface::class)->warning($e); |
|
| 151 | - } |
|
| 152 | - } |
|
| 19 | + use Configurable; |
|
| 20 | + use Extensible; |
|
| 21 | + use Injectable; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Default search options |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + * @config |
|
| 28 | + */ |
|
| 29 | + private static $search_options = [ |
|
| 30 | + 'hl' => 'true', |
|
| 31 | + ]; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Additional search options to send to search when spellcheck |
|
| 35 | + * is included |
|
| 36 | + * |
|
| 37 | + * @var array |
|
| 38 | + * @config |
|
| 39 | + */ |
|
| 40 | + private static $spellcheck_options = [ |
|
| 41 | + 'spellcheck' => 'true', |
|
| 42 | + 'spellcheck.collate' => 'true', |
|
| 43 | + // spellcheck.dictionary can also be configured to use '_spellcheck' |
|
| 44 | + // dictionary when indexing fields under the _spellcheckText column |
|
| 45 | + 'spellcheck.dictionary' => 'default', |
|
| 46 | + ]; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Build a SearchQuery for a new search |
|
| 50 | + * |
|
| 51 | + * @param string $keywords |
|
| 52 | + * @param array $classes |
|
| 53 | + * @return SearchQuery |
|
| 54 | + */ |
|
| 55 | + protected function getSearchQuery($keywords, $classes) |
|
| 56 | + { |
|
| 57 | + $query = new SearchQuery(); |
|
| 58 | + $query->classes = $classes; |
|
| 59 | + $query->search($keywords); |
|
| 60 | + $query->exclude('SiteTree_ShowInSearch', 0); |
|
| 61 | + $query->exclude('File_ShowInSearch', 0); |
|
| 62 | + |
|
| 63 | + // Artificially lower the amount of results to prevent too high resource usage. |
|
| 64 | + // on subsequent canView check loop. |
|
| 65 | + $query->limit(100); |
|
| 66 | + return $query; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Get solr search options for this query |
|
| 71 | + * |
|
| 72 | + * @param bool $spellcheck True if we should include spellcheck support |
|
| 73 | + * @return array |
|
| 74 | + */ |
|
| 75 | + protected function getSearchOptions($spellcheck) |
|
| 76 | + { |
|
| 77 | + $options = $this->config()->search_options; |
|
| 78 | + if ($spellcheck) { |
|
| 79 | + $options = array_merge($options, $this->config()->spellcheck_options); |
|
| 80 | + } |
|
| 81 | + return $options; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Get results for a search term |
|
| 86 | + * |
|
| 87 | + * @param string $keywords |
|
| 88 | + * @param array $classes |
|
| 89 | + * @param SolrIndex $searchIndex |
|
| 90 | + * @param int $limit Max number of results for this page |
|
| 91 | + * @param int $start Skip this number of records |
|
| 92 | + * @param bool $spellcheck True to enable spellcheck |
|
| 93 | + * @return CwpSearchResult |
|
| 94 | + */ |
|
| 95 | + protected function getResult($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $spellcheck = false) |
|
| 96 | + { |
|
| 97 | + // Prepare options |
|
| 98 | + $query = $this->getSearchQuery($keywords, $classes); |
|
| 99 | + $options = $this->getSearchOptions($spellcheck); |
|
| 100 | + |
|
| 101 | + // Get results |
|
| 102 | + $solrResult = $searchIndex->search( |
|
| 103 | + $query, |
|
| 104 | + $start, |
|
| 105 | + $limit, |
|
| 106 | + $options |
|
| 107 | + ); |
|
| 108 | + |
|
| 109 | + return CwpSearchResult::create($keywords, $solrResult); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get a CwpSearchResult for a given criterea |
|
| 114 | + * |
|
| 115 | + * @param string $keywords |
|
| 116 | + * @param array $classes |
|
| 117 | + * @param SolrIndex $searchIndex |
|
| 118 | + * @param int $limit Max number of results for this page |
|
| 119 | + * @param int $start Skip this number of records |
|
| 120 | + * @param bool $followSuggestions True to enable suggested searches to be returned immediately |
|
| 121 | + * @return CwpSearchResult|null |
|
| 122 | + */ |
|
| 123 | + public function search($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $followSuggestions = false) |
|
| 124 | + { |
|
| 125 | + if (empty($keywords)) { |
|
| 126 | + return null; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + try { |
|
| 130 | + // Begin search |
|
| 131 | + $result = $this->getResult($keywords, $classes, $searchIndex, $limit, $start, true); |
|
| 132 | + |
|
| 133 | + // Return results if we don't need to refine this any further |
|
| 134 | + if (!$followSuggestions || $result->hasResults() || !$result->getSuggestion()) { |
|
| 135 | + return $result; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Perform new search with the suggested terms |
|
| 139 | + $suggested = $result->getSuggestion(); |
|
| 140 | + $newResult = $this->getResult($suggested, $classes, $searchIndex, $limit, $start, false); |
|
| 141 | + $newResult->setOriginal($keywords); |
|
| 142 | + |
|
| 143 | + // Compare new results to the original query |
|
| 144 | + if ($newResult->hasResults()) { |
|
| 145 | + return $newResult; |
|
| 146 | + } else { |
|
| 147 | + return $result; |
|
| 148 | + } |
|
| 149 | + } catch (Exception $e) { |
|
| 150 | + Injector::inst()->get(LoggerInterface::class)->warning($e); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | } |
@@ -11,204 +11,204 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class CwpSearchResult extends ViewableData |
| 13 | 13 | { |
| 14 | - private static $casting = [ |
|
| 15 | - 'Original' => 'Text', |
|
| 16 | - 'OriginalLink' => 'Text', |
|
| 17 | - 'Suggestion' => 'Text', |
|
| 18 | - 'SuggestionLink' => 'Text', |
|
| 19 | - 'Query' => 'Text', |
|
| 20 | - 'SearchLink' => 'Text', |
|
| 21 | - 'RSSLink' => 'Text', |
|
| 22 | - 'AtomLink' => 'Text', |
|
| 23 | - ]; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * List of results |
|
| 27 | - * |
|
| 28 | - * @var PaginatedList |
|
| 29 | - */ |
|
| 30 | - protected $matches; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Search terms for these results |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $query; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Suggested search keywords |
|
| 41 | - * Used when this search has suggested terms, but following suggestions isn't enabled |
|
| 42 | - * |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - protected $suggestion; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Original terms superceded by these result. |
|
| 49 | - * Used when a prior search had suggested terms, and follow suggestions is enabled. |
|
| 50 | - * |
|
| 51 | - * @var PaginatedList |
|
| 52 | - */ |
|
| 53 | - protected $original; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Create a new CwpSearchResult |
|
| 57 | - * |
|
| 58 | - * @param string $terms |
|
| 59 | - * @param ArrayData $results Result from SolrIndex |
|
| 60 | - */ |
|
| 61 | - public function __construct($terms = '', ArrayData $results = null) |
|
| 62 | - { |
|
| 63 | - $this->query = $terms; |
|
| 64 | - if ($results) { |
|
| 65 | - // Clean up the results. |
|
| 66 | - $matches = $results->Matches; |
|
| 67 | - foreach ($matches as $result) { |
|
| 68 | - if (!$result->canView()) { |
|
| 69 | - $matches->remove($result); |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $this->matches = $matches; |
|
| 74 | - $this->suggestion = $results->SuggestionNice; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get search results |
|
| 80 | - * |
|
| 81 | - * @return PaginatedList |
|
| 82 | - */ |
|
| 83 | - public function getResults() |
|
| 84 | - { |
|
| 85 | - return $this->matches; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Check if there are found results |
|
| 90 | - * |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function hasResults() |
|
| 94 | - { |
|
| 95 | - return $this->matches && $this->matches->exists(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get search keywords matching these results |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - public function getQuery() |
|
| 104 | - { |
|
| 105 | - return $this->query; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get suggested search keywords |
|
| 110 | - * |
|
| 111 | - * @return string |
|
| 112 | - */ |
|
| 113 | - public function getSuggestion() |
|
| 114 | - { |
|
| 115 | - return $this->suggestion; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get original search keywords superceded by these results |
|
| 120 | - * |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function getOriginal() |
|
| 124 | - { |
|
| 125 | - return $this->original; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Set original keywords |
|
| 130 | - * |
|
| 131 | - * @param string $original |
|
| 132 | - */ |
|
| 133 | - public function setOriginal($original) |
|
| 134 | - { |
|
| 135 | - $this->original = $original; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get the link to the suggested search |
|
| 140 | - * |
|
| 141 | - * @param string $format Optional output format |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function getSuggestionLink($format = null) |
|
| 145 | - { |
|
| 146 | - return $this->getLink($this->suggestion, $format); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Gets the link to the given search |
|
| 151 | - * |
|
| 152 | - * @param string $format Optional output format |
|
| 153 | - * @return string |
|
| 154 | - */ |
|
| 155 | - public function getSearchLink($format = null) |
|
| 156 | - { |
|
| 157 | - return $this->getLink($this->query, $format); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Gets the link to the original search, with suggestions disabled |
|
| 162 | - * |
|
| 163 | - * @param string $format Optional output format |
|
| 164 | - * @return string |
|
| 165 | - */ |
|
| 166 | - public function getOriginalLink($format = null) |
|
| 167 | - { |
|
| 168 | - return $this->getLink($this->original, $format) . "&suggestions=0"; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get link to these results in RSS format |
|
| 173 | - * |
|
| 174 | - * @return string |
|
| 175 | - */ |
|
| 176 | - public function getRSSLink() |
|
| 177 | - { |
|
| 178 | - return $this->getLink($this->query, 'rss'); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get link to these results in atom format |
|
| 183 | - * |
|
| 184 | - * @return string |
|
| 185 | - */ |
|
| 186 | - public function getAtomLink() |
|
| 187 | - { |
|
| 188 | - return $this->getLink($this->query, 'atom'); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Get a search link for given terms |
|
| 193 | - * |
|
| 194 | - * @param string $terms |
|
| 195 | - * @return string|null |
|
| 196 | - */ |
|
| 197 | - protected function getLink($terms, $format = null) |
|
| 198 | - { |
|
| 199 | - if (!$terms) { |
|
| 200 | - return null; |
|
| 201 | - } |
|
| 202 | - $link = 'search/SearchForm?Search='.rawurlencode($terms); |
|
| 203 | - if ($format) { |
|
| 204 | - $link .= '&format='.rawurlencode($format); |
|
| 205 | - } |
|
| 206 | - return $link; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - public function hasField($field) |
|
| 210 | - { |
|
| 211 | - // Fix customise not detecting custom field getters |
|
| 212 | - return array_key_exists($field, $this->config()->casting); |
|
| 213 | - } |
|
| 14 | + private static $casting = [ |
|
| 15 | + 'Original' => 'Text', |
|
| 16 | + 'OriginalLink' => 'Text', |
|
| 17 | + 'Suggestion' => 'Text', |
|
| 18 | + 'SuggestionLink' => 'Text', |
|
| 19 | + 'Query' => 'Text', |
|
| 20 | + 'SearchLink' => 'Text', |
|
| 21 | + 'RSSLink' => 'Text', |
|
| 22 | + 'AtomLink' => 'Text', |
|
| 23 | + ]; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * List of results |
|
| 27 | + * |
|
| 28 | + * @var PaginatedList |
|
| 29 | + */ |
|
| 30 | + protected $matches; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Search terms for these results |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $query; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Suggested search keywords |
|
| 41 | + * Used when this search has suggested terms, but following suggestions isn't enabled |
|
| 42 | + * |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + protected $suggestion; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Original terms superceded by these result. |
|
| 49 | + * Used when a prior search had suggested terms, and follow suggestions is enabled. |
|
| 50 | + * |
|
| 51 | + * @var PaginatedList |
|
| 52 | + */ |
|
| 53 | + protected $original; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Create a new CwpSearchResult |
|
| 57 | + * |
|
| 58 | + * @param string $terms |
|
| 59 | + * @param ArrayData $results Result from SolrIndex |
|
| 60 | + */ |
|
| 61 | + public function __construct($terms = '', ArrayData $results = null) |
|
| 62 | + { |
|
| 63 | + $this->query = $terms; |
|
| 64 | + if ($results) { |
|
| 65 | + // Clean up the results. |
|
| 66 | + $matches = $results->Matches; |
|
| 67 | + foreach ($matches as $result) { |
|
| 68 | + if (!$result->canView()) { |
|
| 69 | + $matches->remove($result); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $this->matches = $matches; |
|
| 74 | + $this->suggestion = $results->SuggestionNice; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get search results |
|
| 80 | + * |
|
| 81 | + * @return PaginatedList |
|
| 82 | + */ |
|
| 83 | + public function getResults() |
|
| 84 | + { |
|
| 85 | + return $this->matches; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Check if there are found results |
|
| 90 | + * |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function hasResults() |
|
| 94 | + { |
|
| 95 | + return $this->matches && $this->matches->exists(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get search keywords matching these results |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + public function getQuery() |
|
| 104 | + { |
|
| 105 | + return $this->query; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get suggested search keywords |
|
| 110 | + * |
|
| 111 | + * @return string |
|
| 112 | + */ |
|
| 113 | + public function getSuggestion() |
|
| 114 | + { |
|
| 115 | + return $this->suggestion; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get original search keywords superceded by these results |
|
| 120 | + * |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function getOriginal() |
|
| 124 | + { |
|
| 125 | + return $this->original; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Set original keywords |
|
| 130 | + * |
|
| 131 | + * @param string $original |
|
| 132 | + */ |
|
| 133 | + public function setOriginal($original) |
|
| 134 | + { |
|
| 135 | + $this->original = $original; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get the link to the suggested search |
|
| 140 | + * |
|
| 141 | + * @param string $format Optional output format |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function getSuggestionLink($format = null) |
|
| 145 | + { |
|
| 146 | + return $this->getLink($this->suggestion, $format); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Gets the link to the given search |
|
| 151 | + * |
|
| 152 | + * @param string $format Optional output format |
|
| 153 | + * @return string |
|
| 154 | + */ |
|
| 155 | + public function getSearchLink($format = null) |
|
| 156 | + { |
|
| 157 | + return $this->getLink($this->query, $format); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Gets the link to the original search, with suggestions disabled |
|
| 162 | + * |
|
| 163 | + * @param string $format Optional output format |
|
| 164 | + * @return string |
|
| 165 | + */ |
|
| 166 | + public function getOriginalLink($format = null) |
|
| 167 | + { |
|
| 168 | + return $this->getLink($this->original, $format) . "&suggestions=0"; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get link to these results in RSS format |
|
| 173 | + * |
|
| 174 | + * @return string |
|
| 175 | + */ |
|
| 176 | + public function getRSSLink() |
|
| 177 | + { |
|
| 178 | + return $this->getLink($this->query, 'rss'); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get link to these results in atom format |
|
| 183 | + * |
|
| 184 | + * @return string |
|
| 185 | + */ |
|
| 186 | + public function getAtomLink() |
|
| 187 | + { |
|
| 188 | + return $this->getLink($this->query, 'atom'); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Get a search link for given terms |
|
| 193 | + * |
|
| 194 | + * @param string $terms |
|
| 195 | + * @return string|null |
|
| 196 | + */ |
|
| 197 | + protected function getLink($terms, $format = null) |
|
| 198 | + { |
|
| 199 | + if (!$terms) { |
|
| 200 | + return null; |
|
| 201 | + } |
|
| 202 | + $link = 'search/SearchForm?Search='.rawurlencode($terms); |
|
| 203 | + if ($format) { |
|
| 204 | + $link .= '&format='.rawurlencode($format); |
|
| 205 | + } |
|
| 206 | + return $link; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + public function hasField($field) |
|
| 210 | + { |
|
| 211 | + // Fix customise not detecting custom field getters |
|
| 212 | + return array_key_exists($field, $this->config()->casting); |
|
| 213 | + } |
|
| 214 | 214 | } |
@@ -199,9 +199,9 @@ |
||
| 199 | 199 | if (!$terms) { |
| 200 | 200 | return null; |
| 201 | 201 | } |
| 202 | - $link = 'search/SearchForm?Search='.rawurlencode($terms); |
|
| 202 | + $link = 'search/SearchForm?Search=' . rawurlencode($terms); |
|
| 203 | 203 | if ($format) { |
| 204 | - $link .= '&format='.rawurlencode($format); |
|
| 204 | + $link .= '&format=' . rawurlencode($format); |
|
| 205 | 205 | } |
| 206 | 206 | return $link; |
| 207 | 207 | } |
@@ -11,14 +11,14 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class CwpSearchPage extends Page |
| 13 | 13 | { |
| 14 | - private static $hide_ancestor = CwpSearchPage::class; |
|
| 14 | + private static $hide_ancestor = CwpSearchPage::class; |
|
| 15 | 15 | |
| 16 | - public function canViewStage($stage = Versioned::LIVE, $member = null) |
|
| 17 | - { |
|
| 18 | - if (Permission::checkMember($member, 'VIEW_DRAFT_CONTENT')) { |
|
| 19 | - return true; |
|
| 20 | - } |
|
| 16 | + public function canViewStage($stage = Versioned::LIVE, $member = null) |
|
| 17 | + { |
|
| 18 | + if (Permission::checkMember($member, 'VIEW_DRAFT_CONTENT')) { |
|
| 19 | + return true; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - return parent::canViewStage($stage, $member); |
|
| 23 | - } |
|
| 22 | + return parent::canViewStage($stage, $member); |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -8,9 +8,9 @@ |
||
| 8 | 8 | class TaxonomyTermExtension extends DataExtension |
| 9 | 9 | { |
| 10 | 10 | |
| 11 | - private static $api_access = true; |
|
| 11 | + private static $api_access = true; |
|
| 12 | 12 | |
| 13 | - private static $belongs_many_many = array( |
|
| 14 | - 'Pages' => BasePage::class |
|
| 15 | - ); |
|
| 13 | + private static $belongs_many_many = array( |
|
| 14 | + 'Pages' => BasePage::class |
|
| 15 | + ); |
|
| 16 | 16 | } |
@@ -16,52 +16,52 @@ |
||
| 16 | 16 | class SynonymsSiteConfig extends DataExtension |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - private static $db = array( |
|
| 20 | - 'SearchSynonyms' => 'Text', // fulltextsearch synonyms.txt content |
|
| 21 | - ); |
|
| 19 | + private static $db = array( |
|
| 20 | + 'SearchSynonyms' => 'Text', // fulltextsearch synonyms.txt content |
|
| 21 | + ); |
|
| 22 | 22 | |
| 23 | - public function updateCMSFields(FieldList $fields) |
|
| 24 | - { |
|
| 25 | - // Don't show this field if you're not an admin |
|
| 26 | - if (!Permission::check('ADMIN')) { |
|
| 27 | - return; |
|
| 28 | - } |
|
| 23 | + public function updateCMSFields(FieldList $fields) |
|
| 24 | + { |
|
| 25 | + // Don't show this field if you're not an admin |
|
| 26 | + if (!Permission::check('ADMIN')) { |
|
| 27 | + return; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - // Search synonyms |
|
| 31 | - $fields->addFieldToTab( |
|
| 32 | - 'Root.FulltextSearch', |
|
| 33 | - TextareaField::create('SearchSynonyms', _t('CwpConfig.SearchSynonyms', 'Search Synonyms')) |
|
| 34 | - ->setDescription(_t( |
|
| 35 | - 'CwpConfig.SearchSynonyms_Description', |
|
| 36 | - 'Enter as many comma separated synonyms as you wish, where '. |
|
| 37 | - 'each line represents a group of synonyms.<br /> ' . |
|
| 38 | - 'You will need to run <a rel="external" target="_blank" href="dev/tasks/Solr_Configure">' |
|
| 39 | - . 'Solr_Configure</a> if you make any changes' |
|
| 40 | - )) |
|
| 41 | - ); |
|
| 42 | - } |
|
| 30 | + // Search synonyms |
|
| 31 | + $fields->addFieldToTab( |
|
| 32 | + 'Root.FulltextSearch', |
|
| 33 | + TextareaField::create('SearchSynonyms', _t('CwpConfig.SearchSynonyms', 'Search Synonyms')) |
|
| 34 | + ->setDescription(_t( |
|
| 35 | + 'CwpConfig.SearchSynonyms_Description', |
|
| 36 | + 'Enter as many comma separated synonyms as you wish, where '. |
|
| 37 | + 'each line represents a group of synonyms.<br /> ' . |
|
| 38 | + 'You will need to run <a rel="external" target="_blank" href="dev/tasks/Solr_Configure">' |
|
| 39 | + . 'Solr_Configure</a> if you make any changes' |
|
| 40 | + )) |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @inheritdoc |
|
| 46 | - * |
|
| 47 | - * @param ValidationResult $validationResult |
|
| 48 | - */ |
|
| 49 | - public function validate(ValidationResult $validationResult) |
|
| 50 | - { |
|
| 51 | - $validator = new SynonymValidator(array( |
|
| 52 | - 'SearchSynonyms', |
|
| 53 | - )); |
|
| 44 | + /** |
|
| 45 | + * @inheritdoc |
|
| 46 | + * |
|
| 47 | + * @param ValidationResult $validationResult |
|
| 48 | + */ |
|
| 49 | + public function validate(ValidationResult $validationResult) |
|
| 50 | + { |
|
| 51 | + $validator = new SynonymValidator(array( |
|
| 52 | + 'SearchSynonyms', |
|
| 53 | + )); |
|
| 54 | 54 | |
| 55 | - $validator->php(array( |
|
| 56 | - 'SearchSynonyms' => $this->owner->SearchSynonyms |
|
| 57 | - )); |
|
| 55 | + $validator->php(array( |
|
| 56 | + 'SearchSynonyms' => $this->owner->SearchSynonyms |
|
| 57 | + )); |
|
| 58 | 58 | |
| 59 | - $errors = $validator->getErrors(); |
|
| 59 | + $errors = $validator->getErrors(); |
|
| 60 | 60 | |
| 61 | - if (is_array($errors) || $errors instanceof Traversable) { |
|
| 62 | - foreach ($errors as $error) { |
|
| 63 | - $validationResult->error($error['message']); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - } |
|
| 61 | + if (is_array($errors) || $errors instanceof Traversable) { |
|
| 62 | + foreach ($errors as $error) { |
|
| 63 | + $validationResult->error($error['message']); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -33,7 +33,7 @@ |
||
| 33 | 33 | TextareaField::create('SearchSynonyms', _t('CwpConfig.SearchSynonyms', 'Search Synonyms')) |
| 34 | 34 | ->setDescription(_t( |
| 35 | 35 | 'CwpConfig.SearchSynonyms_Description', |
| 36 | - 'Enter as many comma separated synonyms as you wish, where '. |
|
| 36 | + 'Enter as many comma separated synonyms as you wish, where ' . |
|
| 37 | 37 | 'each line represents a group of synonyms.<br /> ' . |
| 38 | 38 | 'You will need to run <a rel="external" target="_blank" href="dev/tasks/Solr_Configure">' |
| 39 | 39 | . 'Solr_Configure</a> if you make any changes' |
@@ -6,154 +6,154 @@ |
||
| 6 | 6 | |
| 7 | 7 | class SynonymValidator extends Validator |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var array |
|
| 11 | - */ |
|
| 12 | - protected $fieldNames; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @inheritdoc |
|
| 16 | - * |
|
| 17 | - * @param array $fieldNames |
|
| 18 | - */ |
|
| 19 | - public function __construct(array $fieldNames) |
|
| 20 | - { |
|
| 21 | - $this->fieldNames = $fieldNames; |
|
| 22 | - |
|
| 23 | - parent::__construct(); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @inheritdoc |
|
| 28 | - * |
|
| 29 | - * @param array $data |
|
| 30 | - * |
|
| 31 | - * @return mixed |
|
| 32 | - */ |
|
| 33 | - public function php($data) |
|
| 34 | - { |
|
| 35 | - foreach ($this->fieldNames as $fieldName) { |
|
| 36 | - if (empty($data[$fieldName])) { |
|
| 37 | - return; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - $this->validateField($fieldName, $data[$fieldName]); |
|
| 41 | - } |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Validate field values, raising errors if the values are invalid. |
|
| 46 | - * |
|
| 47 | - * @param string $fieldName |
|
| 48 | - * @param mixed $value |
|
| 49 | - */ |
|
| 50 | - protected function validateField($fieldName, $value) |
|
| 51 | - { |
|
| 52 | - if (!$this->validateValue($value)) { |
|
| 53 | - $this->validationError( |
|
| 54 | - $fieldName, |
|
| 55 | - _t( |
|
| 56 | - 'FullTextSearch.SynonymValidator.InvalidValue', |
|
| 57 | - 'Synonyms cannot contain words separated by spaces' |
|
| 58 | - ) |
|
| 59 | - ); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Check field values to see that they doesn't contain spaces between words. |
|
| 65 | - * |
|
| 66 | - * @param mixed $value |
|
| 67 | - * |
|
| 68 | - * @return bool |
|
| 69 | - */ |
|
| 70 | - protected function validateValue($value) |
|
| 71 | - { |
|
| 72 | - // strip empty lines |
|
| 73 | - $lines = array_filter( |
|
| 74 | - explode("\n", $value) |
|
| 75 | - ); |
|
| 76 | - |
|
| 77 | - // strip comments (lines beginning with "#") |
|
| 78 | - $lines = array_filter($lines, function ($line) { |
|
| 79 | - $line = trim($line); |
|
| 80 | - |
|
| 81 | - return !empty($line) && $line[0] !== '#'; |
|
| 82 | - }); |
|
| 83 | - |
|
| 84 | - // validate each line |
|
| 85 | - foreach ($lines as $line) { |
|
| 86 | - if (!$this->validateLine($line)) { |
|
| 87 | - return false; |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Check each line to see that it doesn't contain spaces between words. |
|
| 96 | - * |
|
| 97 | - * @param string $line |
|
| 98 | - * |
|
| 99 | - * @return bool |
|
| 100 | - */ |
|
| 101 | - protected function validateLine($line) |
|
| 102 | - { |
|
| 103 | - $line = trim($line); |
|
| 104 | - |
|
| 105 | - $parts = explode(',', $line); |
|
| 106 | - $parts = array_filter($parts); |
|
| 107 | - |
|
| 108 | - foreach ($parts as $part) { |
|
| 109 | - if (!$this->validatePart($part)) { |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return true; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Check each part of the line doesn't contain spaces between words. |
|
| 119 | - * |
|
| 120 | - * @param string $part |
|
| 121 | - * |
|
| 122 | - * @return bool |
|
| 123 | - */ |
|
| 124 | - protected function validatePart($part) |
|
| 125 | - { |
|
| 126 | - if (strpos($part, '=>') !== false) { |
|
| 127 | - $subs = explode('=>', $part); |
|
| 128 | - $subs = array_filter($subs); |
|
| 129 | - |
|
| 130 | - foreach ($subs as $sub) { |
|
| 131 | - if (!$this->validateNoSpaces($sub)) { |
|
| 132 | - return false; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $this->validateNoSpaces($part); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param string $value |
|
| 144 | - * |
|
| 145 | - * @return bool |
|
| 146 | - */ |
|
| 147 | - protected function validateNoSpaces($value) |
|
| 148 | - { |
|
| 149 | - // allow spaces at the beginning and end of the value |
|
| 150 | - $value = trim($value); |
|
| 151 | - |
|
| 152 | - // does the value contain 1 or more whitespace characters? |
|
| 153 | - if (preg_match('/\s+/', $value)) { |
|
| 154 | - return false; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return true; |
|
| 158 | - } |
|
| 9 | + /** |
|
| 10 | + * @var array |
|
| 11 | + */ |
|
| 12 | + protected $fieldNames; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @inheritdoc |
|
| 16 | + * |
|
| 17 | + * @param array $fieldNames |
|
| 18 | + */ |
|
| 19 | + public function __construct(array $fieldNames) |
|
| 20 | + { |
|
| 21 | + $this->fieldNames = $fieldNames; |
|
| 22 | + |
|
| 23 | + parent::__construct(); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @inheritdoc |
|
| 28 | + * |
|
| 29 | + * @param array $data |
|
| 30 | + * |
|
| 31 | + * @return mixed |
|
| 32 | + */ |
|
| 33 | + public function php($data) |
|
| 34 | + { |
|
| 35 | + foreach ($this->fieldNames as $fieldName) { |
|
| 36 | + if (empty($data[$fieldName])) { |
|
| 37 | + return; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + $this->validateField($fieldName, $data[$fieldName]); |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Validate field values, raising errors if the values are invalid. |
|
| 46 | + * |
|
| 47 | + * @param string $fieldName |
|
| 48 | + * @param mixed $value |
|
| 49 | + */ |
|
| 50 | + protected function validateField($fieldName, $value) |
|
| 51 | + { |
|
| 52 | + if (!$this->validateValue($value)) { |
|
| 53 | + $this->validationError( |
|
| 54 | + $fieldName, |
|
| 55 | + _t( |
|
| 56 | + 'FullTextSearch.SynonymValidator.InvalidValue', |
|
| 57 | + 'Synonyms cannot contain words separated by spaces' |
|
| 58 | + ) |
|
| 59 | + ); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Check field values to see that they doesn't contain spaces between words. |
|
| 65 | + * |
|
| 66 | + * @param mixed $value |
|
| 67 | + * |
|
| 68 | + * @return bool |
|
| 69 | + */ |
|
| 70 | + protected function validateValue($value) |
|
| 71 | + { |
|
| 72 | + // strip empty lines |
|
| 73 | + $lines = array_filter( |
|
| 74 | + explode("\n", $value) |
|
| 75 | + ); |
|
| 76 | + |
|
| 77 | + // strip comments (lines beginning with "#") |
|
| 78 | + $lines = array_filter($lines, function ($line) { |
|
| 79 | + $line = trim($line); |
|
| 80 | + |
|
| 81 | + return !empty($line) && $line[0] !== '#'; |
|
| 82 | + }); |
|
| 83 | + |
|
| 84 | + // validate each line |
|
| 85 | + foreach ($lines as $line) { |
|
| 86 | + if (!$this->validateLine($line)) { |
|
| 87 | + return false; |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return true; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Check each line to see that it doesn't contain spaces between words. |
|
| 96 | + * |
|
| 97 | + * @param string $line |
|
| 98 | + * |
|
| 99 | + * @return bool |
|
| 100 | + */ |
|
| 101 | + protected function validateLine($line) |
|
| 102 | + { |
|
| 103 | + $line = trim($line); |
|
| 104 | + |
|
| 105 | + $parts = explode(',', $line); |
|
| 106 | + $parts = array_filter($parts); |
|
| 107 | + |
|
| 108 | + foreach ($parts as $part) { |
|
| 109 | + if (!$this->validatePart($part)) { |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return true; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Check each part of the line doesn't contain spaces between words. |
|
| 119 | + * |
|
| 120 | + * @param string $part |
|
| 121 | + * |
|
| 122 | + * @return bool |
|
| 123 | + */ |
|
| 124 | + protected function validatePart($part) |
|
| 125 | + { |
|
| 126 | + if (strpos($part, '=>') !== false) { |
|
| 127 | + $subs = explode('=>', $part); |
|
| 128 | + $subs = array_filter($subs); |
|
| 129 | + |
|
| 130 | + foreach ($subs as $sub) { |
|
| 131 | + if (!$this->validateNoSpaces($sub)) { |
|
| 132 | + return false; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $this->validateNoSpaces($part); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param string $value |
|
| 144 | + * |
|
| 145 | + * @return bool |
|
| 146 | + */ |
|
| 147 | + protected function validateNoSpaces($value) |
|
| 148 | + { |
|
| 149 | + // allow spaces at the beginning and end of the value |
|
| 150 | + $value = trim($value); |
|
| 151 | + |
|
| 152 | + // does the value contain 1 or more whitespace characters? |
|
| 153 | + if (preg_match('/\s+/', $value)) { |
|
| 154 | + return false; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return true; |
|
| 158 | + } |
|
| 159 | 159 | } |
@@ -75,7 +75,7 @@ |
||
| 75 | 75 | ); |
| 76 | 76 | |
| 77 | 77 | // strip comments (lines beginning with "#") |
| 78 | - $lines = array_filter($lines, function ($line) { |
|
| 78 | + $lines = array_filter($lines, function($line) { |
|
| 79 | 79 | $line = trim($line); |
| 80 | 80 | |
| 81 | 81 | return !empty($line) && $line[0] !== '#'; |
@@ -13,21 +13,21 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class CwpCommentingExtension extends Extension |
| 15 | 15 | { |
| 16 | - public function alterCommentForm(Form $form) |
|
| 17 | - { |
|
| 18 | - $fields = $form->Fields(); |
|
| 16 | + public function alterCommentForm(Form $form) |
|
| 17 | + { |
|
| 18 | + $fields = $form->Fields(); |
|
| 19 | 19 | |
| 20 | 20 | |
| 21 | - if ($emailField = $fields->dataFieldByName(Email::class)) { |
|
| 22 | - $emailField |
|
| 23 | - ->setTitle(_t('CwpCommentingExtension.EMAIL_TITLE', Email::class)) |
|
| 24 | - ->setDescription(_t('CwpCommentingExtension.WILL_NOT_BE_PUBLISHED', 'Will not be published.')); |
|
| 25 | - } |
|
| 21 | + if ($emailField = $fields->dataFieldByName(Email::class)) { |
|
| 22 | + $emailField |
|
| 23 | + ->setTitle(_t('CwpCommentingExtension.EMAIL_TITLE', Email::class)) |
|
| 24 | + ->setDescription(_t('CwpCommentingExtension.WILL_NOT_BE_PUBLISHED', 'Will not be published.')); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - if ($urlField = $fields->dataFieldByName('URL')) { |
|
| 28 | - $urlField |
|
| 29 | - ->setTitle(_t('CwpCommentingExtension.WEBSITE_TITLE', 'Your website (optional)')) |
|
| 30 | - ->setAttribute('placeholder', 'http://'); |
|
| 31 | - } |
|
| 32 | - } |
|
| 27 | + if ($urlField = $fields->dataFieldByName('URL')) { |
|
| 28 | + $urlField |
|
| 29 | + ->setTitle(_t('CwpCommentingExtension.WEBSITE_TITLE', 'Your website (optional)')) |
|
| 30 | + ->setAttribute('placeholder', 'http://'); |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | 33 | } |