Passed
Push — master ( 195422...889b95 )
by Georgi
03:03
created

HomePageSettings::access()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Epesi\Core\HomePage;
4
5
use Epesi\Core\System\Integration\Modules\ModuleView;
6
use Illuminate\Support\Facades\Auth;
7
use Epesi\Core\HomePage\Database\Models\HomePage;
8
use Epesi\Core\Layout\Seeds\ActionBar;
9
use Epesi\Core\System\Seeds\Form;
10
use Spatie\Permission\Models\Role;
0 ignored issues
show
Bug introduced by
The type Spatie\Permission\Models\Role 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...
11
12
class HomePageSettings extends ModuleView
13
{
14
	protected $label = 'Home Page Administration';
15
	
16
	protected $homepages;
17
	protected $grid;
18
	protected $form;
19
	
20
	public static function access()
21
	{
22
		return Auth::user()->can('modify system settings');
23
	}
24
	
25
	public function body()
26
	{
27
		ActionBar::addButton('back')->link('view/system');
28
29
		$this->grid = $this->add([
30
				'CRUD',
31
				'itemCreate' => ActionBar::addButton('add'),
32
				'formCreate' => $this->getHomepageForm(),
33
				'formUpdate' => $this->getHomepageForm(),
34
				'notifyDefault' => ['jsNotify', 'content' => __('Data is saved!'), 'color'   => 'green'],
35
				'canDelete' => false, //use custom delete routine
36
				'paginator' => false,
37
				'menu' => false
38
		]);
39
		
40
		$this->grid->setModel($this->getModel());
41
		
42
		$availablePages = HomePageCommon::getAvailableHomePages();
43
		
44
		$this->grid->addDecorator('path', ['Multiformat', function($row, $column) use ($availablePages) {
1 ignored issue
show
Bug introduced by
The method addDecorator() does not exist on atk4\ui\View. It seems like you code against a sub-type of atk4\ui\View such as atk4\ui\Grid or atk4\ui\Table. ( Ignorable by Annotation )

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

44
		$this->grid->/** @scrutinizer ignore-call */ 
45
               addDecorator('path', ['Multiformat', function($row, $column) use ($availablePages) {
Loading history...
45
			return [['Template', $availablePages[$row[$column]]]];
46
		}]);
47
		
48
		$this->grid->addDragHandler()->onReorder(function ($order) {
0 ignored issues
show
Bug introduced by
The method addDragHandler() does not exist on atk4\ui\View. It seems like you code against a sub-type of atk4\ui\View such as atk4\ui\Grid. ( Ignorable by Annotation )

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

48
		$this->grid->/** @scrutinizer ignore-call */ 
49
               addDragHandler()->onReorder(function ($order) {
Loading history...
49
			$result = true;
50
			foreach ($this->getHomepages() as $homepage) {
51
				$homepage->priority = array_search($homepage->id, $order);
52
				
53
				$result &= $homepage->save();
54
			}
55
			
56
			return $result? $this->notify(__('Homepages reordered!')): $this->notifyError(__('Error saving order!'));
57
		});
58
		
59
		$this->grid->addAction(['icon' => 'red trash'], function ($jschain, $id) {
0 ignored issues
show
Bug introduced by
The method addAction() does not exist on atk4\ui\View. It seems like you code against a sub-type of atk4\ui\View such as atk4\ui\Grid or atk4\ui\FormField\Input. ( Ignorable by Annotation )

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

59
		$this->grid->/** @scrutinizer ignore-call */ 
60
               addAction(['icon' => 'red trash'], function ($jschain, $id) {
Loading history...
60
			HomePage::find($id)->delete();
61
				
62
			return $jschain->closest('tr')->transition('fade left');
63
		}, __('Are you sure?'));
64
	}
65
	
66
	public function getModel()
67
	{
68
		$availablePages = HomePageCommon::getAvailableHomePages();
69
		$availableRoles = Role::get()->pluck('name')->all();
70
71
		$rows = [];
72
		foreach ($this->getHomepages() as $homepage) {
73
			$rows[] = [
74
					'id' => $homepage->id,
75
					'path' => $homepage->path,
76
					'role' => $homepage->role
77
			];
78
		}
79
		
80
		$model = new \atk4\data\Model(new \atk4\data\Persistence_Static($rows));
81
		
82
		$model->hasField('path')->setDefaults(['caption' => __('Page'), 'values' => $availablePages]);
83
		$model->hasField('role')->setDefaults(['caption' => __('Role'), 'enum' => $availableRoles]);
84
85
		return $model;
86
	}
87
	
88
	public function getHomepages()
89
	{
90
		return $this->homepages = $this->homepages?? HomePage::orderBy('priority')->get();
91
	}
92
	
93
	public function getHomepageForm()
94
	{
95
		if (! $this->form) {		
96
			$this->form = new Form(['buttonSave' => ['Button', __('Save'), 'primary']]);
97
	
98
			$this->form->addHook('submit', function($form) {
99
				$values = $form->getValues();
100
				
101
				if ($id = $values['id']?? null) {
102
					HomePage::find($id)->update($values);
103
					
104
					return $this->grid->jsSaveUpdate();
105
				}
106
	
107
				HomePage::create(array_merge($values, [
108
						'priority' => HomePage::max('priority') + 1
109
				]));
110
	
111
				return $this->grid->jsSaveCreate();
112
			});
113
		}
114
115
		return $this->form;
116
	}
117
}
118