]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/MVC/View/ViewFactory.php
Release 6.4.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 /**
46  * Sugar view factory
47  * @api
48  */
49 class ViewFactory{
50         /**
51          * load the correct view
52          * @param string $type View Type
53          * @return valid view
54          */
55         function loadView($type = 'default', $module, $bean = null, $view_object_map = array(), $target_module=''){
56                 $type = strtolower($type);
57
58                 //first let's check if the module handles this view
59
60                 $view = null;
61
62         //Check to see if we should load a custom parent view instance
63         loadParentView($type);
64
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);
70                         }
71                 }
72
73                 if(!isset($view)) {
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);
80                         }else{
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);
86                                 }
87                         }
88                 }
89                 // Default to SugarView if still nothing found/built
90                 if (!isset($view))
91                         $view = new SugarView();
92                 ViewFactory::_loadConfig($view, $type);
93                 return $view;
94         }
95
96         /**
97          * Load the view_<view>_config.php file which holds options used by the view.
98          */
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);
107                 if(!$view_config){
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;
111                         }
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;
115                         }
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;
119                         }
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;
123                         }
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;
127                         }
128                         $view_config = array('actions' => array(), 'req_params' => array(),);
129
130                         //actions
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']);
141
142                         //req_params
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']);
153
154                         sugar_cache_put("VIEW_CONFIG_FILE_".$view->module."_TYPE_".$type, $view_config);
155                 }
156                 $action = strtolower($view->action);
157                 $config = null;
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;
163                             }
164                                 if(!empty($_REQUEST[$key])){
165
166                                         if(!is_array($value['param_value'])){
167                                                 if($value['param_value'] ==  $_REQUEST[$key]){
168                                                         $config = $value['config'];
169                                                         break;
170                                                 }
171                                         }else{
172
173                                                 foreach($value['param_value'] as $v){
174                                                         if($v ==  $_REQUEST[$key]){
175                                                                 $config = $value['config'];
176                                                                 break;
177                                                         }
178
179                                                 }
180
181                                         }
182
183
184
185                                 }
186                         }
187                 }
188                 if($config == null && !empty($view_config['actions']) && !empty($view_config['actions'][$action])){
189                                 $config = $view_config['actions'][$action];
190                 }
191                 if($config != null)
192                         $view->options = $config;
193         }
194
195         /**
196          * This is a private function which just helps the getView function generate the
197          * proper view object
198          *
199          * @return a valid SugarView
200          */
201         function _buildFromFile($file, &$bean, $view_object_map, $type, $module){
202                 require_once($file);
203                 //try ModuleViewType first then try ViewType if that fails then use SugarView
204                 $class = ucfirst($module).'View'.ucfirst($type);
205                 $customClass = 'Custom' . $class;
206
207                 if(class_exists($customClass)){
208                         return ViewFactory::_buildClass($customClass, $bean, $view_object_map);
209                 }
210                 if(class_exists($class)){
211                         return ViewFactory::_buildClass($class, $bean, $view_object_map);
212                 }
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);
218                 }
219                 if(class_exists($class)){
220                         return ViewFactory::_buildClass($class, $bean, $view_object_map);
221                 }
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);
227                         }
228                 }
229                 //if all else fails return SugarView
230                 return new SugarView($bean, $view_object_map);
231
232         }
233
234         /**
235          * instantiate the correct view and call init to pass on any obejcts we need to
236          * from the controller.
237          *
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.
242          *
243          * @return SugarView
244          */
245         function _buildClass($class, $bean, $view_object_map){
246                 $view = new $class();
247                 $view->init($bean, $view_object_map);
248                 if($view instanceof SugarView){
249                         return $view;
250                 }else
251                         return new SugarView($bean, $view_object_map);
252         }
253 }
254 ?>