ActionBar::renderView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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