]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SubPanel/SubPanelTilesTabs.php
Release 6.5.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-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 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         // Set up a mapping from subpanelID, found in the $tabs list, to the source module name
111         // As the $GLOBALS['tabStructure'] array holds the Group Tabs by module name we need to efficiently convert between the two
112         // when constructing the subpanel tabs
113         // Note that we can't use the very similar GroupedTabStructure class as it lacks this mapping, and logically, it is designed
114         // for use when constructing the module by module tabs, not the subpanel tabs, as we move away from using module names to represent
115         // subpanels, and use unique subpanel IDs instead.
116
117         $moduleNames = array () ;
118         foreach ( $tabs as $subpanelID )
119         {
120             // Bug #44344 : Custom relationships under same module only show once in subpanel tabs
121             // use object property instead new object to have ability run unit test (can override subpanel_definitions)
122             $subpanel =  $this->subpanel_definitions->load_subpanel( $subpanelID );
123                 if ($subpanel !== false)
124                   $moduleNames [ $subpanelID ] = $subpanel->get_module_name() ;
125         }
126
127         $groups =  array () ;
128         $found = array () ;
129
130         foreach( $GLOBALS['tabStructure'] as $mainTab => $subModules)
131         {
132             foreach( $subModules['modules'] as $key => $subModule )
133             {
134                         foreach ( $tabs as $subpanelID )
135                     if (isset($moduleNames[ $subpanelID ] ) && strcasecmp( $subModule , $moduleNames[ $subpanelID ] ) === 0)
136                     {
137                         // Bug #44344 : Custom relationships under same module only show once in subpanel tabs
138                         $groups [ translate ( $mainTab ) ] [ 'modules' ] [] = $subpanelID ;
139                         $found [ $subpanelID ] = true ;
140                         }
141             }
142         }
143
144         // Put all the remaining subpanels into the 'Other' tab.
145
146         foreach( $tabs as $subpanelID )
147         {
148                 if ( ! isset ( $found [ $subpanelID ] ) )
149                         $groups [ translate ('LBL_TABGROUP_OTHER') ]['modules'] [] = $subpanelID ;
150         }
151
152         /* Move history to same tab as activities */
153         if(in_array('history', $tabs) && in_array('activities', $tabs))
154         {
155             foreach($groups as $mainTab => $group)
156             {
157                 if(in_array('activities', array_map('strtolower', $group['modules'])))
158                 {
159                         if(!in_array('history', array_map('strtolower', $group['modules'])))
160                     {
161                         /* Move hist from there to here */
162                         $groups[$mainTab]['modules'] []= 'history';
163                     }
164                 }
165                 else if(false !== ($i = array_search('history', array_map('strtolower', $group['modules']))))
166                 {
167                     unset($groups[$mainTab]['modules'][$i]);
168                     if(empty($groups[$mainTab]['modules']))
169                     {
170                         unset($groups[$mainTab]);
171                     }
172                 }
173             }
174         }
175
176         /* Add the 'All' group.
177          * Note that if a tab group already exists with the name 'All',
178          * it will be overwritten in this union operation.
179          */
180         if(count($groups) <= 1)
181                 $groups = array(translate('LBL_TABGROUP_ALL') => array('label' => translate('LBL_TABGROUP_ALL'), 'modules' => $tabs));
182         else
183             $groups = array(translate('LBL_TABGROUP_ALL') => array('label' => translate('LBL_TABGROUP_ALL'), 'modules' => $tabs)) + $groups;
184         /* Note - all $display checking and array_intersects with $tabs
185          * are now redundant (thanks to GroupedTabStructure), and could
186          * be removed for performance, but for now can stay to help ensure
187          * that the tabs get renedered correctly.
188          */
189
190         $retTabs = array();
191         if($showTabs)
192         {
193                 require_once('include/SubPanel/SugarTab.php');
194                 $sugarTab = new SugarTab();
195
196             $displayTabs = array();
197             $otherTabs = array();
198
199             foreach ($groups as $key=>$tab)
200                 {
201                 $display = false;
202                 foreach($tab['modules'] as $subkey=>$subtab)
203                 {
204                     if(in_array(strtolower($subtab), $tabs))
205                     {
206                         $display = true;
207                         break;
208                     }
209                 }
210
211                 $selected = '';
212
213                 if($selectedGroup == $key)
214                 {
215                     $selected = 'current';
216                 }
217
218                 if($display)
219                 {
220                     $relevantTabs = SubPanelTilesTabs::applyUserCustomLayoutToTabs($tabs, $key);
221
222                     $sugarTabs[$key] = array(//'url'=>'index.php?module=' . $_REQUEST['module'] . '&record=' . $_REQUEST['record'] . '&action=' . $_REQUEST['action']. '&subpanel=' . $key.'#tabs',
223                                          //'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);",
224                                          'label'=>( !empty($tab['label']) ? $tab['label']: $key ),
225                                          'type'=>$selected);
226
227                     $otherTabs[$key] = array('key'=>$key, 'tabs'=>array());
228
229                     $orderedTabs = array_intersect($relevantTabs, array_map('strtolower', $groups[$key]['modules']));
230
231                     foreach($orderedTabs as $subkey => $subtab)
232                     {
233                         $otherTabs[$key]['tabs'][$subkey] = array('key'=>$subtab, 'label'=>translate($this->subpanel_definitions->layout_defs['subpanel_setup'][$subtab]['title_key']));
234                     }
235
236                     if($selectedGroup == $key)
237                     {
238                         $displayTabs = $otherTabs[$key]['tabs'];
239                         $retTabs = $orderedTabs;
240                     }
241                 }
242                 }
243
244             if(empty($displayTabs) && !empty($otherTabs))
245             {
246                 //WDong Bug: 12258 "All" tab in the middle of a record's detail view is not localized.
247                 $selectedGroup = translate('LBL_TABGROUP_ALL');
248                 $displayTabs = $otherTabs[$selectedGroup]['tabs'];
249                 $sugarTabs[$selectedGroup]['type'] = 'current';
250                 $retTabs = array_intersect($tabs, array_map('strtolower', $groups[$selectedGroup]['modules']));
251             }
252
253             if (!empty($sugarTabs) || !empty($otherTabs) ) {
254                 $sugarTab->setup($sugarTabs, $otherTabs, $displayTabs, $selectedGroup);
255                 $sugarTab->display();
256             }
257         }
258         else
259         {
260             $tabs = SubPanelTilesTabs::applyUserCustomLayoutToTabs($tabs, $selectedGroup);
261
262             $retTabs = array_intersect($tabs, array_map('strtolower', $groups[$selectedGroup]['modules']));
263         }
264
265                 return $retTabs;
266         }
267 }
268 ?>