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