Passed
Push — master ( 9b6cf0...d3c4d5 )
by Georgi
05:20
created

HomePageSettings::getHomepages()   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\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]]) . ']']];
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

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

51
		$this->grid->/** @scrutinizer ignore-call */ 
52
               addDragHandler()->onReorder(function ($order) {
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $homepage['path'] ?? self::defaultPath also could return the type string which is incompatible with the documented return type Epesi\Core\HomePage\Database\Models\HomePage.
Loading history...
104
	}
105
}
106