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 ********************************************************************************/
42 * Performs the search and returns the HTML widget containing the results
44 * @param $query string what we are searching for
45 * @param $modules array modules we are searching in
46 * @param $offset int search result offset
47 * @return string HTML widget
49 public function searchAndDisplay(
55 $query_encoded = urlencode($query);
56 $results = $this->_performSearch($query, $modules, $offset);
57 $str = '<div id="SpotResults">';
60 foreach($results as $m=>$data){
61 if(empty($data['data'])){
66 $countRemaining = $data['pageData']['offsets']['total'] - count($data['data']);
67 if($offset > 0) $countRemaining -= $offset;
69 $data['pageData']['offsets']['next']++;
70 if($countRemaining > 0){
72 <small class='more' onclick="DCMenu.spotZoom('$query', '$m','{$data['pageData']['offsets']['next']}' )">($countRemaining more)</small>
76 $modDisplayString = $m;
77 if(isset($GLOBALS['app_list_strings']['moduleList'][$m]))
78 $modDisplayString = $GLOBALS['app_list_strings']['moduleList'][$m];
80 $str.= "<div>{$modDisplayString} $more</div>";
82 foreach($data['data'] as $row){
84 if(!empty($row['NAME'])){
87 foreach($row as $k=>$v){
88 if(strpos($k, 'NAME') !== false){
96 <li><a href="index.php?module={$data['pageData']['bean']['moduleDir']}&action=DetailView&record={$row['ID']}">$name</a></li>
102 <button onclick="document.location.href='index.php?module=Home&action=UnifiedSearch&search_form=false&advanced=false&query_string={$query_encoded}'">{$GLOBALS['app_strings']['LBL_EMAIL_SHOW_READ']}</button>
109 * Returns the array containing the $searchFields for a module
111 * @param $moduleName string
114 protected function getSearchFields(
118 if(file_exists("modules/{$moduleName}/metadata/SearchFields.php")) {
119 $searchFields = array();
120 require "modules/{$moduleName}/metadata/SearchFields.php" ;
121 return $searchFields;
128 * Performs the search
130 * @param $query string what we are searching for
131 * @param $modules array modules we are searching in
132 * @param $offset int search result offset
135 protected function _performSearch(
143 require_once 'include/SearchForm/SearchForm2.php' ;
146 $searchEmail = preg_match('/^([^\%]|\%)*@([^\%]|\%)*$/', $query);
148 foreach($modules as $moduleName){
149 if (empty($primary_module)) $primary_module=$moduleName;
151 $searchFields = SugarSpot::getSearchFields($moduleName);
152 $class = $GLOBALS['beanList'][$moduleName];
153 $return_fields = array();
154 $seed = new $class();
155 if (empty($searchFields[$moduleName]))
158 if ($class == 'aCase') {
161 foreach($searchFields[$moduleName] as $k=>$v){
163 $searchFields[$moduleName][$k]['value'] = $query;
165 if(!empty($GLOBALS['dictionary'][$class]['unified_search'])){
166 if(empty($GLOBALS['dictionary'][$class]['fields'][$k]['unified_search'])){
168 if(isset($searchFields[$moduleName][$k]['db_field'])){
169 foreach($searchFields[$moduleName][$k]['db_field'] as $field){
170 if(!empty($GLOBALS['dictionary'][$class]['fields'][$field]['unified_search'])){
171 $return_fields[] = $field;
177 if(strpos($k,'email') === false || !$searchEmail) {
178 unset($searchFields[$moduleName][$k]);
182 $return_fields[] = $k;
184 }else if(empty($GLOBALS['dictionary'][$class]['fields'][$k]) ){;
185 unset($searchFields[$moduleName][$k]);
187 switch($GLOBALS['dictionary'][$class]['fields'][$k]['type']){
192 unset($searchFields[$moduleName][$k]);
194 $return_fields[] = $k;
203 $searchForm = new SearchForm ( $seed, $moduleName ) ;
204 $searchForm->setup (array ( $moduleName => array() ) , $searchFields , '' , 'saved_views' /* hack to avoid setup doing further unwanted processing */ ) ;
205 $where_clauses = $searchForm->generateSearchWhere() ;
207 if (count($where_clauses) > 0){
208 $where = '(('. implode(' ) OR ( ', $where_clauses) . '))';
211 $lvd = new ListViewData();
212 $lvd->additionalDetails = false;
213 $max = ( !empty($sugar_config['max_spotresults_initial']) ? $sugar_config['max_spotresults_initial'] : 5 );
215 $max = ( !empty($sugar_config['max_spotresults_more']) ? $sugar_config['max_spotresults_more'] : 20 );
218 if ( $moduleName == 'Reports') {
219 $params['overrideOrder'] = true;
220 $params['orderBy'] = 'name';
222 $results[$moduleName]= $lvd->getListViewData($seed, $where, $offset, $max, $return_fields,$params,'id') ;
229 * Function used to walk the array and find keys that map the queried string.
230 * if both the pattern and module name is found the promote the string to thet top.
232 protected function _searchKeys(
238 //make the module name singular....
239 if ($patterns[1][strlen($patterns[1])-1] == 's') {
240 $patterns[1]=substr($patterns[1],0,(strlen($patterns[1])-1));
243 $module_exists = stripos($key,$patterns[1]); //primary module name.
244 $pattern_exists = stripos($key,$patterns[0]); //pattern provided by the user.
245 if ($module_exists !== false and $pattern_exists !== false) {
246 $GLOBALS['matching_keys']= array_merge(array(array('NAME'=>$key, 'ID'=>$key, 'VALUE'=>$item1)),$GLOBALS['matching_keys']);
249 if ($pattern_exists !== false) {
250 $GLOBALS['matching_keys'][]=array('NAME'=>$key, 'ID'=>$key, 'VALUE'=>$item1);