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