1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @package elastica |
6
|
|
|
* @subpackage tests |
7
|
|
|
*/ |
8
|
|
|
class FlickrPhotoTO extends DataObject implements TestOnly { |
9
|
|
|
private static $searchable_fields = array('Title','FlickrID','Description','TakenAt', 'TakenAtDT', 'FirstViewed', |
10
|
|
|
'Aperture','ShutterSpeed','FocalLength35mm','ISO','AspectRatio', 'TestMethod', 'TestMethodHTML'); |
11
|
|
|
|
12
|
|
|
private static $searchable_relationships = array('Photographer', 'FlickrTagTOs', 'FlickrSetTOs'); |
13
|
|
|
|
14
|
|
|
private static $searchable_autocomplete = array('Title'); |
15
|
|
|
|
16
|
|
|
// this needs to be declared here and not added by add_extension, as it does not extend DataExtension |
17
|
|
|
private static $extensions = array('FlickrPhotoTOTestIndexingExtension'); |
18
|
|
|
|
19
|
|
|
private static $db = array( |
20
|
|
|
'Title' => 'Varchar(255)', |
21
|
|
|
'FlickrID' => 'Varchar', |
22
|
|
|
'Description' => 'HTMLText', |
23
|
|
|
// test Date and SS_Datetime |
24
|
|
|
'TakenAt' => 'SS_Datetime', |
25
|
|
|
// same as above but different valid classname |
26
|
|
|
'TakenAtDT' => 'Datetime', |
27
|
|
|
'FirstViewed' => 'Date', |
28
|
|
|
'Aperture' => 'Float', |
29
|
|
|
'ShutterSpeed' => 'Varchar', |
30
|
|
|
'FocalLength35mm' => 'Int', |
31
|
|
|
'ISO' => 'Int', |
32
|
|
|
'OriginalHeight' => 'Int', |
33
|
|
|
'OriginalWidth' => 'Int', |
34
|
|
|
'AspectRatio' => 'Double', |
35
|
|
|
'Lat' => 'Decimal(18,15)', |
36
|
|
|
'Lon' => 'Decimal(18,15)', |
37
|
|
|
'ZoomLevel' => 'Int' |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
private static $belongs_many_many = array( |
41
|
|
|
'FlickrSetTOs' => 'FlickrSetTO' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
//1 to many |
45
|
|
|
private static $has_one = array( |
46
|
|
|
'Photographer' => 'FlickrAuthorTO' |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
//many to many |
50
|
|
|
private static $many_many = array( |
51
|
|
|
'FlickrTagTOs' => 'FlickrTagTO' |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
|
55
|
1 |
|
public function TestMethod() { |
56
|
1 |
|
return 'this is a test method'; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function TestMethodHTML() { |
60
|
1 |
|
return '<p>this is a test method that returns <b>HTML</b></p>'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @package elastica |
68
|
|
|
* @subpackage tests |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
class FlickrTagTO extends DataObject implements TestOnly { |
|
|
|
|
71
|
|
|
private static $db = array( |
72
|
|
|
'Value' => 'Varchar', |
73
|
|
|
'FlickrID' => 'Varchar', |
74
|
|
|
'RawValue' => 'HTMLText' |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
//many to many |
78
|
|
|
private static $belongs_many_many = array( |
|
|
|
|
79
|
|
|
'FlickrPhotoTOs' => 'FlickrPhotoTO' |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
private static $searchable_fields = array('RawValue'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @package elastica |
88
|
|
|
* @subpackage tests |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
class FlickrSetTO extends DataObject implements TestOnly { |
|
|
|
|
91
|
|
|
private static $searchable_fields = array('Title','FlickrID','Description'); |
92
|
|
|
|
93
|
|
|
private static $db = array( |
94
|
|
|
'Title' => 'Varchar(255)', |
95
|
|
|
'FlickrID' => 'Varchar', |
96
|
|
|
'Description' => 'HTMLText' |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
private static $many_many = array( |
100
|
|
|
'FlickrPhotoTOs' => 'FlickrPhotoTO' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @package elastica |
108
|
|
|
* @subpackage tests |
109
|
|
|
*/ |
110
|
|
|
class FlickrAuthorTO extends DataObject implements TestOnly { |
|
|
|
|
111
|
|
|
private static $db = array( |
112
|
|
|
'PathAlias' => 'Varchar', |
113
|
|
|
'DisplayName' => 'Varchar' |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
//1 to many |
117
|
|
|
private static $has_many = array('FlickrPhotoTOs' => 'FlickrPhotoTO'); |
|
|
|
|
118
|
|
|
|
119
|
|
|
private static $searchable_fields = array('PathAlias', 'DisplayName'); |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* NOTE: You would not normally want to do this as this means that all of |
123
|
|
|
* each user's FlickrPhotoTOs would be indexed against FlickrAuthorTO, so if |
124
|
|
|
* the user has 10,000 pics then the text of those 10,000 pics would |
125
|
|
|
* be indexed also. This is purely for test purposes with a small and |
126
|
|
|
* controlled dataset |
127
|
|
|
* |
128
|
|
|
* @var array |
129
|
|
|
*/ |
130
|
|
|
private static $searchable_relationships = array('FlickrPhotoTOs'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
|
136
|
|
|
class FlickrPhotoTOTestIndexingExtension extends Extension implements ElasticaIndexingHelperInterface,TestOnly { |
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Add a mapping for the location of the photograph |
140
|
|
|
*/ |
141
|
1 |
|
public function updateElasticsearchMapping(\Elastica\Type\Mapping $mapping) |
142
|
|
|
{ |
143
|
|
|
// get the properties of the individual fields as an array |
144
|
1 |
|
$properties = $mapping->getProperties(); |
145
|
|
|
|
146
|
|
|
// add a location with geo point |
147
|
1 |
|
$precision1cm = array('format' => 'compressed', 'precision' => '1cm'); |
148
|
1 |
|
$properties['location'] = array( |
149
|
1 |
|
'type' => 'geo_point', |
150
|
1 |
|
'fielddata' => $precision1cm, |
151
|
|
|
); |
152
|
|
|
|
153
|
1 |
|
$properties['ShutterSpeed'] = array( |
154
|
1 |
|
'type' => 'string', |
155
|
|
|
'index' => 'not_analyzed' |
156
|
1 |
|
); |
157
|
|
|
|
158
|
1 |
|
$properties['Aperture'] = array( |
159
|
|
|
// do not use float as the rounding makes facets impossible |
160
|
|
|
'type' => 'double' |
161
|
1 |
|
); |
162
|
|
|
|
163
|
1 |
|
$properties['FlickrID'] = array('type' => 'integer'); |
164
|
|
|
|
165
|
|
|
// by default casted as a string, we want a date 2015-07-25 18:15:33 y-M-d H:m:s |
166
|
|
|
//$properties['TakenAt'] = array('type' => 'date', 'format' => 'y-M-d H:m:s'); |
|
|
|
|
167
|
|
|
|
168
|
|
|
// set the new properties on the mapping |
169
|
1 |
|
$mapping->setProperties($properties); |
170
|
|
|
|
171
|
1 |
|
return $mapping; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Populate elastica with the location of the photograph |
177
|
|
|
* @param \Elastica\Document $document Representation of an Elastic Search document |
178
|
|
|
* @return \Elastica\Document modified version of the document |
179
|
|
|
*/ |
180
|
1 |
|
public function updateElasticsearchDocument(\Elastica\Document $document) |
181
|
|
|
{ |
182
|
1 |
|
if ($this->owner->Lat != null && $this->owner->Lon != null) { |
183
|
1 |
|
$coors = array('lat' => $this->owner->Lat, 'lon' => $this->owner->Lon); |
184
|
1 |
|
$document->set('location',$coors); |
185
|
1 |
|
} |
186
|
|
|
|
187
|
1 |
|
$sortable = $this->owner->ShutterSpeed; |
188
|
1 |
|
$sortable = explode('/', $sortable); |
189
|
1 |
|
if (sizeof($sortable) == 1) { |
190
|
1 |
|
$sortable = trim($sortable[0]); |
191
|
|
|
|
192
|
1 |
|
if ($this->owner->ShutterSpeed == null) { |
193
|
|
|
$sortable = null; |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
if ($sortable == 1) { |
197
|
1 |
|
$sortable = '1.000000'; |
198
|
1 |
|
} |
199
|
|
|
|
200
|
1 |
|
} else if (sizeof($sortable) == 2) { |
201
|
1 |
|
$sortable = floatval($sortable[0])/intval($sortable[1]); |
202
|
1 |
|
$sortable = round($sortable,6); |
203
|
1 |
|
} |
204
|
1 |
|
$sortable = $sortable . '|' . $this->owner->ShutterSpeed; |
205
|
1 |
|
$document->set('ShutterSpeed', $sortable); |
206
|
1 |
|
return $document; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
1 |
|
public function updateElasticHTMLFields(array $htmlFields) { |
211
|
1 |
|
array_push($htmlFields, 'TestMethodHTML'); |
212
|
1 |
|
return $htmlFields; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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.