Completed
Push — master ( 686084...a2bb6b )
by
unknown
9s
created

SynonymsSiteConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 17 2
A validate() 0 15 4
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
The private property $db is not used, and could be removed.
Loading history...
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'))
0 ignored issues
show
Bug introduced by
'SearchSynonyms' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
            TextareaField::create(/** @scrutinizer ignore-type */ 'SearchSynonyms', _t(__CLASS__ . '.SearchSynonyms', 'Search Synonyms'))
Loading history...
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