]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SubPanel/SubPanelDefinitions.php
Release 6.5.1
[Github/sugarcrm.git] / include / SubPanel / SubPanelDefinitions.php
1 <?php
2 if (! defined ( 'sugarEntry' ) || ! sugarEntry)
3         die ( 'Not A Valid Entry Point' ) ;
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7  * 
8  * This program is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Affero General Public License version 3 as published by the
10  * Free Software Foundation with the addition of the following permission added
11  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14  * 
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
18  * details.
19  * 
20  * You should have received a copy of the GNU Affero General Public License along with
21  * this program; if not, see http://www.gnu.org/licenses or write to the Free
22  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  * 02110-1301 USA.
24  * 
25  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27  * 
28  * The interactive user interfaces in modified source and object code versions
29  * of this program must display Appropriate Legal Notices, as required under
30  * Section 5 of the GNU Affero General Public License version 3.
31  * 
32  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33  * these Appropriate Legal Notices must retain the display of the "Powered by
34  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35  * technical reasons, the Appropriate Legal Notices must display the words
36  * "Powered by SugarCRM".
37  ********************************************************************************/
38
39
40
41
42
43 //input
44 //      module directory
45 //constructor
46 //      open the layout_definitions file.
47 //
48 /**
49  * Subpanel implementation
50  * @api
51  */
52 class aSubPanel
53 {
54
55         var $name ;
56         var $_instance_properties ;
57
58         var $mod_strings ;
59         var $panel_definition ;
60         var $sub_subpanels ;
61         var $parent_bean ;
62
63     /**
64      * Can we display this subpanel?
65      *
66      * This is set after it loads the def's for the subpanel.  If there are no beans to display in the collection
67      * we don't want to display this as it will just throw errors.
68      *
69      * @var bool
70      */
71     var $canDisplay = true;
72
73         //module's table name and column fields.
74         var $table_name ;
75         var $db_fields ;
76         var $bean_name ;
77         var $template_instance ;
78
79         function aSubPanel ( $name , $instance_properties , $parent_bean , $reload = false , $original_only = false )
80         {
81
82                 $this->_instance_properties = $instance_properties ;
83                 $this->name = $name ;
84                 $this->parent_bean = $parent_bean ;
85
86                 //set language
87                 global $current_language ;
88                 if (! isset ( $parent_bean->mbvardefs ))
89                 {
90                         $mod_strings = return_module_language ( $current_language, $parent_bean->module_dir ) ;
91                 }
92                 $this->mod_strings = $mod_strings ;
93
94         if ($this->isCollection ())
95                 {
96                         $this->canDisplay = $this->load_sub_subpanels () ; //load sub-panel definition.
97                 } else
98                 {
99                         if (!is_dir('modules/' . $this->_instance_properties [ 'module' ])){
100                                 _pstack_trace();
101                         }
102                         $def_path = 'modules/' . $this->_instance_properties [ 'module' ] . '/metadata/subpanels/' . $this->_instance_properties [ 'subpanel_name' ] . '.php' ;
103
104                         $orig_exists = is_file($def_path);
105                         $loaded = false;
106                         if ($orig_exists)
107                         {
108                                 require ($def_path);
109                                 $loaded = true;
110                         }
111                         if (is_file("custom/$def_path") && (!$original_only  || !$orig_exists))
112                         {
113                                 require ("custom/$def_path");
114                                 $loaded = true;
115                         }
116
117                         if (! $original_only && isset ( $this->_instance_properties [ 'override_subpanel_name' ] ) && file_exists ( 'custom/modules/' . $this->_instance_properties [ 'module' ] . '/metadata/subpanels/' . $this->_instance_properties [ 'override_subpanel_name' ] . '.php' ))
118                         {
119                                 $cust_def_path = 'custom/modules/' . $this->_instance_properties [ 'module' ] . '/metadata/subpanels/' . $this->_instance_properties [ 'override_subpanel_name' ] . '.php' ;
120                                 require ($cust_def_path) ;
121                                 $loaded = true;
122                         }
123
124                         if (!$loaded)
125                         {
126                                 $GLOBALS['log']->fatal("Failed to load original or custom subpanel data for $name in $def_path");
127                 $this->canDisplay = false;
128                         }
129
130                         // check that the loaded subpanel definition includes a $subpanel_layout section - some, such as projecttasks/default do not...
131                         $this->panel_definition = array () ;
132                         if (isset($subpanel_layout))
133                         {
134                                 $this->panel_definition = $subpanel_layout ;
135
136                         }
137                         $this->load_module_info () ; //load module info from the module's bean file.
138                 }
139
140         }
141
142     /**
143      * is the sub panel default hidden?
144      *
145      * @return bool
146      */
147     public function isDefaultHidden()
148     {
149         if(isset($this->_instance_properties['default_hidden']) && $this->_instance_properties['default_hidden'] == true) {
150             return true;
151         }
152
153         return false;
154     }
155
156
157         function distinct_query ()
158         {
159                 if (isset ( $this->_instance_properties [ 'get_distinct_data' ] ))
160                 {
161                         return !empty($this->_instance_properties['get_distinct_data']) ? true : false;
162                 }
163                 return false ;
164         }
165
166         //return the translated header value.
167         function get_title ()
168         {
169                 if (empty ( $this->mod_strings [ $this->_instance_properties [ 'title_key' ] ] ))
170                 {
171                         return translate ( $this->_instance_properties [ 'title_key' ], $this->_instance_properties [ 'module' ] ) ;
172                 }
173                 return $this->mod_strings [ $this->_instance_properties [ 'title_key' ] ] ;
174         }
175
176         //return the definition of buttons. looks for buttons in 2 locations.
177         function get_buttons ()
178         {
179                 $buttons = array ( ) ;
180                 if (isset ( $this->_instance_properties [ 'top_buttons' ] ))
181                 {
182                         //this will happen only in the case of sub-panels with multiple sources(activities).
183                         $buttons = $this->_instance_properties [ 'top_buttons' ] ;
184                 } else
185                 {
186                         $buttons = $this->panel_definition [ 'top_buttons' ] ;
187                 }
188
189                 // permissions. hide SubPanelTopComposeEmailButton from activities if email module is disabled.
190                 //only email is  being tested becuase other submodules in activites/history such as notes, tasks, meetings and calls cannot be disabled.
191                 //as of today these are the only 2 sub-panels that use the union clause.
192                 $mod_name = $this->get_module_name () ;
193                 if ($mod_name == 'Activities' || $mod_name == 'History')
194                 {
195                         global $modListHeader ;
196                         global $modules_exempt_from_availability_check ;
197                         if (isset ( $modListHeader ) && (! (array_key_exists ( 'Emails', $modListHeader ) or array_key_exists ( 'Emails', $modules_exempt_from_availability_check ))))
198                         {
199                                 foreach ( $buttons as $key => $button )
200                                 {
201                                         foreach ( $button as $property => $value )
202                                         {
203                                                 if ($value === 'SubPanelTopComposeEmailButton' || $value === 'SubPanelTopArchiveEmailButton')
204                                                 {
205                                                         //remove this button from the array.
206                                                         unset ( $buttons [ $key ] ) ;
207                                                 }
208                                         }
209                                 }
210                         }
211                 }
212
213                 return $buttons ;
214         }
215
216
217     /**
218      * Load the Sub-Panel objects if it can from the metadata files.
219      *
220      * call this function for sub-panels that have unions.
221      *
222      * @return bool         True by default if the subpanel was loaded.  Will return false if none in the collection are
223      *                      allowed by the current user.
224      */
225         function load_sub_subpanels ()
226         {
227
228                 global $modListHeader ;
229                 // added a check for security of tabs to see if an user has access to them
230                 // this prevents passing an "unseen" tab to the query string and pulling up its contents
231                 if (! isset ( $modListHeader ))
232                 {
233                         global $current_user ;
234                         if (isset ( $current_user ))
235                         {
236                                 $modListHeader = query_module_access_list ( $current_user ) ;
237                         }
238                 }
239
240         //by default all the activities modules are exempt, so hiding them won't affect their appearance unless the 'activity' subpanel itself is hidden.
241         //add email to the list temporarily so it is not affected in activities subpanel
242         global $modules_exempt_from_availability_check ;
243         $modules_exempt_from_availability_check['Emails'] = 'Emails';
244
245                 $listFieldMap = array();
246
247                 if (empty ( $this->sub_subpanels ))
248                 {
249                         $panels = $this->get_inst_prop_value ( 'collection_list' ) ;
250                         foreach ( $panels as $panel => $properties )
251                         {
252                                 if (array_key_exists ( $properties [ 'module' ], $modListHeader ) or array_key_exists ( $properties [ 'module' ], $modules_exempt_from_availability_check ))
253                                 {
254                                         $this->sub_subpanels [ $panel ] = new aSubPanel ( $panel, $properties, $this->parent_bean ) ;
255                                 }
256                         }
257             // if it's empty just dump out as there is nothing to process.
258             if(empty($this->sub_subpanels)) return false;
259                         //Sync displayed list fields across the subpanels
260                         $display_fields = $this->getDisplayFieldsFromCollection($this->sub_subpanels);
261                         $query_fields = array();
262                         foreach ( $this->sub_subpanels as $key => $subpanel )
263                         {
264                                 $list_fields = $subpanel->get_list_fields();
265                                 $listFieldMap[$key] = array();
266                                 $index = 0;
267                                 foreach($list_fields as $field => $def)
268                                 {
269                                         if (isset($def['vname']) && isset($def['width']))
270                                         {
271                                                 $index++;
272                                                 if(!empty($def['alias']))
273                                                         $listFieldMap[$key][$def['alias']] = $field;
274                                                 else
275                                                         $listFieldMap[$key][$field] = $field;
276                                                 if (!isset($display_fields[$def['vname']]))
277                                                 {
278                                                         if(sizeof($display_fields) > $index)
279                                                         {
280                                                                 //Try to insert the new field in an order that makes sense
281                                                                 $start = array_slice($display_fields, 0, $index);
282                                                                 $end = array_slice($display_fields, $index);
283                                                                 $display_fields = array_merge(
284                                                                         $start,
285                                                                         array($def['vname'] => array('name' => $field, 'vname' => $def['vname'], 'width' => $def['width'] )),
286                                                                         $end
287                                                                 );
288                                                         } else
289                                                         {
290                                                                 $display_fields[$def['vname']] = array(
291                                                                         'name' => empty($def['alias']) ? $field : $def['alias'],
292                                                                         'vname' => $def['vname'],
293                                                                         'width' => $def['width'],
294                                                                 );
295                                                         }
296                                                 }
297                                         } else {
298                                                 $query_fields[$field] = $def;
299                                         }
300                                 }
301                         }
302                         foreach ( $this->sub_subpanels as $key => $subpanel )
303                         {
304                                 $list_fields = array();
305                                 foreach($display_fields as $vname => $def)
306                                 {
307                                         $field = $def['name'];
308                                         $list_key = isset($listFieldMap[$key][$field]) ? $listFieldMap[$key][$field] : $field;
309
310                                         if (isset($subpanel->panel_definition['list_fields'][$field]))
311                                         {
312                                                 $list_fields[$field] = $subpanel->panel_definition['list_fields'][$field];
313                                         }
314                                     else if ($list_key != $field && isset($subpanel->panel_definition['list_fields'][$list_key]))
315                     {
316                         $list_fields[$list_key] = $subpanel->panel_definition['list_fields'][$list_key];
317
318                     }
319                                         else {
320                                                 $list_fields[$field] = $display_fields[$vname];
321                                         }
322                                 }
323                                 foreach($query_fields as $field => $def)
324                                 {
325                                         if (isset($subpanel->panel_definition['list_fields'][$field]))
326                                         {
327                                                 $list_fields[$field] = $subpanel->panel_definition['list_fields'][$field];
328                                         }
329                                         else {
330                                                 $list_fields[$field] = $def;
331                                         }
332                                 }
333                                 $subpanel->panel_definition['list_fields'] = $list_fields;
334                         }
335                 }
336
337         return true;
338         }
339
340         protected function getDisplayFieldsFromCollection($sub_subpanels)
341         {
342                 $display_fields = array();
343                 foreach ($sub_subpanels as $key => $subpanel )
344                 {
345                         $list_fields = $subpanel->get_list_fields();
346                         $index = 0;
347                         foreach($list_fields as $field => $def)
348                         {
349                                 if (isset($def['vname']) && isset($def['width']))
350                                 {
351                                         $index++;
352                                         if (!isset($display_fields[$def['vname']]))
353                                         {
354                                                 if(sizeof($display_fields) > $index)
355                                                 {
356                                                         //Try to insert the new field in an order that makes sense
357                                                         $start = array_slice($display_fields, 0, $index);
358                                                         $end = array_slice($display_fields, $index);
359                                                         $display_fields = array_merge(
360                                                                 $start,
361                                                                 array($def['vname'] => array('name' => $field, 'vname' => $def['vname'], 'width' => $def['width'] )),
362                                                                 $end
363                                                         );
364                                                 } else
365                                                 {
366                                                         $display_fields[$def['vname']] = array(
367                                                                 'name' => $field,
368                                                                 'vname' => $def['vname'],
369                                                                 'width' => $def['width'],
370                                                         );
371                                                 }
372                                         }
373                                 }
374                         }
375                 }
376         }
377
378         function isDatasourceFunction ()
379         {
380                 if (strpos ( $this->get_inst_prop_value ( 'get_subpanel_data' ), 'function' ) === false)
381                 {
382                         return false ;
383                 }
384                 return true ;
385         }
386
387     /**
388      * Test to see if the sub panels defs contain a collection
389      *
390      * @return bool
391      */
392         function isCollection ()
393         {
394                 return ($this->get_inst_prop_value ( 'type' ) == 'collection');
395         }
396
397         //get value of a property defined at the panel instance level.
398         function get_inst_prop_value ( $name )
399         {
400                 return isset($this->_instance_properties[$name]) ? $this->_instance_properties [ $name ] : null;
401         }
402         //get value of a property defined at the panel definition level.
403         function get_def_prop_value ( $name )
404         {
405                 if (isset ( $this->panel_definition [ $name ] ))
406                 {
407                         return $this->panel_definition [ $name ] ;
408                 } else
409                 {
410                         return null ;
411                 }
412         }
413
414         //if datasource is of the type function then return the function name
415         //else return the value as is.
416         function get_function_parameters ()
417         {
418                 $parameters = array ( ) ;
419                 if ($this->isDatasourceFunction ())
420                 {
421                         $parameters = $this->get_inst_prop_value ( 'function_parameters' ) ;
422                 }
423                 return $parameters ;
424         }
425
426         function get_data_source_name ( $check_set_subpanel_data = false )
427         {
428                 $prop_value = null ;
429                 if ($check_set_subpanel_data)
430                 {
431                         $prop_value = $this->get_inst_prop_value ( 'set_subpanel_data' ) ;
432                 }
433                 if (! empty ( $prop_value ))
434                 {
435                         return $prop_value ;
436                 } else
437                 {
438                         //fall back to default behavior.
439                 }
440                 if ($this->isDatasourceFunction ())
441                 {
442                         return (substr_replace ( $this->get_inst_prop_value ( 'get_subpanel_data' ), '', 0, 9 )) ;
443                 } else
444                 {
445                         return $this->get_inst_prop_value ( 'get_subpanel_data' ) ;
446                 }
447         }
448
449         //returns the where clause for the query.
450         function get_where ()
451         {
452                 return $this->get_def_prop_value ( 'where' ) ;
453         }
454
455         function is_fill_in_additional_fields ()
456         {
457                 // do both. inst_prop returns values from metadata/subpaneldefs.php and def_prop returns from subpanel/default.php
458                 $temp = $this->get_inst_prop_value ( 'fill_in_additional_fields' ) || $this->get_def_prop_value ( 'fill_in_additional_fields' ) ;
459                 return $temp ;
460         }
461
462         function get_list_fields ()
463         {
464                 if (isset ( $this->panel_definition [ 'list_fields' ] ))
465                 {
466                         return $this->panel_definition [ 'list_fields' ] ;
467                 } else
468                 {
469                         return array ( ) ;
470                 }
471         }
472
473         function get_module_name ()
474         {
475                 return $this->get_inst_prop_value ( 'module' ) ;
476         }
477
478         function get_name ()
479         {
480                 return $this->name ;
481         }
482
483         //load subpanel module's table name and column fields.
484         function load_module_info ()
485         {
486                 global $beanList ;
487                 global $beanFiles ;
488
489                 $module_name = $this->get_module_name () ;
490                 if (! empty ( $module_name ))
491                 {
492
493                         $bean_name = $beanList [ $this->get_module_name () ] ;
494
495                         $this->bean_name = $bean_name ;
496
497                         include_once ($beanFiles [ $bean_name ]) ;
498                         $this->template_instance = new $bean_name ( ) ;
499                         $this->template_instance->force_load_details = true ;
500                         $this->table_name = $this->template_instance->table_name ;
501                         //$this->db_fields=$this->template_instance->column_fields;
502                 }
503         }
504         //this function is to be used only with sub-panels that are based
505         //on collections.
506         function get_header_panel_def ()
507         {
508                 if (! empty ( $this->sub_subpanels ))
509                 {
510                         if (! empty ( $this->_instance_properties [ 'header_definition_from_subpanel' ] ) && ! empty ( $this->sub_subpanels [ $this->_instance_properties [ 'header_definition_from_subpanel' ] ] ))
511                         {
512                                 return $this->sub_subpanels [ $this->_instance_properties [ 'header_definition_from_subpanel' ] ] ;
513                         } else
514                         {
515                                 $display_fields = array();
516                                 //If we are not pulling from a specific subpanel, create a list of all list fields and use that.
517                                 foreach($this->sub_subpanels as $subpanel)
518                                 {
519                                         $list_fields = $subpanel->get_list_fields();
520                                         foreach($list_fields as $field => $def)
521                                         {
522
523                                         }
524                                 }
525
526                                 reset ( $this->sub_subpanels ) ;
527                                 return current ( $this->sub_subpanels ) ;
528                         }
529                 }
530                 return null ;
531         }
532
533         /**
534          * Returns an array of current properties of the class.
535          * It will simply give the class name for instances of classes.
536          */
537         function _to_array ()
538         {
539                 return array ( '_instance_properties' => $this->_instance_properties , 'db_fields' => $this->db_fields , 'mod_strings' => $this->mod_strings , 'name' => $this->name , 'panel_definition' => $this->panel_definition , 'parent_bean' => get_class ( $this->parent_bean ) , 'sub_subpanels' => $this->sub_subpanels , 'table_name' => $this->table_name , 'template_instance' => get_class ( $this->template_instance ) ) ;
540         }
541 }
542 ;
543
544 class SubPanelDefinitions
545 {
546
547         var $_focus ;
548         var $_visible_tabs_array ;
549         var $panels ;
550         var $layout_defs ;
551
552         /**
553          * Enter description here...
554          *
555          * @param BEAN $focus - this is the bean you want to get the data from
556          * @param STRING $layout_def_key - if you wish to use a layout_def defined in the default metadata/subpaneldefs.php that is not keyed off of $bean->module_dir pass in the key here
557          * @param ARRAY $layout_def_override - if you wish to override the default loaded layout defs you pass them in here.
558          * @return SubPanelDefinitions
559          */
560         function SubPanelDefinitions ( $focus , $layout_def_key = '' , $layout_def_override = '' )
561         {
562                 $this->_focus = $focus ;
563                 if (! empty ( $layout_def_override ))
564                 {
565                         $this->layout_defs = $layout_def_override ;
566
567                 } else
568                 {
569                         $this->open_layout_defs ( false, $layout_def_key ) ;
570                 }
571         }
572
573         /**
574          * This function returns an ordered list of all "tabs", actually subpanels, for this module
575          * The source list is obtained from the subpanel layout contained in the layout_defs for this module,
576          * found either in the modules metadata/subpaneldefs.php file, or in the modules custom/.../Ext/Layoutdefs/layoutdefs.ext.php file
577          * and filtered through an ACL check.
578          * Note that the keys for the resulting array of tabs are in practice the name of the underlying source relationship for the subpanel
579          * So for example, the key for a custom module's subpanel with Accounts might be 'one_one_accounts', as generated by the Studio Relationship Editor
580          * Although OOB module subpanels have keys such as 'accounts', which might on the face of it appear to be a reference to the related module, in fact 'accounts' is still the relationship name
581          * @param boolean       Optional - include the subpanel title label in the return array (false)
582          * @return array        All tabs that pass an ACL check
583          */
584         function get_available_tabs ($FromGetModuleSubpanels=false)
585         {
586                 global $modListHeader ;
587                 global $modules_exempt_from_availability_check ;
588
589                 if (isset ( $this->_visible_tabs_array ))
590                         return $this->_visible_tabs_array ;
591
592                 if (empty($modListHeader))
593                     $modListHeader = query_module_access_list($GLOBALS['current_user']);
594
595                 $this->_visible_tabs_array = array ( ) ; // bug 16820 - make sure this is an array for the later ksort
596
597                 if (isset ( $this->layout_defs [ 'subpanel_setup' ] )) // bug 17434 - belts-and-braces - check that we have some subpanels first
598                 {
599                         //retrieve list of hidden subpanels
600                         $hidden_panels = $this->get_hidden_subpanels();
601
602                         //activities is a special use case in that if it is hidden,
603                         //then the history tab should be hidden too.
604                         if(!empty($hidden_panels) && is_array($hidden_panels) && in_array('activities',$hidden_panels)){
605                                 //add history to list hidden_panels
606                                 $hidden_panels['history'] = 'history';
607                         }
608
609                         foreach ( $this->layout_defs [ 'subpanel_setup' ] as $key => $values_array )
610                         {
611                                 //exclude if this subpanel is hidden from admin screens
612                 $module = $key;
613                 if ( isset($values_array['module']) )
614                     $module = strtolower($values_array['module']);
615                                  if ($hidden_panels && is_array($hidden_panels) && (in_array($module, $hidden_panels) || array_key_exists($module, $hidden_panels)) ){
616                                         //this panel is hidden, skip it
617                                         continue;
618                                  }
619
620                                 // make sure the module attribute is set, else none of this works...
621                                 if ( !isset($values_array [ 'module' ])) {
622                                         $GLOBALS['log']->debug("SubPanelDefinitions->get_available_tabs(): no module defined in subpaneldefs for '$key' =>" . var_export($values_array,true) . " - ingoring subpanel defintion") ;
623                                         continue;
624                                 }
625
626                                 //check permissions.
627                                 $exempt = array_key_exists ( $values_array [ 'module' ], $modules_exempt_from_availability_check ) ;
628                                 $ok = $exempt || ( (! ACLController::moduleSupportsACL ( $values_array [ 'module' ] ) || ACLController::checkAccess ( $values_array [ 'module' ], 'list', true ) ) ) ;
629
630                                 $GLOBALS [ 'log' ]->debug ( "SubPanelDefinitions->get_available_tabs(): " . $key . "= " . ( $exempt ? "exempt " : "not exempt " .( $ok ? " ACL OK" : "" ) ) ) ;
631
632                                 if ( $ok )
633                                 {
634                                         while ( ! empty ( $this->_visible_tabs_array [ $values_array [ 'order' ] ] ) )
635                                         {
636                                                 $values_array [ 'order' ] ++ ;
637                                         }
638
639                                         $this->_visible_tabs_array [ $values_array ['order'] ] = ($FromGetModuleSubpanels) ? array($key=>$values_array['title_key']) : $key ;
640                                 }
641                         }
642                 }
643
644                 ksort ( $this->_visible_tabs_array ) ;
645                 return $this->_visible_tabs_array ;
646         }
647
648         /**
649          * Load the definition of the a sub-panel.
650          * Also the sub-panel is added to an array of sub-panels.
651          * use of reload has been deprecated, since the subpanel is initialized every time.
652      *
653      * @param string $name              The name of the sub-panel to reload
654      * @param boolean $reload           Reload the sub-panel (unused)
655      * @param boolean $original_only    Only load the original sub-panel and no custom ones
656      * @return boolean|aSubPanel        Returns aSubPanel object or boolean false if one is not found or it can't be
657      *      displayed due to ACL reasons.
658          */
659         function load_subpanel ( $name , $reload = false , $original_only = false )
660         {
661                 if (!is_dir('modules/' . $this->layout_defs [ 'subpanel_setup' ][ strtolower ( $name ) ] [ 'module' ]))
662                   return false;
663
664         $subpanel = new aSubPanel ( $name, $this->layout_defs [ 'subpanel_setup' ] [ strtolower ( $name ) ], $this->_focus, $reload, $original_only ) ;
665
666         // only return the subpanel object if we can display it.
667         if($subpanel->canDisplay == true) {
668             return $subpanel;
669         }
670
671         // by default return false so we don't show anything if it's not required.
672         return false;
673         }
674
675         /**
676          * Load the layout def file and associate the definition with a variable in the file.
677          */
678         function open_layout_defs ( $reload = false , $layout_def_key = '' , $original_only = false )
679         {
680                 $layout_defs [ $this->_focus->module_dir ] = array ( ) ;
681                 $layout_defs [ $layout_def_key ] = array ( ) ;
682
683                 if (empty ( $this->layout_defs ) || $reload || (! empty ( $layout_def_key ) && ! isset ( $layout_defs [ $layout_def_key ] )))
684                 {
685                         if (file_exists ( 'modules/' . $this->_focus->module_dir . '/metadata/subpaneldefs.php' ))
686                                 require ('modules/' . $this->_focus->module_dir . '/metadata/subpaneldefs.php') ;
687
688                         if (! $original_only && file_exists ( 'custom/modules/' . $this->_focus->module_dir . '/Ext/Layoutdefs/layoutdefs.ext.php' ))
689                                 require ('custom/modules/' . $this->_focus->module_dir . '/Ext/Layoutdefs/layoutdefs.ext.php') ;
690
691                         if (! empty ( $layout_def_key ))
692                                 $this->layout_defs = $layout_defs [ $layout_def_key ] ;
693                         else
694                                 $this->layout_defs = $layout_defs [ $this->_focus->module_dir ] ;
695
696                 }
697
698         }
699
700         /**
701          * Removes a tab from the list of loaded tabs.
702          * Returns true if successful, false otherwise.
703          * Hint: Used by Campaign's DetailView.
704          */
705         function exclude_tab ( $tab_name )
706         {
707                 $result = false ;
708                 //unset layout definition
709                 if (! empty ( $this->layout_defs [ 'subpanel_setup' ] [ $tab_name ] ))
710                 {
711                         unset ( $this->layout_defs [ 'subpanel_setup' ] [ $tab_name ] ) ;
712                 }
713                 //unset instance from _visible_tab_array
714                 if (! empty ( $this->_visible_tabs_array ))
715                 {
716                         $key = array_search ( $tab_name, $this->_visible_tabs_array ) ;
717                         if ($key !== false)
718                         {
719                                 unset ( $this->_visible_tabs_array [ $key ] ) ;
720                         }
721                 }
722                 return $result ;
723         }
724
725
726         /**
727          * return all available subpanels that belong to the list of tab modules.  You can optionally return all
728          * available subpanels, and also optionally group by module (prepends the key with the bean class name).
729          */
730         function get_all_subpanels( $return_tab_modules_only = true, $group_by_module = false )
731         {
732                 global $moduleList, $beanFiles, $beanList, $module;
733
734                 //use tab controller function to get module list with named keys
735                 require_once("modules/MySettings/TabController.php");
736                 $modules_to_check = TabController::get_key_array($moduleList);
737
738                 //change case to match subpanel processing later on
739                 $modules_to_check = array_change_key_case($modules_to_check);
740         // Append on the CampaignLog module, because that is where the subpanels point, not directly to Campaigns
741         $modules_to_check['campaignlog'] = "CampaignLog";
742
743
744                 $spd = '';
745                 $spd_arr = array();
746                 //iterate through modules and build subpanel array
747                 foreach($modules_to_check as $mod_name){
748
749                         //skip if module name is not in bean list, otherwise get the bean class name
750                         if(!isset($beanList[$mod_name])) continue;
751                         $class = $beanList[$mod_name];
752
753                         //skip if class name is not in file list, otherwise require the bean file and create new class
754                         if(!isset($beanFiles[$class]) || !file_exists($beanFiles[$class])) continue;
755
756                         //retrieve subpanels for this bean
757                         require_once($beanFiles[$class]);
758                         $bean_class = new $class();
759
760                         //create new subpanel definition instance and get list of tabs
761                         $spd = new SubPanelDefinitions($bean_class) ;
762                         $sub_tabs = $spd->get_available_tabs();
763
764                         //add each subpanel to array of total subpanles
765                         foreach( $sub_tabs as $panel_key){
766                                 $panel_key = strtolower($panel_key);
767                 $panel_module = $panel_key;
768                 if ( isset($spd->layout_defs['subpanel_setup'][$panel_key]['module']) )
769                     $panel_module = strtolower($spd->layout_defs['subpanel_setup'][$panel_key]['module']);
770                 //if module_only flag is set, only if it is also in module array
771                                 if($return_tab_modules_only && !array_key_exists($panel_module, $modules_to_check)) continue;
772                                 $panel_key_name = $panel_module;
773
774                                 //group_by_key_name is set to true, then array will hold an entry for each
775                                 //subpanel, with the module name prepended in the key
776                                 if($group_by_module) $panel_key_name = $class.'_'.$panel_key_name;
777                                 //add panel name to subpanel array
778                                 $spd_arr[$panel_key_name] = $panel_module;
779                         }
780                 }
781                 return  $spd_arr;
782         }
783
784         /*
785          * save array of hidden panels to mysettings category in config table
786          */
787         function set_hidden_subpanels($panels){
788                 $administration = new Administration();
789                 $serialized = base64_encode(serialize($panels));
790                 $administration->saveSetting('MySettings', 'hide_subpanels', $serialized);
791         }
792
793         /*
794          * retrieve hidden subpanels
795          */
796         function get_hidden_subpanels(){
797                 global $moduleList;
798
799                 //create variable as static to minimize queries
800                 static $hidden_subpanels = null;
801
802                 // if the static value is not already cached, then retrieve it.
803                 if(empty($hidden_subpanels))
804                 {
805
806                         //create Administration object and retrieve any settings for panels
807                         $administration = new Administration();
808                         $administration->retrieveSettings('MySettings');
809
810                         if(isset($administration->settings) && isset($administration->settings['MySettings_hide_subpanels'])){
811                                 $hidden_subpanels = $administration->settings['MySettings_hide_subpanels'];
812                                 $hidden_subpanels = trim($hidden_subpanels);
813
814                                 //make sure serialized string is not empty
815                                 if (!empty($hidden_subpanels)){
816                                         //decode and unserialize to retrieve the array
817                                         $hidden_subpanels = base64_decode($hidden_subpanels);
818                                         $hidden_subpanels = unserialize($hidden_subpanels);
819
820                                         //Ensure modules saved in the preferences exist.
821                                         //get user preference
822                                         //unserialize and add to array if not empty
823                                         $pref_hidden = array();
824                                         foreach($pref_hidden as $id => $pref_hidden_panel) {
825                                                 $hidden_subpanels[] = $pref_hidden_panel;
826                                         }
827
828
829                                 }else{
830                                         //no settings found, return empty
831                                         return $hidden_subpanels;
832                                 }
833                         }
834                         else
835                         {       //no settings found, return empty
836                                 return $hidden_subpanels;
837                         }
838                 }
839
840                 return $hidden_subpanels;
841         }
842
843
844 }
845 ?>