1 <?php
2
3 /*
4 * This file is part of the Brickrouge package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Brickrouge;
13
14 /**
15 * An actions element that can be used with forms or popovers.
16 */
17 class Actions extends Element
18 {
19 /**
20 * Actions.
21 *
22 * @var string|array
23 */
24 protected $actions;
25
26 /**
27 * @param boolean|array|string|Element $actions Actions can be defined as a boolean, an array
28 * a string or an instance of the {@link Element} class. @see {@link render_inner_html()}
29 *
30 * @param array $attributes
31 */
32 public function __construct($actions, array $attributes=array())
33 {
34 $this->actions = $actions;
35
36 parent::__construct('div', $attributes + array('class' => 'actions'));
37 }
38
39 /**
40 * Renders the actions.
41 *
42 * If actions are defined as the string "boolean" they are replaced by an array with the
43 * buttons `button[data-action="cancel"]` and
44 * `button[data-action="ok"][type=submit].btn-primary`.
45 *
46 * If actions are defined as a boolean, they are replaced by a
47 * `button[type=submit][data-action="ok"].btn-primary` element with the label "Send".
48 *
49 * If actions are defined as an array, the array is concatenated with the glue
50 * `<span class="separator"> </span>`.
51 *
52 * Otherwise actions are used as is.
53 *
54 * @see Element::render_inner_html()
55 */
56 protected function render_inner_html()
57 {
58 $html = parent::render_inner_html();
59 $actions = $this->actions;
60
61 if ($actions == 'boolean')
62 {
63 $actions = array
64 (
65 new Button('Cancel', array('data-action' => 'cancel')),
66 new Button('Ok', array('data-action' => 'ok', 'type' => 'submit', 'class' => 'btn-primary')),
67 );
68 }
69
70 if (is_array($actions))
71 {
72 foreach ($actions as $name => $action)
73 {
74 if (!is_string($name) || !($action instanceof Element) || $action['name'] !== null)
75 {
76 continue;
77 }
78
79 $action['name'] = $name;
80 }
81
82 $actions = implode('<span class="separator"> </span>', $actions);
83 }
84 else if ($actions === true)
85 {
86 $actions = new Button('Ok', array('dataset' => array('action' => 'ok'), 'type' => 'submit', 'class' => 'btn-primary'));
87 }
88
89 return $html . $actions;
90 }
91 }