2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
40 * View factory class. This file is used by the controller along with a view paramter to build the
43 require_once('include/MVC/View/SugarView.php');
51 * load the correct view
52 * @param string $type View Type
55 function loadView($type = 'default', $module, $bean = null, $view_object_map = array(), $target_module=''){
56 $type = strtolower($type);
58 //first let's check if the module handles this view
62 //Check to see if we should load a custom parent view instance
63 loadParentView($type);
65 if(!empty($target_module)) {
66 if(file_exists('custom/modules/'.$target_module.'/views/view.'.$type.'.php')){
67 $view = ViewFactory::_buildFromFile('custom/modules/'.$target_module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $target_module);
68 }else if(file_exists('modules/'.$target_module.'/views/view.'.$type.'.php')){
69 $view = ViewFactory::_buildFromFile('modules/'.$target_module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $target_module);
74 if(file_exists('custom/modules/'.$module.'/views/view.'.$type.'.php')){
75 $view = ViewFactory::_buildFromFile('custom/modules/'.$module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
76 }else if(file_exists('modules/'.$module.'/views/view.'.$type.'.php')){
77 $view = ViewFactory::_buildFromFile('modules/'.$module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
78 }else if(file_exists('custom/include/MVC/View/views/view.'.$type.'.php')){
79 $view = ViewFactory::_buildFromFile('custom/include/MVC/View/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
81 //if the module does not handle this view, then check if Sugar handles it OOTB
82 $file = 'include/MVC/View/views/view.'.$type.'.php';
83 if(file_exists($file)){
84 //it appears Sugar does have the proper logic for this file.
85 $view = ViewFactory::_buildFromFile($file, $bean, $view_object_map, $type, $module);
89 // Default to SugarView if still nothing found/built
91 $view = new SugarView();
92 ViewFactory::_loadConfig($view, $type);
97 * Load the view_<view>_config.php file which holds options used by the view.
99 function _loadConfig(&$view, $type){
100 $view_config_custom = array();
101 $view_config_module = array();
102 $view_config_root_cstm = array();
103 $view_config_root = array();
104 $view_config_app = array();
105 $config_file_name = 'view.'.$type.'.config.php';
106 $view_config = sugar_cache_retrieve("VIEW_CONFIG_FILE_".$view->module."_TYPE_".$type);
108 if(file_exists('custom/modules/'.$view->module.'/views/'.$config_file_name)){
109 require_once('custom/modules/'.$view->module.'/views/'.$config_file_name);
110 $view_config_custom = $view_config;
112 if(file_exists('modules/'.$view->module.'/views/'.$config_file_name)){
113 require_once('modules/'.$view->module.'/views/'.$config_file_name);
114 $view_config_module = $view_config;
116 if(file_exists('custom/include/MVC/View/views/'.$config_file_name)){
117 require_once('custom/include/MVC/View/views/'.$config_file_name);
118 $view_config_root_cstm = $view_config;
120 if(file_exists('include/MVC/View/views/'.$config_file_name)){
121 require_once('include/MVC/View/views/'.$config_file_name);
122 $view_config_root = $view_config;
124 if(file_exists('include/MVC/View/views/view.config.php')){
125 require_once('include/MVC/View/views/view.config.php');
126 $view_config_app = $view_config;
128 $view_config = array('actions' => array(), 'req_params' => array(),);
131 if(!empty($view_config_app) && !empty($view_config_app['actions']))
132 $view_config['actions'] = array_merge($view_config['actions'], $view_config_app['actions']);
133 if(!empty($view_config_root) && !empty($view_config_root['actions']))
134 $view_config['actions'] = array_merge($view_config['actions'], $view_config_root['actions']);
135 if(!empty($view_config_root_cstm) && !empty($view_config_root_cstm['actions']))
136 $view_config['actions'] = array_merge($view_config['actions'], $view_config_root_cstm['actions']);
137 if(!empty($view_config_module) && !empty($view_config_module['actions']))
138 $view_config['actions'] = array_merge($view_config['actions'], $view_config_module['actions']);
139 if(!empty($view_config_custom) && !empty($view_config_custom['actions']))
140 $view_config['actions'] = array_merge($view_config['actions'], $view_config_custom['actions']);
143 if(!empty($view_config_app) && !empty($view_config_app['req_params']))
144 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_app['req_params']);
145 if(!empty($view_config_root) && !empty($view_config_root['req_params']))
146 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_root['req_params']);
147 if(!empty($view_config_root_cstm) && !empty($view_config_root_cstm['req_params']))
148 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_root_cstm['req_params']);
149 if(!empty($view_config_module) && !empty($view_config_module['req_params']))
150 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_module['req_params']);
151 if(!empty($view_config_custom) && !empty($view_config_custom['req_params']))
152 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_custom['req_params']);
154 sugar_cache_put("VIEW_CONFIG_FILE_".$view->module."_TYPE_".$type, $view_config);
156 $action = strtolower($view->action);
158 if(!empty($view_config['req_params'])){
159 //try the params first
160 foreach($view_config['req_params'] as $key => $value){
161 if(!empty($_REQUEST[$key]) && $_REQUEST[$key] == "false") {
162 $_REQUEST[$key] = false;
164 if(!empty($_REQUEST[$key])){
166 if(!is_array($value['param_value'])){
167 if($value['param_value'] == $_REQUEST[$key]){
168 $config = $value['config'];
173 foreach($value['param_value'] as $v){
174 if($v == $_REQUEST[$key]){
175 $config = $value['config'];
188 if($config == null && !empty($view_config['actions']) && !empty($view_config['actions'][$action])){
189 $config = $view_config['actions'][$action];
192 $view->options = $config;
196 * This is a private function which just helps the getView function generate the
199 * @return a valid SugarView
201 function _buildFromFile($file, &$bean, $view_object_map, $type, $module){
203 //try ModuleViewType first then try ViewType if that fails then use SugarView
204 $class = ucfirst($module).'View'.ucfirst($type);
205 $customClass = 'Custom' . $class;
207 if(class_exists($customClass)){
208 return ViewFactory::_buildClass($customClass, $bean, $view_object_map);
210 if(class_exists($class)){
211 return ViewFactory::_buildClass($class, $bean, $view_object_map);
213 //Now try the next set of possibilites if it was none of the above
214 $class = 'View'.ucfirst($type);
215 $customClass = 'Custom' . $class;
216 if(class_exists($customClass)){
217 return ViewFactory::_buildClass($customClass, $bean, $view_object_map);
219 if(class_exists($class)){
220 return ViewFactory::_buildClass($class, $bean, $view_object_map);
222 //Now check if there is a custom SugarView for generic handling
223 if(file_exists('custom/include/MVC/View/SugarView.php')){
224 require_once('custom/include/MVC/View/SugarView.php');
225 if(class_exists('CustomSugarView')){
226 return new CustomSugarView($bean, $view_object_map);
229 //if all else fails return SugarView
230 return new SugarView($bean, $view_object_map);
235 * instantiate the correct view and call init to pass on any obejcts we need to
236 * from the controller.
238 * @param string class - the name of the class to instantiate
239 * @param object bean = the bean to pass to the view
240 * @param array view_object_map - the array which holds obejcts to pass between the
241 * controller and the view.
245 function _buildClass($class, $bean, $view_object_map){
246 $view = new $class();
247 $view->init($bean, $view_object_map);
248 if($view instanceof SugarView){
251 return new SugarView($bean, $view_object_map);