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