1 | <?php |
||
2 | |||
3 | namespace CWP\Search\Extensions; |
||
4 | |||
5 | use Traversable; |
||
6 | use SilverStripe\Forms\FieldList; |
||
7 | use SilverStripe\Security\Permission; |
||
8 | use SilverStripe\Forms\TextareaField; |
||
9 | use SilverStripe\ORM\ValidationResult; |
||
10 | use SilverStripe\ORM\DataExtension; |
||
11 | |||
12 | /** |
||
13 | * Allows siteconfig to configure synonyms for fulltext search |
||
14 | * Requires silverstripe/fulltextsearch 1.1.1 or above |
||
15 | */ |
||
16 | class SynonymsSiteConfig extends DataExtension |
||
17 | { |
||
18 | |||
19 | private static $db = array( |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
20 | 'SearchSynonyms' => 'Text', // fulltextsearch synonyms.txt content |
||
21 | ); |
||
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 | } |
||
29 | |||
30 | // Search synonyms |
||
31 | $fields->addFieldToTab( |
||
32 | 'Root.FulltextSearch', |
||
33 | TextareaField::create('SearchSynonyms', _t(__CLASS__ . '.SearchSynonyms', 'Search Synonyms')) |
||
34 | ->setDescription(_t( |
||
35 | __CLASS__ . '.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 | |||
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 | |||
55 | $validator->php(array( |
||
56 | 'SearchSynonyms' => $this->owner->SearchSynonyms |
||
57 | )); |
||
58 | |||
59 | $errors = $validator->getErrors(); |
||
60 | |||
61 | if (is_array($errors) || $errors instanceof Traversable) { |
||
62 | foreach ($errors as $error) { |
||
63 | $validationResult->addError($error['message']); |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 |