1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * @package application.modules.mobile.components
39: */
40: class MenuList extends X2Widget {
41:
42: /**
43: * @var array list of menu items. Each menu item is specified as an array of name-value pairs.
44: * Possible option names include the following:
45: * <ul>
46: * <li>label: string, optional, specifies the menu item label. When {@link encodeLabel} is true, the label
47: * will be HTML-encoded. If the label is not specified, it defaults to an empty string.</li>
48: * <li>url: string or array, optional, specifies the URL of the menu item. It is passed to {@link CHtml::normalizeUrl}
49: * to generate a valid URL. If this is not set, the menu item will be rendered as a span text.</li>
50: * <li>active: boolean, optional, whether this menu item is in active state (currently selected).
51: * </ul>
52: */
53: public $items = array();
54:
55: /**
56: * @var string the HTML element name that will be used to wrap the label of all menu links.
57: * For example, if this property is set as 'span', a menu item may be rendered as
58: * <li><a href="url"><span>label</span></a></li>
59: * This is useful when implementing menu items using the sliding window technique.
60: * Defaults to null, meaning no wrapper tag will be generated.
61: * @since 0.6
62: */
63: public $linkLabelWrapper;
64: private $rightOptions = array('data-role' => 'button', 'data-icon' => 'arrow-r', 'data-iconpos' => 'right', );
65: private $leftOptions = array('data-role' => 'button', 'data-icon' => 'arrow-l', 'data-iconpos' => 'left', 'data-direction' => "reverse" );
66: private $cs;
67:
68: public function init() {
69: parent::init();
70: $this->cs=Yii::app()->clientScript;
71: }
72:
73: /**
74: * Calls {@link renderMenu} to render the menu.
75: */
76: public function run() {
77: $this->renderMenu($this->items);
78: }
79:
80: /**
81: * Renders the menu items.
82: * @param array $items menu items. Each menu item will be an array with at least two elements: 'label' and 'url'.
83: * It may have one other optional element: 'active'.
84: * @since 0.6
85: */
86: protected function renderMenu($items) {
87: if (count($items)) {
88: echo CHtml::openTag('div') . "\n";
89: $this->renderMenuRecursive($items);
90: echo CHtml::closeTag('div');
91: }
92: }
93:
94: protected function renderMenuRecursive($items) {
95: $n = count($items);
96: foreach ($items as $item) {
97: $active = isset($item['active']) ? $item['active'] : true;
98: if ($active) {
99: echo CHtml::openTag('p');
100: $menu = $this->renderMenuItem($item);
101: echo $menu;
102: echo CHtml::closeTag('p') . "\n";
103: }
104: }
105: }
106:
107: /**
108: * Renders the content of a menu item.
109: * Note that the container and the sub-menus are not rendered here.
110: * @param array $item the menu item to be rendered. Please see {@link items} on what data might be in the item.
111: * @since 0.6
112: */
113: protected function renderMenuItem($item) {
114: if (isset($item['url'])) {
115: $url = $item['url'];
116:
117: $params = array();
118: if (!is_string($url)) {
119: $route = $url[0];
120: $params = array_splice($url,1);
121: }
122: // absolute url needed for phonegap app
123: $route = $this->getController()->createAbsoluteUrl($route,$params);
124: //Yii::trace('url|route='.(is_string ($url) ? $url : 'Array').'|'.$route);
125: $label = $this->linkLabelWrapper === null ? $item['label'] : '<' . $this->linkLabelWrapper . '>' . $item['label'] . '</' . $this->linkLabelWrapper . '>';
126: $options = X2Html::mergeHtmlOptions (
127: isset ($item['left']) ? $this->leftOptions : $this->rightOptions,
128: isset ($item['linkOptions']) ? $item['linkOptions'] : array ());
129: return CHtml::link($label, $route, $options);
130: }
131: }
132:
133: }
134:
135: ?>
136: