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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
40 * Description: TODO: To be written.
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
48 class UnifiedSearchAdvanced {
50 var $query_string = '';
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;
61 function getDropDownDiv($tpl = 'modules/Home/UnifiedSearchAdvanced.tpl') {
62 global $app_list_strings;
64 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
66 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
68 global $mod_strings, $modListHeader, $app_list_strings, $current_user, $app_strings, $beanList;
69 $users_modules = $current_user->getPreference('globalSearch', 'search');
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];
78 $current_user->setPreference('globalSearch', $users_modules, 0, 'search');
80 $sugar_smarty = new Sugar_Smarty();
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;
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;
98 return $sugar_smarty->fetch($tpl);
102 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
105 include $GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php';
106 require_once 'include/ListView/ListViewSmarty.php';
109 global $modListHeader, $beanList, $beanFiles, $current_language, $app_strings, $current_user, $mod_strings;
110 $home_mod_strings = return_module_language($current_language, 'Home');
113 $this->query_string = $GLOBALS['db']->quote(securexss(from_html(clean_string($this->query_string, 'UNIFIED_SEARCH'))));
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]];
122 $current_user->setPreference('globalSearch', $modules_to_search, 0, 'search'); // save selections to user preference
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;
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];
140 $current_user->setPreference('globalSearch', $modules_to_search, 'search');
142 echo $this->getDropDownDiv('modules/Home/UnifiedSearchAdvancedForm.tpl');
144 $module_results = array();
145 $module_counts = array();
146 $has_results = false;
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 )
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']) )
159 $innerJoins[$field] = $def;
160 $def['innerjoin'] = str_replace('INNER', 'LEFT', $def['innerjoin']);
162 $unifiedSearchFields[ $moduleName ] [ $field ] = $def ;
163 $unifiedSearchFields[ $moduleName ] [ $field ][ 'value' ] = $this->query_string ;
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
170 require_once $beanFiles[$beanName] ;
171 $seed = new $beanName();
172 require_once 'include/SearchForm/SearchForm2.php' ;
173 $searchForm = new SearchForm ( $seed, $moduleName ) ;
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;
189 if (count($where_clauses) > 0 )
190 $where = '(('. implode(' ) OR ( ', $where_clauses) . '))';
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');
198 require_once('modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
201 if ( !isset($listViewDefs) || !isset($listViewDefs[$seed->module_dir]) )
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;
211 if(count($displayColumns) > 0) $lv->displayColumns = $displayColumns;
212 else $lv->displayColumns = $listViewDefs[$seed->module_dir];
215 $lv->mergeduplicates = false;
216 $lv->multiSelect = false;
219 $lv->showMassupdateFields = false;
224 else $lv->overlib = false;
228 $lv->setup($seed, 'include/ListView/ListViewGeneric.tpl', $where, $params, 0, 10);
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'];
233 if($lv->data['pageData']['offsets']['total'] == 0) {
234 $module_results[$moduleName] .= '<h2>' . $home_mod_strings['LBL_NO_RESULTS_IN_MODULE'] . '</h2>';
238 $module_results[$moduleName] .= $lv->display(false, false);
244 arsort($module_counts);
245 foreach($module_counts as $name=>$value) {
246 echo $module_results[$name];
251 echo $home_mod_strings['LBL_NO_RESULTS'];
252 echo $home_mod_strings['LBL_NO_RESULTS_TIPS'];
257 function buildCache()
260 global $beanList, $beanFiles, $dictionary;
262 $supported_modules = array();
264 foreach($beanList as $moduleName=>$beanName)
266 if (!isset($beanFiles[$beanName]))
269 if($beanName == 'aCase') $beanName = 'Case';
271 $manager = new VardefManager ( );
272 $manager->loadVardef( $moduleName , $beanName ) ;
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');
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" ;
287 if(isset($dictionary[$beanName]['unified_search']) && $dictionary[$beanName]['unified_search']) // if bean participates in uf
291 foreach ( $dictionary [ $beanName ][ 'fields' ] as $field => $def )
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
297 if (strpos($field,'email') !== false)
300 //bug: 38139 - allow phone to be searched through Global Search
301 if (strpos($field,'phone') !== false)
304 if ( isset($def['unified_search']) && $def['unified_search'] && isset ( $searchFields [ $moduleName ] [ $field ] ))
306 $fields [ $field ] = $searchFields [ $moduleName ] [ $field ] ;
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;
317 $supported_modules [$moduleName]['default'] = true;
324 ksort($supported_modules);
325 write_array_to_file('unified_search_modules', $supported_modules, $GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');