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]]) . ']']]; |
|
|
|
|
54
|
|
|
}]); |
55
|
|
|
|
56
|
|
|
$this->grid->addDragHandler()->onReorder(function ($order) { |
|
|
|
|
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) { |
|
|
|
|
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; |
|
|
|
|
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|