Passed
Push — master ( d7fd6a...9124ff )
by Robbie
11:58
created

SynonymsSiteConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 15 2
A validate() 0 14 4
1
<?php
2
3
/**
4
 * Allows siteconfig to configure synonyms for fulltext search
5
 * Requires silverstripe/fulltextsearch 1.1.1 or above
6
 */
7
class SynonymsSiteConfig extends DataExtension {
8
	
9
	private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
10
		'SearchSynonyms' => 'Text', // fulltextsearch synonyms.txt content
11
	);
12
	
13
	public function updateCMSFields(FieldList $fields) {
14
		// Don't show this field if you're not an admin
15
		if(!Permission::check('ADMIN')) {
16
			return;
17
		}
18
		
19
		// Search synonyms
20
		$fields->addFieldToTab(
21
			'Root.FulltextSearch',
22
			TextareaField::create('SearchSynonyms', _t('CwpConfig.SearchSynonyms', 'Search Synonyms'))
23
				->setDescription(_t(
24
					'CwpConfig.SearchSynonyms_Description',
25
					'Enter as many comma separated synonyms as you wish, where '.
26
					'each line represents a group of synonyms.<br /> ' .
27
					'You will need to run <a rel="external" target="_blank" href="dev/tasks/Solr_Configure">Solr_Configure</a> if you make any changes'
28
				))
29
		);
30
	}
31
32
	/**
33
	 * @inheritdoc
34
	 *
35
	 * @param ValidationResult $validationResult
36
	 */
37
	public function validate(ValidationResult $validationResult) {
38
		$validator = new SynonymValidator(array(
39
			'SearchSynonyms',
40
		));
41
42
		$validator->php(array(
43
			'SearchSynonyms' => $this->owner->SearchSynonyms
44
		));
45
46
		$errors = $validator->getErrors();
47
48
		if (is_array($errors) || $errors instanceof Traversable) {
49
			foreach ($errors as $error) {
50
				$validationResult->error($error['message']);
51
			}
52
		}
53
	}
54
}
55