Completed
Push — dev2 ( 12047b...a70cfe )
by Gordon
05:03
created

SearchableHelper::storeMethodTextValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4286
cc 2
eloc 8
nc 2
nop 4
1
<?php
2
namespace SilverStripe\Elastica;
3
4
class SearchableHelper {
5
6
7
	public static function addIndexedFields($name, &$spec, $ownerClassName) {
8
		// in the case of a relationship type will not be set
9
		if(isset($spec['type'])) {
10
			if($spec['type'] == 'string') {
11
				$unstemmed = array();
12
				$unstemmed['type'] = "string";
13
				$unstemmed['analyzer'] = "unstemmed";
14
				$unstemmed['term_vector'] = "yes";
15
				$extraFields = array('standard' => $unstemmed);
16
17
				$shingles = array();
18
				$shingles['type'] = "string";
19
				$shingles['analyzer'] = "shingles";
20
				$shingles['term_vector'] = "yes";
21
				$extraFields['shingles'] = $shingles;
22
23
				//Add autocomplete field if so required
24
				$autocomplete = \Config::inst()->get($ownerClassName, 'searchable_autocomplete');
25
26
				if(isset($autocomplete) && in_array($name, $autocomplete)) {
27
					$autocompleteField = array();
28
					$autocompleteField['type'] = "string";
29
					$autocompleteField['index_analyzer'] = "autocomplete_index_analyzer";
30
					$autocompleteField['search_analyzer'] = "autocomplete_search_analyzer";
31
					$autocompleteField['term_vector'] = "yes";
32
					$extraFields['autocomplete'] = $autocompleteField;
33
				}
34
35
				$spec['fields'] = $extraFields;
36
				// FIXME - make index/locale specific, get from settings
37
				$spec['analyzer'] = 'stemmed';
38
				$spec['term_vector'] = "yes";
39
			}
40
		}
41
	}
42
43
44
	/**
45
	 * @param string &$name
46
	 * @param boolean $storeMethodName
47
	 * @param boolean $recurse
48
	 */
49
	public static function assignSpecForRelationship(&$name, $resultType, &$spec, $storeMethodName, $recurse) {
50
		$resultTypeInstance = \Injector::inst()->create($resultType);
51
		$resultTypeMapping = array();
52
		// get the fields for the result type, but do not recurse
53
		if($recurse) {
54
			$resultTypeMapping = $resultTypeInstance->getElasticaFields($storeMethodName, false);
55
		}
56
		$resultTypeMapping['ID'] = array('type' => 'integer');
57
		if($storeMethodName) {
58
			$resultTypeMapping['__method'] = $name;
59
		}
60
		$spec = array('properties' => $resultTypeMapping);
61
		// we now change the name to the result type, not the method name
62
		$name = $resultType;
63
	}
64
65
66
	/**
67
	 * @param string $name
68
	 */
69
	public static function assignSpecForStandardFieldType($name, $class, &$spec, &$html_fields, &$mappings) {
70
		if(($pos = strpos($class, '('))) {
71
			// Valid in the case of Varchar(255)
72
			$class = substr($class, 0, $pos);
73
		}
74
75
		if(array_key_exists($class, $mappings)) {
76
			$spec['type'] = $mappings[$class];
77
			if($spec['type'] === 'date') {
78
				$spec['format'] = SearchableHelper::getFormatForDate($class);
79
			}
80
81
			if($class === 'HTMLText' || $class === 'HTMLVarchar') {
82
				array_push($html_fields, $name);
83
			}
84
		}
85
	}
86
87
88
	public static function getFormatForDate($class) {
89
		$format = 'y-M-d'; // default
90
		switch ($class) {
91
			case 'Date':
92
				$format = 'y-M-d';
93
				break;
94
			case 'SS_Datetime':
95
				$format = 'y-M-d H:m:s';
96
				break;
97
			case 'Datetime':
98
				$format = 'y-M-d H:m:s';
99
				break;
100
			case 'Time':
101
				$format = 'H:m:s';
102
				break;
103
		}
104
105
		return $format;
106
	}
107
108
109
110
	public static function getListRelationshipMethods($instance) {
111
		$has_manys = $instance->has_many();
112
		$many_manys = $instance->many_many();
113
114
		// array of method name to retuned object ClassName for relationships returning lists
115
		$has_lists = $has_manys;
116
		foreach(array_keys($many_manys) as $key) {
117
			$has_lists[$key] = $many_manys[$key];
118
		}
119
120
		return $has_lists;
121
	}
122
123
124
	public static function isInSiteTree($classname) {
125
		$inSiteTree = ($classname === 'SiteTree' ? true : false);
126
		if(!$inSiteTree) {
127
			$class = new \ReflectionClass($classname);
128
			while($class = $class->getParentClass()) {
129
				$parentClass = $class->getName();
130
				if($parentClass == 'SiteTree') {
131
					$inSiteTree = true;
132
					break;
133
				}
134
			}
135
		}
136
		return $inSiteTree;
137
	}
138
139
140
	public static function storeMethodTextValue($instance, $field, &$fields, $html_fields) {
0 ignored issues
show
Unused Code introduced by
The parameter $html_fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
		if(in_array($field, $fields)) {
142
			// Parse short codes in HTML, and then convert to text
143
			$fields[$field] = $this->owner->$field;
0 ignored issues
show
Bug introduced by
The property owner does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
144
			$html = ShortcodeParser::get_active()->parse($this->owner->$field());
145
			$txt = \Convert::html2raw($html);
146
			$fields[$field] = $txt;
147
		} else {
148
			// Plain text
149
			$fields[$field] = $instance->$field();
150
		}
151
	}
152
153
154
	/*
155
	Evaluate each field, e.g. 'Title', 'Member.Name'
156
	 */
157
	public static function fieldsToElasticaConfig($fields) {
158
		// Copied from DataObject::searchableFields() as there is no separate accessible method
159
		$rewrite = array();
160
		foreach($fields as $name => $specOrName) {
161
			$identifer = (is_int($name)) ? $specOrName : $name;
162
			$rewrite[$identifer] = array();
163
			if(!isset($rewrite[$identifer]['title'])) {
164
				$rewrite[$identifer]['title'] = (isset($labels[$identifer]))
165
					? $labels[$identifer] : \FormField::name_to_label($identifer);
166
			}
167
			if(!isset($rewrite[$identifer]['filter'])) {
168
				$rewrite[$identifer]['filter'] = 'PartialMatchFilter';
169
			}
170
		}
171
172
		return $rewrite;
173
	}
174
175
176
177
}
178