1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Epesi\Core\HomePage\Database\Models; |
4
|
|
|
|
5
|
|
|
use Epesi\Core\Data\Model; |
6
|
|
|
use Spatie\Permission\Models\Role; |
7
|
|
|
use Epesi\Core\HomePage\Integration\Joints\HomePageJoint; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
|
10
|
|
|
class HomePage extends Model |
11
|
|
|
{ |
12
|
|
|
public $table = 'home_pages'; |
13
|
|
|
|
14
|
|
|
public $caption = 'Home Page'; |
15
|
|
|
|
16
|
|
|
public $title_field = 'path'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Fallback path in case no home page set for the user |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected static $defaultPath = 'view/user.settings'; |
24
|
|
|
|
25
|
|
|
function init() { |
26
|
|
|
parent::init(); |
27
|
|
|
|
28
|
|
|
$this->addFields([ |
29
|
|
|
[ |
30
|
|
|
'path', |
31
|
|
|
'caption' => __('Page'), |
32
|
|
|
'values' => self::list(), |
33
|
|
|
'ui' => [ |
34
|
|
|
'table' => [ |
35
|
|
|
'KeyValue', |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
], |
39
|
|
|
['role', 'caption' => __('Role'), 'enum' => Role::get()->pluck('name')->all()], |
40
|
|
|
'priority', |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$this->setOrder('priority'); |
44
|
|
|
|
45
|
|
|
$this->addHook('beforeInsert', function($model, & $data) { |
46
|
|
|
$data['priority'] = $data['priority']?: $this->action('fx', ['max', 'priority'])->getOne() + 1; |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Collect all home pages from module joints |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public static function list() |
56
|
|
|
{ |
57
|
|
|
static $cache; |
58
|
|
|
|
59
|
|
|
if (! isset($cache)) { |
60
|
|
|
$cache = []; |
61
|
|
|
foreach (HomePageJoint::collect() as $joint) { |
62
|
|
|
$cache[$joint->link()] = $joint->caption(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $cache; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the current user home page |
71
|
|
|
* |
72
|
|
|
* @return HomePage|null |
73
|
|
|
*/ |
74
|
|
|
public static function ofUser() |
75
|
|
|
{ |
76
|
|
|
if (! $user = Auth::user()) return; |
77
|
|
|
|
78
|
|
|
return self::addCondition('role', $user->roles()->pluck('name')->toArray())->loadAny(); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the current user home page path |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public static function pathOfUser() |
87
|
|
|
{ |
88
|
|
|
return HomePage::ofUser()['path']?: self::$defaultPath; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|