]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/MVC/View/views/view.list.php
Merge pull request #100 from collinlee/master
[Github/sugarcrm.git] / include / MVC / View / views / view.list.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 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 require_once('include/MVC/View/SugarView.php');
38
39 require_once('include/ListView/ListViewSmarty.php');
40
41 require_once('modules/MySettings/StoreQuery.php');
42 class ViewList extends SugarView{
43     var $type ='list';
44     var $lv;
45     var $searchForm;
46     var $use_old_search;
47     var $headers;
48     var $seed;
49     var $params;
50     var $listViewDefs;
51     var $storeQuery;
52     var $where = '';
53     function ViewList(){
54         parent::SugarView();
55     }
56
57
58     function oldSearch(){
59
60     }
61     function newSearch(){
62
63     }
64
65     function listViewPrepare(){
66         $module = $GLOBALS['module'];
67
68         $metadataFile = $this->getMetaDataFile();
69
70         if( !file_exists($metadataFile) )
71             sugar_die($GLOBALS['app_strings']['LBL_NO_ACTION'] );
72
73         require($metadataFile);
74
75         $this->listViewDefs = $listViewDefs;
76
77         if(!empty($this->bean->object_name) && isset($_REQUEST[$module.'2_'.strtoupper($this->bean->object_name).'_offset'])) {//if you click the pagination button, it will poplate the search criteria here
78             if(!empty($_REQUEST['current_query_by_page'])) {//The code support multi browser tabs pagination
79                 $blockVariables = array('mass', 'uid', 'massupdate', 'delete', 'merge', 'selectCount', 'request_data', 'current_query_by_page',$module.'2_'.strtoupper($this->bean->object_name).'_ORDER_BY' );
80                 if(isset($_REQUEST['lvso'])){
81                     $blockVariables[] = 'lvso';
82                 }
83                 $current_query_by_page = unserialize(base64_decode($_REQUEST['current_query_by_page']));
84                 foreach($current_query_by_page as $search_key=>$search_value) {
85                     if($search_key != $module.'2_'.strtoupper($this->bean->object_name).'_offset' && !in_array($search_key, $blockVariables)) {
86                         if (!is_array($search_value)) {
87                             $_REQUEST[$search_key] = $GLOBALS['db']->quote($search_value);
88                         }
89                         else {
90                             foreach ($search_value as $key=>&$val) {
91                                 $val = $GLOBALS['db']->quote($val);
92                             }
93                             $_REQUEST[$search_key] = $search_value;
94                         }
95                     }
96                 }
97             }
98         }
99         if(!empty($_REQUEST['saved_search_select'])) {
100             if ($_REQUEST['saved_search_select']=='_none' || !empty($_REQUEST['button'])) {
101                 $_SESSION['LastSavedView'][$_REQUEST['module']] = '';
102                 unset($_REQUEST['saved_search_select']);
103                 unset($_REQUEST['saved_search_select_name']);
104
105                 //use the current search module, or the current module to clear out layout changes
106                 if(!empty($_REQUEST['search_module']) || !empty($_REQUEST['module'])){
107                     $mod = !empty($_REQUEST['search_module']) ? $_REQUEST['search_module'] : $_REQUEST['module'];
108                     global $current_user;
109                     //Reset the current display columns to default.
110                     $current_user->setPreference('ListViewDisplayColumns', array(), 0, $mod);
111                 }
112             }
113             else if(empty($_REQUEST['button']) && (empty($_REQUEST['clear_query']) || $_REQUEST['clear_query']!='true')) {
114                 $this->saved_search = loadBean('SavedSearch');
115                 $this->saved_search->retrieveSavedSearch($_REQUEST['saved_search_select']);
116                 $this->saved_search->populateRequest();
117             }
118             elseif(!empty($_REQUEST['button'])) { // click the search button, after retrieving from saved_search
119                 $_SESSION['LastSavedView'][$_REQUEST['module']] = '';
120                 unset($_REQUEST['saved_search_select']);
121                 unset($_REQUEST['saved_search_select_name']);
122             }
123         }
124         $this->storeQuery = new StoreQuery();
125         if(!isset($_REQUEST['query'])){
126             $this->storeQuery->loadQuery($this->module);
127             $this->storeQuery->populateRequest();
128         }else{
129             $this->storeQuery->saveFromRequest($this->module);
130         }
131
132         $this->seed = $this->bean;
133
134         $displayColumns = array();
135         if(!empty($_REQUEST['displayColumns'])) {
136             foreach(explode('|', $_REQUEST['displayColumns']) as $num => $col) {
137                 if(!empty($this->listViewDefs[$module][$col]))
138                     $displayColumns[$col] = $this->listViewDefs[$module][$col];
139             }
140         }
141         else {
142             foreach($this->listViewDefs[$module] as $col => $this->params) {
143                 if(!empty($this->params['default']) && $this->params['default'])
144                     $displayColumns[$col] = $this->params;
145             }
146         }
147         $this->params = array('massupdate' => true);
148         if(!empty($_REQUEST['orderBy'])) {
149             $this->params['orderBy'] = $_REQUEST['orderBy'];
150             $this->params['overrideOrder'] = true;
151             if(!empty($_REQUEST['sortOrder'])) $this->params['sortOrder'] = $_REQUEST['sortOrder'];
152         }
153         $this->lv->displayColumns = $displayColumns;
154
155         $this->module = $module;
156
157         $this->prepareSearchForm();
158
159         if(isset($this->options['show_title']) && $this->options['show_title']) {
160             $moduleName = isset($this->seed->module_dir) ? $this->seed->module_dir : $GLOBALS['mod_strings']['LBL_MODULE_NAME'];
161             echo $this->getModuleTitle(true);
162         }
163     }
164
165     function listViewProcess(){
166         $this->processSearchForm();
167         $this->lv->searchColumns = $this->searchForm->searchColumns;
168
169         if(!$this->headers)
170             return;
171         if(empty($_REQUEST['search_form_only']) || $_REQUEST['search_form_only'] == false){
172             $this->lv->ss->assign("SEARCH",true);
173             $this->lv->setup($this->seed, 'include/ListView/ListViewGeneric.tpl', $this->where, $this->params);
174             $savedSearchName = empty($_REQUEST['saved_search_select_name']) ? '' : (' - ' . $_REQUEST['saved_search_select_name']);
175             echo $this->lv->display();
176         }
177     }
178     function prepareSearchForm(){
179     $this->searchForm = null;
180
181         //search
182         $view = 'basic_search';
183         if(!empty($_REQUEST['search_form_view']) && $_REQUEST['search_form_view'] == 'advanced_search')
184             $view = $_REQUEST['search_form_view'];
185         $this->headers = true;
186         if(!empty($_REQUEST['search_form_only']) && $_REQUEST['search_form_only'])
187             $this->headers = false;
188         elseif(!isset($_REQUEST['search_form']) || $_REQUEST['search_form'] != 'false') {
189             if(isset($_REQUEST['searchFormTab']) && $_REQUEST['searchFormTab'] == 'advanced_search') {
190                 $view = 'advanced_search';
191             }else {
192                 $view = 'basic_search';
193             }
194         }
195
196         $this->use_old_search = true;
197         if ((file_exists('modules/' . $this->module . '/SearchForm.html')
198                 && !file_exists('modules/' . $this->module . '/metadata/searchdefs.php'))
199             || (file_exists('custom/modules/' . $this->module . '/SearchForm.html')
200                 && !file_exists('custom/modules/' . $this->module . '/metadata/searchdefs.php'))){
201             require_once('include/SearchForm/SearchForm.php');
202             $this->searchForm = new SearchForm($this->module, $this->seed);
203         }else{
204             $this->use_old_search = false;
205             require_once('include/SearchForm/SearchForm2.php');
206
207             if(file_exists('custom/modules/'.$this->module.'/metadata/metafiles.php')){
208                 require('custom/modules/'.$this->module.'/metadata/metafiles.php');
209             }elseif(file_exists('modules/'.$this->module.'/metadata/metafiles.php')){
210                 require('modules/'.$this->module.'/metadata/metafiles.php');
211             }
212
213 /*          if(!empty($metafiles[$this->module]['searchdefs']))
214                 require_once($metafiles[$this->module]['searchdefs']);
215             elseif(file_exists('modules/'.$this->module.'/metadata/searchdefs.php'))
216                 require_once('modules/'.$this->module.'/metadata/searchdefs.php');
217 */
218
219             if (file_exists('custom/modules/'.$this->module.'/metadata/searchdefs.php'))
220             {
221                 require('custom/modules/'.$this->module.'/metadata/searchdefs.php');
222             }
223             elseif (!empty($metafiles[$this->module]['searchdefs']))
224             {
225                 require($metafiles[$this->module]['searchdefs']);
226             }
227             elseif (file_exists('modules/'.$this->module.'/metadata/searchdefs.php'))
228             {
229                 require('modules/'.$this->module.'/metadata/searchdefs.php');
230             }
231
232
233             if(!empty($metafiles[$this->module]['searchfields']))
234                 require($metafiles[$this->module]['searchfields']);
235             elseif(file_exists('modules/'.$this->module.'/metadata/SearchFields.php'))
236                 require('modules/'.$this->module.'/metadata/SearchFields.php');
237
238             if(file_exists('custom/modules/'.$this->module.'/metadata/SearchFields.php')){
239                 require('custom/modules/'.$this->module.'/metadata/SearchFields.php');
240             }
241
242
243             $this->searchForm = new SearchForm($this->seed, $this->module, $this->action);
244             $this->searchForm->setup($searchdefs, $searchFields, 'SearchFormGeneric.tpl', $view, $this->listViewDefs);
245             $this->searchForm->lv = $this->lv;
246         }
247     }
248     function processSearchForm(){
249         if(isset($_REQUEST['query']))
250         {
251             // we have a query
252             if(!empty($_SERVER['HTTP_REFERER']) && preg_match('/action=EditView/', $_SERVER['HTTP_REFERER'])) { // from EditView cancel
253                 $this->searchForm->populateFromArray($this->storeQuery->query);
254             }
255             else {
256                 $this->searchForm->populateFromRequest();
257             }
258
259             $where_clauses = $this->searchForm->generateSearchWhere(true, $this->seed->module_dir);
260
261             if (count($where_clauses) > 0 )$this->where = '('. implode(' ) AND ( ', $where_clauses) . ')';
262             $GLOBALS['log']->info("List View Where Clause: $this->where");
263         }
264         if($this->use_old_search){
265             switch($view) {
266                 case 'basic_search':
267                     $this->searchForm->setup();
268                     $this->searchForm->displayBasic($this->headers);
269                     break;
270                  case 'advanced_search':
271                     $this->searchForm->setup();
272                     $this->searchForm->displayAdvanced($this->headers);
273                     break;
274                  case 'saved_views':
275                     echo $this->searchForm->displaySavedViews($this->listViewDefs, $this->lv, $this->headers);
276                    break;
277             }
278         }else{
279             echo $this->searchForm->display($this->headers);
280         }
281     }
282     function preDisplay(){
283         $this->lv = new ListViewSmarty();
284     }
285     function display(){
286         if(!$this->bean || !$this->bean->ACLAccess('list')){
287             ACLController::displayNoAccess();
288         } else {
289             $this->listViewPrepare();
290             $this->listViewProcess();
291         }
292     }
293 }
294 ?>