]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SubPanel/SubPanelTiles.php
Release 6.2.0
[Github/sugarcrm.git] / include / SubPanel / SubPanelTiles.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
39
40 require_once('include/SubPanel/SubPanel.php');
41 require_once('include/SubPanel/SubPanelTilesTabs.php');
42 require_once('include/SubPanel/SubPanelDefinitions.php');
43
44 class SubPanelTiles
45 {
46         var $id;
47         var $module;
48         var $focus;
49         var $start_on_field;
50         var $layout_manager;
51         var $layout_def_key;
52         var $show_tabs = false;
53
54         var $subpanel_definitions;
55
56         var $hidden_tabs=array(); //consumer of this class can array of tabs that should be hidden. the tab name
57                                                         //should be the array.
58
59         function SubPanelTiles(&$focus, $layout_def_key='', $layout_def_override = '')
60         {
61                 $this->focus = $focus;
62                 $this->id = $focus->id;
63                 $this->module = $focus->module_dir;
64                 $this->layout_def_key = $layout_def_key;
65                 $this->subpanel_definitions=new SubPanelDefinitions($focus, $layout_def_key, $layout_def_override);
66         }
67
68         /*
69          * Return the current selected or requested subpanel tab
70          * @return      string  The identifier for the selected subpanel tab (e.g., 'Other')
71          */
72     function getSelectedGroup()
73     {
74         global $current_user;
75
76         if(isset($_REQUEST['subpanelTabs']))
77             $_SESSION['subpanelTabs'] = $_REQUEST['subpanelTabs'];
78
79         require_once 'include/tabConfig.php' ; // include/tabConfig.php in turn includes the customized file at custom/include/tabConfig.php...
80
81         $subpanelTabsPref = $current_user->getPreference('subpanel_tabs');
82         if(!isset($subpanelTabsPref)) $subpanelTabsPref = $GLOBALS['sugar_config']['default_subpanel_tabs'];
83         if(!empty($GLOBALS['tabStructure']) && (!empty($_SESSION['subpanelTabs']) || !empty($sugar_config['subpanelTabs']) || !empty($subpanelTabsPref)))
84         {
85             // Determine selected group
86             if(!empty($_REQUEST['subpanel']))
87             {
88                 $selected_group = $_REQUEST['subpanel'];
89             }
90             elseif(!empty($_COOKIE[$this->module.'_sp_tab']))
91             {
92                 $selected_group = $_COOKIE[$this->module.'_sp_tab'];
93             }
94             elseif(!empty($_SESSION['parentTab']) && !empty($GLOBALS['tabStructure'][$_SESSION['parentTab']]) && in_array($this->module, $GLOBALS['tabStructure'][$_SESSION['parentTab']]['modules']))
95             {
96                 $selected_group = $_SESSION['parentTab'];
97             }
98             else
99             {
100                 $selected_group = '';
101                 foreach($GLOBALS['tabStructure'] as $mainTab => $group)
102                 {
103                     if(in_array($this->module, $group['modules']))
104                     {
105                         $selected_group = $mainTab;
106                         break;
107                     }
108                 }
109                 if(!$selected_group)
110                 {
111                     $selected_group = 'All';
112                 }
113             }
114         }
115         else
116         {
117                 $selected_group = '';
118         }
119         return $selected_group;
120     }
121
122     /*
123      * Determine which subpanels should be shown within the selected tab group (e.g., 'Other');
124      * @param boolean $showTabs         True if we should call the code to render each visible tab
125      * @param string $selectedGroup     The requested tab group
126      * @return array Visible tabs
127      */
128     function getTabs($showTabs = true, $selectedGroup='')
129     {
130         global $current_user;
131
132         //get all the "tabs" - this actually means all the subpanels available for display within a tab
133         $tabs = $this->subpanel_definitions->get_available_tabs();
134
135         if(!empty($selectedGroup))
136         {
137                 return SubPanelTilesTabs::getTabs($tabs, $showTabs, $selectedGroup);
138             }
139         else
140         {
141             // see if user current user has custom subpanel layout
142             $tabs = SubPanelTilesTabs::applyUserCustomLayoutToTabs($tabs);
143
144             /* Check if the preference is set now,
145              * because there's no point in executing this code if
146              * we aren't going to render anything.
147              */
148             $subpanelLinksPref = $current_user->getPreference('subpanel_links');
149             if(!isset($subpanelLinksPref)) $subpanelLinksPref = $GLOBALS['sugar_config']['default_subpanel_links'];
150
151             if($showTabs && $subpanelLinksPref){
152                require_once('include/SubPanel/SugarTab.php');
153                $sugarTab = new SugarTab();
154
155                $displayTabs = array();
156
157                foreach($tabs as $tab){
158                    $displayTabs []= array('key'=>$tab, 'label'=>translate($this->subpanel_definitions->layout_defs['subpanel_setup'][$tab]['title_key']));
159                    //echo '<td nowrap="nowrap"><a class="subTabLink" href="#' . $tab . '">' .  translate($this->subpanel_definitions->layout_defs['subpanel_setup'][$tab]['title_key']) .  '</a></td><td> | </td>';
160                }
161                $sugarTab->setup(array(),array(),$displayTabs);
162                $sugarTab->display();
163             }
164             //echo '<td width="100%">&nbsp;</td></tr></table>';
165         }
166             return $tabs;
167
168         }
169         function display($showContainer = true, $forceTabless = false)
170         {
171                 global $layout_edit_mode, $sugar_version, $sugar_config, $current_user, $app_strings;
172                 if(isset($layout_edit_mode) && $layout_edit_mode){
173                         return;
174                 }
175
176                 global $modListHeader;
177
178                 ob_start();
179     echo '<script type="text/javascript" src="'. getJSPath('include/SubPanel/SubPanelTiles.js') . '"></script>';
180 ?>
181 <script>
182 if(document.DetailView != null &&
183    document.DetailView.elements != null &&
184    document.DetailView.elements.layout_def_key != null &&
185    typeof document.DetailView.elements['layout_def_key'] != 'undefined'){
186     document.DetailView.elements['layout_def_key'].value = '<?php echo $this->layout_def_key; ?>';
187 }
188 </script>
189 <?php
190
191                 $tabs = array();
192                 $default_div_display = 'inline';
193                 if(!empty($sugar_config['hide_subpanels_on_login'])){
194                         if(!isset($_SESSION['visited_details'][$this->focus->module_dir])){
195                                 setcookie($this->focus->module_dir . '_divs', '');
196                                 unset($_COOKIE[$this->focus->module_dir . '_divs']);
197                                 $_SESSION['visited_details'][$this->focus->module_dir] = true;
198
199                         }
200                         $default_div_display = 'none';
201                 }
202                 $div_cookies = get_sub_cookies($this->focus->module_dir . '_divs');
203
204
205                 //Display the group header. this section is executed only if the tabbed interface is being used.
206                 $current_key = '';
207                 if (! empty($this->show_tabs))
208                 {
209                         require_once('include/tabs.php');
210                 $tab_panel = new SugarWidgetTabs($tabs, $current_key, 'showSubPanel');
211                         echo get_form_header('Related', '', false);
212                         echo "<br />" . $tab_panel->display();
213                 }
214
215         if(empty($_REQUEST['subpanels']))
216         {
217             $selected_group = $forceTabless?'':$this->getSelectedGroup();
218             $usersLayout = $current_user->getPreference('subpanelLayout', $this->focus->module_dir);
219
220             // we need to use some intelligence here when restoring the user's layout, as new modules with new subpanels might have been installed since the user's layout was recorded
221             // this means that we can't just restore the old layout verbatim as the new subpanels would then go walkabout
222             // so we need to do a merge while attempting as best we can to preserve the sense of the specified order
223             // this is complicated by the different ordering schemes used in the two sources for the panels: the user's layout uses an ordinal layout, the panels from getTabs have an explicit ordering driven by the 'order' parameter
224             // it's not clear how to best reconcile these two schemes; so we punt on it, and add all new panels to the end of the user's layout. At least this will give them a clue that something has changed...
225             // we also now check for tabs that have been removed since the user saved his or her preferences.
226
227             $tabs = $this->getTabs($showContainer, $selected_group) ;
228
229             if(!empty($usersLayout))
230             {
231                 $availableTabs = $tabs ;
232                 $tabs = array_intersect ( $usersLayout , $availableTabs ) ; // remove any tabs that have been removed since the user's layout was saved
233                 foreach (array_diff ( $availableTabs , $usersLayout ) as $tab)
234                     $tabs [] = $tab ;
235             }
236         }
237         else
238         {
239                 $tabs = explode(',', $_REQUEST['subpanels']);
240         }
241
242         $tab_names = array();
243
244         if($showContainer)
245         {
246             echo '<ul class="noBullet" id="subpanel_list">';
247         }
248         //echo "<li id='hidden_0' style='height: 5px' class='noBullet'>&nbsp;&nbsp;&nbsp;</li>";
249         if (empty($GLOBALS['relationships'])) {
250                 if (!class_exists('Relationship')) {
251                         require('modules/Relationships/Relationship.php');
252                 }
253                 $rel= new Relationship();
254                 $rel->load_relationship_meta();
255         }
256
257         foreach ($tabs as $tab)
258                 {
259                         //load meta definition of the sub-panel.
260                         $thisPanel=$this->subpanel_definitions->load_subpanel($tab);
261             if ($thisPanel === false)
262                 continue;
263                         //this if-block will try to skip over ophaned subpanels. Studio/MB are being delete unloaded modules completely.
264                         //this check will ignore subpanels that are collections (activities, history, etc)
265                         if (!isset($thisPanel->_instance_properties['collection_list']) and isset($thisPanel->_instance_properties['get_subpanel_data']) ) {
266                                 //ignore when data source is a function
267
268                                 if (!isset($this->focus->field_defs[$thisPanel->_instance_properties['get_subpanel_data']])) {
269                                         if (stripos($thisPanel->_instance_properties['get_subpanel_data'],'function:') === false) {
270                                                 $GLOBALS['log']->fatal("Bad subpanel definition, it has incorrect value for get_subpanel_data property " .$tab);
271                                                 continue;
272                                         }
273                                 } else {
274                                         $rel_name='';
275                                         if (isset($this->focus->field_defs[$thisPanel->_instance_properties['get_subpanel_data']]['relationship'])) {
276                                                 $rel_name=$this->focus->field_defs[$thisPanel->_instance_properties['get_subpanel_data']]['relationship'];
277                                         }
278
279                                         if (empty($rel_name) or !isset($GLOBALS['relationships'][$rel_name])) {
280                                                 $GLOBALS['log']->fatal("Missing relationship definition " .$rel_name. ". skipping " .$tab ." subpanel");
281                                                 continue;
282                                         }
283                                 }
284                         }
285
286                         echo '<li class="noBullet" id="whole_subpanel_' . $tab . '">';
287
288                         $display= 'none';
289                         $div_display = $default_div_display;
290                         $cookie_name =   $tab . '_v';
291
292                         if(isset($div_cookies[$cookie_name])){
293                                 $div_display =  $div_cookies[$cookie_name];
294                         }
295                         if(!empty($sugar_config['hide_subpanels'])){
296                                 $div_display = 'none';
297                         }
298                         if($div_display == 'none'){
299                                 $opp_display  = 'inline';
300                         }else{
301                                 $opp_display  = 'none';
302                         }
303
304             if (!empty($this->layout_def_key) ) {
305                 $layout_def_key = $this->layout_def_key;
306             } else {
307                 $layout_def_key = '';
308             }
309
310                         if (empty($this->show_tabs))
311                         {
312                                 $show_icon_html = SugarThemeRegistry::current()->getImage( 'advanced_search', 'alt="' . translate('LBL_SHOW') . '" border="0 align="absmiddle""');
313                                 $hide_icon_html = SugarThemeRegistry::current()->getImage( 'basic_search', 'alt="' . translate('LBL_HIDE') . '" border="0" align="absmiddle"');
314                                 $max_min = "<a name=\"$tab\"> </a><span id=\"show_link_".$tab."\" style=\"display: $opp_display\"><a href='#' class='utilsLink' onclick=\"current_child_field = '".$tab."';showSubPanel('".$tab."',null,null,'".$layout_def_key."');document.getElementById('show_link_".$tab."').style.display='none';document.getElementById('hide_link_".$tab."').style.display='';return false;\">"
315                                         . "" . $show_icon_html . "</a></span>";
316                                 $max_min .= "<span id=\"hide_link_".$tab."\" style=\"display: $div_display\"><a href='#' class='utilsLink' onclick=\"hideSubPanel('".$tab."');document.getElementById('hide_link_".$tab."').style.display='none';document.getElementById('show_link_".$tab."').style.display='';return false;\">"
317                                  . "" . $hide_icon_html . "</a></span>";
318                                 echo '<div id="subpanel_title_' . $tab . '"';
319                 if(empty($sugar_config['lock_subpanels']) || $sugar_config['lock_subpanels'] == false) echo ' onmouseover="this.style.cursor = \'move\';"';
320                 echo '>' . get_form_header( $thisPanel->get_title(), $max_min, false) . '</div>';
321                         }
322
323             echo <<<EOQ
324 <div cookie_name="$cookie_name" id="subpanel_$tab" style="display:$div_display">
325     <script>document.getElementById("subpanel_$tab" ).cookie_name="$cookie_name";</script>
326 EOQ;
327             $display_spd = '';
328             if($div_display != 'none'){
329                 echo "<script>markSubPanelLoaded('$tab');</script>";
330                 $old_contents = ob_get_contents();
331                 @ob_end_clean();
332
333                 ob_start();
334                 include_once('include/SubPanel/SubPanel.php');
335                 $subpanel_object = new SubPanel($this->module, $_REQUEST['record'], $tab,$thisPanel,$layout_def_key);
336                 $subpanel_object->setTemplateFile('include/SubPanel/SubPanelDynamic.html');
337                 $subpanel_object->display();
338                 $subpanel_data = ob_get_contents();
339                 @ob_end_clean();
340
341                 ob_start();
342                 echo $this->get_buttons($thisPanel,$subpanel_object->subpanel_query);
343                 $buttons = ob_get_contents();
344                 @ob_end_clean();
345
346                 ob_start();
347                 echo $old_contents;
348                 //echo $buttons;
349                 $display_spd = $subpanel_data;
350             }
351             echo <<<EOQ
352     <div id="list_subpanel_$tab">$display_spd</div>
353 </div>
354 EOQ;
355                 array_push($tab_names, $tab);
356                 echo '</li>';
357         } // end $tabs foreach
358         if($showContainer)
359         {
360                 echo '</ul>';
361
362
363             if(!empty($selected_group))
364             {
365                 // closing table from tpls/singletabmenu.tpl
366                 echo '</td></tr></table>';
367             }
368         }
369         // drag/drop code
370         $tab_names = '["' . join($tab_names, '","') . '"]';
371         global $sugar_config;
372
373         if(empty($sugar_config['lock_subpanels']) || $sugar_config['lock_subpanels'] == false) {
374             echo <<<EOQ
375     <script>
376         var SubpanelInit = function() {
377                 SubpanelInitTabNames({$tab_names});
378         }
379         var SubpanelInitTabNames = function(tabNames) {
380                 subpanel_dd = new Array();
381                 j = 0;
382                 for(i in tabNames) {
383                         subpanel_dd[j] = new ygDDList('whole_subpanel_' + tabNames[i]);
384                         subpanel_dd[j].setHandleElId('subpanel_title_' + tabNames[i]);
385                         subpanel_dd[j].onMouseDown = SUGAR.subpanelUtils.onDrag;
386                         subpanel_dd[j].afterEndDrag = SUGAR.subpanelUtils.onDrop;
387                         j++;
388                 }
389
390                 YAHOO.util.DDM.mode = 1;
391         }
392         currentModule = '{$this->module}';
393         YAHOO.util.Event.addListener(window, 'load', SubpanelInit);
394     </script>
395 EOQ;
396         }
397
398                 $ob_contents = ob_get_contents();
399                 ob_end_clean();
400                 return $ob_contents;
401         }
402
403
404         function getLayoutManager()
405         {
406                 require_once('include/generic/LayoutManager.php');
407                 if ( $this->layout_manager == null) {
408                 $this->layout_manager = new LayoutManager();
409                 }
410                 return $this->layout_manager;
411         }
412
413         function get_buttons($thisPanel,$panel_query=null)
414         {
415                 $subpanel_def = $thisPanel->get_buttons();
416                 $layout_manager = $this->getLayoutManager();
417                 $widget_contents = '<span><table cellpadding="0" cellspacing="0"><tr>';
418                 foreach($subpanel_def as $widget_data)
419                 {
420                         $widget_data['query']=urlencode($panel_query);
421                         $widget_data['action'] = $_REQUEST['action'];
422                         $widget_data['module'] =  $thisPanel->get_inst_prop_value('module');
423                         $widget_data['focus'] = $this->focus;
424                         $widget_data['subpanel_definition'] = $thisPanel;
425                         $widget_contents .= '<td class="buttons">' . "\n";
426
427                         if(empty($widget_data['widget_class']))
428                         {
429                                 $widget_contents .= "widget_class not defined for top subpanel buttons";
430                         }
431                         else
432                         {
433                                 $widget_contents .= $layout_manager->widgetDisplay($widget_data);
434                         }
435
436                         $widget_contents .= '</td>';
437                 }
438
439                 $widget_contents .= '</tr></table></span>';
440                 return $widget_contents;
441         }
442 }
443 ?>