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
|
|
|
public static $url_handlers = array( |
14
|
|
|
'rss' => 'rss', |
15
|
|
|
'$Vendor!/$Name!' => 'addon', |
16
|
|
|
'$Vendor!' => 'vendor', |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
public static $allowed_actions = array( |
20
|
|
|
'index', |
21
|
|
|
'addon', |
22
|
|
|
'vendor', |
23
|
|
|
'rss', |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
public static $dependencies = array( |
27
|
|
|
'ElasticaService' => '%$ElasticaService' |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \SilverStripe\Elastica\ElasticaService |
32
|
|
|
*/ |
33
|
|
|
private $elastica; |
34
|
|
|
|
35
|
|
|
public function index() { |
36
|
|
|
return $this->renderWith(array('Addons', 'Page')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setElasticaService(ElasticaService $elastica) { |
40
|
|
|
$this->elastica = $elastica; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function addon($request) { |
44
|
|
|
$vendor = $request->param('Vendor'); |
45
|
|
|
$name = $request->param('Name'); |
46
|
|
|
$addon = Addon::get()->filter('Name', "$vendor/$name")->first(); |
47
|
|
|
|
48
|
|
|
if (!$addon) { |
49
|
|
|
$this->httpError(404); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new AddonController($this, $addon); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function vendor($request) { |
56
|
|
|
$name = $request->param('Vendor'); |
57
|
|
|
$vendor = AddonVendor::get()->filter('Name', $name)->first(); |
58
|
|
|
|
59
|
|
|
if (!$vendor) { |
60
|
|
|
$this->httpError(404); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return new VendorController($this, $vendor); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function Title() { |
67
|
|
|
return 'Add-ons'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function Link($slug = null) { |
71
|
|
|
if($slug){ |
72
|
|
|
return Controller::join_links(Director::baseURL(), 'add-ons', $slug); |
73
|
|
|
} else { |
74
|
|
|
return Controller::join_links(Director::baseURL(), 'add-ons'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function ListView() { |
79
|
|
|
$view = $this->request->getVar('view'); |
80
|
|
|
if($view) { |
81
|
|
|
return $view; |
82
|
|
|
} else { |
83
|
|
|
return 'list'; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function Addons() { |
88
|
|
|
$list = Addon::get(); |
89
|
|
|
|
90
|
|
|
$search = $this->request->getVar('search'); |
91
|
|
|
$type = $this->request->getVar('type'); |
92
|
|
|
$compat = $this->request->getVar('compatibility'); |
93
|
|
|
$tags = $this->request->getVar('tags'); |
94
|
|
|
$sort = $this->request->getVar('sort'); |
95
|
|
|
$view = $this->request->getVar('view'); |
96
|
|
|
|
97
|
|
|
if (!$view) { |
98
|
|
|
$view = 'list'; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!in_array($sort, array('name', 'downloads', 'newest'))) { |
102
|
|
|
$sort = null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Proxy out a search to elastic if any parameters are set. |
106
|
|
|
if ($search || $type || $compat || $tags) { |
107
|
|
|
|
108
|
|
|
$bool = new Query\BoolQuery(); |
109
|
|
|
|
110
|
|
|
$query = new Query(); |
111
|
|
|
$query->setQuery($bool); |
112
|
|
|
$query->setSize(count($list)); |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
if ($search) { |
116
|
|
|
$match = new Match(); |
117
|
|
|
$match->setField('_all', $search); |
118
|
|
|
|
119
|
|
|
$bool->addMust($match); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($type) { |
123
|
|
|
$bool->addMust(new Query\Term(array('type' => $type))); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($compat) { |
127
|
|
|
$bool->addMust(new Query\Terms('compatibility', (array) $compat)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($tags) { |
131
|
|
|
$bool->addMust(new Query\Terms('tag', (array) $tags)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$list = new ResultList($this->elastica->getIndex(), $query); |
135
|
|
|
|
136
|
|
|
if ($sort) { |
137
|
|
|
$ids = $list->column('ID'); |
138
|
|
|
|
139
|
|
|
if ($ids) { |
|
|
|
|
140
|
|
|
$list = Addon::get()->byIDs($ids); |
141
|
|
|
} else { |
142
|
|
|
$list = new ArrayList(); |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
|
|
$list = $list->toArrayList(); |
146
|
|
|
} |
147
|
|
|
} else { |
148
|
|
|
if (!$sort) $sort = 'downloads'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
switch ($sort) { |
152
|
|
|
case 'name': $list = $list->sort('Name'); break; |
153
|
|
|
case 'newest': $list = $list->sort('Released', 'DESC'); break; |
154
|
|
|
case 'downloads': $list = $list->sort('Downloads', 'DESC'); break; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$list = new PaginatedList($list, $this->request); |
|
|
|
|
158
|
|
|
$list->setPageLength(16); |
159
|
|
|
|
160
|
|
|
return $list; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function AddonsSearchForm() { |
164
|
|
|
$form = new Form( |
165
|
|
|
$this, |
166
|
|
|
'AddonsSearchForm', |
167
|
|
|
new FieldList(array( |
168
|
|
|
TextField::create('search', 'Search for') |
169
|
|
|
->setValue($this->request->getVar('search')) |
170
|
|
|
->addExtraClass('input-block-level'), |
171
|
|
|
DropdownField::create('sort', 'Sort by') |
172
|
|
|
->setSource(array( |
173
|
|
|
'name' => 'Name', |
174
|
|
|
'downloads' => 'Most downloaded', |
175
|
|
|
'newest' => 'Newest' |
176
|
|
|
)) |
177
|
|
|
->setEmptyString('Best match') |
178
|
|
|
->setValue($this->request->getVar('sort')) |
179
|
|
|
->addExtraClass('input-block-level'), |
180
|
|
|
DropdownField::create('type', 'Add-on type') |
181
|
|
|
->setSource(array( |
182
|
|
|
'module' => 'Modules', |
183
|
|
|
'theme' => 'Themes' |
184
|
|
|
)) |
185
|
|
|
->setEmptyString('Modules and themes') |
186
|
|
|
->setValue($this->request->getVar('type')) |
187
|
|
|
->addExtraClass('input-block-level'), |
188
|
|
|
CheckboxSetField::create('compatibility', 'Compatible SilverStripe versions') |
189
|
|
|
->setSource(SilverStripeVersion::get()->map('Name', 'Name')) |
|
|
|
|
190
|
|
|
->setValue($this->request->getVar('compatibility')) |
191
|
|
|
->setTemplate('AddonsSearchCheckboxSetField') |
192
|
|
|
)), |
193
|
|
|
new FieldList() |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
return $form |
197
|
|
|
->setFormMethod('GET') |
198
|
|
|
->setFormAction($this->Link()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function rss($request, $limit = 10) { |
|
|
|
|
202
|
|
|
$addons = Addon::get() |
203
|
|
|
->sort('Released', 'DESC') |
204
|
|
|
->limit($limit); |
205
|
|
|
|
206
|
|
|
$rss = new RSSFeed( |
207
|
|
|
$addons, |
208
|
|
|
$this->Link(), |
209
|
|
|
"Newest addons on addons.silverstripe.org", |
210
|
|
|
null, |
211
|
|
|
'RSSTitle' |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
return $rss->outputToBrowser(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.