]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SearchForm/SugarSpot.php
Release 6.1.4
[Github/sugarcrm.git] / include / SearchForm / SugarSpot.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 class SugarSpot 
40 {       
41         /**
42          * Performs the search and returns the HTML widget containing the results
43          *
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
48          */
49         public function searchAndDisplay(
50             $query, 
51             $modules, 
52             $offset = -1
53             )
54         {
55                 $query_encoded = urlencode($query);
56             $results = $this->_performSearch($query, $modules, $offset);
57                 $str = '<div id="SpotResults">';
58                 $actions=0;
59                 $foundData = false;
60                 foreach($results as $m=>$data){ 
61                         if(empty($data['data'])){
62                                 continue;
63                         }
64                         $foundData = true;
65                         
66                         $countRemaining = $data['pageData']['offsets']['total'] - count($data['data']);
67                         if($offset > 0) $countRemaining -= $offset;
68                         $more = '';
69                         $data['pageData']['offsets']['next']++;
70                         if($countRemaining > 0){
71                                 $more = <<<EOHTML
72 <small class='more' onclick="DCMenu.spotZoom('$query', '$m','{$data['pageData']['offsets']['next']}' )">($countRemaining more)</small>
73 EOHTML;
74                         }
75                         
76                         $modDisplayString = $m;
77                         if(isset($GLOBALS['app_list_strings']['moduleList'][$m]))
78                             $modDisplayString = $GLOBALS['app_list_strings']['moduleList'][$m];
79                         
80                         $str.= "<div>{$modDisplayString} $more</div>";
81                         $str.= '<ul>';
82                         foreach($data['data'] as $row){
83                                 $name = '';
84                                 if(!empty($row['NAME'])){
85                                         $name = $row['NAME'];
86                                 }else{
87                                         foreach($row as $k=>$v){
88                                                 if(strpos($k, 'NAME') !== false){
89                                                         $name = $v;
90                                                         break;
91                                                 }
92                                         }
93                                 }
94                         
95                                     $str .= <<<EOHTML
96 <li><a href="index.php?module={$data['pageData']['bean']['moduleDir']}&action=DetailView&record={$row['ID']}">$name</a></li>
97 EOHTML;
98                         }
99                         $str.= '</ul>';
100                 }
101                 $str .= <<<EOHTML
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>
103 </div>
104 EOHTML;
105                 return $str;
106         }
107         
108         /**
109          * Returns the array containing the $searchFields for a module
110          *
111          * @param  $moduleName string
112          * @return array
113          */
114         protected function getSearchFields(
115             $moduleName
116             )
117         {
118                 if(file_exists("modules/{$moduleName}/metadata/SearchFields.php")) {
119                         $searchFields = array();
120                     require "modules/{$moduleName}/metadata/SearchFields.php" ;
121                         return $searchFields;
122                 }
123                 else {
124                         return array();
125                 }
126         }
127         /**
128          * Performs the search
129          *
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
133          * @return array
134          */
135         protected function _performSearch(
136             $query, 
137             $modules, 
138             $offset = -1
139             )
140         {
141                 $primary_module='';
142                 $results = array();
143                 require_once 'include/SearchForm/SearchForm2.php' ;
144                 $where = '';
145                 
146                 $searchEmail = preg_match('/^([^\%]|\%)*@([^\%]|\%)*$/', $query);
147                 
148                 foreach($modules as $moduleName){ 
149                         if (empty($primary_module)) $primary_module=$moduleName;
150                         
151                         $searchFields = SugarSpot::getSearchFields($moduleName);
152                         $class = $GLOBALS['beanList'][$moduleName];
153                         $return_fields = array();
154                         $seed = new $class();
155                         if (empty($searchFields[$moduleName]))
156                             continue;
157                             
158                                 if ($class == 'aCase') {
159                                     $class = 'Case';
160                                 }
161                                 foreach($searchFields[$moduleName] as $k=>$v){
162                                         $keep = false;
163                                         $searchFields[$moduleName][$k]['value'] = $query;
164
165                                         if(!empty($GLOBALS['dictionary'][$class]['unified_search'])){  
166                                                 if(empty($GLOBALS['dictionary'][$class]['fields'][$k]['unified_search'])){
167                                                         
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;
172                                                                                 $keep = true;
173                                                                         }
174                                                                 }
175                                                         }
176                                                         if(!$keep){
177                                                                 if(strpos($k,'email') === false || !$searchEmail) {
178                                                                         unset($searchFields[$moduleName][$k]);
179                                                                 }
180                                                         }
181                                                 }else{
182                                                         $return_fields[] = $k;
183                                                 }
184                                         }else if(empty($GLOBALS['dictionary'][$class]['fields'][$k]) ){;
185                                                 unset($searchFields[$moduleName][$k]);
186                                         }else{
187                                                 switch($GLOBALS['dictionary'][$class]['fields'][$k]['type']){
188                                                         case 'id':
189                                                         case 'date':
190                                                         case 'datetime':
191                                                         case 'bool':
192                                                                 unset($searchFields[$moduleName][$k]);
193                                                         default:
194                                                                 $return_fields[] = $k;
195                                                                 
196                                                 }
197                                                 
198                                         }
199                                         
200                                 }
201
202                 
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() ;
206                         $where = "";
207                         if (count($where_clauses) > 0){
208                 $where = '(('. implode(' ) OR ( ', $where_clauses) . '))';
209             }
210                         
211                         $lvd = new ListViewData();
212                         $lvd->additionalDetails = false;
213                         $max = ( !empty($sugar_config['max_spotresults_initial']) ? $sugar_config['max_spotresults_initial'] : 5 );
214                         if($offset !== -1){
215                                 $max = ( !empty($sugar_config['max_spotresults_more']) ? $sugar_config['max_spotresults_more'] : 20 );
216                         }
217                         $params = array();
218                         if ( $moduleName == 'Reports') {
219                             $params['overrideOrder'] = true;
220                             $params['orderBy'] = 'name';
221                         }
222                         $results[$moduleName]= $lvd->getListViewData($seed, $where, $offset,  $max, $return_fields,$params,'id') ;
223                         
224                 }
225         return $results;
226         }       
227         
228         /**
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.
231      */
232     protected function _searchKeys(
233         $item1, 
234         $key, 
235         $patterns
236         ) 
237     {
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));
241         }
242         
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']);
247         } 
248         else {
249             if ($pattern_exists !== false) {
250                 $GLOBALS['matching_keys'][]=array('NAME'=>$key, 'ID'=>$key, 'VALUE'=>$item1);
251             }
252         }
253     }
254 }