|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Elastica\Query; |
|
4
|
|
|
use Elastica\Query\Match; |
|
5
|
|
|
use SilverStripe\Elastica\ElasticaService; |
|
6
|
|
|
use SilverStripe\Elastica\ResultList; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Lists and searches add-ons. |
|
10
|
|
|
*/ |
|
11
|
|
|
class AddonsController extends SiteController |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public static $url_handlers = array( |
|
15
|
|
|
'rss' => 'rss', |
|
16
|
|
|
'$Vendor!/$Name!' => 'addon', |
|
17
|
|
|
'$Vendor!' => 'vendor', |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
public static $allowed_actions = array( |
|
21
|
|
|
'index', |
|
22
|
|
|
'addon', |
|
23
|
|
|
'vendor', |
|
24
|
|
|
'rss', |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
public static $dependencies = array( |
|
28
|
|
|
'ElasticaService' => '%$ElasticaService' |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \SilverStripe\Elastica\ElasticaService |
|
33
|
|
|
*/ |
|
34
|
|
|
private $elastica; |
|
35
|
|
|
|
|
36
|
|
|
public function index() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->renderWith(array('Addons', 'Page')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function setElasticaService(ElasticaService $elastica) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->elastica = $elastica; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function addon($request) |
|
47
|
|
|
{ |
|
48
|
|
|
$vendor = $request->param('Vendor'); |
|
49
|
|
|
$name = $request->param('Name'); |
|
50
|
|
|
$addon = Addon::get()->filter('Name', "$vendor/$name")->first(); |
|
51
|
|
|
|
|
52
|
|
|
if (!$addon) { |
|
53
|
|
|
$this->httpError(404); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return new AddonController($this, $addon); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function vendor($request) |
|
60
|
|
|
{ |
|
61
|
|
|
$name = $request->param('Vendor'); |
|
62
|
|
|
$vendor = AddonVendor::get()->filter('Name', $name)->first(); |
|
63
|
|
|
|
|
64
|
|
|
if (!$vendor) { |
|
65
|
|
|
$this->httpError(404); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new VendorController($this, $vendor); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function Title() |
|
72
|
|
|
{ |
|
73
|
|
|
return 'Add-ons'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function Link($slug = null) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($slug) { |
|
79
|
|
|
return Controller::join_links(Director::baseURL(), 'add-ons', $slug); |
|
80
|
|
|
} else { |
|
81
|
|
|
return Controller::join_links(Director::baseURL(), 'add-ons'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function ListView() |
|
86
|
|
|
{ |
|
87
|
|
|
$view = $this->request->getVar('view'); |
|
88
|
|
|
if ($view) { |
|
89
|
|
|
return $view; |
|
90
|
|
|
} else { |
|
91
|
|
|
return 'list'; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function Addons() |
|
96
|
|
|
{ |
|
97
|
|
|
$list = Addon::get(); |
|
98
|
|
|
|
|
99
|
|
|
$search = $this->request->getVar('search'); |
|
100
|
|
|
$type = $this->request->getVar('type'); |
|
101
|
|
|
$compat = $this->request->getVar('compatibility'); |
|
102
|
|
|
$tags = $this->request->getVar('tags'); |
|
103
|
|
|
$sort = $this->request->getVar('sort'); |
|
104
|
|
|
|
|
105
|
|
|
if (!in_array($sort, array('name', 'downloads', 'newest', 'relative'))) { |
|
106
|
|
|
$sort = null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Proxy out a search to elastic if any parameters are set. |
|
110
|
|
|
if ($search || $type || $compat || $tags) { |
|
111
|
|
|
|
|
112
|
|
|
$bool = new Query\BoolQuery(); |
|
113
|
|
|
|
|
114
|
|
|
$query = new Query(); |
|
115
|
|
|
$query->setQuery($bool); |
|
116
|
|
|
$query->setSize(count($list)); |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
if ($search) { |
|
120
|
|
|
$match = new Match(); |
|
121
|
|
|
$match->setField('_all', $search); |
|
122
|
|
|
|
|
123
|
|
|
$bool->addMust($match); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ($type) { |
|
127
|
|
|
$bool->addMust(new Query\Term(array('type' => $type))); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($compat) { |
|
131
|
|
|
$bool->addMust(new Query\Terms('compatibility', (array)$compat)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ($tags) { |
|
135
|
|
|
$bool->addMust(new Query\Terms('tag', (array)$tags)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$list = new ResultList($this->elastica->getIndex(), $query); |
|
139
|
|
|
|
|
140
|
|
|
if ($sort) { |
|
141
|
|
|
$ids = $list->column('ID'); |
|
142
|
|
|
|
|
143
|
|
|
if ($ids) { |
|
|
|
|
|
|
144
|
|
|
$list = Addon::get()->byIDs($ids); |
|
145
|
|
|
} else { |
|
146
|
|
|
$list = new ArrayList(); |
|
147
|
|
|
} |
|
148
|
|
|
} else { |
|
149
|
|
|
$list = $list->toArrayList(); |
|
150
|
|
|
} |
|
151
|
|
|
} else { |
|
152
|
|
|
if (!$sort) { |
|
153
|
|
|
$sort = 'relative'; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
switch ($sort) { |
|
158
|
|
|
case 'name': |
|
159
|
|
|
$list = $list->sort('Name'); |
|
160
|
|
|
break; |
|
161
|
|
|
case 'newest': |
|
162
|
|
|
$list = $list->sort('Released', 'DESC'); |
|
163
|
|
|
break; |
|
164
|
|
|
case 'downloads': |
|
165
|
|
|
$list = $list->sort('Downloads', 'DESC'); |
|
166
|
|
|
break; |
|
167
|
|
|
case 'relative': |
|
168
|
|
|
if (!$list instanceof ArrayList) { |
|
169
|
|
|
/** @var ArrayList|Addon[] $unsorted */ |
|
170
|
|
|
$unsorted = ArrayList::create($list->toArray()); |
|
171
|
|
|
} else { |
|
172
|
|
|
$unsorted = $list; |
|
173
|
|
|
} |
|
174
|
|
|
foreach ($unsorted as $item) { |
|
175
|
|
|
$item->Score = $item->relativePopularityFormatted() . ' per day'; |
|
176
|
|
|
} |
|
177
|
|
|
$list = $unsorted->sort('relativePopularity DESC'); |
|
178
|
|
|
break; |
|
179
|
|
|
default: |
|
180
|
|
|
$list = $list->sort('Downloads', 'DESC'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$list = new PaginatedList($list, $this->request); |
|
|
|
|
|
|
184
|
|
|
$list->setPageLength(16); |
|
185
|
|
|
|
|
186
|
|
|
return $list; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function AddonsSearchForm() |
|
190
|
|
|
{ |
|
191
|
|
|
$form = new Form( |
|
192
|
|
|
$this, |
|
193
|
|
|
'AddonsSearchForm', |
|
194
|
|
|
new FieldList(array( |
|
195
|
|
|
TextField::create('search', 'Search for') |
|
196
|
|
|
->setValue($this->request->getVar('search')) |
|
197
|
|
|
->addExtraClass('input-block-level'), |
|
198
|
|
|
DropdownField::create('sort', 'Sort by') |
|
199
|
|
|
->setSource(array( |
|
200
|
|
|
'name' => 'Name', |
|
201
|
|
|
'downloads' => 'Most downloaded', |
|
202
|
|
|
'relative' => 'Average downloads per day', |
|
203
|
|
|
'newest' => 'Newest' |
|
204
|
|
|
)) |
|
205
|
|
|
->setEmptyString('Best match') |
|
206
|
|
|
->setValue($this->request->getVar('sort')) |
|
207
|
|
|
->addExtraClass('input-block-level'), |
|
208
|
|
|
DropdownField::create('type', 'Add-on type') |
|
209
|
|
|
->setSource(array( |
|
210
|
|
|
'module' => 'Modules', |
|
211
|
|
|
'theme' => 'Themes' |
|
212
|
|
|
)) |
|
213
|
|
|
->setEmptyString('Modules and themes') |
|
214
|
|
|
->setValue($this->request->getVar('type')) |
|
215
|
|
|
->addExtraClass('input-block-level'), |
|
216
|
|
|
CheckboxSetField::create('compatibility', 'Compatible SilverStripe versions') |
|
217
|
|
|
->setSource(SilverStripeVersion::get()->map('Name', 'Name')) |
|
|
|
|
|
|
218
|
|
|
->setValue($this->request->getVar('compatibility')) |
|
219
|
|
|
->setTemplate('AddonsSearchCheckboxSetField') |
|
220
|
|
|
)), |
|
221
|
|
|
new FieldList() |
|
222
|
|
|
); |
|
223
|
|
|
|
|
224
|
|
|
return $form |
|
225
|
|
|
->setFormMethod('GET') |
|
226
|
|
|
->setFormAction($this->Link()); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function rss($request, $limit = 10) |
|
|
|
|
|
|
230
|
|
|
{ |
|
231
|
|
|
$addons = Addon::get() |
|
232
|
|
|
->sort('Released', 'DESC') |
|
233
|
|
|
->limit($limit); |
|
234
|
|
|
|
|
235
|
|
|
$rss = new RSSFeed( |
|
236
|
|
|
$addons, |
|
237
|
|
|
$this->Link(), |
|
238
|
|
|
"Newest addons on addons.silverstripe.org", |
|
239
|
|
|
null, |
|
240
|
|
|
'RSSTitle' |
|
241
|
|
|
); |
|
242
|
|
|
|
|
243
|
|
|
return $rss->outputToBrowser(); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.