1 <?php
2 /**
3 * @copyright Copyright © 2amigOS! Consulting Group 2013-
4 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
5 * @package foundation.components
6 * @version 1.0.0
7 */
8 namespace foundation\widgets;
9
10 use foundation\helpers\ArrayHelper;
11 use foundation\helpers\Html;
12 use foundation\enum\Enum;
13 use foundation\exception\InvalidConfigurationException;
14
15 /**
16 * Dropdown renders a foundation dropdown. It differs from MenuDropdown helper.
17 *
18 * @see http://foundation.zurb.com/docs/components/dropdown.html
19 *
20 * @author Antonio Ramirez <amigo.cobos@gmail.com>
21 * @package foundation\widgets
22 */
23 class Dropdown extends base\Widget
24 {
25 /**
26 * @var array list of menu items in the dropdown. Each array element represents a single
27 * menu with the following structure:
28 * - label: string, required, the label of the item link
29 * - url: string, optional, the url of the item link. Defaults to "#".
30 * - linkOptions: array, optional, the HTML attributes of the item link.
31 * - options: array, optional, the HTML attributes of the item.
32 *
33 * If its a content dropdown style, this property will be ignored and will $dropdownContent used instead.
34 */
35 public $items = array();
36 /**
37 * @var string the content to be rendered if $type = '
38 */
39 public $dropdownContent = '';
40 /**
41 * @var string $type the type of dropdown, whether is Enum::DROPDOWN_CONTENT or Enum::DROPDOWN_LIST
42 */
43 public $type = Enum::DROPDOWN_LIST;
44 /**
45 * @var boolean whether the labels for header items should be HTML-encoded.
46 */
47 public $encodeLabels = true;
48
49
50 /**
51 * Initializes the widget
52 */
53 public function init()
54 {
55 $this->assets = array(
56 'js' => YII_DEBUG ? 'foundation/foundation.dropdown.js' : 'foundation.min.js'
57 );
58
59 Html::addCssClass($this->htmlOptions, Enum::DROPDOWN_LIST);
60 ArrayHelper::addValue('data-dropdown-content', '-', $this->htmlOptions);
61
62 if ($this->type === Enum::DROPDOWN_CONTENT) {
63 Html::addCssClass($this->htmlOptions, Enum::CONTENT);
64 }
65
66 parent::init();
67 }
68
69 /**
70 * Renders the widget
71 */
72 public function run()
73 {
74 echo $this->renderItems();
75 }
76
77 /**
78 * Renders dropdown items
79 * @return string
80 * @throws InvalidConfigException
81 */
82 protected function renderItems()
83 {
84 $lines = array();
85
86 if ($this->type === Enum::DROPDOWN_CONTENT) {
87 $lines[] = $this->dropdownContent;
88 }
89 if ($this->type === Enum::DROPDOWN_LIST) {
90 foreach ($this->items as $item) {
91 if (is_string($item)) {
92 $lines[] = $item;
93 continue;
94 }
95 if (!isset($item['label'])) {
96 throw new InvalidConfigException("The 'label' option is required.");
97 }
98 $label = $this->encodeLabels ? \CHtml::encode($item['label']) : $item['label'];
99 $options = ArrayHelper::getValue($item, 'options', array());
100 $linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
101 $linkOptions['tabindex'] = '-1';
102 $content = \CHtml::link($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions);
103
104 $lines[] = \CHtml::tag('li', $options, $content);
105 }
106 }
107
108 return \CHtml::tag('ul', $this->htmlOptions, implode("\n", $lines));
109 }
110 }