Passed
Push — master ( 8fde47...cc75d5 )
by Georgi
03:09
created

ActionBar::renderView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Epesi\Core\Layout\Seeds;
4
5
use atk4\ui\View;
6
use Illuminate\Support\Collection;
7
use Epesi\Core\System\Seeds\ActionButton;
8
9
class ActionBar extends View
10
{
11
	public $ui = 'actionbar segment';
12
	
13
	/**
14
	 * @var Collection
15
	 */
16
	protected static $buttons;
17
	
18
	protected static function getPredefined($key)
19
	{
20
		$predefined = [	
21
				'back' => [
22
						'label' => __('Back'),
23
						'icon' => 'arrow left',
24
						'weight' => 10000,
25
						'attr' => [
26
								'href' => $_SERVER['HTTP_REFERER']?? 'javascript:window.history.back()'
27
						],
28
				],
29
				'save' => [
30
						'label' => __('Save'),
31
						'icon' => 'save',
32
				],
33
				'edit' => [
34
						'label' => __('Edit'),
35
						'icon' => 'edit'
36
				],
37
				'delete' => [
38
						'label' => __('Delete'),
39
						'icon' => 'trash'
40
				],
41
		];
42
		
43
		return $predefined[$key]?? ['label' => $key];
44
	}
45
	
46
	public function __construct($label = null, $class = null)
47
	{
48
		parent::__construct($label, $class);
49
		
50
		self::$buttons = collect();
51
	}
52
	
53
	public function renderView()
54
	{
55
		$this->prepareButtons();
56
		
57
		parent::renderView();
58
	}
59
	
60
	protected function prepareButtons()
61
	{
62
		foreach (self::$buttons->sortByDesc(function ($button) {
63
			return $button->weight;
64
		}) as $button) {
65
			$this->add($button);
66
		}
67
	}
68
	
69
	public static function addButton($button)
70
	{
71
		if (is_string($button)) {
72
			$button = self::getPredefined($button);
73
		}
74
		
75
		if (is_array($button)) {
76
			$button = new ActionButton($button);
77
		}
78
		
79
		self::$buttons->add($button);
80
		
81
		return $button;
82
	}
83
	
84
	public static function addButtons($buttons)
85
	{
86
		foreach (is_array($buttons)? $buttons: [$buttons] as $button) {
87
			self::addButton($button);
88
		}
89
	}
90
91
	public static function clear()
92
	{
93
		self::$buttons = collect();
94
	}
95
}
96