]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SubPanel/SubPanel.php
Release 6.2.0
[Github/sugarcrm.git] / include / SubPanel / SubPanel.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40
41
42
43
44
45
46 require_once('include/SubPanel/registered_layout_defs.php');
47
48 //AG todo remove subpanedata and subpanel id  from this class...
49 class SubPanel
50 {
51         var $hideNewButton = false;
52         var $subpanel_id;
53         var $parent_record_id;
54         var $parent_module;  // the name of the parent module
55         var $parent_bean;  // the instantiated bean of the parent
56         var $template_file;
57         var $linked_fields;
58         var $action = 'DetailView';
59         var $show_select_button = true;
60         var $subpanel_define = null;  // contains the layout_def.php
61         var $subpanel_defs; 
62         var $subpanel_query=null;
63     var $layout_def_key='';
64         function SubPanel($module, $record_id, $subpanel_id, $subpanelDef, $layout_def_key='')
65         {
66                 global $theme, $beanList, $beanFiles, $focus, $app_strings;
67         
68                 $this->subpanel_defs=$subpanelDef;
69                 $this->subpanel_id = $subpanel_id;
70                 $this->parent_record_id = $record_id; 
71                 $this->parent_module = $module; 
72         $this->layout_def_key = $layout_def_key;
73
74                 $this->parent_bean = $focus;
75                 $result = $focus;
76
77                 if(empty($result))
78                 {
79                         $parent_bean_name = $beanList[$module];
80                         $parent_bean_file = $beanFiles[$parent_bean_name];
81                         require_once($parent_bean_file);
82                         $this->parent_bean = new $parent_bean_name();
83             $this->parent_bean->retrieve($this->parent_record_id);
84             $result = $this->parent_bean;
85                 }
86                 
87                 if($record_id!='fab4' && $result == null)
88                 {
89                         sugar_die($app_strings['ERROR_NO_RECORD']);
90                 }
91
92                 if (empty($subpanelDef)) {
93                         //load the subpanel by name.
94                         if (!class_exists('MyClass')) {
95                                 require_once 'include/SubPanel/SubPanelDefinitions.php' ;
96                         }
97                         $panelsdef=new SubPanelDefinitions($result,$layout_def_key);
98                         $subpanelDef=$panelsdef->load_subpanel($subpanel_id);
99                         $this->subpanel_defs=$subpanelDef;
100                         
101                 }
102                 
103         }
104
105         function setTemplateFile($template_file)
106         {
107                 $this->template_file = $template_file;
108         }
109         
110         function setBeanList(&$value){
111                 $this->bean_list =$value;
112         }
113         
114         function setHideNewButton($value){
115                 $this->hideNewButton = $value;
116         }
117         
118         
119         function getHeaderText( $currentModule){
120         }
121         
122         function get_buttons( $panel_query=null)
123         {
124                 
125                 $thisPanel =& $this->subpanel_defs;
126                 $subpanel_def = $thisPanel->get_buttons();
127
128                 if(!isset($this->listview)){
129                         $this->listview = new ListView();
130                 }
131                 $layout_manager = $this->listview->getLayoutManager();
132                 $widget_contents = '<div><table cellpadding="0" cellspacing="0"><tr>';
133                 foreach($subpanel_def as $widget_data)
134                 {
135                         $widget_data['query']=urlencode($panel_query);
136                         $widget_data['action'] = $_REQUEST['action'];
137                         $widget_data['module'] =  $thisPanel->get_inst_prop_value('module');
138                         $widget_data['focus'] = $this->parent_bean;
139                         $widget_data['subpanel_definition'] = $thisPanel;
140                         $widget_contents .= '<td style="padding-right: 2px; padding-bottom: 2px;">' . "\n";
141
142                         if(empty($widget_data['widget_class']))
143                         {
144                                 $widget_contents .= "widget_class not defined for top subpanel buttons";
145                         }
146                         else
147                         {
148                                 $widget_contents .= $layout_manager->widgetDisplay($widget_data);
149                         }
150                         
151                         $widget_contents .= '</td>';
152                 }
153                 
154                 $widget_contents .= '</tr></table></div>';
155                 return $widget_contents;
156         }
157
158
159         function ProcessSubPanelListView($xTemplatePath, &$mod_strings)
160         {
161                 global $app_strings;
162                 global $current_user;
163                 global $sugar_config;
164                 
165                 if(isset($this->listview)){
166                         $ListView =& $this->listview;   
167                 }else{
168                         $ListView = new ListView();
169                 }
170                 $ListView->initNewXTemplate($xTemplatePath,$this->subpanel_defs->mod_strings);
171                 $ListView->xTemplateAssign("RETURN_URL", "&return_module=".$this->parent_module."&return_action=DetailView&return_id=".$this->parent_bean->id);
172                 $ListView->xTemplateAssign("RELATED_MODULE", $this->parent_module);  // TODO: what about unions?
173                 $ListView->xTemplateAssign("RECORD_ID", $this->parent_bean->id);
174                 $ListView->xTemplateAssign("EDIT_INLINE_PNG", SugarThemeRegistry::current()->getImage('edit_inline','align="absmiddle" alt="'.$app_strings['LNK_EDIT'].'" border="0"'));
175                 $ListView->xTemplateAssign("DELETE_INLINE_PNG", SugarThemeRegistry::current()->getImage('delete_inline','align="absmiddle" alt="'.$app_strings['LNK_DELETE'].'" border="0"'));
176                 $ListView->xTemplateAssign("REMOVE_INLINE_PNG", SugarThemeRegistry::current()->getImage('delete_inline','align="absmiddle" alt="'.$app_strings['LNK_REMOVE'].'" border="0"'));
177                 $header_text= '';
178         
179                 if(is_admin($current_user) && $_REQUEST['module'] != 'DynamicLayout' && !empty($_SESSION['editinplace']))
180                 {       
181                         $exploded = explode('/', $xTemplatePath);
182                         $file_name = $exploded[sizeof($exploded) - 1];
183                         $mod_name =  $exploded[sizeof($exploded) - 2];
184                         $header_text= "&nbsp;<a href='index.php?action=index&module=DynamicLayout&from_action=$file_name&from_module=$mod_name&mod_lang="
185                                 .$_REQUEST['module']."'>".SugarThemeRegistry::current()->getImage("EditLayout","border='0' alt='Edit Layout' align='bottom'")."</a>";
186                 }
187                 $ListView->setHeaderTitle('');
188                 $ListView->setHeaderText('');
189                 
190                 ob_start();
191
192                 $ListView->is_dynamic = true;
193                 $ListView->records_per_page = $sugar_config['list_max_entries_per_subpanel'] + 0;
194                 $ListView->start_link_wrapper = "javascript:showSubPanel('".$this->subpanel_id."','";
195                 $ListView->subpanel_id = $this->subpanel_id;
196                 $ListView->end_link_wrapper = "',true);";
197         if ( !empty($this->layout_def_key) ) {
198             $ListView->end_link_wrapper = '&layout_def_key='.$this->layout_def_key.$ListView->end_link_wrapper;
199         }
200
201                 $where = '';
202                 $ListView->setQuery($where, '', '', '');
203                 $ListView->show_export_button = false;
204                 
205                 //function returns the query that was used to populate sub-panel data.
206
207                 $query=$ListView->process_dynamic_listview($this->parent_module, $this->parent_bean,$this->subpanel_defs);
208                 $this->subpanel_query=$query; 
209                 $ob_contents = ob_get_contents();
210                 ob_end_clean();
211                 return $ob_contents;
212         }
213
214         function display()
215         {       
216                 global $timedate;
217                 global $mod_strings;
218                 global $app_strings;
219                 global $app_list_strings;
220                 global $beanList;
221                 global $beanFiles;
222                 global $current_language;                                                                                            
223
224                 $result_array = array();
225                 
226                 $return_string = $this->ProcessSubPanelListView($this->template_file,$result_array);
227
228                 print $return_string;
229         }
230        
231         function getModulesWithSubpanels()
232         {
233                 global $beanList;
234                 $dir = dir('modules');
235                 $modules = array();
236                 while($entry = $dir->read())
237                 {
238                         if(file_exists('modules/' . $entry . '/layout_defs.php'))
239                         {
240                                 $modules[$entry] = $entry;      
241                         }       
242                 }
243                 return $modules;
244         }
245   
246   function getModuleSubpanels($module){
247         require_once('include/SubPanel/SubPanelDefinitions.php');
248                 global $beanList, $beanFiles;
249                 if(!isset($beanList[$module])){
250                         return array();
251                 }
252                         
253                 $class = $beanList[$module];
254                 require_once($beanFiles[$class]);
255                 $mod = new $class();
256                 $spd = new SubPanelDefinitions($mod);
257                 $tabs = $spd->get_available_tabs(true);
258                 $ret_tabs = array();
259                 $reject_tabs = array('history'=>1, 'activities'=>1);
260                 foreach($tabs as $key=>$tab){
261                     foreach($tab as $k=>$v){
262                 if (! isset ( $reject_tabs [$k] )) {
263                     $ret_tabs [$k] = $v;
264                 }
265             }
266                 }
267  
268                 return $ret_tabs;
269   
270                 
271   }
272   
273   //saves overrides for defs
274   function saveSubPanelDefOverride( $panel, $subsection, $override){
275                 global $layout_defs, $beanList;
276                 
277                 //save the new subpanel
278                 $name = "subpanel_layout['list_fields']";
279                 
280                 //bugfix: load looks for moduleName/metadata/subpanels, not moduleName/subpanels
281                 $path = 'custom/modules/'. $panel->_instance_properties['module'] . '/metadata/subpanels';
282                 
283                 //bug# 40171: "Custom subpanels not working as expected" 
284                 //each custom subpanel needs to have a unique custom def file
285                 $filename = $panel->parent_bean->object_name . "_subpanel_" . $panel->name; //bug 42262 (filename with $panel->_instance_properties['get_subpanel_data'] can create problem if had word "function" in it)
286                 $oldName1 = '_override' . $panel->parent_bean->object_name .$panel->_instance_properties['module'] . $panel->_instance_properties['subpanel_name'] ;
287                 $oldName2 = '_override' . $panel->parent_bean->object_name .$panel->_instance_properties['get_subpanel_data'] ;
288                 if (file_exists('custom/Extension/modules/'. $panel->parent_bean->module_dir . "/Ext/Layoutdefs/$oldName1.php")){       
289                   unlink('custom/Extension/modules/'. $panel->parent_bean->module_dir . "/Ext/Layoutdefs/$oldName1.php");
290                 }
291                 if (file_exists('custom/Extension/modules/'. $panel->parent_bean->module_dir . "/Ext/Layoutdefs/$oldName2.php")){ 
292          unlink('custom/Extension/modules/'. $panel->parent_bean->module_dir . "/Ext/Layoutdefs/$oldName2.php");
293                 }
294                 $extname = '_override'.$filename;
295                 //end of bug# 40171
296                 
297                 mkdir_recursive($path, true);
298                 write_array_to_file( $name, $override,$path.'/' . $filename .'.php');
299                 
300                 //save the override for the layoutdef
301                 $name = "layout_defs['".  $panel->parent_bean->module_dir. "']['subpanel_setup']['" .strtolower($panel->name). "']"; // tyoung 10.12.07 pushed panel->name to lowercase to match case in subpaneldefs.php files - gave error on bad index 'module' as this override key didn't match the key in the subpaneldefs
302 //      $GLOBALS['log']->debug('SubPanel.php->saveSubPanelDefOverride(): '.$name);
303                 $newValue = override_value_to_string($name, 'override_subpanel_name', $filename);
304                 mkdir_recursive('custom/Extension/modules/'. $panel->parent_bean->module_dir . '/Ext/Layoutdefs', true);
305                 $fp = sugar_fopen('custom/Extension/modules/'. $panel->parent_bean->module_dir . "/Ext/Layoutdefs/$extname.php", 'w');
306                 fwrite($fp, "<?php\n//auto-generated file DO NOT EDIT\n$newValue\n?>");
307                 fclose($fp);
308                 require_once('ModuleInstall/ModuleInstaller.php');
309                 $moduleInstaller = new ModuleInstaller();
310                 $moduleInstaller->silent = true; // make sure that the ModuleInstaller->log() function doesn't echo while rebuilding the layoutdefs
311                 $moduleInstaller->rebuild_layoutdefs();
312                 if (file_exists('modules/'.  $panel->parent_bean->module_dir . '/layout_defs.php'))
313                         include('modules/'.  $panel->parent_bean->module_dir . '/layout_defs.php');     
314                 if (file_exists('custom/modules/'.  $panel->parent_bean->module_dir . '/Ext/Layoutdefs/layoutdefs.ext.php'))
315                         include('custom/modules/'.  $panel->parent_bean->module_dir . '/Ext/Layoutdefs/layoutdefs.ext.php');    
316   }
317   
318         function get_subpanel_setup($module)
319         {
320                 $subpanel_setup = '';
321                 $layout_defs = get_layout_defs();
322                 
323                 if(!empty($layout_defs) && !empty($layout_defs[$module]['subpanel_setup']))
324       {
325         $subpanel_setup = $layout_defs[$module]['subpanel_setup'];
326       }
327       
328       return $subpanel_setup;
329         }
330   
331         /**
332          * Retrieve the subpanel definition from the registered layout_defs arrays.
333          */
334         function getSubPanelDefine($module, $subpanel_id)
335         {
336                 $default_subpanel_define = SubPanel::_get_default_subpanel_define($module, $subpanel_id);
337                 $custom_subpanel_define = SubPanel::_get_custom_subpanel_define($module, $subpanel_id);
338
339                 $subpanel_define = array_merge($default_subpanel_define, $custom_subpanel_define);      
340                 
341                 if(empty($subpanel_define))
342                 {
343                         print('Could not load subpanel definition for: ' . $subpanel_id);
344                 }
345                      
346                 return $subpanel_define;
347         }
348
349         function _get_custom_subpanel_define($module, $subpanel_id)
350         {
351                 $ret_val = array();
352                 
353                 if($subpanel_id != '')
354                 {
355                         $layout_defs = get_layout_defs();
356                         
357                         if(!empty($layout_defs[$module]['custom_subpanel_defines'][$subpanel_id]))
358                         {
359                                 $ret_val = $layout_defs[$module]['custom_subpanel_defines'][$subpanel_id];
360                         }
361                 }
362                 
363                 return $ret_val;
364         }
365
366         function _get_default_subpanel_define($module, $subpanel_id)
367         {
368                 $ret_val = array();
369                 
370                 if($subpanel_id != '')
371                 {
372                         $layout_defs = get_layout_defs();
373
374                         if(!empty($layout_defs[$subpanel_id]['default_subpanel_define']))
375                         {
376                                 $ret_val = $layout_defs[$subpanel_id]['default_subpanel_define'];
377                         }
378                 }
379                 
380                 return $ret_val;
381         }
382 }
383 ?>