| Total Complexity | 12 |
| Total Lines | 85 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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() |
||
| 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() |
||
| 94 | } |
||
| 95 | } |
||
| 96 |