]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SearchForm/SearchForm.php
Release 6.4.0beta3
[Github/sugarcrm.git] / include / SearchForm / SearchForm.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
40
41 class SearchForm {
42    /**
43      * SearchForm Template to use (xtpl)
44      * @var string
45      */
46     var $tpl;
47     /**
48      * SearchField meta data array to use. Populated from moduleDir/metadata/SearchFields
49      * @var array
50      */
51     var $searchFields;
52     /**
53      * Seed bean to use
54      * @var bean
55      */
56     var $bean;
57     /**
58      * Module the search from is for
59      * @var string
60      */
61     var $module;
62     /**
63      * meta data for the tabs to display
64      * @var array
65      */
66     var $tabs;
67     /**
68      * XTPL object
69      * @var object
70      */
71     var $xtpl;
72     /**
73      * Use to determine whether or not to show the saved search options
74      * @var boolean
75      */
76     var $showSavedSearchOptions = true;
77
78     /**
79      * loads SearchFields MetaData, sets member variables
80      *
81      * @param string $module moduleDir
82      * @param bean $seedBean seed bean to use
83      * @param string $tpl template to use, defaults to moduleDir/SearchForm.html
84      *
85      */
86     function SearchForm($module, &$seedBean, $tpl = null) {
87         global $app_strings;
88
89         $this->module = $module;
90         require_once('modules/' . $module . '/metadata/SearchFields.php');
91         if(file_exists('custom/modules/' . $module . '/metadata/SearchFields.php')){
92             require_once('custom/modules/' . $module . '/metadata/SearchFields.php');
93         }
94
95
96         //require_once('modules/' . $module . '/metadata/SearchFields.php');
97         $this->searchFields = $searchFields[$module];
98         if(empty($tpl)) {
99             $this->tpl = 'modules/' . $module . '/SearchForm.html';
100             if(!empty($GLOBALS['layout_edit_mode'])){
101                  $this->tpl = sugar_cached('studio/custom/working/modules/' . $module . '/SearchForm.html');
102             }
103         }
104         else {
105             $this->tpl = $tpl;
106         }
107
108         $this->bean = $seedBean;
109         $this->tabs = array(array('title'  => $app_strings['LNK_BASIC_SEARCH'],
110                                   'link'   => $module . '|basic_search',
111                                   'key'    => $module . '|basic_search'),
112                             array('title'  => $app_strings['LNK_ADVANCED_SEARCH'],
113                                   'link'   => $module . '|advanced_search',
114                                   'key'    => $module . '|advanced_search'));
115
116         if(file_exists('modules/'.$this->module.'/index.php')){
117             $this->tabs[] = array('title'  => $app_strings['LNK_SAVED_VIEWS'],
118                                   'link'   => $module . '|saved_views',
119                                   'key'    => $module . '|saved_views');
120         }
121
122         }
123
124     /**
125      * Populate the searchFields from an array
126      *
127      * @param array $array array to search through
128      * @param string $switchVar variable to use in switch statement
129      * @param bool $addAllBeanFields true to process at all bean fields
130      */
131     function populateFromArray(&$array, $switchVar = null, $addAllBeanFields = true) {
132
133        //CL Bug:33176
134        if(empty($array['searchFormTab']) && empty($switchVar)) {
135           $array['searchFormTab'] = 'advanced_search';
136        }
137
138        if(!empty($array['searchFormTab']) || !empty($switchVar)) {
139             $arrayKeys = array_keys($array);
140             $searchFieldsKeys = array_keys($this->searchFields);
141             if(empty($switchVar)) $switchVar = $array['searchFormTab'];
142             switch($switchVar) {
143                 case 'basic_search':
144                     foreach($this->searchFields as $name => $params) {
145                         if(isset($array[$name . '_basic'])) {
146                             $this->searchFields[$name]['value'] =
147                                 is_string($array[$name . '_basic'])?trim($array[$name . '_basic']):$array[$name . '_basic'];
148                         }
149                     }
150                     if($addAllBeanFields) {
151                         foreach($this->bean->field_name_map as $key => $params) {
152                             if(in_array($key . '_basic' , $arrayKeys) && !in_array($key, $searchFieldsKeys)) {
153
154                                 $this->searchFields[$key] = array('query_type' => 'default',
155                                                                   'value'      => $array[$key . '_basic']);
156                             }
157                         }
158                     }
159                     break;
160                 case 'advanced_search':
161                    foreach($this->searchFields as $name => $params) {
162                         if(isset($array[$name])) {
163                             $this->searchFields[$name]['value'] = is_string($array[$name])?trim($array[$name]):$array[$name];
164                         }
165                     }
166                     if((empty($array['massupdate']) || $array['massupdate'] == 'false') && $addAllBeanFields) {
167                         foreach($this->bean->field_name_map as $key => $params) {
168                             if(in_array($key, $arrayKeys) && !in_array($key, $searchFieldsKeys)) {
169                                 $this->searchFields[$key] = array('query_type' => 'default',
170                                                                   'value'      => $array[$key]);
171                             }
172                         }
173                     }
174                     break;
175                 case 'saved_views':
176                     foreach($this->searchFields as $name => $params) {
177                         if(isset($array[$name . '_basic'])) {  // save basic first
178                             $this->searchFields[$name]['value'] = $array[$name . '_basic'];
179                         }
180                         if(isset($array[$name])) {  // overwrite by advanced if available
181                             $this->searchFields[$name]['value'] = $array[$name];
182                         }
183                     }
184                     if($addAllBeanFields) {
185                         foreach($this->bean->field_name_map as $key => $params) {
186                             if(!in_array($key, $searchFieldsKeys)) {
187                                 if(in_array($key . '_basic', $arrayKeys) ) {
188                                     $this->searchFields[$key] = array('query_type' => 'default',
189                                                                       'value'      => $array[$key . '_basic']);
190                                 }
191                                 if(in_array($key, $arrayKeys)) {
192                                     $this->searchFields[$key] = array('query_type' => 'default',
193                                                                       'value'      => $array[$key]);
194                                 }
195                             }
196                         }
197                     }
198             }
199         }
200     }
201
202     /**
203      * Populate the searchFields from $_REQUEST
204      *
205      * @param string $switchVar variable to use in switch statement
206      * @param bool $addAllBeanFields true to process at all bean fields
207      */
208     function populateFromRequest($switchVar = null, $addAllBeanFields = true) {
209         $this->populateFromArray($_REQUEST, $switchVar, $addAllBeanFields);
210     }
211
212
213     /**
214      * The fuction will returns an array of filter conditions.
215      *
216      */
217     function generateSearchWhere($add_custom_fields = false, $module='') {
218         global $timedate;
219         $values = $this->searchFields;
220
221         $where_clauses = array();
222         //$like_char = '%';
223         $table_name = $this->bean->object_name;
224
225         foreach($this->searchFields as $field=>$parms) {
226                         $customField = false;
227             // Jenny - Bug 7462: We need a type check here to avoid database errors
228             // when searching for numeric fields. This is a temporary fix until we have
229             // a generic search form validation mechanism.
230             $type = (!empty($this->bean->field_name_map[$field]['type']))?$this->bean->field_name_map[$field]['type']:'';
231                 if(!empty($this->bean->field_name_map[$field]['source']) && $this->bean->field_name_map[$field]['source'] == 'custom_fields'){
232                 $customField = true;
233               }
234
235             if ($type == 'int') {
236                 if (!empty($parms['value'])) {
237                     $tempVal = explode(',', $parms['value']);
238                     $newVal = '';
239                     foreach($tempVal as $key => $val) {
240                         if (!empty($newVal))
241                             $newVal .= ',';
242                         if(!empty($val) && !(is_numeric($val)))
243                             $newVal .= -1;
244                         else
245                             $newVal .= $val;
246                     }
247                     $parms['value'] = $newVal;
248                 }
249             }
250             // do not include where clause for custom fields with checkboxes that are unchecked
251             elseif($type == 'bool' && empty($parms['value']) && $customField) {
252                 continue;
253             }
254             elseif($type == 'bool' && !empty($parms['value'])){
255                 if ($parms['value'] == 'on'){
256                         $parms['value'] = 1;
257                 }
258             }
259
260             if(isset($parms['value']) && $parms['value'] != "") {
261                 $operator = 'like';
262                 if(!empty($parms['operator'])) {
263                     $operator = strtolower($parms['operator']);
264                 }
265
266                 if(is_array($parms['value'])) {
267                     $field_value = '';
268
269                     // If it is a custom field of mutliselect we have to do some special processing
270                     if($customField && !empty($this->bean->field_name_map[$field]['isMultiSelect']) && $this->bean->field_name_map[$field]['isMultiSelect']) {
271                             $operator = 'custom_enum';
272                             $db_field = $this->bean->table_name .  "_cstm." . $field;
273                             foreach($parms['value'] as $key => $val) {
274                                 if($val != ' ' and $val != '') {
275                                        $qVal = $GLOBALS['db']->quote($val);
276                                        if (!empty($field_value)) {
277                                            $field_value .= ' or ';
278                                        }
279                                        $field_value .= "$db_field like '$qVal' or $db_field like '%$qVal^%' or $db_field like '%^$qVal%' or $db_field like '%^$qVal^%'";
280                                 }
281                             }
282                     } else {
283                         $operator = $operator != 'subquery' ? 'in' : $operator;
284                             foreach($parms['value'] as $key => $val) {
285                                 if($val != ' ' and $val != '') {
286                                     if (!empty($field_value)) {
287                                         $field_value .= ',';
288                                     }
289                                     $field_value .= "'" . $GLOBALS['db']->quote($val) . "'";
290                                 }
291                             }
292                     }
293                 }
294                 else {
295                     $field_value = $GLOBALS['db']->quote($parms['value']);
296                 }
297
298                 //set db_fields array.
299                 if(!isset($parms['db_field'])) {
300                     $parms['db_field'] = array($field);
301                 }
302
303                 if(isset($parms['my_items']) and $parms['my_items'] == true) {
304                     global $current_user;
305                     $field_value = $GLOBALS['db']->quote($current_user->id);
306                     $operator = '=';
307                 }
308
309                 $where = '';
310                 $itr = 0;
311                 if($field_value != '') {
312                     foreach ($parms['db_field'] as $db_field) {
313
314                         if (strstr($db_field, '.') === false) {
315                                 if(!$customField){
316                                 $db_field = $this->bean->table_name .  "." . $db_field;
317                                 }else{
318                                         $db_field = $this->bean->table_name .  "_cstm." . $db_field;
319                                 }
320
321                         }
322
323                         if($type == 'date') {
324                            // The regular expression check is to circumvent special case YYYY-MM
325                             $operator = '=';
326                             if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
327                                $db_field = $this->bean->db->convert($db_field, "date_format", "%Y-%m");
328                            } else {
329                                $field_value = $timedate->to_db_date($field_value, false);
330                                $db_field = $this->bean->db->convert($db_field, "date_format", "%Y-%m-%d");
331                            }
332                         }
333
334                         if($type == 'datetime'|| $type == 'datetimecombo') {
335                             $dates = $timedate->getDayStartEndGMT($field_value);
336                             $field_value = array($this->bean->db->convert($dates["start"], "datetime"),
337                                 $this->bean->db->convert($dates["end"], "datetime"));
338                             $operator = 'between';
339                         }
340
341                         if($this->bean->db->supports('case_sensitive') && isset($parms['query_type']) && $parms['query_type'] == 'case_insensitive') {
342                               $db_field = 'upper(' . $db_field . ")";
343                               $field_value = strtoupper($field_value);
344                         }
345
346                         $itr++;
347                         if(!empty($where)) {
348                             $where .= " OR ";
349                         }
350                         switch($operator) {
351                                 case 'subquery':
352                                 $in = 'IN';
353                                 if ( isset($parms['subquery_in_clause']) ) {
354                                     if ( !is_array($parms['subquery_in_clause']) ) {
355                                         $in = $parms['subquery_in_clause'];
356                                     }
357                                     elseif ( isset($parms['subquery_in_clause'][$field_value]) ) {
358                                         $in = $parms['subquery_in_clause'][$field_value];
359                                     }
360                                 }
361                                 $sq = $parms['subquery'];
362                                 if(is_array($sq)){
363                                     $and_or = ' AND ';
364                                     if (isset($sq['OR'])){
365                                         $and_or = ' OR ';
366                                     }
367                                     $first = true;
368                                     foreach($sq as $q){
369                                         if(empty($q) || strlen($q)<2) continue;
370                                         if(!$first){
371                                             $where .= $and_or;
372                                         }
373                                         $where .= " {$db_field} $in ({$q} '{$field_value}%') ";
374                                         $first = false;
375                                     }
376                                 }elseif(!empty($parms['query_type']) && $parms['query_type'] == 'format'){
377                                     $stringFormatParams = array(0 => $field_value, 1 => $GLOBALS['current_user']->id);
378                                     $where .= "{$db_field} $in (".string_format($parms['subquery'], $stringFormatParams).")";
379                                 }else{
380                                     $where .= "{$db_field} $in ({$parms['subquery']} '{$field_value}%')";
381                                 }
382
383                                 break;
384                             case 'like':
385                                 $where .=  $db_field . " like ".$this->bean->db->quoted($field_value.'%');
386                                 break;
387                             case 'in':
388                                 $where .=  $db_field . " in (".$field_value.')';
389                                 break;
390                             case '=':
391                                 $where .=  $db_field . " = ".$this->bean->db->quoted($field_value);
392                                 break;
393                             case 'between':
394                                 if(!is_array($field_value)) {
395                                     $field_value = explode('<>', $field_value);
396                                 }
397                                 $where .= "(". $db_field . " >= ".$this->bean->db->quoted($field_value[0]) .
398                                         " AND " .$db_field . " <= ".$this->bean->db->quoted($field_value[1]).")";
399                                 break;
400                         }
401                     }
402                 }
403                 if(!empty($where)) {
404                     if($itr > 1) {
405                         array_push($where_clauses, '( '.$where.' )');
406                     }
407                     else {
408                         array_push($where_clauses, $where);
409                     }
410                 }
411             }
412         }
413
414         return $where_clauses;
415     }
416
417     /**
418      * displays the tabs (top of the search form)
419      *
420      * @param string $currentKey key in $this->tabs to show as the current tab
421      *
422      * @return string html
423      */
424     function displayTabs($currentKey) {
425         $GLOBALS['log']->debug('SearchForm.php->displayTabs(): tabs='.print_r($this->tabs,true));
426
427         $tabPanel = new SugarWidgetTabs($this->tabs, $currentKey, 'SUGAR.searchForm.searchFormSelect');
428
429         if(isset($_REQUEST['saved_search_select']) && $_REQUEST['saved_search_select']!='_none') {
430             $saved_search=loadBean('SavedSearch');
431             $saved_search->retrieveSavedSearch($_REQUEST['saved_search_select']);
432         }
433
434         $str = $tabPanel->display();
435         $str .= '<script>';
436         if(!empty($_REQUEST['displayColumns']))
437             $str .= 'SUGAR.savedViews.displayColumns = "' . $_REQUEST['displayColumns'] . '";';
438         elseif(isset($saved_search->contents['displayColumns']) && !empty($saved_search->contents['displayColumns']))
439             $str .= 'SUGAR.savedViews.displayColumns = "' . $saved_search->contents['displayColumns'] . '";';
440         if(!empty($_REQUEST['hideTabs']))
441             $str .= 'SUGAR.savedViews.hideTabs = "' . $_REQUEST['hideTabs'] . '";';
442         elseif(isset($saved_search->contents['hideTabs']) && !empty($saved_search->contents['hideTabs']))
443             $str .= 'SUGAR.savedViews.hideTabs = "' . $saved_search->contents['hideTabs'] . '";';
444         if(!empty($_REQUEST['orderBy']))
445             $str .= 'SUGAR.savedViews.selectedOrderBy = "' . $_REQUEST['orderBy'] . '";';
446         elseif(isset($saved_search->contents['orderBy']) && !empty($saved_search->contents['orderBy']))
447             $str .= 'SUGAR.savedViews.selectedOrderBy = "' . $saved_search->contents['orderBy'] . '";';
448         if(!empty($_REQUEST['sortOrder']))
449             $str .= 'SUGAR.savedViews.selectedSortOrder = "' . $_REQUEST['sortOrder'] . '";';
450         elseif(isset($saved_search->contents['sortOrder']) && !empty($saved_search->contents['sortOrder']))
451             $str .= 'SUGAR.savedViews.selectedSortOrder = "' . $saved_search->contents['sortOrder'] . '";';
452
453         $str .= '</script>';
454
455         return $str;
456     }
457
458     /**
459      * sets up the search forms, populates the preset values
460      *
461      */
462     function setup() {
463         global $mod_strings, $app_strings, $app_list_strings, $theme, $timedate;
464         $GLOBALS['log']->debug('SearchForm.php->setup()');
465         $this->xtpl = new XTemplate($this->tpl);
466         $this->xtpl->assign("MOD", $mod_strings);
467         $this->xtpl->assign("APP", $app_strings);
468         $this->xtpl->assign("THEME", $theme);
469         $this->xtpl->assign("CALENDAR_DATEFORMAT", $timedate->get_cal_date_format());
470         $this->xtpl->assign("USER_DATEFORMAT", '('. $timedate->get_user_date_format().')');
471
472         foreach($this->searchFields as $name => $params) {
473             if(isset($params['template_var'])) $templateVar = $params['template_var'];
474             else $templateVar = strtoupper($name);
475             if(isset($params['value'])) { // populate w/ preselected values
476                 if(isset($params['options'])) {
477                     $options = $app_list_strings[$params['options']];
478                     if(isset($params['options_add_blank']) && $params['options_add_blank']) array_unshift($options, '');
479                     $this->xtpl->assign($templateVar, get_select_options_with_id($options, $params['value']));
480                 }
481                 else {
482                     if(isset($params['input_type'])) {
483                         switch($params['input_type']) {
484                             case 'checkbox': // checkbox input
485                                 if($params['value'] == 'on' || $params['value'])
486                                     $this->xtpl->assign($templateVar, 'checked');
487                                 break;
488                         }
489                     }
490                     else {// regular text input
491                         $this->xtpl->assign($templateVar, to_html($params['value']));
492                     }
493                 }
494             }
495             else { // populate w/o preselected values
496                 if(isset($params['options'])) {
497                     $options = $app_list_strings[$params['options']];
498                     if(isset($params['options_add_blank']) && $params['options_add_blank']) array_unshift($options, '');
499                     $this->xtpl->assign($templateVar, get_select_options_with_id($options, ''));
500                 }
501             }
502         }
503         if (!empty($_REQUEST['assigned_user_id'])) $this->xtpl->assign("USER_FILTER", get_select_options_with_id(get_user_array(FALSE), $_REQUEST['assigned_user_id']));
504         else $this->xtpl->assign("USER_FILTER", get_select_options_with_id(get_user_array(FALSE), ''));
505
506         // handle my items only
507         if(isset($this->searchFields['current_user_only']) && isset($this->searchFields['current_user_only']['value']))
508             $this->xtpl->assign("CURRENT_USER_ONLY", "checked");
509     }
510
511     /**
512      * displays the search form header
513      *
514      * @param string $view which view is currently being displayed
515      *
516      */
517     function displayHeader($view) {
518         global $current_user;
519         $GLOBALS['log']->debug('SearchForm.php->displayHeader()');
520         $header_text = '';
521         if(is_admin($current_user) && $_REQUEST['module'] != 'DynamicLayout' && !empty($_SESSION['editinplace'])){
522             $header_text = "<a href='index.php?action=index&module=DynamicLayout&from_action=SearchForm&from_module=".$_REQUEST['module'] ."'>".SugarThemeRegistry::current()->getImage("EditLayout","border='0' align='bottom'",null,null,'.gif','Edit Layout')."</a>";
523         }
524
525         echo $header_text . $this->displayTabs($this->module . '|' . $view);
526         echo "<form name='search_form' class='search_form'>" .
527              "<input type='hidden' name='searchFormTab' value='{$view}'/>" .
528              "<input type='hidden' name='module' value='{$_REQUEST['module']}'/>" .
529              "<input type='hidden' name='action' value='{$_REQUEST['action']}'/>" .
530              "<input type='hidden' name='query' value='true'/>";
531     }
532
533     /**
534      * displays the search form body, for example if basic_search is being displayed, then the function call would be
535      * displayWithHeaders('basic_search', $htmlForBasicSearchBody) {
536      *
537      * @param string $view which view is currently being displayed
538      * @param string $basic_search_text body of the basic search tab
539      * @param string $advanced_search_text body of the advanced search tab
540      * @param string $saved_views_text body of the saved views tab
541      *
542      */
543     function displayWithHeaders($view, $basic_search_text = '', $advanced_search_text = '', $saved_views_text = '') {
544         $GLOBALS['log']->debug('SearchForm.php->displayWithHeaders()');
545         $this->displayHeader($view);
546         echo "<div id='{$this->module}basic_searchSearchForm' " . (($view == 'basic_search') ? '' : "style='display: none'") . ">" . $basic_search_text . "</div>";
547         echo "<div id='{$this->module}advanced_searchSearchForm' " . (($view == 'advanced_search') ? '' : "style='display: none'") . ">" . $advanced_search_text . "</div>";
548         echo "<div id='{$this->module}saved_viewsSearchForm' " . (($view == 'saved_views') ? '' : "style='display: none'") . ">" . $saved_views_text . "</div>";
549         echo $this->getButtons();
550         echo '</form>';
551        // echo '<script type="text/javascript">Calendar.setup ({inputField : "search_jscal_field", ifFormat : "'.$timedate->get_cal_date_format().'", showsTime : false, button : "search_jscal_trigger", singleClick : true, step : 1});</script>';
552     }
553
554     /**
555      * displays the basic search form body
556      *
557      * @param bool $header display this with headers
558      * @param bool $return echo or return the html
559      *
560      * @return string html of contents
561      */
562     function displayBasic($header = true, $return = false) {
563         global $current_user;
564
565         $this->bean->custom_fields->populateAllXTPL($this->xtpl, 'search' );
566         $this->xtpl->parse("main");
567         if(!empty($GLOBALS['layout_edit_mode'])){
568          $this->xtpl->parse("advanced");
569         }
570         $text = $this->xtpl->text("main");
571         if(!empty($GLOBALS['layout_edit_mode'])){
572                 $text .= $this->xtpl->text("advanced");
573         }
574         if($header && empty($GLOBALS['layout_edit_mode'])) {
575             $this->displayWithHeaders('basic_search', $text);
576         }
577         else {
578             if($return) return $text;
579             else echo $text;
580         }
581     }
582
583     /**
584      * displays the advanced search form body
585      *
586      * @param bool $header display this with headers
587      * @param bool $return echo or return the html
588      *
589      * @return string html of contents
590      */
591     function displayAdvanced($header = true, $return = false, $listViewDefs='', $lv='') {
592         global $current_user, $current_language;
593         $GLOBALS['log']->debug('SearchForm.php->displayAdvanced()');
594         $this->bean->custom_fields->populateAllXTPL($this->xtpl, 'search' );
595         if(!empty($listViewDefs) && !empty($lv)){
596             $GLOBALS['log']->debug('SearchForm.php->displayAdvanced(): showing saved search');
597             $savedSearch = new SavedSearch($listViewDefs[$this->module], $lv->data['pageData']['ordering']['orderBy'], $lv->data['pageData']['ordering']['sortOrder']);
598             $this->xtpl->assign('SAVED_SEARCH', $savedSearch->getForm($this->module, false));
599             $this->xtpl->assign('MOD_SAVEDSEARCH', return_module_language($current_language, 'SavedSearch'));
600             $this->xtpl->assign('ADVANCED_SEARCH_IMG', SugarThemeRegistry::current()->getImageURL('advanced_search.gif'));
601             //this determines whether the saved search subform should be rendered open or not
602             if(isset($_REQUEST['showSSDIV']) && $_REQUEST['showSSDIV']=='yes'){
603                 $this->xtpl->assign('SHOWSSDIV', 'yes');
604                 $this->xtpl->assign('DISPLAYSS', '');
605             }else{
606                 $this->xtpl->assign('SHOWSSDIV', 'no');
607                 $this->xtpl->assign('DISPLAYSS', 'display:none');
608             }
609         }
610         $this->xtpl->parse("advanced");
611         $text = $this->xtpl->text("advanced");
612
613         if($header) {
614             $this->displayWithHeaders('advanced_search', '', $text);
615         }
616         else {
617             if($return) return $text;
618             else echo $text;
619         }
620     }
621
622     /**
623      * displays the saved views form body
624      *
625      * @param bool $header display this with headers
626      * @param bool $return echo or return the html
627      *
628      * @return string html of contents
629      */
630     function displaySavedViews($listViewDefs, $lv, $header = true, $return = false) {
631         global $current_user;
632
633         $savedSearch = new SavedSearch($listViewDefs[$this->module], $lv->data['pageData']['ordering']['orderBy'], $lv->data['pageData']['ordering']['sortOrder']);
634
635         if($header) {
636             $this->displayWithHeaders('saved_views', $this->displayBasic(false, true), $this->displayAdvanced(false, true), $savedSearch->getForm($this->module));
637             echo '<script>SUGAR.savedViews.handleForm();</script>';
638         }
639         else {
640             echo $savedSearch->getForm($this->module, false);
641         }
642     }
643
644     /**
645      * get the search buttons
646      *
647      * @return string html of contents
648      */
649     function getButtons() {
650         global $app_strings;
651
652         $SAVED_SEARCHES_OPTIONS = '';
653         $savedSearch = new SavedSearch();
654         $SAVED_SEARCHES_OPTIONS = $savedSearch->getSelect($this->module);
655         $str = "<input tabindex='2' title='{$app_strings['LBL_SEARCH_BUTTON_TITLE']}' accessKey='{$app_strings['LBL_SEARCH_BUTTON_KEY']}' onclick='SUGAR.savedViews.setChooser()' class='button' type='submit' name='button' value='{$app_strings['LBL_SEARCH_BUTTON_LABEL']}' id='search_form_submit'/>&nbsp;";
656         $str .= "<input tabindex='2' title='{$app_strings['LBL_CLEAR_BUTTON_TITLE']}' accessKey='{$app_strings['LBL_CLEAR_BUTTON_KEY']}' onclick='SUGAR.searchForm.clear_form(this.form); return false;' class='button' type='button' name='clear' value=' {$app_strings['LBL_CLEAR_BUTTON_LABEL']} ' id='search_form_clear'/>";
657
658         if(!empty($SAVED_SEARCHES_OPTIONS) && $this->showSavedSearchOptions){
659             $str .= "   <span class='white-space'>
660                         &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<b>{$app_strings['LBL_SAVED_SEARCH_SHORTCUT']}</b>&nbsp;
661                         {$SAVED_SEARCHES_OPTIONS}
662                         <span id='go_btn_span' style='display:none'><input tabindex='2' title='go_select' id='go_select'  onclick='SUGAR.searchForm.clear_form(this.form);' class='button' type='button' name='go_select' value=' {$app_strings['LBL_GO_BUTTON_LABEL']} '/></span>
663                     </span>
664                     </form>";
665         }
666         $str .= "
667                 <script>
668                     function toggleInlineSearch(){
669                         if (document.getElementById('inlineSavedSearch').style.display == 'none'){
670                             document.getElementById('showSSDIV').value = 'yes'
671                             document.getElementById('inlineSavedSearch').style.display = '';
672
673                             document.getElementById('up_down_img').src='".SugarThemeRegistry::current()->getImageURL('basic_search.gif')."';
674                             document.getElementById('up_down_img').setAttribute('alt','".$GLOBALS['app_strings']['LBL_ALT_HIDE_OPTIONS']."');
675
676                         }else{
677
678                             document.getElementById('up_down_img').src='".SugarThemeRegistry::current()->getImageURL('advanced_search.gif')."';
679                             document.getElementById('up_down_img').setAttribute('alt','".$GLOBALS['app_strings']['LBL_ALT_SHOW_OPTIONS']."');
680                             document.getElementById('showSSDIV').value = 'no';
681                             document.getElementById('inlineSavedSearch').style.display = 'none';
682                         }
683                     }
684
685
686                 </script>
687             ";
688         return $str;
689     }
690 }
691
692 ?>