DdsGrid::addActionButtonColumn()   F
last analyzed

Complexity

Conditions 16
Paths 600

Size

Total Lines 58
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 58
rs 1.9555
c 1
b 0
f 0
cc 16
nc 600
nop 1
ccs 0
cts 53
cp 0
crap 272

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace neon\core\grid;
3
4
use neon\core\form\interfaces\IField;
5
use neon\core\grid\column\Column;
6
use \neon\core\grid\GridBase;
7
use \neon\core\grid\DdsDataProvider;
8
use neon\core\grid\query\IQuery;
9
use neon\core\helpers\Html;
10
use neon\core\helpers\Url;
11
use neon\core\interfaces\IProperties;
12
use neon\core\traits\PropertiesTrait;
13
14
class DdsGrid extends GridBase
15
{
16
	/**
17
	 * Url to delete a grid item. Set this to null to remove a delete button,
18
	 * to a string to use the default parameters or an array of the form
19
	 * [url, 'param1'=>value]
20
	 * The additional parameters 'type' and 'id' will be added automatically
21
	 * for the classType and uuid of the row object
22
	 * @var string|arrary
0 ignored issues
show
Bug introduced by
The type neon\core\grid\arrary was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
	 */
24
	public $gridDeleteUrl = '/daedalus/index/delete-object';
25
26
	/**
27
	 * Url to edit a grid item. Set this to null to remove an edit button,
28
	 * to a string to use the default parameters or an array of the form
29
	 * [url, 'param1'=>value]
30
	 * The additional parameters 'type' and 'id' will be added automatically
31
	 * for the classType and uuid of the row object
32
	 * @var string|array
33
	 */
34
	public $gridEditUrl = '/daedalus/index/edit-object';
35
36
	/**
37
	 * Url to view a grid item. Set this to null to remove a view button,
38
	 * to a string to use the default parameters or an array of the form
39
	 * [url, 'param1'=>value]
40
	 * The additional parameters 'type' and 'id' will be added automatically
41
	 * for the classType and uuid of the row object
42
	 * @var string|array
43
	 */
44
	public $gridViewUrl = '/daedalus/index/view-object';
45
46
	/**
47
	 * Url to a grids change log. Set this to null to remove a change log button,
48
	 * to a string to use the default parameters or an array of the form
49
	 * [url, 'param1'=>value]. It will only show if there is a change log for the class
50
	 * The additional parameters 'type' and 'id' will be added automatically
51
	 * for the classType and uuid of the row object
52
	 * @var string|array
53
	 */
54
	public $gridLogUrl = '/daedalus/change-log/list';
55
56
	/**
57
	 * Set whether or not the _created column is shown
58
	 * Set to true or an array of configuration parameters to show
59
	 * @var bool
60
	 */
61
	public $showCreatedColumn = false;
62
63
	/**
64
	 * Default config for the '_created' column
65
	 * @var array
66
	 */
67
	public $createdColumnConfig = [
68
		'filter' => false
69
	];
70
71
	/**
72
	 * Set whether or not the _updated column is shown
73
	 * Set to true or an array of configuration parameters to show
74
	 * @var bool | array
75
	 */
76
	public $showUpdatedColumn = false;
77
78
	/**
79
	 * Default config for the '_updated' column
80
	 * @var array
81
	 */
82
	public $updatedColumnConfig = [
83
		'filter' => false
84
	];
85
86
	/**
87
	 * Set whether or not to show the _uuid column
88
	 * Set to true or an array of configuration parameters to show
89
	 * @var bool | array
90
	 */
91
	public $showUuidColumn = false;
92
93
	/**
94
	 * Default config for the '_uuid' column
95
	 * @var array
96
	 */
97
	public $uuidColumnConfig = [
98
		'width' => '8rem'
99
	];
100
101
	/**
102
	 * @var bool - whether to show the undelete button
103
	 */
104
	public $showGridUndeleteButton = true;
105
106
	/**
107
	 * @var bool - whether to show the destroy button
108
	 */
109
	public $showGridDestroyButton = true;
110
111
	/**
112
	 * @var bool - whether or not to use the active scope.
113
	 * This shows the set of active (not deleted) items in a scope
114
	 */
115
	public $useActiveScope = false;
116
117
	/**
118
	 * @var bool - whether or not to use the deleted scope.
119
	 * This shows the set of deleted items in a scope so they can be restored
120
	 */
121
	public $useDeletedScope = false;
122
123
	/**
124
	 * @inheritdoc
125
	 */
126
	public function getProperties()
127
	{
128
		/**
129
		 * Return all the properties that are needed for generic rendering
130
		 * or exporting of this grid. Include parent properties.
131
		 */
132
		return array_merge(
133
			[
134
				'classType', 'gridDeleteUrl', 'gridEditUrl', 'showCreatedColumn',
135
				'createdColumnConfig', 'showUpdatedColumn', 'updatedColumnConfig',
136
				'showUuidColumn', 'uuidColumnConfig', 'showGridUndeleteButton',
137
				'showGridDestroyButton', 'useActiveScope', 'useDeletedScope'
138
			],
139
			parent::getProperties()
140
		);
141
	}
142
143
	/**
144
	 * Set the DDS class type
145
	 * @param string $value
146
	 */
147
	public function setClassType($value)
148
	{
149
		$this->_classType = $value;
150
		// Set a default title
151
		if ($this->title === null)
152
			$this->title = $this->_classType;
153
	}
154
155
	/**
156
	 * Return the dds class type string
157
	 * @return string
158
	 */
159
	public function getClassType()
160
	{
161
		return $this->_classType;
162
	}
163
164
	/**
165
	 * Get the grids Data provider object
166
	 * @return DdsDataProvider
167
	 */
168
	public function getDataProvider()
169
	{
170
		if (!$this->_dataProvider) {
171
			$this->_dataProvider = new DdsDataProvider([
0 ignored issues
show
Documentation Bug introduced by
It seems like new neon\core\grid\DdsDa...$this->getClassType())) of type neon\core\grid\DdsDataProvider is incompatible with the declared type yii\data\ActiveDataProvider of property $_dataProvider.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
172
				'classType' => $this->getClassType(),
173
			]);
174
		}
175
		return $this->_dataProvider;
176
	}
177
178
	/**
179
	 * The DDS class type
180
	 * @var string
181
	 */
182
	protected $_classType;
183
184
	/**
185
	 * Configure default columns based on the members in the dds class type
186
	 * If you like to change which columns are added or remove or add additional ones then override this method
187
	 * or implement the setup method.
188
	 */
189
	public function init()
190
	{
191
		parent::init();
192
		// don't configure the grid if the class type has not been set!
193
		if ($this->_classType === null) {
194
			if (neon()->debug)
0 ignored issues
show
Bug Best Practice introduced by
The property debug does not exist on neon\core\ApplicationWeb. Since you implemented __get, consider adding a @property annotation.
Loading history...
195
				throw new \RuntimeException('You need to configure the grid class type before calling init');
196
			return null;
197
		}
198
199
		$this->addColumnsFromClassMembers();
200
		$this->addDdsObjectColumns();
201
202
		if ($this->useActiveScope)
203
			$this->addScope('active', 'Active');
204
		if ($this->useDeletedScope)
205
			$this->addScope('deleted', 'Deleted');
206
207
	}
208
209
	/**
210
	 * @inheritdoc
211
	 */
212
	public function setup()
213
	{
214
		$this->addActionButtonColumn();
215
	}
216
217
	/**
218
	 * @param IQuery $query
219
	 */
220
	public function scopeActive(IQuery $query)
221
	{
222
		$query->where('_deleted', '=', 0);
223
	}
224
225
	/**
226
	 * @param IQuery $query
227
	 */
228
	public function scopeDeleted(IQuery $query)
229
	{
230
		$query->where('_deleted', '=', 1);
231
	}
232
233
	/**
234
	 * Add the edit and delete DDS object buttons to the grid
235
	 */
236
	public function addActionButtonColumn($title=false)
237
	{
238
		$buttonCol = $this->addButtonColumn('edit')->setTitle($title);
239
240
		// add a view button
241
		if ($this->gridViewUrl) {
242
			$viewUrl = is_string($this->gridViewUrl) ? [$this->gridViewUrl] : $this->gridViewUrl;
243
			$viewUrl['type'] = $this->getClassType();
244
			$viewUrl['id'] = '{{_uuid}}';
245
			$buttonCol->addButton('view', $viewUrl, '', 'fa fa-eye', 'btn-default', ['data-toggle' => 'tooltip', 'title'=>'View']);
246
		}
247
248
		if ($this->hasScope('deleted') && $this->isScopeActive('deleted')) {
249
			if ($this->showGridUndeleteButton) {
250
				$buttonCol->addButton('undelete', '','', 'fa fa-undo', 'btn-default', [
251
					'data-toggle' => 'tooltip',
252
					'title' => 'Restore',
253
					'data-action' => 'actionUndelete'
254
				]);
255
			}
256
			if ($this->showGridDestroyButton) {
257
				$buttonCol->addButton('destroy', '', '', 'fa fa-bolt', 'btn-danger', [
258
					'data-toggle' => 'tooltip',
259
					'title' => 'Destroy Permanently',
260
					'data-action' => 'actionDestroy',
261
				]);
262
			}
263
		}
264
265
		if (!$this->hasScope('deleted') || !$this->isScopeActive('deleted')) {
266
			if ($this->gridEditUrl) {
267
				// add an edit and view
268
				$editUrl = is_string($this->gridEditUrl) ? [$this->gridEditUrl] : $this->gridEditUrl;
269
				$editUrl['type'] = $this->getClassType();
270
				$editUrl['id'] = '{{_uuid}}';
271
				$buttonCol->addButton('edit', $editUrl, '', '', '', [
272
					'data-toggle' => 'tooltip',
273
					'title'=>'Edit'
274
				]);
275
			}
276
			if ($this->gridDeleteUrl) {
277
				$deleteUrl = is_string($this->gridDeleteUrl) ? [$this->gridDeleteUrl] : $this->gridDeleteUrl;
278
				$deleteUrl['type'] = $this->getClassType();
279
				$deleteUrl['id'] = '{{_uuid}}';
280
				$deleteUrl['redirect'] = false;
281
				$buttonCol->addButton('delete', $deleteUrl, '', '', '', [
282
					'data-toggle' => 'tooltip',
283
					'title'=>'Delete',
284
					'data-delete-ajax' => 'true'
285
				]);
286
			}
287
		}
288
		if ($this->gridLogUrl) {
289
			if (neon('dds')->IDdsChangeLogManagement->hasChangeLog($this->getClassType())) {
0 ignored issues
show
Bug introduced by
The method hasChangeLog() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

289
			if (neon('dds')->IDdsChangeLogManagement->/** @scrutinizer ignore-call */ hasChangeLog($this->getClassType())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The property IDdsChangeLogManagement does not exist on neon\core\ApplicationWeb. Since you implemented __get, consider adding a @property annotation.
Loading history...
290
				$logUrl = is_string($this->gridLogUrl) ? [$this->gridLogUrl] : $this->gridLogUrl;
291
				$logUrl['type'] = $this->getClassType();
292
				$logUrl['uuid'] = '{{_uuid}}';
293
				$buttonCol->addButton('Logs', $logUrl, '', 'fa fa-history', '', ['data-toggle' => 'tooltip', 'title'=>'View Change Log']);
294
			}
295
		}
296
297
	}
298
299
	/**
300
	 * Perform a permanent destroy
301
	 * @param $id
302
	 */
303
	public function actionDestroy($id)
304
	{
305
		// the permissions should be configured when adding a button,
306
		// the grid can then recheck these before running this action
307
		if (neon()->user->hasRole('neon-administrator')) {
308
			$object = neon('phoebe')->getService()->getPhoebeType('daedalus')->getObject($id);
309
			$object->destroyObject();
310
		}
311
	}
312
313
	/**
314
	 * Perform an undelete, restore of an object
315
	 * @param $id
316
	 */
317
	public function actionUndelete($id)
318
	{
319
		// the permissions should be configured when adding a button,
320
		// the grid can then recheck these before running this action
321
		if (neon()->user->hasRole('neon-administrator')) {
322
			$object = neon('phoebe')->getService()->getPhoebeType('daedalus')->getObject($id);
323
			$object->undeleteObject();
324
		}
325
	}
326
327
	/**
328
	 * Adds the columns to the grid based on the members in the dds class type
329
	 */
330
	public function addColumnsFromClassMembers()
331
	{
332
		$formDefinition = neon()->phoebe->getFormDefinition('daedalus', $this->getClassType());
333
		$memberForm = new \neon\core\form\Form($formDefinition);
334
		foreach ($memberForm->getFields() as $member) {
335
			$this->addDdsColumn($member);
336
		}
337
	}
338
339
	/**
340
	 * Add Dds Object Columns. This will add the _created, _updated and _uuid fields if the appropriate
341
	 * showCreatedColumn etc is set.
342
	 * @throws \Exception
343
	 */
344
	public function addDdsObjectColumns()
345
	{
346
		if ($this->showCreatedColumn) {
347
			$this->addDdsObjectColumn('_created', '\neon\core\form\fields\DateTime', $this->createdColumnConfig);
348
		}
349
		if ($this->showUpdatedColumn) {
350
			$this->addDdsObjectColumn('_updated', '\neon\core\form\fields\DateTime', $this->updatedColumnConfig);
351
		}
352
		$this->addDdsObjectColumn('_uuid', '\neon\core\form\fields\Uuid64', $this->uuidColumnConfig)
353
			->setDbField('_uuid')
354
			->setAsIndex()->setVisible($this->showUuidColumn);
355
		return $this;
356
	}
357
358
	/**
359
	 * @param $member
360
	 * @param $columnType
361
	 * @param $config
362
	 * @throws \Exception
363
	 * @return Column
364
	 */
365
	public function addDdsObjectColumn($member, $columnType, $config)
366
	{
367
		$colConfig = [
368
			'class' => 'neon\core\grid\column\DefinitionColumn',
369
			'member' => new $columnType($member),
370
		];
371
		if (is_array($config))
372
			$colConfig = array_merge($colConfig, $config);
373
		return $this->addColumn($member, $colConfig);
374
	}
375
376
	/**
377
	 * Load appropriate columns based on DDS details
378
	 * @deprecated - just set the classType - either in the init or the config
379
	 */
380
	public function ddsLoadGrid($classType, $includeDeleted=false, $pageSize=50)
0 ignored issues
show
Unused Code introduced by
The parameter $includeDeleted is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

380
	public function ddsLoadGrid($classType, /** @scrutinizer ignore-unused */ $includeDeleted=false, $pageSize=50)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pageSize is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

380
	public function ddsLoadGrid($classType, $includeDeleted=false, /** @scrutinizer ignore-unused */ $pageSize=50)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
381
	{
382
		if ($this->title == null)
383
			$this->title = $classType;
384
		// TODO: should be in constructor can't create a DDS grid without the DDS class ref
385
		$this->classType = $classType;
0 ignored issues
show
Bug Best Practice introduced by
The property classType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
386
387
		// set the data provider to be a DDS one!
388
		$dataProvider = new DdsDataProvider([
389
			'classType' => $classType,
390
		]);
391
		$this->setDataProvider($dataProvider);
392
393
		$formDefinition = neon()->phoebe->getFormDefinition('daedalus', $this->classType);
394
		$memberForm = new \neon\core\form\Form($formDefinition);
395
		foreach ($memberForm->getFields() as $member) {
396
			$this->addDdsColumn($member);
397
		}
398
		$this->addActionButtonColumn();
399
		return $this;
400
	}
401
402
	/**
403
	 * Add a column with the correct config
404
	 * @param IField $member
405
	 * @return column\Column
0 ignored issues
show
Bug introduced by
The type neon\core\grid\column\Column\Column was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
406
	 * @throws \Exception
407
	 */
408
	public function addDdsColumn(IField $member)
409
	{
410
		// get grid column class for member
411
		return $this->addColumn($member->getDataKey(), [
412
			'member' => $member,
413
			'class' => 'neon\core\grid\column\Column',
414
			'title' => $member->getLabel()
415
		]);
416
	}
417
}
418
419