PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/include/SubPanel/SubPanel.php

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