Passed
Push — master ( 4954b5...87483c )
by Georgi
03:11
created

HomePageSettings   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 68
c 3
b 0
f 0
dl 0
loc 161
rs 10
wmc 21

8 Methods

Rating   Name   Duplication   Size   Complexity  
A access() 0 3 2
A getAvailableHomePages() 0 12 3
A getUserHomePagePath() 0 5 2
A getHomepages() 0 3 1
A getModel() 0 27 5
A body() 0 39 3
A getHomepageForm() 0 23 3
A getUserHomePage() 0 5 2
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;
11
use Epesi\Core\HomePage\Integration\Joints\HomePageJoint;
12
13
class HomePageSettings extends ModuleView
14
{
15
	protected $label = 'Home Page Administration';
16
	
17
	protected $homepages;
18
	protected $grid;
19
	protected $form;
20
	
21
	/**
22
	 * Fallback path in case no home page set for the user
23
	 *
24
	 * @var string
25
	 */
26
	protected static $defaultPath = 'view/user.settings';
27
	
28
	public static function access()
29
	{
30
		return Auth::user()->can('modify system settings') && self::getAvailableHomePages();
31
	}
32
	
33
	public function body()
34
	{
35
		ActionBar::addButton('back')->link(url('view/system'));
36
37
		$this->grid = $this->add([
38
				'CRUD',
39
				'itemCreate' => ActionBar::addButton('add'),
40
				'formCreate' => $this->getHomepageForm(),
41
				'formUpdate' => $this->getHomepageForm(),
42
				'notifyDefault' => ['jsNotify', 'content' => __('Data is saved!'), 'color'   => 'green'],
43
				'canDelete' => false, //use custom delete routine
44
				'paginator' => false,
45
				'menu' => false
46
		]);
47
		
48
		$this->grid->setModel($this->getModel());
49
		
50
		$availablePages = self::getAvailableHomePages();
51
52
		$this->grid->addDecorator('path', ['Multiformat', function($row, $column) use ($availablePages) {
53
			return [['Template', $availablePages[$row[$column]]?? '[' . __('missing: :path', ['path' => $row[$column]]) . ']']];
0 ignored issues
show
Bug introduced by
Are you sure __('missing: :path', arr...ath' => $row[$column])) of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

53
			return [['Template', $availablePages[$row[$column]]?? '[' . /** @scrutinizer ignore-type */ __('missing: :path', ['path' => $row[$column]]) . ']']];
Loading history...
54
		}]);
55
		
56
		$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

56
		$this->grid->/** @scrutinizer ignore-call */ 
57
               addDragHandler()->onReorder(function ($order) {
Loading history...
57
			$result = true;
58
			foreach ($this->getHomepages() as $homepage) {
59
				$homepage->priority = array_search($homepage->id, $order);
60
				
61
				$result &= $homepage->save();
62
			}
63
			
64
			return $result? $this->notify(__('Homepages reordered!')): $this->notifyError(__('Error saving order!'));
65
		});
66
		
67
		$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

67
		$this->grid->/** @scrutinizer ignore-call */ 
68
               addAction(['icon' => 'red trash'], function ($jschain, $id) {
Loading history...
68
			HomePage::find($id)->delete();
69
				
70
			return $jschain->closest('tr')->transition('fade left');
71
		}, __('Are you sure?'));
72
	}
73
	
74
	public function getModel()
75
	{
76
		$availablePages = self::getAvailableHomePages();
77
		$availableRoles = Role::get()->pluck('name')->all();
78
79
		$rows = [];
80
		foreach ($this->getHomepages() as $homepage) {
81
			$rows[] = [
82
					'id' => $homepage->id,
83
					'path' => $homepage->path,
84
					'role' => $homepage->role
85
			];
86
		}
87
		
88
		$rowsEmpty = [];
89
		
90
		$model = new \atk4\data\Model($rows? new \atk4\data\Persistence_Static($rows): new \atk4\data\Persistence_Array($rowsEmpty));
91
	
92
		$pathField = $rows? $model->hasField('path'): $model->addField('path');
93
			
94
		$roleField = $rows? $model->hasField('role'): $model->addField('role');
95
		
96
		$pathField->setDefaults(['caption' => __('Page'), 'values' => $availablePages]);
97
98
		$roleField->setDefaults(['caption' => __('Role'), 'enum' => $availableRoles]);
99
100
		return $model;
101
	}
102
	
103
	public function getHomepages()
104
	{
105
		return $this->homepages = $this->homepages?? HomePage::orderBy('priority')->get();
106
	}
107
	
108
	public function getHomepageForm()
109
	{
110
		if (! $this->form) {		
111
			$this->form = new Form(['buttonSave' => ['Button', __('Save'), 'primary']]);
112
	
113
			$this->form->addHook('submit', function($form) {
114
				$values = $form->getValues();
115
				
116
				if ($id = $values['id']?? null) {
117
					HomePage::find($id)->update($values);
118
					
119
					return $this->grid->jsSaveUpdate();
120
				}
121
	
122
				HomePage::create(array_merge($values, [
123
						'priority' => HomePage::max('priority') + 1
124
				]));
125
	
126
				return $this->grid->jsSaveCreate();
127
			});
128
		}
129
130
		return $this->form;
131
	}
132
	
133
	/**
134
	 * Collect all home pages from module joints
135
	 *
136
	 * @return array
137
	 */
138
	public static function getAvailableHomePages()
139
	{
140
		static $cache;
141
		
142
		if (! isset($cache)) {
143
			$cache = [];
144
			foreach (HomePageJoint::collect() as $joint) {
145
				$cache[$joint->link()] = $joint->caption();
146
			}
147
		}
148
		
149
		return $cache;
150
	}
151
	
152
	/**
153
	 * Get the current user home page
154
	 *
155
	 * @return HomePage
156
	 */
157
	public static function getUserHomePage()
158
	{
159
		if (! $user = Auth::user()) return;
160
		
161
		return HomePage::whereIn('role', $user->roles()->pluck('name'))->orderBy('priority')->first();
162
	}
163
	
164
	/**
165
	 * Get the current user home page path
166
	 *
167
	 * @return HomePage
168
	 */
169
	public static function getUserHomePagePath()
170
	{
171
		$homepage = self::getUserHomePage();
172
		
173
		return $homepage? $homepage->path: self::$defaultPath;
0 ignored issues
show
introduced by
$homepage is of type Epesi\Core\HomePage\Database\Models\HomePage, thus it always evaluated to true.
Loading history...
Bug Best Practice introduced by
The expression return $homepage ? $home...ath : self::defaultPath returns the type string which is incompatible with the documented return type Epesi\Core\HomePage\Database\Models\HomePage.
Loading history...
174
	}
175
}
176