]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SubPanel/SubPanelTilesTabs.php
Release 6.4.0
[Github/sugarcrm.git] / include / SubPanel / SubPanelTilesTabs.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-2011 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 require_once('include/SubPanel/SubPanel.php');
39 require_once('include/SubPanel/SubPanelDefinitions.php');
40 require_once('include/SubPanel/SubPanelTiles.php');
41 /**
42  * Tabbed subpanel tiles
43  * @api
44  */
45 class SubPanelTilesTabs extends SubPanelTiles
46 {
47
48         function SubPanelTiles(&$focus, $layout_def_key='', $layout_def_override = '')
49         {
50
51                 $this->focus = $focus;
52                 $this->id = $focus->id;
53                 $this->module = $focus->module_dir;
54                 $this->layout_def_key = $layout_def_key;
55                 $this->subpanel_definitions = new SubPanelDefinitions($focus, $layout_def_key, $layout_def_override);
56         }
57
58     function getSubpanelGroupLayout($selectedGroup)
59     {
60         global $current_user;
61
62         $layoutParams = $this->module;
63         //WDong Bug: 12258 "All" tab in the middle of a record's detail view is not localized.
64         if($selectedGroup != translate('LBL_TABGROUP_ALL'))
65         {
66             $layoutParams .= ':'.$selectedGroup;
67         }
68
69         // see if user current user has custom subpanel layout
70         return $current_user->getPreference('subpanelLayout', $layoutParams);
71     }
72
73     function applyUserCustomLayoutToTabs($tabs, $key='All')
74     {
75         //WDong Bug: 12258 "All" tab in the middle of a record's detail view is not localized.
76         if($key=='All')
77         {
78                 $key=translate('LBL_TABGROUP_ALL');
79         }
80         $usersCustomLayout = SubPanelTilesTabs::getSubpanelGroupLayout($key);
81         if(!empty($usersCustomLayout))
82         {
83             /* Return elements of the custom layout
84              * which occur in $tabs in unchanged order.
85              * Then append elements of $tabs which are
86              * not included in the layout. */
87             $diff = array_diff($tabs, $usersCustomLayout);
88             $tabs = array_intersect($usersCustomLayout, $tabs);
89             foreach($diff as $subpanel)
90             {
91                 $tabs []= $subpanel;
92             }
93         }
94
95         return $tabs;
96     }
97
98     /*
99      * Place subpanels into tabs for display on a DetailView
100      * @param array $tabs       Array containing the ids of all subpanels to be placed into tabs
101      * @param boolean $showTabs Call the view code to display the generated tabs
102      * @param string $selectedGroup     (Optional) Name of any selected tab (defaults to 'All')
103      */
104         function getTabs($tabs, $showTabs = true, $selectedGroup='All')
105     {
106         //WDong Bug: 12258 "All" tab in the middle of a record's detail view is not localized.
107         if($selectedGroup=='All')
108                 $selectedGroup=translate('LBL_TABGROUP_ALL');
109
110         require_once 'include/SubPanel/SubPanelDefinitions.php' ;
111         $spd = new SubPanelDefinitions ( $this->focus ) ;
112
113         // Set up a mapping from subpanelID, found in the $tabs list, to the source module name
114         // As the $GLOBALS['tabStructure'] array holds the Group Tabs by module name we need to efficiently convert between the two
115         // when constructing the subpanel tabs
116         // Note that we can't use the very similar GroupedTabStructure class as it lacks this mapping, and logically, it is designed
117         // for use when constructing the module by module tabs, not the subpanel tabs, as we move away from using module names to represent
118         // subpanels, and use unique subpanel IDs instead.
119
120         $moduleNames = array () ;
121         foreach ( $tabs as $subpanelID )
122         {
123                 $subpanel =  $spd->load_subpanel( $subpanelID );
124                 if ($subpanel !== false)
125                   $moduleNames [ $subpanelID ] = $subpanel->get_module_name() ;
126         }
127
128         $groups =  array () ;
129         $found = array () ;
130
131         foreach( $GLOBALS['tabStructure'] as $mainTab => $subModules)
132         {
133             foreach( $subModules['modules'] as $key => $subModule )
134             {
135                         foreach ( $tabs as $subpanelID )
136                     if (isset($moduleNames[ $subpanelID ] ) && strcasecmp( $subModule , $moduleNames[ $subpanelID ] ) === 0)
137                     {
138                         $groups [ translate ( $mainTab ) ] [ 'modules' ] [ $key ] = $subpanelID ;
139                         $found [ $subpanelID ] = true ;
140                         break;
141                         }
142             }
143         }
144
145         // Put all the remaining subpanels into the 'Other' tab.
146
147         foreach( $tabs as $subpanelID )
148         {
149                 if ( ! isset ( $found [ $subpanelID ] ) )
150                         $groups [ translate ('LBL_TABGROUP_OTHER') ]['modules'] [] = $subpanelID ;
151         }
152
153         /* Move history to same tab as activities */
154         if(in_array('history', $tabs) && in_array('activities', $tabs))
155         {
156             foreach($groups as $mainTab => $group)
157             {
158                 if(in_array('activities', array_map('strtolower', $group['modules'])))
159                 {
160                         if(!in_array('history', array_map('strtolower', $group['modules'])))
161                     {
162                         /* Move hist from there to here */
163                         $groups[$mainTab]['modules'] []= 'history';
164                     }
165                 }
166                 else if(false !== ($i = array_search('history', array_map('strtolower', $group['modules']))))
167                 {
168                     unset($groups[$mainTab]['modules'][$i]);
169                     if(empty($groups[$mainTab]['modules']))
170                     {
171                         unset($groups[$mainTab]);
172                     }
173                 }
174             }
175         }
176
177         /* Add the 'All' group.
178          * Note that if a tab group already exists with the name 'All',
179          * it will be overwritten in this union operation.
180          */
181         if(count($groups) <= 1)
182                 $groups = array(translate('LBL_TABGROUP_ALL') => array('label' => translate('LBL_TABGROUP_ALL'), 'modules' => $tabs));
183         else
184             $groups = array(translate('LBL_TABGROUP_ALL') => array('label' => translate('LBL_TABGROUP_ALL'), 'modules' => $tabs)) + $groups;
185         /* Note - all $display checking and array_intersects with $tabs
186          * are now redundant (thanks to GroupedTabStructure), and could
187          * be removed for performance, but for now can stay to help ensure
188          * that the tabs get renedered correctly.
189          */
190
191         $retTabs = array();
192         if($showTabs)
193         {
194                 require_once('include/SubPanel/SugarTab.php');
195                 $sugarTab = new SugarTab();
196
197             $displayTabs = array();
198             $otherTabs = array();
199
200             foreach ($groups as $key=>$tab)
201                 {
202                 $display = false;
203                 foreach($tab['modules'] as $subkey=>$subtab)
204                 {
205                     if(in_array(strtolower($subtab), $tabs))
206                     {
207                         $display = true;
208                         break;
209                     }
210                 }
211
212                 $selected = '';
213
214                 if($selectedGroup == $key)
215                 {
216                     $selected = 'current';
217                 }
218
219                 if($display)
220                 {
221                     $relevantTabs = SubPanelTilesTabs::applyUserCustomLayoutToTabs($tabs, $key);
222
223                     $sugarTabs[$key] = array(//'url'=>'index.php?module=' . $_REQUEST['module'] . '&record=' . $_REQUEST['record'] . '&action=' . $_REQUEST['action']. '&subpanel=' . $key.'#tabs',
224                                          //'url'=>"javascript:SUGAR.util.retrieveAndFill('index.php?to_pdf=1&module=MySettings&action=LoadTabSubpanels&loadModule={$_REQUEST['module']}&record={$_REQUEST['record']}&subpanel=$key','subpanel_list',null,null,null);",
225                                          'label'=>( !empty($tab['label']) ? $tab['label']: $key ),
226                                          'type'=>$selected);
227
228                     $otherTabs[$key] = array('key'=>$key, 'tabs'=>array());
229
230                     $orderedTabs = array_intersect($relevantTabs, array_map('strtolower', $groups[$key]['modules']));
231
232                     foreach($orderedTabs as $subkey => $subtab)
233                     {
234                         $otherTabs[$key]['tabs'][$subkey] = array('key'=>$subtab, 'label'=>translate($this->subpanel_definitions->layout_defs['subpanel_setup'][$subtab]['title_key']));
235                     }
236
237                     if($selectedGroup == $key)
238                     {
239                         $displayTabs = $otherTabs[$key]['tabs'];
240                         $retTabs = $orderedTabs;
241                     }
242                 }
243                 }
244
245             if(empty($displayTabs) && !empty($otherTabs))
246             {
247                 //WDong Bug: 12258 "All" tab in the middle of a record's detail view is not localized.
248                 $selectedGroup = translate('LBL_TABGROUP_ALL');
249                 $displayTabs = $otherTabs[$selectedGroup]['tabs'];
250                 $sugarTabs[$selectedGroup]['type'] = 'current';
251                 $retTabs = array_intersect($tabs, array_map('strtolower', $groups[$selectedGroup]['modules']));
252             }
253
254             if (!empty($sugarTabs) || !empty($otherTabs) ) {
255                 $sugarTab->setup($sugarTabs, $otherTabs, $displayTabs, $selectedGroup);
256                 $sugarTab->display();
257             }
258         }
259         else
260         {
261             $tabs = SubPanelTilesTabs::applyUserCustomLayoutToTabs($tabs, $selectedGroup);
262
263             $retTabs = array_intersect($tabs, array_map('strtolower', $groups[$selectedGroup]['modules']));
264         }
265
266                 return $retTabs;
267         }
268 }
269 ?>