Completed
Push — feature/ss4-upgrade ( f41a3f )
by
unknown
10:12
created

AddonsController::AddonsSearchForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 31
nc 1
nop 0
1
<?php
2
namespace SilverStripe\Addons\Controllers;
3
4
use Elastica\Query;
5
use Elastica\Query\Match;
6
use SilverStripe\Elastica\ElasticaService;
7
use SilverStripe\Elastica\ResultList;
8
use SilverStripe\Addons\Model\Addon;
9
use SilverStripe\Addons\Model\AddonVendor;
10
use SilverStripe\Addons\Model\SilverStripeVersion;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Control\Director;
13
use SilverStripe\ORM\PaginatedList;
14
use SilverStripe\Forms\Form;
15
use SilverStripe\Forms\FieldList;
16
use SilverStripe\Forms\DropdownField;
17
use SilverStripe\Forms\CheckboxSetField;
18
use SilverStripe\Forms\TextField;
19
use SilverStripe\ORM\ArrayList;
20
21
/**
22
 * Lists and searches add-ons.
23
 */
24
class AddonsController extends SiteController 
25
{
26
27
	private static $url_handlers = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
28
		'rss' => 'rss',
29
		'$Vendor!/$Name!' => 'addon',
30
		'$Vendor!' => 'vendor',
31
	];
32
33
	private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
34
		'index',
35
		'addon',
36
		'vendor',
37
		'rss',
38
	];
39
40
	private static $dependencies = [
41
		'ElasticaService' => '%$ElasticaService'
42
	];
43
44
	/**
45
	 * @var \SilverStripe\Elastica\ElasticaService
46
	 */
47
	private $elastica;
48
49
	public function index() 
50
	{
51
		return $this->renderWith(array('Addons', 'Page'));
52
	}
53
54
	public function setElasticaService(ElasticaService $elastica) 
55
	{
56
		$this->elastica = $elastica;
57
	}
58
59
	public function addon($request) 
60
	{
61
		$vendor = $request->param('Vendor');
62
		$name = $request->param('Name');
63
		$addon = Addon::get()->filter('Name', "$vendor/$name")->first();
64
65
		if (!$addon) {
66
			$this->httpError(404);
67
		}
68
69
		return new AddonController($this, $addon);
0 ignored issues
show
Documentation introduced by
$addon is of type null|object<SilverStripe\ORM\DataObject>, but the function expects a object<SilverStripe\Addons\Model\Addon>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
	}
71
72
	public function vendor($request) 
73
	{
74
		$name = $request->param('Vendor');
75
		$vendor = AddonVendor::get()->filter('Name', $name)->first();
76
77
		if (!$vendor) {
78
			$this->httpError(404);
79
		}
80
81
		return new VendorController($this, $vendor);
0 ignored issues
show
Documentation introduced by
$vendor is of type null|object<SilverStripe\ORM\DataObject>, but the function expects a object<SilverStripe\Addons\Model\AddonVendor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
	}
83
84
	public function Title() 
85
	{
86
		return 'Add-ons';
87
	}
88
89
	public function Link($slug = null) 
90
	{
91
		if($slug){
92
			return Controller::join_links(Director::baseURL(), 'add-ons', $slug);
93
		} else {
94
			return Controller::join_links(Director::baseURL(), 'add-ons');
95
		}
96
	}
97
98
	public function ListView() 
99
	{
100
		$view = $this->request->getVar('view');
101
		if($view) {
102
			return $view;
103
		} else {
104
			return 'list';
105
		}
106
	}
107
108
	public function Addons() 
109
	{
110
		$list = Addon::get();
111
112
		$search = $this->request->getVar('search');
113
		$type = $this->request->getVar('type');
114
		$compat = $this->request->getVar('compatibility');
115
		$tags = $this->request->getVar('tags');
116
		$sort = $this->request->getVar('sort');
117
		$view = $this->request->getVar('view');
118
119
		if (!$view) {
120
			$view = 'list';
0 ignored issues
show
Unused Code introduced by
$view is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
121
		}
122
123
		if (!in_array($sort, array('name', 'downloads', 'newest'))) {
124
			$sort = null;
125
		}
126
127
		// Proxy out a search to elastic if any parameters are set.
128
		if ($search || $type || $compat || $tags) {
129
130
			$bool = new Query\BoolQuery();
131
132
			$query = new Query();
133
			$query->setQuery($bool);
134
			$query->setSize(count($list));
135
136
137
			if ($search) {
138
				$match = new Match();
139
				$match->setField('_all', $search);
140
141
				$bool->addMust($match);
142
			}
143
144
			if ($type) {
145
				$bool->addMust(new Query\Term(array('type' => $type)));
146
			}
147
148
			if ($compat) {
149
				$bool->addMust(new Query\Terms('compatibility', (array) $compat));
150
			}
151
152
			if ($tags) {
153
				$bool->addMust(new Query\Terms('tag', (array) $tags));
154
			}
155
156
			$list = new ResultList($this->elastica->getIndex(), $query);
157
158
			if ($sort) {
159
				$ids = $list->column('ID');
160
161
				if ($ids) {
162
					$list = Addon::get()->byIDs($ids);
163
				} else {
164
					$list = new ArrayList();
165
				}
166
			} else {
167
				$list = $list->toArrayList();
168
			}
169
		} else {
170
			if (!$sort) $sort = 'downloads';
171
		}
172
173
		switch ($sort) {
174
			case 'name': $list = $list->sort('Name'); break;
175
			case 'newest': $list = $list->sort('Released', 'DESC'); break;
176
			case 'downloads': $list = $list->sort('Downloads', 'DESC'); break;
177
		}
178
179
		$list = new PaginatedList($list, $this->request);
0 ignored issues
show
Documentation introduced by
$this->request is of type object<SilverStripe\Control\HTTPRequest>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
180
		$list->setPageLength(16);
181
182
		return $list;
183
	}
184
185
	public function AddonsSearchForm() 
186
	{
187
		$form = new Form(
188
			$this,
189
			'AddonsSearchForm',
190
			new FieldList(array(
191
				TextField::create('search', 'Search for')
192
					->setValue($this->request->getVar('search'))
193
					->addExtraClass('input-block-level'),
194
				DropdownField::create('sort', 'Sort by')
195
					->setSource(array(
196
						'name' => 'Name',
197
						'downloads' => 'Most downloaded',
198
						'newest' => 'Newest'
199
					))
200
					->setEmptyString('Best match')
201
					->setValue($this->request->getVar('sort'))
202
					->addExtraClass('input-block-level'),
203
				DropdownField::create('type', 'Add-on type')
204
					->setSource(array(
205
						'module' => 'Modules',
206
						'theme' => 'Themes'
207
					))
208
					->setEmptyString('Modules and themes')
209
					->setValue($this->request->getVar('type'))
210
					->addExtraClass('input-block-level'),
211
				CheckboxSetField::create('compatibility', 'Compatible SilverStripe versions')
212
					->setSource(SilverStripeVersion::get()->map('Name', 'Name'))
213
					->setValue($this->request->getVar('compatibility'))
214
					->setTemplate('AddonsSearchCheckboxSetField')
215
			)),
216
			new FieldList()
217
		);
218
219
		return $form
220
			->setFormMethod('GET')
221
			->setFormAction($this->Link());
222
	}
223
224
	public function rss($request, $limit = 10) 
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
225
	{
226
		$addons = Addon::get()
227
			->sort('Released', 'DESC')
228
			->limit($limit);
229
230
		$rss = new RSSFeed(
231
			$addons, 
232
			$this->Link(), 
233
			"Newest addons on addons.silverstripe.org",
234
			null,
235
			'RSSTitle'
236
		);
237
238
    	return $rss->outputToBrowser();
239
	}
240
241
}
242