]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.sugar_action_menu.php
Release 6.5.1
[Github/sugarcrm.git] / include / Smarty / plugins / function.sugar_action_menu.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /**
39  * Smarty plugin
40  * @package Smarty
41  * @subpackage plugins
42  */
43
44 /**
45  * Smarty plugin:
46  * This is a Smarty plugin to handle the creation of HTML List elements for Sugar Action Menus.
47  * Based on the theme, the plugin generates a proper group of button lists.
48  *
49  * @param $params array - its structure is
50  *     'buttons' => list of button htmls, such as ( html_element1, html_element2, ..., html_element_n),
51  *     'id' => id property for ul element
52  *     'class' => class property for ul element
53  *         'flat' => controls the display of the menu as a dropdown or flat buttons (if the value is assigned, it will be not affected by enable_action_menu setting.)
54  * @param $smarty
55  *
56  * @return string - compatible sugarActionMenu structure, such as
57  * <ul>
58  *     <li>html_element1
59  *         <ul>
60  *              <li>html_element2</li>
61  *                  ...
62  *              </li>html_element_n</li>
63  *         </ul>
64  *     </li>
65  * </ul>
66  * ,which is generated by @see function smarty_function_sugar_menu
67  *
68  * <pre>
69  * 1. SugarButton on smarty
70  *
71  * add appendTo to generate button lists
72  * {{sugar_button ... appendTo='buttons'}}
73  *
74  * ,and then create menu
75  * {{sugar_action_menu ... buttons=$buttons ...}}
76  *
77  * 2. Code generate in PHP
78  * <?php
79  * ...
80  *
81  * $buttons = array(
82  *      '<input ...',
83  *      '<a href ...',
84  *      ...
85  * );
86  * require_once('include/Smarty/plugins/function.sugar_action_menu.php');
87  * $action_button = smarty_function_sugar_action_menu(array(
88  *     'id' => ...,
89  *     'buttons' => $buttons,
90  *     ...
91  * ),$xtpl);
92  * $template->assign("ACTION_BUTTON", $action_button);
93  * ?>
94  * 3. Passing array to smarty in PHP
95  * $action_button = array(
96  *      'id' => 'id',
97  *      'buttons' => array(
98  *          '<input ...',
99  *          '<a href ...',
100  *          ...
101  *      ),
102  *      ...
103  * );
104  * $tpl->assign('action_button', $action_button);
105  * in the template file
106  * {sugar_action_menu params=$action_button}
107  *
108  * 4. Append button element in the Smarty
109  * {php}
110  * $this->append('buttons', "<a ...");
111  * $this->append('buttons', "<input ...");
112  * {/php}
113  * {{sugar_action_menu ... buttons=$buttons ...}}
114  * </pre>
115  *
116  * @author Justin Park (jpark@sugarcrm.com)
117  */
118 function smarty_function_sugar_action_menu($params, &$smarty)
119 {
120     global $sugar_config;
121
122     if( !empty($params['params']) ) {
123         $addition_params = $params['params'];
124         unset($params['params']);
125         $params = array_merge_recursive($params, $addition_params);
126     }
127     $flat = isset($params['flat']) ? $params['flat'] : (isset($sugar_config['enable_action_menu']) ? !$sugar_config['enable_action_menu'] : false);
128     //if buttons have not implemented, it returns empty string;
129     if(empty($params['buttons']))
130         return '';
131
132     if(is_array($params['buttons']) && !$flat) {
133
134         $menus = array(
135             'html' => array_shift($params['buttons']),
136             'items' => array()
137         );
138
139         foreach($params['buttons'] as $item) {
140             if(is_array($item)) {
141                 $sub = array();
142                 $sub_first = array_shift($item);
143                 foreach($item as $subitem) {
144                     $sub[] = array(
145                         'html' => $subitem
146                     );
147                 }
148                 array_push($menus['items'],array(
149                     'html' => $sub_first,
150                     'items' => $sub,
151                     'submenuHtmlOptions' => array(
152                         'class' => 'subnav-sub'
153                     )
154                 ));
155             } else if(strlen($item)) {
156                 array_push($menus['items'],array(
157                    'html' => $item
158                 ));
159             }
160         }
161         $action_menu = array(
162             'id' => !empty($params['id']) ? (is_array($params['id']) ? $params['id'][0] : $params['id']) : '',
163             'htmlOptions' => array(
164                 'class' => !empty($params['class']) && strpos($params['class'], 'clickMenu') !== false  ? $params['class'] : 'clickMenu '. (!empty($params['class']) ? $params['class'] : ''),
165                 'name' => !empty($params['name']) ? $params['name'] : '',
166             ),
167             'itemOptions' => array(
168                 'class' => (count($menus['items']) == 0) ? 'single' : 'sugar_action_button'
169             ),
170             'submenuHtmlOptions' => array(
171                 'class' => 'subnav'
172             ),
173             'items' => array(
174                 $menus
175             )
176         );
177         require_once('function.sugar_menu.php');
178         return smarty_function_sugar_menu($action_menu, $smarty);
179
180     }
181
182     if (is_array($params['buttons'])) {
183         return '<div class="action_buttons">' . implode_r(' ', $params['buttons'], true).'<div class="clear"></div></div>';
184     } else if(is_array($params)) {
185         return '<div class="action_buttons">' . implode_r(' ', $params, true).'<div class="clear"></div></div>';
186     }
187
188     return $params['buttons'];
189 }
190
191 function implode_r($glue, $pieces, $extract_first_item = false) {
192     $result = array_shift($pieces);
193     if(is_array($result)) {
194         $result = implode_r($glue, $result);
195     }
196     foreach($pieces as $item) {
197         if(is_array($item)) {
198             $result .= empty($extract_first_item) ? implode_r($glue, $item) : $glue.$item[0];
199         } else {
200             $result .= $glue.$item;
201         }
202     }
203     return $result;
204 }
205 ?>