]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Home/UnifiedSearchAdvanced.php
Release 6.2.0beta4
[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, $app_strings;
63
64                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
65                 {
66                         $this->buildCache();
67                 }
68                 
69                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
70
71                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php'))
72                 {
73                    $this->createUnifiedSearchModulesDisplay();
74                 }
75                 
76                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php');            
77                 
78                 global $mod_strings, $modListHeader, $app_list_strings, $current_user, $app_strings, $beanList;
79                 $users_modules = $current_user->getPreference('globalSearch', 'search');
80
81                 // preferences are empty, select all
82                 if(empty($users_modules)) {                     
83                         $users_modules = array();
84                         foreach($unified_search_modules_display as $module=>$data) {
85                                 if (!empty($data['visible']) ) {
86                     $users_modules[$module] = $beanList[$module];
87                 }
88                         }
89                         $current_user->setPreference('globalSearch', $users_modules, 0, 'search');
90                 }
91                 
92                 $sugar_smarty = new Sugar_Smarty();
93
94                 $modules_to_search = array();
95
96                 foreach($users_modules as $key=>$module) 
97                 {
98             if(ACLController::checkAccess($key, 'list', true)) 
99             {
100                 $modules_to_search[$key]['checked'] = true;
101             }
102                 }
103                 
104                 if(!empty($this->query_string))
105                 {
106                         $sugar_smarty->assign('query_string', securexss($this->query_string));
107                 } else {
108                         $sugar_smarty->assign('query_string', '');
109                 }
110                 
111                 $sugar_smarty->assign('MOD', return_module_language($GLOBALS['current_language'], 'Administration'));
112                 $sugar_smarty->assign('APP', $app_strings);
113                 $sugar_smarty->assign('USE_SEARCH_GIF', 0);
114                 $sugar_smarty->assign('LBL_SEARCH_BUTTON_LABEL', $app_strings['LBL_SEARCH_BUTTON_LABEL']);
115                 
116                 $json_enabled = array();
117                 $json_disabled = array();
118                 
119                 //Now add the rest of the modules that are searchable via Global Search settings
120                 foreach($unified_search_modules_display as $module=>$data)
121                 {
122                         if(!isset($modules_to_search[$module]) && $data['visible'] && ACLController::checkAccess($module, 'list', true))
123                         {
124                            $modules_to_search[$module]['checked'] = false;     
125                         } else if (isset($modules_to_search[$module]) && !$data['visible']) {
126                            unset($modules_to_search[$module]);
127                         }
128                 }               
129
130                 //Create the two lists (doing it this way preserves the user's ordering choice for enabled modules)
131                 foreach($modules_to_search as $module=>$data)
132                 {
133                         $label = isset($app_list_strings['moduleList'][$module]) ? $app_list_strings['moduleList'][$module] : $module;
134                         if(!empty($data['checked']))
135                         {
136                                 $json_enabled[] = array("module" => $module, 'label' => $label);
137                         } else {
138                                 $json_disabled[] = array("module" => $module, 'label' => $label);
139                         }       
140                 }
141                 
142                 $sugar_smarty->assign('enabled_modules', json_encode($json_enabled));
143                 $sugar_smarty->assign('disabled_modules', json_encode($json_disabled));                 
144                 
145                 $showDiv = $current_user->getPreference('showGSDiv', 'search');
146                 if(!isset($showDiv))
147                 {
148                    $showDiv = 'no';
149                 }           
150
151                 $sugar_smarty->assign('SHOWGSDIV', $showDiv);
152                 $sugar_smarty->debugging = true;
153                 return $sugar_smarty->fetch($tpl);
154         }
155
156         function search() {
157                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
158                 {
159                         $this->buildCache();
160                 }
161                 
162                 include $GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php';
163                 require_once 'include/ListView/ListViewSmarty.php';
164                 
165                 global $modListHeader, $beanList, $beanFiles, $current_language, $app_strings, $current_user, $mod_strings;
166                 $home_mod_strings = return_module_language($current_language, 'Home');
167
168                 $overlib = true;
169                 $this->query_string = $GLOBALS['db']->quote(securexss(from_html(clean_string($this->query_string, 'UNIFIED_SEARCH'))));
170
171                 if(!empty($_REQUEST['advanced']) && $_REQUEST['advanced'] != 'false') {
172                         $modules_to_search = array();
173                         if(!empty($_REQUEST['search_modules']))
174                         {
175                             foreach(explode (',', $_REQUEST['search_modules'] ) as $e)
176                     {
177                         $modules_to_search[$e] = $beanList[$e];
178                     }
179                         }
180
181                         $current_user->setPreference('showGSDiv', isset($_REQUEST['showGSDiv']) ? $_REQUEST['showGSDiv'] : 'no', 0, 'search');
182                         $current_user->setPreference('globalSearch', $modules_to_search, 0, 'search'); // save selections to user preference
183                 } else {
184                         $users_modules = $current_user->getPreference('globalSearch', 'search');
185                         $modules_to_search = array();
186                                                 
187                         if(!empty($users_modules)) { 
188                                 // use user's previous selections
189                             foreach ( $users_modules as $key => $value ) {
190                                 if ( isset($unified_search_modules[$key]) ) {
191                                     $modules_to_search[$key] = $value;
192                                 }
193                             }
194                         } else { 
195                                 // select all the modules (ie first time user has used global search)
196                                 foreach($unified_search_modules as $module=>$data) {
197                                     if (!empty($data['default']) ) {
198                                         $modules_to_search[$module] = $beanList[$module];
199                                     }
200                                 }
201                         }
202                         $current_user->setPreference('globalSearch', $modules_to_search, 'search');
203                 }
204                 
205
206                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php'))
207                 {
208                    $this->createUnifiedSearchModulesDisplay();
209                 }
210                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php');
211                 foreach($modules_to_search as $module=>$data)
212                 {
213                         if(isset($unified_search_modules_display[$module]['visible']) && !$unified_search_modules_display[$module]['visible'])
214                         {
215                            unset($modules_to_search[$module]);
216                         }
217                 }
218         
219                 $templateFile = 'modules/Home/UnifiedSearchAdvancedForm.tpl';
220                 if(file_exists('custom/' . $templateFile))
221                 {
222                    $templateFile = 'custom/'.$templateFile;
223                 }
224                 
225                 echo $this->getDropDownDiv($templateFile);
226
227                 $module_results = array();
228                 $module_counts = array();
229                 $has_results = false;
230
231                 if(!empty($this->query_string)) {
232                         foreach($modules_to_search as $moduleName => $beanName) {
233                 require_once $beanFiles[$beanName] ;
234                 $seed = new $beanName();
235                 
236                 $lv = new ListViewSmarty();
237                 $lv->lvd->additionalDetails = false;
238                 $mod_strings = return_module_language($current_language, $seed->module_dir);
239                 
240                 //retrieve the original list view defs and store for processing in case of custom layout changes
241                 require('modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
242                                 $orig_listViewDefs = $listViewDefs;
243                                 
244                 if(file_exists('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php'))
245                 {
246                     require('custom/modules/'.$seed->module_dir.'/metadata/listviewdefs.php');
247                 }
248                 
249                 if ( !isset($listViewDefs) || !isset($listViewDefs[$seed->module_dir]) )
250                 {
251                     continue;
252                 }
253                 
254                             $unifiedSearchFields = array () ;
255                 $innerJoins = array();
256                 foreach ( $unified_search_modules[ $moduleName ]['fields'] as $field=>$def )
257                 {
258                         $listViewCheckField = strtoupper($field);
259                         //check to see if the field is in listview defs
260                                         if ( empty($listViewDefs[$seed->module_dir][$listViewCheckField]['default']) ) {
261                                                 //check to see if field is in original list view defs (in case we are using custom layout defs)
262                                                 if (!empty($orig_listViewDefs[$seed->module_dir][$listViewCheckField]['default']) ) {
263                                                         //if we are here then the layout has been customized, but the field is still needed for query creation
264                                                         $listViewDefs[$seed->module_dir][$listViewCheckField] = $orig_listViewDefs[$seed->module_dir][$listViewCheckField];
265                                                 }
266
267                                         }
268                     
269                     //bug: 34125 we might want to try to use the LEFT JOIN operator instead of the INNER JOIN in the case we are
270                     //joining against a field that has not been populated.
271                     if(!empty($def['innerjoin']) )
272                     {
273                         if (empty($def['db_field']) )
274                         {
275                             continue;
276                         }
277                         $innerJoins[$field] = $def;
278                         $def['innerjoin'] = str_replace('INNER', 'LEFT', $def['innerjoin']);
279                     }
280                     $unifiedSearchFields[ $moduleName ] [ $field ] = $def ;
281                     $unifiedSearchFields[ $moduleName ] [ $field ][ 'value' ] = $this->query_string ;
282                 }
283                 
284                 /*
285                  * Use searchForm2->generateSearchWhere() to create the search query, as it can generate SQL for the full set of comparisons required
286                  * generateSearchWhere() expects to find the search conditions for a field in the 'value' parameter of the searchFields entry for that field
287                  */
288                 require_once $beanFiles[$beanName] ;
289                 $seed = new $beanName();
290                                  require_once 'include/SearchForm/SearchForm2.php' ;
291                 $searchForm = new SearchForm ( $seed, $moduleName ) ;
292                 
293                 $searchForm->setup (array ( $moduleName => array() ) , $unifiedSearchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
294                 $where_clauses = $searchForm->generateSearchWhere() ;
295                 //add inner joins back into the where clause
296                 $params = array('custom_select' => "");
297                 foreach($innerJoins as $field=>$def) {
298                     if (isset ($def['db_field'])) {
299                       foreach($def['db_field'] as $dbfield)
300                           $where_clauses[] = $dbfield . " LIKE '" . $this->query_string . "%'";
301                           $params['custom_select'] .= ", $dbfield";
302                           $params['distinct'] = true;
303                           //$filterFields[$dbfield] = $dbfield;
304                     }
305                 }
306
307                 if (count($where_clauses) > 0) 
308                 {
309                     $where = '(('. implode(' ) OR ( ', $where_clauses) . '))';
310                 }
311       
312                 $displayColumns = array();
313                 foreach($listViewDefs[$seed->module_dir] as $colName => $param) 
314                 {
315                     if(!empty($param['default']) && $param['default'] == true) 
316                     {
317                         $param['url_sort'] = true;//bug 27933
318                         $displayColumns[$colName] = $param;
319                     }
320                 }
321
322                 if(count($displayColumns) > 0) 
323                 {
324                         $lv->displayColumns = $displayColumns;
325                 } else {
326                         $lv->displayColumns = $listViewDefs[$seed->module_dir];
327                 }
328
329                 $lv->export = false;
330                 $lv->mergeduplicates = false;
331                 $lv->multiSelect = false;
332                 $lv->delete = false;
333                 $lv->select = false;
334                 $lv->showMassupdateFields = false;
335                 $lv->email = false;
336                 if($overlib) {
337                     $lv->overlib = true;
338                     $overlib = false;
339                 } else {
340                         $lv->overlib = false;
341                 }
342                 
343                 $lv->setup($seed, 'include/ListView/ListViewNoMassUpdate.tpl', $where, $params, 0, 10);
344                 
345                 $module_results[$moduleName] = '<br /><br />' . get_form_header($GLOBALS['app_list_strings']['moduleList'][$seed->module_dir] . ' (' . $lv->data['pageData']['offsets']['total'] . ')', '', false);
346                 $module_counts[$moduleName] = $lv->data['pageData']['offsets']['total'];
347
348                 if($lv->data['pageData']['offsets']['total'] == 0) {
349                     //$module_results[$moduleName] .= "<li class='noBullet' id='whole_subpanel_{$moduleName}'><div id='div_{$moduleName}'><h2>" . $home_mod_strings['LBL_NO_RESULTS_IN_MODULE'] . '</h2></div></li>';
350                     $module_results[$moduleName] .= '<h2>' . $home_mod_strings['LBL_NO_RESULTS_IN_MODULE'] . '</h2>';
351                 } else {
352                     $has_results = true;
353                     //$module_results[$moduleName] .= "<li class='noBullet' id='whole_subpanel_{$moduleName}'><div id='div_{$moduleName}'>" . $lv->display(false, false) . '</div></li>';
354                     $module_results[$moduleName] .= $lv->display(false, false);
355                 }
356                 
357                         }
358                 }
359                                 
360                 if($has_results) {
361                         //arsort($module_counts);
362                         foreach($module_counts as $name=>$value) {
363                                 echo $module_results[$name];
364                         }
365                 } else {
366                         echo $home_mod_strings['LBL_NO_RESULTS'];
367                         echo $home_mod_strings['LBL_NO_RESULTS_TIPS'];
368                 }
369
370         }
371
372         function buildCache()
373         {
374
375                 global $beanList, $beanFiles, $dictionary;
376
377                 $supported_modules = array();
378
379                 foreach($beanList as $moduleName=>$beanName)
380                 {
381                         if (!isset($beanFiles[$beanName]))
382                                 continue;
383
384                         if($beanName == 'aCase') $beanName = 'Case';
385                         
386                         $manager = new VardefManager ( );
387                         $manager->loadVardef( $moduleName , $beanName ) ;
388
389                         // obtain the field definitions used by generateSearchWhere (duplicate code in view.list.php)
390                         if(file_exists('custom/modules/'.$moduleName.'/metadata/metafiles.php')){
391                 require('custom/modules/'.$moduleName.'/metadata/metafiles.php');       
392             }elseif(file_exists('modules/'.$moduleName.'/metadata/metafiles.php')){
393                 require('modules/'.$moduleName.'/metadata/metafiles.php');
394             }
395                 
396                         
397                         if(!empty($metafiles[$moduleName]['searchfields']))
398                         {
399                                 require $metafiles[$moduleName]['searchfields'] ;
400                         } else if(file_exists("modules/{$moduleName}/metadata/SearchFields.php")) {
401                                 require "modules/{$moduleName}/metadata/SearchFields.php" ;
402                         }               
403                         
404                         $isCustomModule = preg_match('/^([a-z0-9]{1,5})_([a-z0-9_]+)$/i' , $moduleName);
405                         
406                         //If the bean supports unified search or if it's a custom module bean and unified search is not defined
407                         if(!empty($dictionary[$beanName]['unified_search']) || $isCustomModule)
408                         {
409                                 $fields = array();
410                                 foreach ( $dictionary [ $beanName ][ 'fields' ] as $field => $def )
411                                 {
412                                         // We cannot enable or disable unified_search for email in the vardefs as we don't actually have a vardef entry for 'email' -
413                                         // the searchFields entry for 'email' doesn't correspond to any vardef entry. Instead it contains SQL to directly perform the search.
414                                         // 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
415
416                                         if (strpos($field,'email') !== false)
417                                                 $field = 'email' ;
418                                                 
419                                         //bug: 38139 - allow phone to be searched through Global Search
420                                         if (strpos($field,'phone') !== false)
421                                                 $field = 'phone' ;
422
423                                         if ( !empty($def['unified_search']) && isset ( $searchFields [ $moduleName ] [ $field ]  ))
424                                         {
425                                                 $fields [ $field ] = $searchFields [ $moduleName ] [ $field ] ;
426                                         }
427                                 }
428
429                                 if(count($fields) > 0) {
430                                         $supported_modules [$moduleName] ['fields'] = $fields;
431                                         if (isset($dictionary[$beanName]['unified_search_default_enabled']) && $dictionary[$beanName]['unified_search_default_enabled'] === TRUE) {
432                         $supported_modules [$moduleName]['default'] = true;
433                     } else {
434                         $supported_modules [$moduleName]['default'] = false;
435                     }
436                                 }
437
438                         }
439
440                 }
441                 
442                 ksort($supported_modules);
443                 write_array_to_file('unified_search_modules', $supported_modules, $GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
444         }
445
446         
447         /**
448          * 
449          */
450         function modifyGlobalSearchSettings()
451         {
452                 global $mod_strings, $app_strings, $app_list_strings;
453
454                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php'))
455                 {
456                         $this->createUnifiedSearchModulesDisplay();
457                 }
458                 
459                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php');
460                                 
461                 $sugar_smarty = new Sugar_Smarty();             
462                 $sugar_smarty->assign('APP', $app_strings);
463                 $sugar_smarty->assign('MOD', $mod_strings);
464                 
465                 //Add the translated attribute for display label
466                 $json_enabled = array();
467                 $json_disabled = array();
468                 foreach($unified_search_modules_display as $module=>$data)
469                 {
470                         $label = isset($app_list_strings['moduleList'][$module]) ? $app_list_strings['moduleList'][$module] : $module;
471                         if($data['visible'] === true)
472                         {
473                            $json_enabled[] = array("module" => $module, 'label' => $label);
474                         } else {
475                            $json_disabled[] = array("module" => $module, 'label' => $label);
476                         }
477                 }
478                 
479                 //If the file doesn't exist
480                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
481                 {
482                         $this->buildCache();
483                 }
484                 
485                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');            
486                 
487                 //Now add any new modules that may have since been added to unified_search_modules.php
488                 foreach($unified_search_modules as $module=>$data)
489                 {
490                         if(!isset($unified_search_modules_display[$module]))
491                         {
492                             $label = isset($app_list_strings['moduleList'][$module]) ? $app_list_strings['moduleList'][$module] : $module;
493                                 if($data['default'])
494                                 {
495                                   $json_enabled[] = array("module" => $module, 'label' => $label);
496                                 } else {
497                                   $json_disabled[] = array("module" => $module, 'label' => $label);
498                                 }
499                         }
500                 }
501                 
502                 $sugar_smarty->assign('enabled_modules', json_encode($json_enabled));
503                 $sugar_smarty->assign('disabled_modules', json_encode($json_disabled));         
504                 
505                 //uasort($unified_search_modules_display, 'unified_search_modules_cmp');
506                 $tpl = 'modules/Administration/templates/GlobalSearchSettings.tpl';
507                 if(file_exists('custom/' . $tpl))
508                 {
509                    $tpl = 'custom/' . $tpl;
510                 }
511                 return $sugar_smarty->fetch($tpl);                              
512         }
513         
514         
515         /**
516          * addModuleToUnifiedSearch
517          * This method handles adding a new module to the unified search list of modules.  It will add an 
518          * entry to the unified_search_modules.php if it already exists
519          * 
520          * @param module String value of the module entry to add
521          * @return boolean value indiciating whether or not the module was added to the unified_search_modules.php file
522          */
523         function addModuleToUnifiedSearch($module='')
524         {
525                 if(empty($module))
526                 {
527                    return false;
528                 }
529                 
530                 //If the file doesn't exist
531                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
532                 {
533                         include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
534                         $this->buildCache();
535                         return isset($unified_search_modules[$module]) ? true : false;
536                 }
537                 
538                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
539                 
540                 //If modules is already in list, unset it and rebuild
541                 if(isset($unified_search_modules[$module]))
542                 {
543                    unset($unified_search_modules[$module]);
544                 }
545                 
546                 //Build the entry
547                 global $beanList, $beanFiles, $dictionary;
548                 
549                 if(!isset($beanList[$module]))
550                 {
551                    $GLOBALS['log']->fatal('no beanList entry!');
552                    $beanName = $module;
553                    //return false;
554                 }
555                 
556                 $beanName = $beanList[$module];
557                 
558                 if (!isset($beanFiles[$beanName]))
559                 {
560                         $GLOBALS['log']->fatal('no beanFiles entry!');
561                     //return false;
562                 }
563                 
564                 if($beanName == 'aCase') 
565                 {
566                         $beanName = 'Case';
567                 }
568                         
569                 $manager = new VardefManager();
570                 $manager->loadVardef($module, $beanName);
571
572                 // obtain the field definitions used by generateSearchWhere (duplicate code in view.list.php)
573                 if(file_exists('custom/modules/'.$module.'/metadata/metafiles.php')) 
574                 {
575            require('custom/modules/'.$module.'/metadata/metafiles.php');        
576         } elseif (file_exists('modules/'.$module.'/metadata/metafiles.php')) {
577            require('modules/'.$module.'/metadata/metafiles.php');
578         }
579                 
580                         
581             if(!empty($metafiles[$module]['searchfields']))
582                 {
583                         require $metafiles[$moduleName]['searchfields'] ;
584                 } else if(file_exists("modules/{$module}/metadata/SearchFields.php")) {
585                         require "modules/{$module}/metadata/SearchFields.php" ;
586                 }               
587                         
588                 $isCustomModule = preg_match('/^([a-z0-9]{1,5})_([a-z0-9_]+)$/i' , $module);
589                         
590                 //If the bean supports unified search or if it's a custom module bean and unified search is not defined
591                 if(!empty($dictionary[$beanName]['unified_search']) || $isCustomModule)
592                 {
593                         $GLOBALS['log']->fatal("found dictionary entry!");
594                         $fields = array();
595                         foreach ( $dictionary [ $beanName ][ 'fields' ] as $field => $def )
596                         {
597                                 // We cannot enable or disable unified_search for email in the vardefs as we don't actually have a vardef entry for 'email' -
598                                 // the searchFields entry for 'email' doesn't correspond to any vardef entry. Instead it contains SQL to directly perform the search.
599                                 // 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
600                                 if (strpos($field,'email') !== false)
601                                         $field = 'email' ;
602                                         
603                                 //bug: 38139 - allow phone to be searched through Global Search
604                                 if (strpos($field,'phone') !== false)
605                                         $field = 'phone' ;
606
607                                 if (!empty($def['unified_search']) && isset ($searchFields [$module] [ $field ]))
608                                 {
609                                         $fields[ $field ] = $searchFields [$module] [ $field ] ;
610                                 }
611                         }
612
613                         if(count($fields) > 0) {
614                                 $unified_search_modules [$module] ['fields'] = $fields;
615                                 if (isset($dictionary[$beanName]['unified_search_default_enabled']) && $dictionary[$beanName]['unified_search_default_enabled'] === TRUE) {
616                     $unified_search_modules[$module]['default'] = true;
617                 } else {
618                     $unified_search_modules[$module]['default'] = false;
619                 }
620                         }
621                  }              
622                 
623                  if(!isset($unified_search_modules[$module]))
624                  {
625                         return false;
626                  }
627                  
628                  return write_array_to_file('unified_search_modules', $unified_search_modules, $GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
629         }
630         
631         /**
632          * saveGlobalSearchSettings
633          * This method handles the administrator's request to save the searchable modules selected and stores
634          * the results in the unified_search_modules_display.php file
635          * 
636          */
637         function saveGlobalSearchSettings()
638         {
639                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php'))
640                 {
641                         $this->createUnifiedSearchModulesDisplay();
642                 }
643
644                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php');
645                 
646                 if(isset($_REQUEST['enabled_modules'])) 
647                 {
648                         $new_unified_search_modules_display = array();
649                         
650             foreach(explode (',', $_REQUEST['enabled_modules'] ) as $module)
651             {
652                 $new_unified_search_modules_display[$module]['visible'] = true;
653             }
654      
655                         foreach($unified_search_modules_display as $module=>$data)
656                         {
657                                 if(!isset($new_unified_search_modules_display[$module]))
658                                 {
659                                    $new_unified_search_modules_display[$module]['visible'] = false;
660                                 }
661                         }
662
663                         $this->writeUnifiedSearchModulesDisplayFile($new_unified_search_modules_display);
664                 }
665         }
666         
667         
668         public static function unlinkUnifiedSearchModulesFile() {
669                 //clear the unified_search_module.php file
670         if(file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
671         {
672                 $GLOBALS['log']->info("unlink {$GLOBALS['sugar_config']['cache_dir']}modules/unified_search_modules.php file");
673                 unlink($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
674         }       
675         }
676         
677         /**
678          * createUnifiedSearchModulesDisplay
679          * method to create the unified_search_modules_display.php file
680          * 
681          */
682         function createUnifiedSearchModulesDisplay()
683         {
684                 //Make directory if it doesn't exist
685                 if(!file_exists('cache/modules'))
686                 {
687                    mkdir_recursive('cache/modules');    
688                 }
689                 
690                 //Load unified_search_modules.php file
691                 if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php'))
692                 {
693                         $this->buildCache();
694                 }
695                 
696                 include($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules.php');
697                 
698                 $unified_search_modules_display = array();
699                 
700                 if(!empty($unified_search_modules))
701                 {
702                         foreach($unified_search_modules as $module=>$data)
703                         {
704                                 $unified_search_modules_display[$module]['visible'] = (isset($data['default']) && $data['default']) ? true : false;
705                         }
706                 }
707                 
708                 $this->writeUnifiedSearchModulesDisplayFile($unified_search_modules_display);
709         }
710         
711         
712         /*
713          * writeUnifiedSearchModulesDisplayFile
714          * Private method to handle writing the unified_search_modules_display value to file
715          * 
716          * @param mixed The array of the unified search modules and their display attributes
717          * @return boolean value indication whether or not file was successfully written
718          * @throws Exception Thrown if the file write operation fails
719          */
720         private function writeUnifiedSearchModulesDisplayFile($unified_search_modules_display)
721         {
722                 if(is_null($unified_search_modules_display) || empty($unified_search_modules_display))
723                 {
724                    return false;
725                 }
726                 
727             if(!write_array_to_file("unified_search_modules_display", $unified_search_modules_display, $GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php')) 
728             {
729                 //Log error message and throw Exception
730                 global $app_strings;
731                 $msg = string_format($app_strings['ERR_FILE_WRITE'], array($GLOBALS['sugar_config']['cache_dir'].'modules/unified_search_modules_display.php'));
732                 $GLOBALS['log']->error($msg);
733                 throw new Exception($msg);
734             }           
735             
736             return true;
737         }
738 }
739
740
741 function unified_search_modules_cmp($a, $b) {
742         if(!isset($a['translated']) || !isset($b['translated']))
743         {
744            return 0;
745         }
746         
747         $name1 = strtolower($a['translated']);
748         $name2 = strtolower($b['translated']);
749         
750         return $name1 < $name2 ? -1 : 1;
751 }       
752
753 ?>