]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SearchForm/SearchForm2.php
Release 6.2.3
[Github/sugarcrm.git] / include / SearchForm / SearchForm2.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 /*
38  * Created on May 30, 2007
39  *
40  * To change the template for this generated file go to
41  * Window - Preferences - PHPeclipse - PHP - Code Templates
42  */
43 require_once('include/tabs.php');
44 require_once('include/ListView/ListViewSmarty.php');
45
46 require_once('include/TemplateHandler/TemplateHandler.php');
47 require_once('include/EditView/EditView2.php');
48
49
50  class SearchForm extends EditView{
51         var $seed = null;
52         var $module = '';
53         var $action = 'index';
54         var $searchdefs = array();
55         var $listViewDefs = array();
56         var $lv;
57         var $th;
58     var $tpl;
59     var $view = 'SearchForm';
60     var $displayView = 'basic_search';
61     var $formData;
62     var $fieldDefs;
63     var $customFieldDefs;
64     var $tabs;
65     var $parsedView = 'basic';
66     //may remove
67     var $searchFields;
68     var $displaySavedSearch = true;
69     //show the advanced tab
70     var $showAdvanced = true;
71     //show the basic tab
72     var $showBasic = true;
73     //array of custom tab to show declare in searchdefs (no custom tab if false)
74     var $showCustom = false;
75     // nb of tab to show
76     var $nbTabs = 0;
77     // hide saved searches drop and down near the search button
78     var $showSavedSearchesOptions = true;
79
80     var $displayType = 'searchView';
81
82         function SearchForm($seed, $module, $action = 'index'){
83                 $this->th = new TemplateHandler();
84                 $this->th->loadSmarty();
85                 $this->seed = $seed;
86                 $this->module = $module;
87                 $this->action = $action;
88         $this->tabs = array(array('title'  => $GLOBALS['app_strings']['LNK_BASIC_SEARCH'],
89                             'link'   => $module . '|basic_search',
90                             'key'    => $module . '|basic_search',
91                             'name'   => 'basic',
92                             'displayDiv'   => ''),
93                       array('title'  => $GLOBALS['app_strings']['LNK_ADVANCED_SEARCH'],
94                             'link'   => $module . '|advanced_search',
95                             'key'    => $module . '|advanced_search',
96                             'name'   => 'advanced',
97                             'displayDiv'   => 'display:none'),
98                        );
99         $this->searchColumns = array () ;
100         }
101
102         function setup($searchdefs, $searchFields = array(), $tpl, $displayView = 'basic_search', $listViewDefs = array()){
103                 $this->searchdefs =  $searchdefs[$this->module];
104                 $this->tpl = $tpl;
105                 //used by advanced search
106                 $this->listViewDefs = $listViewDefs;
107                 $this->displayView = $displayView;
108                 $this->view = $this->view.'_'.$displayView;
109                 $tokens = explode('_', $this->displayView);
110                 $this->parsedView = $tokens[0];
111                 if($this->displayView != 'saved_views'){
112                         $this->_build_field_defs();
113                 }
114
115         $this->searchFields = $searchFields[$this->module];
116
117         // Setub the tab array
118         $this->tabs = array();
119         if($this->showBasic){
120             $this->nbTabs++;
121             $this->tabs[]=array('title'  => $GLOBALS['app_strings']['LNK_BASIC_SEARCH'],
122                                 'link'   => $this->module . '|basic_search',
123                                 'key'    => $this->module . '|basic_search',
124                                 'name'   => 'basic',
125                                 'displayDiv' => '');
126         }
127         if($this->showAdvanced){
128             $this->nbTabs++;
129             $this->tabs[]=array('title'  => $GLOBALS['app_strings']['LNK_ADVANCED_SEARCH'],
130                                 'link'   => $this->module . '|advanced_search',
131                                 'key'    => $this->module . '|advanced_search',
132                                 'name'   => 'advanced',
133                                 'displayDiv' => 'display:none');
134         }
135         if($this->showCustom){
136             foreach($this->showCustom as $v){
137                 $this->nbTabs++;
138                 $this->tabs[]=array('title'  => $GLOBALS['app_strings']["LNK_" . strtoupper($v)],
139                     'link'   => $this->module . '|' . $v,
140                     'key'    => $this->module . '|' . $v,
141                     'name'   => str_replace('_search','',$v),
142                     'displayDiv' => 'display:none',);
143             }
144         }
145         }
146
147         function display($header = true){
148         global $theme, $timedate;
149                 $header_txt = '';
150                 $footer_txt = '';
151                 $return_txt = '';
152                 $this->th->ss->assign('module', $this->module);
153                 $this->th->ss->assign('action', $this->action);
154                 $this->th->ss->assign('displayView', $this->displayView);
155                 $this->th->ss->assign('APP', $GLOBALS['app_strings']);
156                 //Show the tabs only if there is more than one
157                 if($this->nbTabs>1){
158                     $this->th->ss->assign('TABS', $this->_displayTabs($this->module . '|' . $this->displayView));
159                 }
160                 $this->th->ss->assign('searchTableColumnCount',
161                     ((isset($this->searchdefs['templateMeta']['maxColumns']) ? $this->searchdefs['templateMeta']['maxColumns'] : 2) * 2 ) - 1);
162                 $this->th->ss->assign('fields', $this->fieldDefs);
163                 $this->th->ss->assign('customFields', $this->customFieldDefs);
164                 $this->th->ss->assign('formData', $this->formData);
165         $time_format = $timedate->get_user_time_format();
166         $this->th->ss->assign('TIME_FORMAT', $time_format);
167         $this->th->ss->assign('USER_DATEFORMAT', $timedate->get_user_date_format());
168
169         $date_format = $timedate->get_cal_date_format();
170         $time_separator = ":";
171         if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
172            $time_separator = $match[1];
173         }
174         // Create Smarty variables for the Calendar picker widget
175         $t23 = strpos($time_format, '23') !== false ? '%H' : '%I';
176         if(!isset($match[2]) || $match[2] == '') {
177           $this->th->ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M");
178         } else {
179           $pm = $match[2] == "pm" ? "%P" : "%p";
180           $this->th->ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M" . $pm);
181         }
182         $this->th->ss->assign('TIME_SEPARATOR', $time_separator);
183
184         //Show and hide the good tab form
185         foreach($this->tabs as $tabkey=>$viewtab){
186             $viewName=str_replace(array($this->module . '|','_search'),'',$viewtab['key']);
187             if(strpos($this->view,$viewName)!==false){
188                 $this->tabs[$tabkey]['displayDiv']='';
189                 //if this is advanced tab, use form with saved search sub form built in
190                 if($viewName=='advanced'){
191                     $this->tpl = 'include/SearchForm/tpls/SearchFormGenericAdvanced.tpl';
192                     if ($this->action =='ListView') {
193                         $this->th->ss->assign('DISPLAY_SEARCH_HELP', true);
194                     }
195                     $this->th->ss->assign('DISPLAY_SAVED_SEARCH', $this->displaySavedSearch);
196                     $this->th->ss->assign('SAVED_SEARCH', $this->displaySavedSearch());
197                     //this determines whether the saved search subform should be rendered open or not
198                     if(isset($_REQUEST['showSSDIV']) && $_REQUEST['showSSDIV']=='yes'){
199                         $this->th->ss->assign('SHOWSSDIV', 'yes');
200                         $this->th->ss->assign('DISPLAYSS', '');
201                     }else{
202                         $this->th->ss->assign('SHOWSSDIV', 'no');
203                         $this->th->ss->assign('DISPLAYSS', 'display:none');
204                     }
205                 }
206             }else{
207                 $this->tabs[$tabkey]['displayDiv']='display:none';
208             }
209
210         }
211
212         $this->th->ss->assign('TAB_ARRAY', $this->tabs);
213
214         $totalWidth = 0;
215         if ( isset($this->searchdefs['templateMeta']['widths'])
216                 && isset($this->searchdefs['templateMeta']['maxColumns'])) {
217             $totalWidth = ( $this->searchdefs['templateMeta']['widths']['label'] +
218                                 $this->searchdefs['templateMeta']['widths']['field'] ) *
219                                 $this->searchdefs['templateMeta']['maxColumns'];
220             // redo the widths in case they are too big
221             if ( $totalWidth > 100 ) {
222                 $resize = 100 / $totalWidth;
223                 $this->searchdefs['templateMeta']['widths']['label'] =
224                     $this->searchdefs['templateMeta']['widths']['label'] * $resize;
225                 $this->searchdefs['templateMeta']['widths']['field'] =
226                     $this->searchdefs['templateMeta']['widths']['field'] * $resize;
227             }
228         }
229         $this->th->ss->assign('templateMeta', $this->searchdefs['templateMeta']);
230         $this->th->ss->assign('HAS_ADVANCED_SEARCH', !empty($this->searchdefs['layout']['advanced_search']));
231         $this->th->ss->assign('displayType', $this->displayType);
232         // return the form of the shown tab only
233         if($this->showSavedSearchesOptions){
234             $this->th->ss->assign('SAVED_SEARCHES_OPTIONS', $this->displaySavedSearchSelect());
235         }
236         if ($this->module == 'Documents'){
237             $this->th->ss->assign('DOCUMENTS_MODULE', true);
238         }
239         $return_txt = $this->th->displayTemplate($this->seed->module_dir, 'SearchForm_'.$this->parsedView, $this->tpl);
240         if($header){
241                         $this->th->ss->assign('return_txt', $return_txt);
242                         $header_txt = $this->th->displayTemplate($this->seed->module_dir, 'SearchFormHeader', 'include/SearchForm/tpls/header.tpl');
243             //pass in info to render the select dropdown below the form
244             $footer_txt = $this->th->displayTemplate($this->seed->module_dir, 'SearchFormFooter', 'include/SearchForm/tpls/footer.tpl');
245                         $return_txt = $header_txt.$footer_txt;
246                 }
247                 return $return_txt;
248         }
249
250   function displaySavedSearch(){
251         $savedSearch = new SavedSearch($this->listViewDefs[$this->module], $this->lv->data['pageData']['ordering']['orderBy'], $this->lv->data['pageData']['ordering']['sortOrder']);
252         return $savedSearch->getForm($this->module, false);
253     }
254
255
256   function displaySavedSearchSelect(){
257         $savedSearch = new SavedSearch($this->listViewDefs[$this->module], $this->lv->data['pageData']['ordering']['orderBy'], $this->lv->data['pageData']['ordering']['sortOrder']);
258         return $savedSearch->getSelect($this->module);
259     }
260
261
262
263         /**
264      * displays the tabs (top of the search form)
265      *
266      * @param string $currentKey key in $this->tabs to show as the current tab
267      *
268      * @return string html
269      */
270     function _displayTabs($currentKey)
271     {
272         if(isset($_REQUEST['saved_search_select']) && $_REQUEST['saved_search_select']!='_none') {
273             $saved_search=loadBean('SavedSearch');
274             $saved_search->retrieveSavedSearch($_REQUEST['saved_search_select']);
275         }
276
277         $str = '<script>';
278         if(!empty($_REQUEST['displayColumns']))
279             $str .= 'SUGAR.savedViews.displayColumns = "' . $_REQUEST['displayColumns'] . '";';
280         elseif(isset($saved_search->contents['displayColumns']) && !empty($saved_search->contents['displayColumns']))
281             $str .= 'SUGAR.savedViews.displayColumns = "' . $saved_search->contents['displayColumns'] . '";';
282         if(!empty($_REQUEST['hideTabs']))
283             $str .= 'SUGAR.savedViews.hideTabs = "' . $_REQUEST['hideTabs'] . '";';
284         elseif(isset($saved_search->contents['hideTabs']) && !empty($saved_search->contents['hideTabs']))
285             $str .= 'SUGAR.savedViews.hideTabs = "' . $saved_search->contents['hideTabs'] . '";';
286         if(!empty($_REQUEST['orderBy']))
287             $str .= 'SUGAR.savedViews.selectedOrderBy = "' . $_REQUEST['orderBy'] . '";';
288         elseif(isset($saved_search->contents['orderBy']) && !empty($saved_search->contents['orderBy']))
289             $str .= 'SUGAR.savedViews.selectedOrderBy = "' . $saved_search->contents['orderBy'] . '";';
290         if(!empty($_REQUEST['sortOrder']))
291             $str .= 'SUGAR.savedViews.selectedSortOrder = "' . $_REQUEST['sortOrder'] . '";';
292         elseif(isset($saved_search->contents['sortOrder']) && !empty($saved_search->contents['sortOrder']))
293             $str .= 'SUGAR.savedViews.selectedSortOrder = "' . $saved_search->contents['sortOrder'] . '";';
294
295         $str .= '</script>';
296
297         return $str;
298     }
299
300         /*
301          * Generate the data
302          */
303         function _build_field_defs(){
304                 $this->formData = array();
305                 $this->fieldDefs = array();
306                 foreach($this->searchdefs['layout'][$this->displayView] as $data){
307                         if(is_array($data)){
308                                 //Fields may be listed but disabled so that when they are enabled, they have the correct custom display data.
309                                 if (isset($data['enabled']) && $data['enabled'] == false)
310                                         continue;
311                                 $data['name'] = $data['name'].'_'.$this->parsedView;
312                                 $this->formData[] = array('field' => $data);
313                                 $this->fieldDefs[$data['name']]= $data;
314                         } else {
315                                 $this->formData[] = array('field' => array('name'=>$data.'_'.$this->parsedView));
316                         }
317                 }
318
319                 if($this->seed){
320                         $this->seed->fill_in_additional_detail_fields();
321                         // hack to make the employee status field for the Users/Employees module display correctly
322                         if($this->seed->object_name == 'Employee' || $this->seed->object_name == 'User'){
323                 $this->seed->field_defs['employee_status']['type'] = 'enum';
324                 $this->seed->field_defs['employee_status']['massupdate'] = true;
325                 $this->seed->field_defs['employee_status']['options'] = 'employee_status_dom';
326                 unset($this->seed->field_defs['employee_status']['function']);
327             }
328
329                 foreach($this->seed->toArray() as $name => $value) {
330                     if(!empty($this->fieldDefs[$name.'_'.$this->parsedView]))
331                         $this->fieldDefs[$name.'_'.$this->parsedView] = array_merge($this->seed->field_defs[$name], $this->fieldDefs[$name.'_'.$this->parsedView]);
332                     else{
333                         $this->fieldDefs[$name.'_'.$this->parsedView] = $this->seed->field_defs[$name];
334                         $this->fieldDefs[$name.'_'.$this->parsedView]['name'] = $this->fieldDefs[$name.'_'.$this->parsedView]['name'].'_'.$this->parsedView;
335                     }
336
337                     if(isset($this->fieldDefs[$name.'_'.$this->parsedView]['type']) && $this->fieldDefs[$name.'_'.$this->parsedView]['type'] == 'relate') {
338                         if(isset($this->fieldDefs[$name.'_'.$this->parsedView]['id_name'])) {
339                            $this->fieldDefs[$name.'_'.$this->parsedView]['id_name'] .= '_'.$this->parsedView;
340                         }
341                     }
342
343                     if(isset($this->fieldDefs[$name.'_'.$this->parsedView]['options']) && isset($GLOBALS['app_list_strings'][$this->fieldDefs[$name.'_'.$this->parsedView]['options']])) {
344                         $this->fieldDefs[$name.'_'.$this->parsedView]['options'] = $GLOBALS['app_list_strings'][$this->fieldDefs[$name.'_'.$this->parsedView]['options']]; // fill in enums
345                     }
346
347                     if(isset($this->fieldDefs[$name.'_'.$this->parsedView]['function'])) {
348
349                         $this->fieldDefs[$name.'_'.$this->parsedView]['type']='multienum';
350
351                                 if(is_array($this->fieldDefs[$name.'_'.$this->parsedView]['function'])) {
352                                    $this->fieldDefs[$name.'_'.$this->parsedView]['function']['preserveFunctionValue']=true;
353                                 }
354
355                                 $function = $this->fieldDefs[$name.'_'.$this->parsedView]['function'];
356
357                                 if(is_array($function) && isset($function['name'])){
358                                         $function_name = $this->fieldDefs[$name.'_'.$this->parsedView]['function']['name'];
359                                 }else{
360                                         $function_name = $this->fieldDefs[$name.'_'.$this->parsedView]['function'];
361                                 }
362
363                                         if(!empty($this->fieldDefs[$name.'_'.$this->parsedView]['function']['returns']) && $this->fieldDefs[$name.'_'.$this->parsedView]['function']['returns'] == 'html'){
364                                                 if(!empty($this->fieldDefs[$name.'_'.$this->parsedView]['function']['include'])){
365                                                                 require_once($this->fieldDefs[$name.'_'.$this->parsedView]['function']['include']);
366                                                 }
367                                                 $value = $function_name($this->seed, $name, $value, $this->view);
368                                                 $this->fieldDefs[$name.'_'.$this->parsedView]['value'] = $value;
369                                         }else{
370                                                 if(!isset($function['params']) || !is_array($function['params'])) {
371                                                         $this->fieldDefs[$name.'_'.$this->parsedView]['options'] = $function_name($this->seed, $name, $value, $this->view);
372                                                 } else {
373                                                         $this->fieldDefs[$name.'_'.$this->parsedView]['options'] = call_user_func_array($function_name, $function['params']);
374                                                 }
375                                         }
376                         }
377                         if(isset($this->fieldDefs[$name]['type']) && $this->fieldDefs[$name.'_'.$this->parsedView]['type'] == 'function' && isset($this->fieldDefs[$name.'_'.$this->parsedView]['function_name'])){
378                                 $value = $this->callFunction($this->fieldDefs[$name.'_'.$this->parsedView]);
379                                 $this->fieldDefs[$name.'_'.$this->parsedView]['value'] = $value;
380                         }
381
382                     $this->fieldDefs[$name]['value'] = $value;
383
384
385                     if((!empty($_REQUEST[$name.'_'.$this->parsedView]) || (isset($_REQUEST[$name.'_'.$this->parsedView]) && $_REQUEST[$name.'_'.$this->parsedView] == '0'))
386                 && empty($this->fieldDefs[$name.'_'.$this->parsedView]['function']['preserveFunctionValue'])) {
387                         $value = $_REQUEST[$name.'_'.$this->parsedView];
388                         $this->fieldDefs[$name.'_'.$this->parsedView]['value'] = $value;
389                     }
390
391                 } //foreach
392
393
394                 }
395
396         }
397
398             /**
399      * Populate the searchFields from an array
400      *
401      * @param array $array array to search through
402      * @param string $switchVar variable to use in switch statement
403      * @param bool $addAllBeanFields true to process at all bean fields
404      */
405     function populateFromArray(&$array, $switchVar = null, $addAllBeanFields = true) {
406
407        if((!empty($array['searchFormTab']) || !empty($switchVar)) && !empty($this->searchFields)) {
408                         $arrayKeys = array_keys($array);
409             $searchFieldsKeys = array_keys($this->searchFields);
410             if(empty($switchVar)) $switchVar = $array['searchFormTab'];
411             //name of  the search tab
412             $SearchName=str_replace('_search', '', $switchVar);
413             if($switchVar=='saved_views'){
414                 foreach($this->searchFields as $name => $params) {
415                     foreach($this->tabs as $tabName){
416                         if(!empty($array[$name . '_' . $tabName['name']])) {
417                              $this->searchFields[$name]['value'] = $array[$name . '_' . $tabName['name']];
418                              if(empty($this->fieldDefs[$name . '_' . $tabName['name']]['value'])) $this->fieldDefs[$name . '_' . $tabName['name']]['value'] = $array[$name . '_' . $tabName['name']];
419                         }
420                     }
421                 }
422                 if($addAllBeanFields) {
423                     foreach($this->seed->field_name_map as $key => $params) {
424                         if(!in_array($key, $searchFieldsKeys)) {
425                             foreach($this->tabs->name as $tabName){
426                                 if(in_array($key . '_' . $tabName['name'], $arrayKeys) ) {
427                                                                         $this->searchFields[$key] = array('query_type' => 'default',
428                                                                       'value'      => $array[$key . '_' . $tabName['name']]);
429                                 }
430                             }
431                         }
432                     }
433                 }
434
435             }else{
436
437                 $fromMergeRecords = isset($array['merge_module']);
438
439                 foreach($this->searchFields as $name => $params) {
440                                         $long_name = $name.'_'.$SearchName;           
441                                         /*nsingh 21648: Add additional check for bool values=0. empty() considers 0 to be empty Only repopulates if value is 0 or 1:( */
442                         if(isset($array[$long_name]) && !$this->isEmptyDropdownField($long_name, $array[$long_name]) && ( $array[$long_name] !== '' || (isset($this->fieldDefs[$long_name]['type']) && $this->fieldDefs[$long_name]['type'] == 'bool'&& ($array[$long_name]=='0' || $array[$long_name]=='1'))))
443                                         {                               
444                         $this->searchFields[$name]['value'] = $array[$long_name];
445                         if(empty($this->fieldDefs[$long_name]['value'])) {
446                                 $this->fieldDefs[$long_name]['value'] = $array[$long_name];
447                         }
448                     }else if(!empty($array[$name]) && !$fromMergeRecords && !$this->isEmptyDropdownField($name, $array[$name])) { //basic               
449                         $this->searchFields[$name]['value'] = $array[$name];
450                         if(empty($this->fieldDefs[$long_name]['value'])) {
451                                 $this->fieldDefs[$long_name]['value'] = $array[$name];
452                         }
453                     }
454                     
455                     if(!empty($params['enable_range_search']) && isset($this->searchFields[$name]['value']))
456                                         {
457                                                 if(preg_match('/^range_(.*?)$/', $long_name, $match) && isset($array[$match[1].'_range_choice']))
458                                                 {
459                                                         $this->searchFields[$name]['operator'] = $array[$match[1].'_range_choice'];
460                                                 }
461                                         }
462
463                                         if(!empty($params['is_date_field']) && isset($this->searchFields[$name]['value']))
464                                         {
465                                                 global $timedate;
466                                                 // FG - bug 45287 - to db conversion is ok, but don't adjust timezone (not now), otherwise you'll jump to the day before (if at GMT-xx)
467                                                 $date_value = $timedate->to_db_date($this->searchFields[$name]['value'], false);
468                                                 $this->searchFields[$name]['value'] = $date_value == '' ? $this->searchFields[$name]['value'] : $date_value;
469                                         }                    
470                 }
471
472                 if((empty($array['massupdate']) || $array['massupdate'] == 'false') && $addAllBeanFields) {
473                     foreach($this->seed->field_name_map as $key => $params) {
474                         if($key != 'assigned_user_name' && $key != 'modified_by_name')
475                         {
476                                 $long_name = $key.'_'.$SearchName;
477                                 
478                                 if(in_array($key.'_'.$SearchName, $arrayKeys) && !in_array($key, $searchFieldsKeys) && !$this->isEmptyDropdownField($long_name, $array[$long_name])) 
479                                 {                                       
480                                         
481                                         $this->searchFields[$key] = array('query_type' => 'default', 'value' => $array[$long_name]);
482                                         
483                                 if (!empty($params['type']) && $params['type'] == 'parent'
484                                     && !empty($params['type_name']) && !empty($this->searchFields[$key]['value']))
485                                 {
486                                             require_once('include/SugarFields/SugarFieldHandler.php');
487                                                                                 $sfh = new SugarFieldHandler();
488                                                                 $sf = $sfh->getSugarField('Parent');
489                                         
490                                         $this->searchFields[$params['type_name']] = array('query_type' => 'default',
491                                                                                           'value'      => $sf->getSearchInput($params['type_name'], $array));
492                                 }
493                                 
494                                 if(empty($this->fieldDefs[$long_name]['value'])) {
495                                     $this->fieldDefs[$long_name]['value'] =  $array[$long_name];
496                                 }
497                             }
498                         }
499                     }
500                 }
501
502             }
503         }
504
505
506        if ( is_array($this->searchFields) ) {
507            foreach ( $this->searchFields as $fieldName => $field ) {
508                if ( !empty($field['value']) && is_string($field['value']) ) {
509                    $this->searchFields[$fieldName]['value'] = trim($field['value']);
510                }
511            }
512        } 
513
514     }
515
516     /**
517      * Populate the searchFields from $_REQUEST
518      *
519      * @param string $switchVar variable to use in switch statement
520      * @param bool $addAllBeanFields true to process at all bean fields
521      */
522     function populateFromRequest($switchVar = null, $addAllBeanFields = true) {
523         $this->populateFromArray($_REQUEST, $switchVar, $addAllBeanFields);
524     }
525
526         function generateSearchWhere($add_custom_fields = false, $module='') {
527         global $timedate;
528
529         $this->searchColumns = array () ;
530         $values = $this->searchFields;
531
532         $where_clauses = array();
533         $like_char = '%';
534         $table_name = $this->seed->object_name;
535         $this->seed->fill_in_additional_detail_fields();
536
537         //rrs check for team_id
538         
539         foreach($this->searchFields as $field=>$parms) {
540                         $customField = false;
541             // Jenny - Bug 7462: We need a type check here to avoid database errors
542             // when searching for numeric fields. This is a temporary fix until we have
543             // a generic search form validation mechanism.
544             $type = (!empty($this->seed->field_name_map[$field]['type']))?$this->seed->field_name_map[$field]['type']:'';
545
546                         if(!empty($parms['enable_range_search']) && empty($type))
547                         {                               
548                                 if(preg_match('/^start_range_(.*?)$/', $field, $match))
549                                 {
550                                         $real_field = $match[1];
551                                         $start_field = 'start_range_' . $real_field;
552                                         $end_field = 'end_range_' . $real_field;
553
554                                         if(isset($this->searchFields[$start_field]['value']) && isset($this->searchFields[$end_field]['value']))
555                                         {                                                               
556                                                 $this->searchFields[$real_field]['value'] = $this->searchFields[$start_field]['value'] . '<>' . $this->searchFields[$end_field]['value'];
557                                                 $this->searchFields[$real_field]['operator'] = 'between';
558                                                 $parms['value'] = $this->searchFields[$real_field]['value'];
559                                                 $parms['operator'] = 'between';
560
561                                                 $field_type = isset($this->seed->field_name_map[$real_field]['type']) ? $this->seed->field_name_map[$real_field]['type'] : '';                                  
562                                                 if($field_type == 'datetimecombo' || $field_type == 'datetime')
563                                                 {
564                                                     $type = $field_type;
565                                                 }
566
567                                                 $field = $real_field;
568                                                 unset($this->searchFields[$end_field]['value']);
569                                         }
570                                 } else if (preg_match('/^range_(.*?)$/', $field, $match) && isset($this->searchFields[$field]['value'])) {
571                                         $real_field = $match[1];
572                                         
573                                         //Special case for datetime and datetimecombo fields.  By setting the type here we allow an actual between search
574                                         if($parms['operator'] == '=')
575                                         {
576                                            $field_type = isset($this->seed->field_name_map[$real_field]['type']) ? $this->seed->field_name_map[$real_field]['type'] : '';                                       
577                                            if($field_type == 'datetimecombo' || $field_type == 'datetime')
578                                            {
579                                                   $type = $field_type;
580                                            }
581                                         }
582                                         
583                                         $this->searchFields[$real_field]['value'] = $this->searchFields[$field]['value'];
584                                         $this->searchFields[$real_field]['operator'] = $this->searchFields[$field]['operator'];                                         
585                                         $params['value'] = $this->searchFields[$field]['value'];
586                                         $params['operator'] = $this->searchFields[$field]['operator'];
587                                         unset($this->searchFields[$field]['value']);
588                                         $field = $real_field;
589                                 } else {
590                             //Skip this range search field, it is the end field THIS IS NEEDED or the end range date will break the query
591                         continue;
592                                 }
593                         }
594             
595                 if(!empty($this->seed->field_name_map[$field]['source'])
596                         && ($this->seed->field_name_map[$field]['source'] == 'custom_fields' ||
597                                 //Non-db custom fields, such as custom relates
598                                 ($this->seed->field_name_map[$field]['source'] == 'non-db'
599                                 && (!empty($this->seed->field_name_map[$field]['custom_module']) ||
600                                          isset($this->seed->field_name_map[$field]['ext2']))))){
601                 $customField = true;
602               }
603
604             if ($type == 'int') {
605                 if (!empty($parms['value'])) {
606                     $tempVal = explode(',', $parms['value']);
607                     $newVal = '';
608                     foreach($tempVal as $key => $val) {
609                         if (!empty($newVal))
610                             $newVal .= ',';
611                         if(!empty($val) && !(is_numeric($val)))
612                             $newVal .= -1;
613                         else
614                             $newVal .= $val;
615                     }
616                     $parms['value'] = $newVal;
617                 }
618             }
619
620             //Navjeet- 6/24/08 checkboxes have been changed to dropdowns, so we can query unchecked checkboxes! Bug: 21648.
621
622             // elseif($type == 'bool' && empty($parms['value']) && preg_match("/current_user_only/", string subject, array subpatterns, int flags, [int offset])) {
623             //     continue;
624             // }
625             //
626             elseif($type == 'html' && $customField) {
627                 continue;
628             }
629
630             
631             if(isset($parms['value']) && $parms['value'] != "") {               
632                 
633                 $operator = 'like';
634                 if(!empty($parms['operator'])) {
635                     $operator = $parms['operator'];
636                 }
637
638                 if(is_array($parms['value'])) {
639                     $field_value = '';
640
641                     // always construct the where clause for multiselects using the 'like' form to handle combinations of multiple $vals and multiple $parms
642                      if(/*$GLOBALS['db']->dbType != 'mysql' &&*/ !empty($this->seed->field_name_map[$field]['isMultiSelect']) && $this->seed->field_name_map[$field]['isMultiSelect']) {
643                         // construct the query for multenums
644                         // use the 'like' query for all mssql and oracle examples as both custom and OOB multienums are implemented with types that cannot be used with an 'in'
645                         $operator = 'custom_enum';
646                         $table_name = $this->seed->table_name ;
647                         if ($customField)
648                             $table_name .= "_cstm" ;
649                         $db_field = $table_name . "." . $field;
650
651                             foreach($parms['value'] as $key => $val) {
652                             if($val != ' ' and $val != '') {
653                                        $qVal = $GLOBALS['db']->quote($val);
654                                        if (!empty($field_value)) {
655                                            $field_value .= ' or ';
656                                        }
657                                        $field_value .= "$db_field like '%^$qVal^%'";
658                                 } else {
659                                         $field_value .= '('.$db_field . ' IS NULL or '.$db_field."='^^' or ".$db_field."='')";
660                                 }
661                             }
662
663                     } else {
664                         $operator = $operator != 'subquery' ? 'in' : $operator;
665                             foreach($parms['value'] as $key => $val) {
666                                 if($val != ' ' and $val != '') {
667                                     if (!empty($field_value)) {
668                                         $field_value .= ',';
669                                     }
670                                     $field_value .= "'" . $GLOBALS['db']->quote($val) . "'";
671                                 }
672                                 // Bug 41209: adding a new operator "isnull" here
673                                 // to handle the case when blank is selected from dropdown.
674                                 // In that case, $val is empty.
675                                 // When $val is empty, we need to use "IS NULL",
676                                 // as "in (null)" won't work
677                                 else if ($operator=='in') {
678                                     $operator = 'isnull';
679                                 }
680                             }
681                     }
682
683                 } else {
684                     $field_value = $GLOBALS['db']->quote($parms['value']);
685                 }
686
687                 //set db_fields array.
688                 if(!isset($parms['db_field'])) {
689                     $parms['db_field'] = array($field);
690                 }
691
692                 //This if-else block handles the shortcut checkbox selections for "My Items" and "Closed Only"
693                 if(!empty($parms['my_items'])) {
694                     if( $parms['value'] == false ) { 
695                                                 continue;
696                                         } else { 
697                                                 //my items is checked.
698                                                 global $current_user;
699                             $field_value = $GLOBALS['db']->quote($current_user->id);
700                                                 $operator = '=' ;
701                                         }
702                 } else if(!empty($parms['closed_values']) && is_array($parms['closed_values'])) {
703                     if( $parms['value'] == false ) { 
704                                                 continue;
705                                         } else { 
706                                                 $field_value = '';
707                                                 foreach($parms['closed_values'] as $closed_value)
708                                                 {
709                                                         $field_value .= ",'" . $GLOBALS['db']->quote($closed_value) . "'";
710                                                 }
711                             $field_value = substr($field_value, 1);
712                                         }                       
713                 }                     
714                 
715                 $where = '';
716                 $itr = 0;
717
718                 if($field_value != '' || $operator=='isnull') {
719
720                     $this->searchColumns [ strtoupper($field) ] = $field ;
721
722                     foreach ($parms['db_field'] as $db_field) {
723                                                 if (strstr($db_field, '.') === false) {
724                                 //Try to get the table for relate fields from link defs
725                                 if ($type == 'relate' && !empty($this->seed->field_name_map[$field]['link'])
726                                         && !empty($this->seed->field_name_map[$field]['rname'])) {
727                                                 $link = $this->seed->field_name_map[$field]['link'];
728                                                 $relname = $link['relationship'];
729                                                 if (($this->seed->load_relationship($link))){
730                                                                                 //Martin fix #27494
731                                                                                 $db_field = $this->seed->field_name_map[$field]['name'];
732                                                 } else {
733                                                         //Best Guess for table name
734                                                         $db_field = strtolower($link['module']) . '.' . $db_field;
735                                                 }
736
737
738                                 }
739                                 else if ($type == 'parent') {
740                                         if (!empty($this->searchFields['parent_type'])) {
741                                                 $parentType = $this->searchFields['parent_type'];
742                                                 $rel_module = $parentType['value'];
743                                                                         global $beanFiles, $beanList;
744                                                 if(!empty($beanFiles[$beanList[$rel_module]])) {
745                                                                         require_once($beanFiles[$beanList[$rel_module]]);
746                                                                             $rel_seed = new $beanList[$rel_module]();
747                                                                             $db_field = 'parent_' . $rel_module . '_' . $rel_seed->table_name . '.name';
748                                                 }
749                                         }
750                                 }
751                                 // Relate fields in custom modules and custom relate fields
752                                 else if ($type == 'relate' && $customField && !empty($this->seed->field_name_map[$field]['module'])) {
753                                         $db_field = !empty($this->seed->field_name_map[$field]['name'])?$this->seed->field_name_map[$field]['name']:'name';
754                                 }
755                            else if(!$customField){
756                                if ( !empty($this->seed->field_name_map[$field]['db_concat_fields']) )
757                                    $db_field = db_concat($this->seed->table_name, $this->seed->field_name_map[$db_field]['db_concat_fields']);
758                                else
759                                    $db_field = $this->seed->table_name .  "." . $db_field;
760                                 }else{
761                                         if ( !empty($this->seed->field_name_map[$field]['db_concat_fields']) )
762                                    $db_field = db_concat($this->seed->table_name .  "_cstm.", $this->seed->field_name_map[$db_field]['db_concat_fields']);
763                                else
764                                    $db_field = $this->seed->table_name .  "_cstm." . $db_field;
765                                 }
766
767                         }
768
769                         if($type == 'date') {
770                            // Collin - Have mysql as first because it's usually the case
771                            // The regular expression check is to circumvent special case YYYY-MM
772                            if($GLOBALS['db']->dbType == 'mysql') {
773                                  if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
774                                     $field_value = $timedate->to_db_date($field_value, false);
775                                     $operator = '=';
776                                  } else {
777                                     $operator = 'db_date';
778                                  }
779                            } else if($GLOBALS['db']->dbType == 'mssql') {
780                                  if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
781                                     $field_value = "Convert(DateTime, '".$timedate->to_db_date($field_value, false)."')";
782                                  }
783                                  $operator = 'db_date';
784                            } else {
785                                      $field_value = $timedate->to_db_date($field_value, false);
786                                      $operator = '=';
787                            }
788                         }
789
790                         if($type == 'datetime' || $type == 'datetimecombo') {
791                                 try {
792                                 // FG - bug45287 - If User asked for a range, takes edges from it.
793                                 $placeholderPos = strpos($field_value, "<>");
794                                 if ($placeholderPos !== FALSE && $placeholderPos > 0)
795                                 {
796                                     $datesLimit = explode("<>", $field_value);
797                                     $dateStart = $timedate->getDayStartEndGMT($datesLimit[0]);
798                                     $dateEnd = $timedate->getDayStartEndGMT($datesLimit[1]);
799                                     $dates = $dateStart;
800                                     $dates['end'] = $dateEnd['end'];
801                                     $dates['enddate'] = $dateEnd['enddate'];
802                                     $dates['endtime'] = $dateEnd['endtime'];
803                                 }
804                                 else
805                                 {
806                                     $dates = $timedate->getDayStartEndGMT($field_value);
807                                 }
808                                 // FG - bug45287 - Note "start" and "end" are the correct interval at GMT timezone
809                                 $field_value = $dates["start"] . "<>" . $dates["end"];
810                                 $operator = 'between';
811                                 } catch(Exception $timeException) {
812                                         //In the event that a date value is given that cannot be correctly processed by getDayStartEndGMT method,
813                                         //just skip searching on this field and continue.  This may occur if user switches locale date formats 
814                                         //in another browser screen, but re-runs a search with the previous format on another screen
815                                         $GLOBALS['log']->error($timeException->getMessage());
816                                         continue;
817                                 }
818                         }
819
820                         // adjust date searches to take account for user timezone
821                         // 'equals' and 'is between' cases are handled above.
822                         if ($type =='' && !empty($parms['enable_range_search']) && $parms['enable_range_search']==true) {
823                             // check if value is a db date or db datetime format
824                             if (preg_match('/^(\d{4}-\d{2}-\d{2})( \d{2}:\d{2}:\d{2})?$/', $field_value)) {
825                                 if ($operator == 'not_equal') {
826                                     $adjDate = $timedate->getDayStartEndGMT($field_value);
827                                     $field_value = $adjDate['start'] . '<>' . $adjDate['end'];
828                                     $operator = 'date_not_equal';
829                                 } elseif ($operator == 'greater_than' || $operator == 'less_than_equals') {
830                                     $adjDate = $timedate->getDayStartEndGMT($field_value);
831                                     $field_value = $adjDate['end'];
832                                 } elseif ($operator == 'less_than' || $operator == 'greater_than_equals') {
833                                     $adjDate = $timedate->getDayStartEndGMT($field_value);
834                                     $field_value = $adjDate['start'];
835                                 }
836                                 // check if value is something like [last_month]|[next_7_days]|[this_year]|etc...
837                             } elseif (preg_match('/^\[[(this|last|next)_][_a-z0-9]*\]$/', $field_value)) {
838                                 switch($operator) {
839                                     case 'last_7_days':
840                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y', time() - (7 * 24 * 60 * 60)));
841                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y'));
842                                         break;
843                                     case 'next_7_days':
844                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y'));
845                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y', time() + (7 * 24 * 60 * 60)));
846                                         break;
847                                     case 'next_month':
848                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, date("m")+1, 01,   date("Y"))));
849                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, -1, date("m")+2, 01,   date("Y"))));
850                                         break;
851                                     case 'last_month':
852                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, date("m")-1, 01,   date("Y"))));
853                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, -1, date("m"), 01,   date("Y"))));
854                                         break;
855                                     case 'this_month':
856                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, date("m"), 01,   date("Y"))));
857                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, -1, date("m")+1, 01,   date("Y"))));
858                                         break;
859                                     case 'last_30_days':
860                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y', time() - (30 * 24 * 60 * 60)));
861                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y'));
862                                         break;
863                                     case 'next_30_days':
864                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y'));
865                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y', time() + (30 * 24 * 60 * 60)));
866                                         break;
867                                     case 'this_year':
868                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, 01, 01,   date("Y"))));
869                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, 12, 31,   date("Y"))));
870                                         break;
871                                     case 'last_year':
872                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, 01, 01,   date("Y")-1)));
873                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, 12, 31,   date("Y")-1)));
874                                         break;
875                                     case 'next_year':
876                                         $startDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, 01, 01,   date("Y")+1)));
877                                         $endDate = $timedate->getDayStartEndGMT(date('m/d/Y', mktime(0, 0, 0, 12, 31,   date("Y")+1)));
878                                         break;
879                                 }
880                                 $field_value = $startDate['start'] . "<>" . $endDate['end'];
881                                 $operator = 'between';
882                             }
883                         }
884                         
885                         if($type == 'decimal' || $type == 'float' || $type == 'currency' || (!empty($parms['enable_range_search']) && empty($parms['is_date_field']))) {
886                                                         require_once('modules/Currencies/Currency.php');
887
888                                                         //we need to handle formatting either a single value or 2 values in case the 'between' search option is set
889                                                         //start by splitting the string if the between operator exists
890                                                         $fieldARR = explode('<>', $field_value);        
891                                                         //set the first pass through boolean
892                                                         $first_between = true;
893         
894                                                         foreach($fieldARR as $fk => $fv){
895                                                                 //reset the field value, it will be rebuild in the foreach loop below
896                                                                 $tmpfield_value = unformat_number($fv);
897                 
898                                                                 if ( $type == 'currency' && stripos($field,'_usdollar')!==FALSE ) {
899                                                                         // It's a US Dollar field, we need to do some conversions from the user's local currency
900                                                                         $currency_id = $GLOBALS['current_user']->getPreference('currency');
901                                                                         if ( empty($currency_id) ) {
902                                                                                 $currency_id = -99;
903                                                                         }
904                                                                         if ( $currency_id != -99 ) {
905                                                                                 $currency = new Currency();
906                                                                                 $currency->retrieve($currency_id);
907                                                                                 $field_value = $currency->convertToDollar($tmpfield_value);
908                                                                         }
909                                                                 }
910
911                                                                 //recreate the field value
912                                                                 if($first_between){
913                                                                         //set the field value with the new formatted temp value
914                                                                         $field_value = $tmpfield_value;
915                                                                 }else{
916                                                                         //this is a between query, so append the between operator and add the second formatted temp value
917                                                                         $field_value .= '<>'.$tmpfield_value;
918                                                                 }
919                                                                 //set the first pass through variable to false
920                                                                 $first_between = false;                                                         
921                                                         }
922                                                                 
923                                                         if(!empty($parms['enable_range_search']) && $parms['operator'] == '=')
924                                                         {
925                                                                 // Databases can't really search for floating point numbers, because they can't be accurately described in binary,
926                                                                 // So we have to fuzz out the math a little bit                                                         
927                                                                 $field_value = ($field_value - 0.01) . "<>" . ($field_value + 0.01);
928                                                                 $operator = 'between';
929                                                         }                                                                               
930                                                 }
931
932
933
934                         $itr++;
935                         if(!empty($where)) {
936                             $where .= " OR ";
937                         }
938                         
939                         switch(strtolower($operator)) {
940                                 case 'subquery':
941                                     $in = 'IN';
942                                     if ( isset($parms['subquery_in_clause']) ) {
943                                         if ( !is_array($parms['subquery_in_clause']) ) {
944                                             $in = $parms['subquery_in_clause'];
945                                         }
946                                         elseif ( isset($parms['subquery_in_clause'][$field_value]) ) {
947                                             $in = $parms['subquery_in_clause'][$field_value];
948                                         }
949                                     }
950                                 $sq = $parms['subquery'];
951                                         if(is_array($sq)){
952                                     $and_or = ' AND ';
953                                     if (isset($sq['OR'])){
954                                         $and_or = ' OR ';
955                                     }
956                                     $first = true;
957                                     foreach($sq as $q){
958                                         if(empty($q) || strlen($q)<2) continue;
959                                         if(!$first){
960                                             $where .= $and_or;
961                                         }
962                                         $where .= " {$db_field} $in ({$q} '{$field_value}%') ";
963                                         $first = false;
964                                     }
965                                 }elseif(!empty($parms['query_type']) && $parms['query_type'] == 'format'){
966                                     $stringFormatParams = array(0 => $field_value, 1 => $GLOBALS['current_user']->id);
967                                     $where .= "{$db_field} $in (".string_format($parms['subquery'], $stringFormatParams).")";
968                                 }else{
969                                     $where .= "{$db_field} $in ({$parms['subquery']} '{$field_value}%')";
970                                 }
971
972                                 break;
973
974                             case 'like':
975                                 if($type == 'bool' && $field_value == 0) {
976                                     // Bug 43452 - FG - Added parenthesis surrounding the OR (without them the WHERE clause would be broken)
977                                     $where .=  "( " . $db_field . " = '0' OR " . $db_field . " IS NULL )";
978                                 }
979                                 else {
980                                         //check to see if this is coming from unified search or not
981                                         $UnifiedSearch = !empty($parms['force_unifiedsearch']);
982                                         if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'UnifiedSearch'){
983                                                 $UnifiedSearch = true;
984                                         }
985
986                                         //check to see if this is a universal search OR the field has db_concat_fields set in vardefs, AND the field name is "last_name"
987                                                                         if(($UnifiedSearch || !empty($this->seed->field_name_map[$field]['db_concat_fields'])) && strpos($db_field, 'last_name') !== false){
988                                                                                 //split the string value, and the db field name
989                                                                                 $string = explode(' ', $field_value);
990                                                                                 $column_name =  explode('.', $db_field);
991                                                                                 //when a search is done with a space, we concatenate and search against the full name.
992                                                                                 if(count($string)>1){
993                                                                                     //add where clause against concatenated fields
994                                                                                         $where .= $GLOBALS['db']->concat($column_name[0],array('first_name','last_name')) . " LIKE '{$field_value}%'";
995                                                                                     $where .= ' OR ' . $GLOBALS['db']->concat($column_name[0],array('last_name','first_name')) . " LIKE '{$field_value}%'";
996                                                                                 }else{
997                                                                                         //no space was found, add normal where clause
998                                                                                         $where .=  $db_field . " like '".$field_value.$like_char."'";
999                                                                                 }
1000
1001                                                                         }
1002                                                                         else {
1003
1004                                                                                 //Check if this is a first_name, last_name search
1005                                                                                 if(isset($this->seed->field_name_map) && isset($this->seed->field_name_map[$db_field]))
1006                                                                                 {
1007                                                                                         $vardefEntry = $this->seed->field_name_map[$db_field];
1008                                                                                         if(!empty($vardefEntry['db_concat_fields']) && in_array('first_name', $vardefEntry['db_concat_fields']) && in_array('last_name', $vardefEntry['db_concat_fields']))
1009                                                                 {
1010                                                                           if(!empty($GLOBALS['app_list_strings']['salutation_dom']) && is_array($GLOBALS['app_list_strings']['salutation_dom']))
1011                                                                           {
1012                                                                                  foreach($GLOBALS['app_list_strings']['salutation_dom'] as $salutation)
1013                                                                                  {
1014                                                                                         if(!empty($salutation) && strpos($field_value, $salutation) == 0)
1015                                                                                         {
1016                                                                                            $field_value = trim(substr($field_value, strlen($salutation)));
1017                                                                                            break;
1018                                                                                         }
1019                                                                                  }
1020                                                                           }
1021                                                                 }
1022                                                                                 }
1023
1024                                                                                 //field is not last name or this is not from global unified search, so do normal where clause
1025                                                                                 $where .=  $db_field . " like '".$field_value.$like_char."'";
1026                                                                         }
1027                                 }
1028                                 break;
1029                             case 'not in':    
1030                                 $where .= $db_field . ' not in ('.$field_value.')';
1031                                 break;
1032                             case 'in':
1033                                 $where .=  $db_field . ' in ('.$field_value.')';
1034                                 break;
1035                             case '=':
1036                                 if($type == 'bool' && $field_value == 0) {
1037                                     $where .=  $db_field . " = '0' OR " . $db_field . " IS NULL";
1038                                 }
1039                                 else {
1040                                     $where .=  $db_field . " = '".$field_value ."'";
1041                                 }
1042                                 break;
1043                             case 'db_date':
1044                                 if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
1045                                   $where .=  $db_field . " = ". $field_value;
1046                                 } else {
1047                                   // Create correct date_format conversion String
1048                                   if($GLOBALS['db']->dbType == 'oci8') {
1049                                         $where .= db_convert($db_field,'date_format',array("'YYYY-MM'")) . " = '" . $field_value . "'";
1050                                   } else {
1051                                         $where .= db_convert($db_field,'date_format',array("'%Y-%m'")) . " = '" . $field_value . "'";
1052                                   }
1053                                 }
1054                                 break;
1055                             // tyoung bug 15971 - need to add these special cases into the $where query
1056                             case 'custom_enum':
1057                                 $where .= $field_value;
1058                                 break;
1059                             case 'between':
1060                                 $field_value = explode('<>', $field_value);
1061                                 $where .= $db_field . " >= '".$field_value[0] . "' AND " .$db_field . " <= '".$field_value[1]."'";
1062                                 break;
1063                             case 'date_not_equal':
1064                                 $field_value = explode('<>', $field_value);
1065                                 $where .= $db_field . " < '".$field_value[0] . "' OR " .$db_field . " > '".$field_value[1]."'";
1066                                 break;
1067                             case 'innerjoin':
1068                                 $this->seed->listview_inner_join[] = $parms['innerjoin'] . " '" . $parms['value'] . "%')";
1069                                 break;
1070                                                         case 'not_equal':
1071                                                                 $where .= $db_field . " != '". $field_value . "'";
1072                                                                 break;
1073                                                         case 'greater_than':
1074                                                                 $where .= $db_field . " > '". $field_value . "'";
1075                                                                 break;
1076                                                         case 'greater_than_equals':
1077                                                                 $where .= $db_field . " >= '". $field_value . "'";
1078                                                                 break;
1079                                                         case 'less_than':
1080                                                                 $where .= $db_field . " < '". $field_value . "'";
1081                                                                 break;
1082                                                         case 'less_than_equals':
1083                                                                 $where .= $db_field . " <= '". $field_value . "'";
1084                                                                 break;
1085                             case 'isnull':
1086                                 // OOTB fields are NULL, custom fields are blank
1087                                 $where .= '('.$db_field . ' IS NULL or ' . $db_field . "='')"; 
1088                                 if ($field_value != '')
1089                                     $where .=  ' OR ' . $db_field . " in (".$field_value.')';
1090                                 break;
1091                         }
1092                     }
1093                 }
1094
1095                 if(!empty($where)) {
1096                     if($itr > 1) {
1097                         array_push($where_clauses, '( '.$where.' )');
1098                     }
1099                     else {
1100                         array_push($where_clauses, $where);
1101                     }
1102                 }
1103             }
1104         }
1105
1106         return $where_clauses;
1107     }
1108     
1109     
1110     /**
1111      * isEmptyDropdownField
1112      * 
1113      * This function checks to see if a blank dropdown field was supplied.  This scenario will occur where
1114      * a dropdown select is in single selection mode
1115      * 
1116      * @param $value Mixed dropdown value
1117      */
1118     private function isEmptyDropdownField($name='', $value=array())
1119     {
1120         $result = is_array($value) && isset($value[0]) && $value[0] == '';
1121         $GLOBALS['log']->debug("Found empty value for {$name} dropdown search key");
1122         return $result;
1123     }    
1124  }
1125 ?>