]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/MVC/View/ViewFactory.php
Release 6.2.3
[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         //Check to see if we should load a custom parent view instance
59         loadParentView($type);
60         
61                 if(!empty($target_module)) {
62                         if(file_exists('custom/modules/'.$target_module.'/views/view.'.$type.'.php')){
63                                 $view = ViewFactory::_buildFromFile('custom/modules/'.$target_module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $target_module);
64                         }else if(file_exists('modules/'.$target_module.'/views/view.'.$type.'.php')){
65                                 $view = ViewFactory::_buildFromFile('modules/'.$target_module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $target_module);
66                         }               
67                 }       
68                 
69                 if(!isset($view)) {
70                         if(file_exists('custom/modules/'.$module.'/views/view.'.$type.'.php')){
71                                 $view = ViewFactory::_buildFromFile('custom/modules/'.$module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
72                         }else if(file_exists('modules/'.$module.'/views/view.'.$type.'.php')){
73                                 $view = ViewFactory::_buildFromFile('modules/'.$module.'/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
74                         }else if(file_exists('custom/include/MVC/View/views/view.'.$type.'.php')){
75                                 $view = ViewFactory::_buildFromFile('custom/include/MVC/View/views/view.'.$type.'.php', $bean, $view_object_map, $type, $module);
76                         }else{
77                                 //if the module does not handle this view, then check if Sugar handles it OOTB
78                                 $file = 'include/MVC/View/views/view.'.$type.'.php';
79                                 if(file_exists($file)){
80                                         //it appears Sugar does have the proper logic for this file.
81                                         $view = ViewFactory::_buildFromFile($file, $bean, $view_object_map, $type, $module);
82                                 }
83                         }       
84                 }
85                 // Default to SugarView if still nothing found/built
86                 if (!isset($view)) 
87                         $view = new SugarView();
88                 ViewFactory::_loadConfig($view, $type);
89                 return $view;
90         }
91         
92         /**
93          * Load the view_<view>_config.php file which holds options used by the view.
94          */
95         function _loadConfig(&$view, $type){
96                 $view_config_custom = array();
97                 $view_config_module = array();
98                 $view_config_root_cstm = array();
99                 $view_config_root = array();
100                 $view_config_app = array();
101                 $config_file_name = 'view.'.$type.'.config.php';
102                 $view_config = sugar_cache_retrieve("VIEW_CONFIG_FILE_".$view->module."_TYPE_".$type);
103                 if(!$view_config){
104                         if(file_exists('custom/modules/'.$view->module.'/views/'.$config_file_name)){
105                                 require_once('custom/modules/'.$view->module.'/views/'.$config_file_name);
106                                 $view_config_custom = $view_config;
107                         }
108                         if(file_exists('modules/'.$view->module.'/views/'.$config_file_name)){
109                                 require_once('modules/'.$view->module.'/views/'.$config_file_name);
110                                 $view_config_module = $view_config;
111                         }
112                         if(file_exists('custom/include/MVC/View/views/'.$config_file_name)){
113                                 require_once('custom/include/MVC/View/views/'.$config_file_name);
114                                 $view_config_root_cstm = $view_config;
115                         }
116                         if(file_exists('include/MVC/View/views/'.$config_file_name)){
117                                 require_once('include/MVC/View/views/'.$config_file_name);
118                                 $view_config_root = $view_config;
119                         }       
120                         if(file_exists('include/MVC/View/views/view.config.php')){
121                                 require_once('include/MVC/View/views/view.config.php');
122                                 $view_config_app = $view_config;
123                         }
124                         $view_config = array('actions' => array(), 'req_params' => array(),);
125                         
126                         //actions
127                         if(!empty($view_config_app) && !empty($view_config_app['actions']))
128                                 $view_config['actions'] = array_merge($view_config['actions'], $view_config_app['actions']);
129                         if(!empty($view_config_root) && !empty($view_config_root['actions']))
130                                 $view_config['actions'] = array_merge($view_config['actions'], $view_config_root['actions']);
131                         if(!empty($view_config_root_cstm) && !empty($view_config_root_cstm['actions']))
132                                 $view_config['actions'] = array_merge($view_config['actions'], $view_config_root_cstm['actions']);
133                         if(!empty($view_config_module) && !empty($view_config_module['actions']))
134                                 $view_config['actions'] = array_merge($view_config['actions'], $view_config_module['actions']);
135                         if(!empty($view_config_custom) && !empty($view_config_custom['actions']))
136                                 $view_config['actions'] = array_merge($view_config['actions'], $view_config_custom['actions']); 
137                         
138                         //req_params
139                         if(!empty($view_config_app) && !empty($view_config_app['req_params']))
140                                 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_app['req_params']);
141                         if(!empty($view_config_root) && !empty($view_config_root['req_params']))
142                                 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_root['req_params']);
143                         if(!empty($view_config_root_cstm) && !empty($view_config_root_cstm['req_params']))
144                                 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_root_cstm['req_params']);
145                         if(!empty($view_config_module) && !empty($view_config_module['req_params']))
146                                 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_module['req_params']);
147                         if(!empty($view_config_custom) && !empty($view_config_custom['req_params']))
148                                 $view_config['req_params'] = array_merge($view_config['req_params'], $view_config_custom['req_params']);        
149                 
150                         sugar_cache_put("VIEW_CONFIG_FILE_".$view->module."_TYPE_".$type, $view_config);
151                 }
152                 $action = strtolower($view->action);
153                 $config = null;
154                 if(!empty($view_config['req_params'])){
155                         //try the params first  
156                         foreach($view_config['req_params'] as $key => $value){
157                             if(!empty($_REQUEST[$key]) && $_REQUEST[$key] == "false") {
158                                 $_REQUEST[$key] = false;
159                             }
160                                 if(!empty($_REQUEST[$key])){
161                                         
162                                         if(!is_array($value['param_value'])){
163                                                 if($value['param_value'] ==  $_REQUEST[$key]){
164                                                         $config = $value['config'];
165                                                         break;
166                                                 }
167                                         }else{
168                                                 
169                                                 foreach($value['param_value'] as $v){
170                                                         if($v ==  $_REQUEST[$key]){
171                                                                 $config = $value['config'];
172                                                                 break;
173                                                         }
174                                                         
175                                                 }
176                                                 
177                                         }
178                                         
179                                         
180                                         
181                                 }
182                         }
183                 }
184                 if($config == null && !empty($view_config['actions']) && !empty($view_config['actions'][$action])){
185                                 $config = $view_config['actions'][$action];
186                 }
187                 if($config != null)
188                         $view->options = $config;
189         }       
190         
191         /**
192          * This is a private function which just helps the getView function generate the
193          * proper view object
194          * 
195          * @return a valid SugarView
196          */
197         function _buildFromFile($file, &$bean, $view_object_map, $type, $module){
198                 require_once($file);
199                 //try ModuleViewType first then try ViewType if that fails then use SugarView
200                 $class = ucfirst($module).'View'.ucfirst($type);
201                 $customClass = 'Custom' . $class;
202                 
203                 if(class_exists($customClass)){
204                         return ViewFactory::_buildClass($customClass, $bean, $view_object_map);
205                 }
206                 if(class_exists($class)){
207                         return ViewFactory::_buildClass($class, $bean, $view_object_map);
208                 }
209                 //Now try the next set of possibilites if it was none of the above
210                 $class = 'View'.ucfirst($type);
211                 $customClass = 'Custom' . $class;
212                 if(class_exists($customClass)){
213                         return ViewFactory::_buildClass($customClass, $bean, $view_object_map);
214                 }
215                 if(class_exists($class)){
216                         return ViewFactory::_buildClass($class, $bean, $view_object_map);
217                 }
218                 //Now check if there is a custom SugarView for generic handling
219                 if(file_exists('custom/include/MVC/View/SugarView.php')){
220                         require_once('custom/include/MVC/View/SugarView.php');
221                         if(class_exists('CustomSugarView')){
222                                 return new CustomSugarView($bean, $view_object_map);
223                         }
224                 }
225                 //if all else fails return SugarView
226                 return new SugarView($bean, $view_object_map);
227                 
228         }
229         
230         /**
231          * instantiate the correct view and call init to pass on any obejcts we need to
232          * from the controller.
233          * 
234          * @param string class - the name of the class to instantiate
235          * @param object bean = the bean to pass to the view
236          * @param array view_object_map - the array which holds obejcts to pass between the
237          *                                controller and the view.
238          * 
239          * @return SugarView
240          */
241         function _buildClass($class, $bean, $view_object_map){
242                 $view = new $class();
243                 $view->init($bean, $view_object_map);
244                 if($view instanceof SugarView){
245                         return $view;
246                 }else
247                         return new SugarView($bean, $view_object_map);
248         }
249 }
250 ?>