Completed
Push — dev2 ( 91be19...12047b )
by Gordon
18:13
created

SearchableHelper::getListRelationshipMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 7
nc 2
nop 1
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 View Code Duplication
		if(!$inSiteTree) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
141
}
142