|
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\HomePage\Integration\Joints\HomePageJoint; |
|
10
|
|
|
|
|
11
|
|
|
class HomePageSettings extends ModuleView |
|
12
|
|
|
{ |
|
13
|
|
|
protected $label = 'Home Page Administration'; |
|
14
|
|
|
|
|
15
|
|
|
protected $homepages; |
|
16
|
|
|
protected $grid; |
|
17
|
|
|
protected $form; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Fallback path in case no home page set for the user |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $defaultPath = 'view/user.settings'; |
|
25
|
|
|
|
|
26
|
|
|
public static function access() |
|
27
|
|
|
{ |
|
28
|
|
|
return Auth::user()->can('modify system settings') && self::getAvailableHomePages(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function body() |
|
32
|
|
|
{ |
|
33
|
|
|
ActionBar::addButton('back')->link(url('view/system')); |
|
34
|
|
|
|
|
35
|
|
|
$this->grid = $this->add([ |
|
36
|
|
|
'CRUD', |
|
37
|
|
|
'displayFields' => ['path', 'role'], |
|
38
|
|
|
'editFields' => ['path', 'role'], |
|
39
|
|
|
'notifyDefault' => ['jsNotify', 'content' => __('Data is saved!'), 'color' => 'green'], |
|
40
|
|
|
'paginator' => false |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$this->grid->setModel(HomePage::create()); |
|
44
|
|
|
|
|
45
|
|
|
$availablePages = self::getAvailableHomePages(); |
|
46
|
|
|
|
|
47
|
|
|
$this->grid->addDecorator('path', ['Multiformat', function($row, $column) use ($availablePages) { |
|
48
|
|
|
return [['Template', $availablePages[$row[$column]]?? '[' . __('missing: :path', ['path' => $row[$column]]) . ']']]; |
|
|
|
|
|
|
49
|
|
|
}]); |
|
50
|
|
|
|
|
51
|
|
|
$this->grid->addDragHandler()->onReorder(function ($order) { |
|
|
|
|
|
|
52
|
|
|
$result = true; |
|
53
|
|
|
foreach (HomePage::create() as $homepage) { |
|
54
|
|
|
$homepage['priority'] = array_search($homepage['id'], $order); |
|
55
|
|
|
|
|
56
|
|
|
$result &= $homepage->save(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $result? $this->notify(__('Homepages reordered!')): $this->notifyError(__('Error saving order!')); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Collect all home pages from module joints |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function getAvailableHomePages() |
|
69
|
|
|
{ |
|
70
|
|
|
static $cache; |
|
71
|
|
|
|
|
72
|
|
|
if (! isset($cache)) { |
|
73
|
|
|
$cache = []; |
|
74
|
|
|
foreach (HomePageJoint::collect() as $joint) { |
|
75
|
|
|
$cache[$joint->link()] = $joint->caption(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $cache; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the current user home page |
|
84
|
|
|
* |
|
85
|
|
|
* @return HomePage |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function getUserHomePage() |
|
88
|
|
|
{ |
|
89
|
|
|
if (! $user = Auth::user()) return; |
|
90
|
|
|
|
|
91
|
|
|
return HomePage::create()->addCondition('role', $user->roles()->pluck('name')->toArray())->loadAny(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get the current user home page path |
|
96
|
|
|
* |
|
97
|
|
|
* @return HomePage |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function getUserHomePagePath() |
|
100
|
|
|
{ |
|
101
|
|
|
$homepage = self::getUserHomePage(); |
|
102
|
|
|
|
|
103
|
|
|
return $homepage['path']?? self::$defaultPath; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|