]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Home/UnifiedSearchAdvanced.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Home / UnifiedSearchAdvanced.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46
47
48 class UnifiedSearchAdvanced {
49     
50     var $query_string = '';
51     
52     function __construct(){
53         if(!empty($_REQUEST['query_string'])){
54             $query_string = trim($_REQUEST['query_string']);
55             if(!empty($query_string)){
56                 $this->query_string = $query_string;
57             }
58         }
59     }
60     
61         function getDropDownDiv($tpl = 'modules/Home/UnifiedSearchAdvanced.tpl') {
62                 global $app_list_strings;
63
64                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
65                 $this->buildCache();
66                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
67
68                 global $mod_strings, $modListHeader, $app_list_strings, $current_user, $app_strings, $beanList;
69                 $users_modules = $current_user->getPreference('globalSearch', 'search');
70
71                 if(!isset($users_modules)) { // preferences are empty, select all
72                         $users_modules = array();
73                         foreach($unified_search_modules as $module=>$data) {
74                                 if ( !empty($data['default']) ) {
75                     $users_modules[$module] = $beanList[$module];
76                 }
77                         }
78                         $current_user->setPreference('globalSearch', $users_modules, 0, 'search');
79                 }
80                 $sugar_smarty = new Sugar_Smarty();
81
82                 $modules_to_search = array();
83                 foreach($unified_search_modules as $module => $data) {
84             if(ACLController::checkAccess($module, 'list', true)) {
85                 $modules_to_search[$module] = array('translated' => $app_list_strings['moduleList'][$module]);
86                 if(array_key_exists($module, $users_modules)) $modules_to_search[$module]['checked'] = true;
87                 else $modules_to_search[$module]['checked'] = false;
88             }
89                 }
90
91                 if(!empty($this->query_string)) $sugar_smarty->assign('query_string', securexss($this->query_string));
92                 else $sugar_smarty->assign('query_string', '');
93                 $sugar_smarty->assign('USE_SEARCH_GIF', 0);
94                 $sugar_smarty->assign('LBL_SEARCH_BUTTON_LABEL', $app_strings['LBL_SEARCH_BUTTON_LABEL']);
95                 $sugar_smarty->assign('MODULES_TO_SEARCH', $modules_to_search);
96                 $sugar_smarty->debugging = true;
97
98                 return $sugar_smarty->fetch($tpl);
99         }
100
101         function search() {
102                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
103                         $this->buildCache();
104
105                 include $GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php';
106                 require_once 'include/ListView/ListViewSmarty.php';
107                 
108
109                 global $modListHeader, $beanList, $beanFiles, $current_language, $app_strings, $current_user, $mod_strings;
110                 $home_mod_strings = return_module_language($current_language, 'Home');
111
112                 $overlib = true;
113                 $this->query_string = $GLOBALS['db']->quote(securexss(from_html(clean_string($this->query_string, 'UNIFIED_SEARCH'))));
114
115                 if(!empty($_REQUEST['advanced']) && $_REQUEST['advanced'] != 'false') {
116                         $modules_to_search = array();
117                         foreach($_REQUEST as $param => $value) {
118                                 if(preg_match('/^search_mod_(.*)$/', $param, $match)) {
119                                         $modules_to_search[$match[1]] = $beanList[$match[1]];
120                                 }
121                         }
122                         $current_user->setPreference('globalSearch', $modules_to_search, 0, 'search'); // save selections to user preference
123                 }
124                 else {
125                         $users_modules = $current_user->getPreference('globalSearch', 'search');
126                         if(isset($users_modules)) { // use user's previous selections
127                             foreach ( $users_modules as $key => $value ) {
128                                 if ( isset($unified_search_modules[$key]) ) {
129                                     $modules_to_search[$key] = $value;
130                                 }
131                             }
132                         }
133                         else { // select all the modules (ie first time user has used global search)
134                                 foreach($unified_search_modules as $module=>$data) {
135                                     if ( !empty($data['default']) ) {
136                                         $modules_to_search[$module] = $beanList[$module];
137                                     }
138                                 }
139                         }
140                         $current_user->setPreference('globalSearch', $modules_to_search, 'search');
141                 }
142                 echo $this->getDropDownDiv('modules/Home/UnifiedSearchAdvancedForm.tpl');
143
144                 $module_results = array();
145                 $module_counts = array();
146                 $has_results = false;
147
148                 if(!empty($this->query_string)) {
149                         foreach($modules_to_search as $moduleName => $beanName) {
150                             $unifiedSearchFields = array () ;
151                 $innerJoins = array();
152                 foreach ( $unified_search_modules[ $moduleName ]['fields'] as $field=>$def )
153                 {
154                     //bug: 34125 we might want to try to use the LEFT JOIN operator instead of the INNER JOIN in the case we are
155                     //joining against a field that has not been populated.
156                     if(!empty($def['innerjoin']) ){
157                         if (empty($def['db_field']) )
158                             continue;
159                         $innerJoins[$field] = $def;
160                         $def['innerjoin'] = str_replace('INNER', 'LEFT', $def['innerjoin']);
161                     }
162                     $unifiedSearchFields[ $moduleName ] [ $field ] = $def ;
163                     $unifiedSearchFields[ $moduleName ] [ $field ][ 'value' ] = $this->query_string ;
164                 }
165
166                 /*
167                  * Use searchForm2->generateSearchWhere() to create the search query, as it can generate SQL for the full set of comparisons required
168                  * generateSearchWhere() expects to find the search conditions for a field in the 'value' parameter of the searchFields entry for that field
169                  */
170                 require_once $beanFiles[$beanName] ;
171                 $seed = new $beanName();
172                                  require_once 'include/SearchForm/SearchForm2.php' ;
173                 $searchForm = new SearchForm ( $seed, $moduleName ) ;
174
175                 $searchForm->setup (array ( $moduleName => array() ) , $unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
176                 $where_clauses = $searchForm->generateSearchWhere() ;
177                 //add inner joins back into the where clause
178                 $params = array('custom_select' => "");
179                 foreach($innerJoins as $field=>$def) {
180                     if (isset ($def['db_field'])) {
181                       foreach($def['db_field'] as $dbfield)
182                           $where_clauses[] = $dbfield . " LIKE '" . $this->query_string . "%'";
183                           $params['custom_select'] .= ", $dbfield";
184                           $params['distinct'] = true;
185                           //$filterFields[$dbfield] = $dbfield;
186                     }
187                 }
188
189                                     if (count($where_clauses) > 0 )
190                                         $where = '(('. implode(' ) OR ( ', $where_clauses) . '))';
191
192                 $lv = new ListViewSmarty();
193                 $lv->lvd->additionalDetails = false;
194                 $mod_strings = return_module_language($current_language, $seed->module_dir);
195                 if(file_exists('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php')){
196                     require_once('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
197                 }else{
198                     require_once('modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
199
200                 }
201                 if ( !isset($listViewDefs) || !isset($listViewDefs[$seed->module_dir]) )
202                     continue;
203                                 $displayColumns = array();
204                 foreach($listViewDefs[$seed->module_dir] as $colName => $param) {
205                     if(!empty($param['default']) && $param['default'] == true) {
206                         $param['url_sort'] = true;//bug 27933
207                         $displayColumns[$colName] = $param;
208                     }
209                 }
210
211                 if(count($displayColumns) > 0) $lv->displayColumns = $displayColumns;
212                 else $lv->displayColumns = $listViewDefs[$seed->module_dir];
213
214                 $lv->export = false;
215                 $lv->mergeduplicates = false;
216                 $lv->multiSelect = false;
217                 $lv->delete = false;
218                 $lv->select = false;
219                 $lv->showMassupdateFields = false;
220                 if($overlib) {
221                     $lv->overlib = true;
222                     $overlib = false;
223                 }
224                 else $lv->overlib = false;
225                 
226                 
227                 
228                 $lv->setup($seed, 'include/ListView/ListViewGeneric.tpl', $where, $params, 0, 10);
229
230                 $module_results[$moduleName] = '<br /><br />' . get_form_header($GLOBALS['app_list_strings']['moduleList'][$seed->module_dir] . ' (' . $lv->data['pageData']['offsets']['total'] . ')', '', false);
231                 $module_counts[$moduleName] = $lv->data['pageData']['offsets']['total'];
232
233                 if($lv->data['pageData']['offsets']['total'] == 0) {
234                     $module_results[$moduleName] .= '<h2>' . $home_mod_strings['LBL_NO_RESULTS_IN_MODULE'] . '</h2>';
235                 }
236                 else {
237                     $has_results = true;
238                     $module_results[$moduleName] .= $lv->display(false, false);
239                 }
240                         }
241                 }
242
243                 if($has_results) {
244                         arsort($module_counts);
245                         foreach($module_counts as $name=>$value) {
246                                 echo $module_results[$name];
247                         }
248                 }
249                 else {
250                         echo '<br>';
251                         echo $home_mod_strings['LBL_NO_RESULTS'];
252                         echo $home_mod_strings['LBL_NO_RESULTS_TIPS'];
253                 }
254
255         }
256
257         function buildCache()
258         {
259
260                 global $beanList, $beanFiles, $dictionary;
261
262                 $supported_modules = array();
263
264                 foreach($beanList as $moduleName=>$beanName)
265                 {
266                         if (!isset($beanFiles[$beanName]))
267                                 continue;
268
269                         if($beanName == 'aCase') $beanName = 'Case';
270                         
271                         $manager = new VardefManager ( );
272                         $manager->loadVardef( $moduleName , $beanName ) ;
273
274                         // obtain the field definitions used by generateSearchWhere (duplicate code in view.list.php)
275                         if(file_exists('custom/modules/'.$moduleName.'/metadata/metafiles.php')){
276                 require('custom/modules/'.$moduleName.'/metadata/metafiles.php');       
277             }elseif(file_exists('modules/'.$moduleName.'/metadata/metafiles.php')){
278                 require('modules/'.$moduleName.'/metadata/metafiles.php');
279             }
280                 
281                         
282                         if(!empty($metafiles[$moduleName]['searchfields']))
283                                 require $metafiles[$moduleName]['searchfields'] ;
284                         elseif(file_exists("modules/{$moduleName}/metadata/SearchFields.php"))
285                                 require "modules/{$moduleName}/metadata/SearchFields.php" ;
286
287                         if(isset($dictionary[$beanName]['unified_search']) && $dictionary[$beanName]['unified_search']) // if bean participates in uf
288                         {
289
290                                 $fields = array();
291                                 foreach ( $dictionary [ $beanName ][ 'fields' ] as $field => $def )
292                                 {
293                                         // We cannot enable or disable unified_search for email in the vardefs as we don't actually have a vardef entry for 'email' -
294                                         // the searchFields entry for 'email' doesn't correspond to any vardef entry. Instead it contains SQL to directly perform the search.
295                                         // So as a proxy we allow any field in the vardefs that has a name starting with 'email...' to be tagged with the 'unified_search' parameter
296
297                                         if (strpos($field,'email') !== false)
298                                                 $field = 'email' ;
299                                                 
300                                         //bug: 38139 - allow phone to be searched through Global Search
301                                         if (strpos($field,'phone') !== false)
302                                                 $field = 'phone' ;
303
304                                         if ( isset($def['unified_search']) && $def['unified_search'] && isset ( $searchFields [ $moduleName ] [ $field ]  ))
305                                         {
306                                                 $fields [ $field ] = $searchFields [ $moduleName ] [ $field ] ;
307                                         }
308                                 }
309
310                                 if(count($fields) > 0) {
311                                         $supported_modules [$moduleName] ['fields'] = $fields;
312                                         if ( isset($dictionary[$beanName]['unified_search_default_enabled']) && 
313                                                 $dictionary[$beanName]['unified_search_default_enabled'] == FALSE ) {
314                         $supported_modules [$moduleName]['default'] = false;
315                     }
316                     else {
317                         $supported_modules [$moduleName]['default'] = true;
318                     }
319                                 }
320
321                         }
322
323                 }
324                 ksort($supported_modules);
325                 write_array_to_file('unified_search_modules', $supported_modules, $GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
326
327         }
328 }
329
330 ?>