2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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');
47 * load the correct view
48 * @param string $type View Type
51 function loadView($type = 'default', $module, $bean = null, $view_object_map = array(), $target_module=''){
52 $type = strtolower($type);
54 //first let's check if the module handles this view
59 if(!empty($target_module)) {
60 if(file_exists('custom/modules/'.$target_module.'/views/view.'.$type.'.php')){
61 $view = ViewFactory::_buildFromFile('custom/modules/'.$target_module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $target_module);
62 }else if(file_exists('modules/'.$target_module.'/views/view.'.$type.'.php')){
63 $view = ViewFactory::_buildFromFile('modules/'.$target_module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $target_module);
68 if(file_exists('custom/modules/'.$module.'/views/view.'.$type.'.php')){
69 $view = ViewFactory::_buildFromFile('custom/modules/'.$module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
70 }else if(file_exists('modules/'.$module.'/views/view.'.$type.'.php')){
71 $view = ViewFactory::_buildFromFile('modules/'.$module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
72 }else if(file_exists('custom/include/MVC/View/views/view.'.$type.'.php')){
73 $view = ViewFactory::_buildFromFile('custom/include/MVC/View/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
75 //if the module does not handle this view, then check if Sugar handles it OOTB
76 $file = 'include/MVC/View/views/view.'.$type.'.php';
77 if(file_exists($file)){
78 //it appears Sugar does have the proper logic for this file.
79 $view = ViewFactory::_buildFromFile($file, $bean, $view_object_map, $type, $module);
83 // Default to SugarView if still nothing found/built
85 $view = new SugarView();
86 ViewFactory::_loadConfig($view, $type);
91 * Load the view_<view>_config.php file which holds options used by the view.
93 function _loadConfig(&$view, $type){
94 $view_config_custom = array();
95 $view_config_module = array();
96 $view_config_root_cstm = array();
97 $view_config_root = array();
98 $view_config_app = array();
99 $config_file_name = 'view.'.$type.'.config.php';
100 $view_config = sugar_cache_retrieve("VIEW_CONFIG_FILE_".$view->module."_TYPE_".$type);
102 if(file_exists('custom/modules/'.$view->module.'/views/'.$config_file_name)){
103 require_once('custom/modules/'.$view->module.'/views/'.$config_file_name);
104 $view_config_custom = $view_config;
106 if(file_exists('modules/'.$view->module.'/views/'.$config_file_name)){
107 require_once('modules/'.$view->module.'/views/'.$config_file_name);
108 $view_config_module = $view_config;
110 if(file_exists('custom/include/MVC/View/views/'.$config_file_name)){
111 require_once('custom/include/MVC/View/views/'.$config_file_name);
112 $view_config_root_cstm = $view_config;
114 if(file_exists('include/MVC/View/views/'.$config_file_name)){
115 require_once('include/MVC/View/views/'.$config_file_name);
116 $view_config_root = $view_config;
118 if(file_exists('include/MVC/View/views/view.config.php')){
119 require_once('include/MVC/View/views/view.config.php');
120 $view_config_app = $view_config;
122 $view_config = array('actions' => array(), 'req_params' => array(),);
125 if(!empty($view_config_app) && !empty($view_config_app['actions']))
126 $view_config['actions'] = array_merge($view_config['actions'], $view_config_app['actions']);
127 if(!empty($view_config_root) && !empty($view_config_root['actions']))
128 $view_config['actions'] = array_merge($view_config['actions'], $view_config_root['actions']);
129 if(!empty($view_config_root_cstm) && !empty($view_config_root_cstm['actions']))
130 $view_config['actions'] = array_merge($view_config['actions'], $view_config_root_cstm['actions']);
131 if(!empty($view_config_module) && !empty($view_config_module['actions']))
132 $view_config['actions'] = array_merge($view_config['actions'], $view_config_module['actions']);
133 if(!empty($view_config_custom) && !empty($view_config_custom['actions']))
134 $view_config['actions'] = array_merge($view_config['actions'], $view_config_custom['actions']);
137 if(!empty($view_config_app) && !empty($view_config_app['req_params']))
138 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_app['req_params']);
139 if(!empty($view_config_root) && !empty($view_config_root['req_params']))
140 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_root['req_params']);
141 if(!empty($view_config_root_cstm) && !empty($view_config_root_cstm['req_params']))
142 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_root_cstm['req_params']);
143 if(!empty($view_config_module) && !empty($view_config_module['req_params']))
144 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_module['req_params']);
145 if(!empty($view_config_custom) && !empty($view_config_custom['req_params']))
146 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_custom['req_params']);
148 sugar_cache_put("VIEW_CONFIG_FILE_".$view->module."_TYPE_".$type, $view_config);
150 $action = strtolower($view->action);
152 if(!empty($view_config['req_params'])){
153 //try the params first
154 foreach($view_config['req_params'] as $key => $value){
155 if(!empty($_REQUEST[$key]) && $_REQUEST[$key] == "false") {
156 $_REQUEST[$key] = false;
158 if(!empty($_REQUEST[$key])){
160 if(!is_array($value['param_value'])){
161 if($value['param_value'] == $_REQUEST[$key]){
162 $config = $value['config'];
167 foreach($value['param_value'] as $v){
168 if($v == $_REQUEST[$key]){
169 $config = $value['config'];
182 if($config == null && !empty($view_config['actions']) && !empty($view_config['actions'][$action])){
183 $config = $view_config['actions'][$action];
186 $view->options = $config;
190 * This is a private function which just helps the getView function generate the
193 * @return a valid SugarView
195 function _buildFromFile($file, &$bean, $view_object_map, $type, $module){
197 //try ModuleViewType first then try ViewType if that fails then use SugarView
198 $class = ucfirst($module).'View'.ucfirst($type);
199 $customClass = 'Custom' . $class;
201 if(class_exists($customClass)){
202 return ViewFactory::_buildClass($customClass, $bean, $view_object_map);
204 if(class_exists($class)){
205 return ViewFactory::_buildClass($class, $bean, $view_object_map);
207 //Now try the next set of possibilites if it was none of the above
208 $class = 'View'.ucfirst($type);
209 $customClass = 'Custom' . $class;
210 if(class_exists($customClass)){
211 return ViewFactory::_buildClass($customClass, $bean, $view_object_map);
213 if(class_exists($class)){
214 return ViewFactory::_buildClass($class, $bean, $view_object_map);
216 //Now check if there is a custom SugarView for generic handling
217 if(file_exists('custom/include/MVC/View/SugarView.php')){
218 require_once('custom/include/MVC/View/SugarView.php');
219 if(class_exists('CustomSugarView')){
220 return new CustomSugarView($bean, $view_object_map);
223 //if all else fails return SugarView
224 return new SugarView($bean, $view_object_map);
229 * instantiate the correct view and call init to pass on any obejcts we need to
230 * from the controller.
232 * @param string class - the name of the class to instantiate
233 * @param object bean = the bean to pass to the view
234 * @param array view_object_map - the array which holds obejcts to pass between the
235 * controller and the view.
239 function _buildClass($class, $bean, $view_object_map){
240 $view = new $class();
241 $view->init($bean, $view_object_map);
242 if($view instanceof SugarView){
245 return new SugarView($bean, $view_object_map);