]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/MVC/View/ViewFactory.php
Release 6.2.0
[Github/sugarcrm.git] / include / MVC / View / ViewFactory.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37   /**
38   * ViewFactory
39   *
40   * View factory class. This file is used by the controller along with a view paramter to build the
41   * requested view.
42   */
43 require_once('include/MVC/View/SugarView.php');
44
45 class ViewFactory{
46         /**
47          * load the correct view
48          * @param string $type View Type
49          * @return valid view
50          */
51         function loadView($type = 'default', $module, $bean = null, $view_object_map = array(), $target_module=''){
52                 $type = strtolower($type);
53                 
54                 //first let's check if the module handles this view
55
56                 $view = null;
57                 
58                 $view = null;
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);
64                         }               
65                 }       
66                 
67                 if(!isset($view)) {
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);
74                         }else{
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);
80                                 }
81                         }       
82                 }
83                 // Default to SugarView if still nothing found/built
84                 if (!isset($view)) 
85                         $view = new SugarView();
86                 ViewFactory::_loadConfig($view, $type);
87                 return $view;
88         }
89         
90         /**
91          * Load the view_<view>_config.php file which holds options used by the view.
92          */
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);
101                 if(!$view_config){
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;
105                         }
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;
109                         }
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;
113                         }
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;
117                         }       
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;
121                         }
122                         $view_config = array('actions' => array(), 'req_params' => array(),);
123                         
124                         //actions
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']); 
135                         
136                         //req_params
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']);        
147                 
148                         sugar_cache_put("VIEW_CONFIG_FILE_".$view->module."_TYPE_".$type, $view_config);
149                 }
150                 $action = strtolower($view->action);
151                 $config = null;
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;
157                             }
158                                 if(!empty($_REQUEST[$key])){
159                                         
160                                         if(!is_array($value['param_value'])){
161                                                 if($value['param_value'] ==  $_REQUEST[$key]){
162                                                         $config = $value['config'];
163                                                         break;
164                                                 }
165                                         }else{
166                                                 
167                                                 foreach($value['param_value'] as $v){
168                                                         if($v ==  $_REQUEST[$key]){
169                                                                 $config = $value['config'];
170                                                                 break;
171                                                         }
172                                                         
173                                                 }
174                                                 
175                                         }
176                                         
177                                         
178                                         
179                                 }
180                         }
181                 }
182                 if($config == null && !empty($view_config['actions']) && !empty($view_config['actions'][$action])){
183                                 $config = $view_config['actions'][$action];
184                 }
185                 if($config != null)
186                         $view->options = $config;
187         }       
188         
189         /**
190          * This is a private function which just helps the getView function generate the
191          * proper view object
192          * 
193          * @return a valid SugarView
194          */
195         function _buildFromFile($file, &$bean, $view_object_map, $type, $module){
196                 require_once($file);
197                 //try ModuleViewType first then try ViewType if that fails then use SugarView
198                 $class = ucfirst($module).'View'.ucfirst($type);
199                 $customClass = 'Custom' . $class;
200                 
201                 if(class_exists($customClass)){
202                         return ViewFactory::_buildClass($customClass, $bean, $view_object_map);
203                 }
204                 if(class_exists($class)){
205                         return ViewFactory::_buildClass($class, $bean, $view_object_map);
206                 }
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);
212                 }
213                 if(class_exists($class)){
214                         return ViewFactory::_buildClass($class, $bean, $view_object_map);
215                 }
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);
221                         }
222                 }
223                 //if all else fails return SugarView
224                 return new SugarView($bean, $view_object_map);
225                 
226         }
227         
228         /**
229          * instantiate the correct view and call init to pass on any obejcts we need to
230          * from the controller.
231          * 
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.
236          * 
237          * @return SugarView
238          */
239         function _buildClass($class, $bean, $view_object_map){
240                 $view = new $class();
241                 $view->init($bean, $view_object_map);
242                 if($view instanceof SugarView){
243                         return $view;
244                 }else
245                         return new SugarView($bean, $view_object_map);
246         }
247 }
248 ?>