]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SearchForm/SearchForm.php
Release 6.2.0
[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 = $GLOBALS['sugar_config']['cache_dir'].'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 = $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                            // Collin - Have mysql as first because it's usually the case
325                            // The regular expression check is to circumvent special case YYYY-MM
326                            if($GLOBALS['db']->dbType == 'mysql') {
327                                  if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
328                                     $field_value = $timedate->to_db_date($field_value, false);
329                                     $operator = '=';
330                                  } else {
331                                     $operator = 'db_date';
332                                  }
333                            } else if($GLOBALS['db']->dbType == 'oci8') {
334                                  if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
335                                     $field_value = $timedate->to_db_date($field_value, false);
336                                     $field_value = "to_date('" . $field_value . "', 'YYYY-MM-DD hh24:mi:ss')";
337                                  }
338                                  $operator = 'db_date';
339                            } else if($GLOBALS['db']->dbType == 'mssql') {
340                                  if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
341                                     $field_value = "Convert(DateTime, '".$timedate->to_db_date($field_value, false)."')";
342                                  }
343                                  $operator = 'db_date';
344                            } else {
345                                      $field_value = $timedate->to_db_date($field_value, false);
346                                      $operation = '=';
347                            }
348                         }
349
350                         if($type == 'datetime'|| $type == 'datetimecombo') {
351                             $dates = $timedate->getDayStartEndGMT($field_value);
352                             $field_value = $dates["start"] . "<>" . $dates["end"];
353                             $operator = 'between';
354                         }
355
356                         if($GLOBALS['db']->dbType == 'oci8' && isset($parms['query_type']) && $parms['query_type'] == 'case_insensitive') {
357                               $db_field = 'upper(' . $db_field . ")";
358                               $field_value = strtoupper($field_value);
359                         }
360
361                         $itr++;
362                         if(!empty($where)) {
363                             $where .= " OR ";
364                         }
365                         switch(strtolower($operator)) {
366                                 case 'subquery':
367                                 $in = 'IN';
368                                 if ( isset($parms['subquery_in_clause']) ) {
369                                     if ( !is_array($parms['subquery_in_clause']) ) {
370                                         $in = $parms['subquery_in_clause'];
371                                     }
372                                     elseif ( isset($parms['subquery_in_clause'][$field_value]) ) {
373                                         $in = $parms['subquery_in_clause'][$field_value];
374                                     }
375                                 }
376                                 $sq = $parms['subquery'];
377                                 if(is_array($sq)){
378                                     $and_or = ' AND ';
379                                     if (isset($sq['OR'])){
380                                         $and_or = ' OR ';
381                                     }
382                                     $first = true;
383                                     foreach($sq as $q){
384                                         if(empty($q) || strlen($q)<2) continue;
385                                         if(!$first){
386                                             $where .= $and_or;
387                                         }
388                                         $where .= " {$db_field} $in ({$q} '{$field_value}%') ";
389                                         $first = false;
390                                     }
391                                 }elseif(!empty($parms['query_type']) && $parms['query_type'] == 'format'){
392                                     $stringFormatParams = array(0 => $field_value, 1 => $GLOBALS['current_user']->id);
393                                     $where .= "{$db_field} $in (".string_format($parms['subquery'], $stringFormatParams).")";
394                                 }else{
395                                     $where .= "{$db_field} $in ({$parms['subquery']} '{$field_value}%')";
396                                 }
397
398                                 break;
399                             case 'like':
400                                 $where .=  $db_field . " like '".$field_value.$like_char."'";
401                                 break;
402                             case 'in':
403                                 $where .=  $db_field . " in (".$field_value.')';
404                                 break;
405                             case '=':
406                                 $where .=  $db_field . " = '".$field_value ."'";
407                                 break;
408                             case 'db_date':
409                                 if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
410                                   $where .=  $db_field . " = ". $field_value;
411                                 } else {
412                                   // Create correct date_format conversion String
413                                   if($GLOBALS['db']->dbType == 'oci8') {
414                                         $where .= db_convert($db_field,'date_format',array("'YYYY-MM'")) . " = '" . $field_value . "'";
415                                   } else {
416                                         $where .= db_convert($db_field,'date_format',array("'%Y-%m'")) . " = '" . $field_value . "'";
417                                   }
418                                 }
419                                 break;
420                             case 'between':
421                                 $field_value = explode('<>', $field_value);
422                                 $where .= $db_field . " >= '".$field_value[0] . "' AND " .$db_field . " <= '".$field_value[1]."'";
423                                 break;
424                         }
425                     }
426                 }
427                 if(!empty($where)) {
428                     if($itr > 1) {
429                         array_push($where_clauses, '( '.$where.' )');
430                     }
431                     else {
432                         array_push($where_clauses, $where);
433                     }
434                 }
435             }
436         }
437
438         return $where_clauses;
439     }
440
441     /**
442      * displays the tabs (top of the search form)
443      *
444      * @param string $currentKey key in $this->tabs to show as the current tab
445      *
446      * @return string html
447      */
448     function displayTabs($currentKey) {
449         $GLOBALS['log']->debug('SearchForm.php->displayTabs(): tabs='.print_r($this->tabs,true));
450
451         $tabPanel = new SugarWidgetTabs($this->tabs, $currentKey, 'SUGAR.searchForm.searchFormSelect');
452
453         if(isset($_REQUEST['saved_search_select']) && $_REQUEST['saved_search_select']!='_none') {
454             $saved_search=loadBean('SavedSearch');
455             $saved_search->retrieveSavedSearch($_REQUEST['saved_search_select']);
456         }
457
458         $str = $tabPanel->display();
459         $str .= '<script>';
460         if(!empty($_REQUEST['displayColumns']))
461             $str .= 'SUGAR.savedViews.displayColumns = "' . $_REQUEST['displayColumns'] . '";';
462         elseif(isset($saved_search->contents['displayColumns']) && !empty($saved_search->contents['displayColumns']))
463             $str .= 'SUGAR.savedViews.displayColumns = "' . $saved_search->contents['displayColumns'] . '";';
464         if(!empty($_REQUEST['hideTabs']))
465             $str .= 'SUGAR.savedViews.hideTabs = "' . $_REQUEST['hideTabs'] . '";';
466         elseif(isset($saved_search->contents['hideTabs']) && !empty($saved_search->contents['hideTabs']))
467             $str .= 'SUGAR.savedViews.hideTabs = "' . $saved_search->contents['hideTabs'] . '";';
468         if(!empty($_REQUEST['orderBy']))
469             $str .= 'SUGAR.savedViews.selectedOrderBy = "' . $_REQUEST['orderBy'] . '";';
470         elseif(isset($saved_search->contents['orderBy']) && !empty($saved_search->contents['orderBy']))
471             $str .= 'SUGAR.savedViews.selectedOrderBy = "' . $saved_search->contents['orderBy'] . '";';
472         if(!empty($_REQUEST['sortOrder']))
473             $str .= 'SUGAR.savedViews.selectedSortOrder = "' . $_REQUEST['sortOrder'] . '";';
474         elseif(isset($saved_search->contents['sortOrder']) && !empty($saved_search->contents['sortOrder']))
475             $str .= 'SUGAR.savedViews.selectedSortOrder = "' . $saved_search->contents['sortOrder'] . '";';
476
477         $str .= '</script>';
478
479         return $str;
480     }
481
482     /**
483      * sets up the search forms, populates the preset values
484      *
485      */
486     function setup() {
487         global $mod_strings, $app_strings, $app_list_strings, $theme, $timedate;
488         $GLOBALS['log']->debug('SearchForm.php->setup()');
489         $this->xtpl = new XTemplate($this->tpl);
490         $this->xtpl->assign("MOD", $mod_strings);
491         $this->xtpl->assign("APP", $app_strings);
492         $this->xtpl->assign("THEME", $theme);
493         $this->xtpl->assign("CALENDAR_DATEFORMAT", $timedate->get_cal_date_format());
494         $this->xtpl->assign("USER_DATEFORMAT", '('. $timedate->get_user_date_format().')');
495
496         foreach($this->searchFields as $name => $params) {
497             if(isset($params['template_var'])) $templateVar = $params['template_var'];
498             else $templateVar = strtoupper($name);
499             if(isset($params['value'])) { // populate w/ preselected values
500                 if(isset($params['options'])) {
501                     $options = $app_list_strings[$params['options']];
502                     if(isset($params['options_add_blank']) && $params['options_add_blank']) array_unshift($options, '');
503                     $this->xtpl->assign($templateVar, get_select_options_with_id($options, $params['value']));
504                 }
505                 else {
506                     if(isset($params['input_type'])) {
507                         switch($params['input_type']) {
508                             case 'checkbox': // checkbox input
509                                 if($params['value'] == 'on' || $params['value'])
510                                     $this->xtpl->assign($templateVar, 'checked');
511                                 break;
512                         }
513                     }
514                     else {// regular text input
515                         $this->xtpl->assign($templateVar, to_html($params['value']));
516                     }
517                 }
518             }
519             else { // populate w/o preselected values
520                 if(isset($params['options'])) {
521                     $options = $app_list_strings[$params['options']];
522                     if(isset($params['options_add_blank']) && $params['options_add_blank']) array_unshift($options, '');
523                     $this->xtpl->assign($templateVar, get_select_options_with_id($options, ''));
524                 }
525             }
526         }
527         if (!empty($_REQUEST['assigned_user_id'])) $this->xtpl->assign("USER_FILTER", get_select_options_with_id(get_user_array(FALSE), $_REQUEST['assigned_user_id']));
528         else $this->xtpl->assign("USER_FILTER", get_select_options_with_id(get_user_array(FALSE), ''));
529
530         // handle my items only
531         if(isset($this->searchFields['current_user_only']) && isset($this->searchFields['current_user_only']['value']))
532             $this->xtpl->assign("CURRENT_USER_ONLY", "checked");
533     }
534
535     /**
536      * displays the search form header
537      *
538      * @param string $view which view is currently being displayed
539      *
540      */
541     function displayHeader($view) {
542         global $current_user;
543         $GLOBALS['log']->debug('SearchForm.php->displayHeader()');
544         $header_text = '';
545         if(is_admin($current_user) && $_REQUEST['module'] != 'DynamicLayout' && !empty($_SESSION['editinplace'])){
546             $header_text = "<a href='index.php?action=index&module=DynamicLayout&from_action=SearchForm&from_module=".$_REQUEST['module'] ."'>".SugarThemeRegistry::current()->getImage("EditLayout","border='0' alt='Edit Layout' align='bottom'")."</a>";
547         }
548
549         echo $header_text . $this->displayTabs($this->module . '|' . $view);
550         echo "<form name='search_form' class='search_form'>" .
551              "<input type='hidden' name='searchFormTab' value='{$view}'/>" .
552              "<input type='hidden' name='module' value='{$_REQUEST['module']}'/>" .
553              "<input type='hidden' name='action' value='{$_REQUEST['action']}'/>" .
554              "<input type='hidden' name='query' value='true'/>";
555     }
556
557     /**
558      * displays the search form body, for example if basic_search is being displayed, then the function call would be
559      * displayWithHeaders('basic_search', $htmlForBasicSearchBody) {
560      *
561      * @param string $view which view is currently being displayed
562      * @param string $basic_search_text body of the basic search tab
563      * @param string $advanced_search_text body of the advanced search tab
564      * @param string $saved_views_text body of the saved views tab
565      *
566      */
567     function displayWithHeaders($view, $basic_search_text = '', $advanced_search_text = '', $saved_views_text = '') {
568         $GLOBALS['log']->debug('SearchForm.php->displayWithHeaders()');
569         $this->displayHeader($view);
570         echo "<div id='{$this->module}basic_searchSearchForm' " . (($view == 'basic_search') ? '' : "style='display: none'") . ">" . $basic_search_text . "</div>";
571         echo "<div id='{$this->module}advanced_searchSearchForm' " . (($view == 'advanced_search') ? '' : "style='display: none'") . ">" . $advanced_search_text . "</div>";
572         echo "<div id='{$this->module}saved_viewsSearchForm' " . (($view == 'saved_views') ? '' : "style='display: none'") . ">" . $saved_views_text . "</div>";
573         echo $this->getButtons();
574         echo '</form>';
575        // 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>';
576     }
577
578     /**
579      * displays the basic search form body
580      *
581      * @param bool $header display this with headers
582      * @param bool $return echo or return the html
583      *
584      * @return string html of contents
585      */
586     function displayBasic($header = true, $return = false) {
587         global $current_user;
588
589         $this->bean->custom_fields->populateAllXTPL($this->xtpl, 'search' );
590         $this->xtpl->parse("main");
591         if(!empty($GLOBALS['layout_edit_mode'])){
592          $this->xtpl->parse("advanced");
593         }
594         $text = $this->xtpl->text("main");
595         if(!empty($GLOBALS['layout_edit_mode'])){
596                 $text .= $this->xtpl->text("advanced");
597         }
598         if($header && empty($GLOBALS['layout_edit_mode'])) {
599             $this->displayWithHeaders('basic_search', $text);
600         }
601         else {
602             if($return) return $text;
603             else echo $text;
604         }
605     }
606
607     /**
608      * displays the advanced search form body
609      *
610      * @param bool $header display this with headers
611      * @param bool $return echo or return the html
612      *
613      * @return string html of contents
614      */
615     function displayAdvanced($header = true, $return = false, $listViewDefs='', $lv='') {
616         global $current_user, $current_language;
617         $GLOBALS['log']->debug('SearchForm.php->displayAdvanced()');
618         $this->bean->custom_fields->populateAllXTPL($this->xtpl, 'search' );
619         if(!empty($listViewDefs) && !empty($lv)){
620             $GLOBALS['log']->debug('SearchForm.php->displayAdvanced(): showing saved search');
621             $savedSearch = new SavedSearch($listViewDefs[$this->module], $lv->data['pageData']['ordering']['orderBy'], $lv->data['pageData']['ordering']['sortOrder']);
622             $this->xtpl->assign('SAVED_SEARCH', $savedSearch->getForm($this->module, false));
623             $this->xtpl->assign('MOD_SAVEDSEARCH', return_module_language($current_language, 'SavedSearch'));
624             $this->xtpl->assign('ADVANCED_SEARCH_IMG', SugarThemeRegistry::current()->getImageURL('advanced_search.gif'));
625             //this determines whether the saved search subform should be rendered open or not
626             if(isset($_REQUEST['showSSDIV']) && $_REQUEST['showSSDIV']=='yes'){
627                 $this->xtpl->assign('SHOWSSDIV', 'yes');
628                 $this->xtpl->assign('DISPLAYSS', '');
629             }else{
630                 $this->xtpl->assign('SHOWSSDIV', 'no');
631                 $this->xtpl->assign('DISPLAYSS', 'display:none');
632             }
633         }
634         $this->xtpl->parse("advanced");
635         $text = $this->xtpl->text("advanced");
636
637         if($header) {
638             $this->displayWithHeaders('advanced_search', '', $text);
639         }
640         else {
641             if($return) return $text;
642             else echo $text;
643         }
644     }
645
646     /**
647      * displays the saved views form body
648      *
649      * @param bool $header display this with headers
650      * @param bool $return echo or return the html
651      *
652      * @return string html of contents
653      */
654     function displaySavedViews($listViewDefs, $lv, $header = true, $return = false) {
655         global $current_user;
656
657         $savedSearch = new SavedSearch($listViewDefs[$this->module], $lv->data['pageData']['ordering']['orderBy'], $lv->data['pageData']['ordering']['sortOrder']);
658
659         if($header) {
660             $this->displayWithHeaders('saved_views', $this->displayBasic(false, true), $this->displayAdvanced(false, true), $savedSearch->getForm($this->module));
661             echo '<script>SUGAR.savedViews.handleForm();</script>';
662         }
663         else {
664             echo $savedSearch->getForm($this->module, false);
665         }
666     }
667
668     /**
669      * get the search buttons
670      *
671      * @return string html of contents
672      */
673     function getButtons() {
674         global $app_strings;
675
676         $SAVED_SEARCHES_OPTIONS = '';
677         $savedSearch = new SavedSearch();
678         $SAVED_SEARCHES_OPTIONS = $savedSearch->getSelect($this->module);
679         $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']}'/>&nbsp;";
680         $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']} '/>";
681
682         if(!empty($SAVED_SEARCHES_OPTIONS) && $this->showSavedSearchOptions){
683             $str .= "   <span class='white-space'>
684                         &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<b>{$app_strings['LBL_SAVED_SEARCH_SHORTCUT']}</b>&nbsp;
685                         {$SAVED_SEARCHES_OPTIONS}
686                         <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>
687                     </span>
688                     </form>";
689         }
690         $str .= "
691                 <script>
692                     function toggleInlineSearch(){
693                         if (document.getElementById('inlineSavedSearch').style.display == 'none'){
694                             document.getElementById('showSSDIV').value = 'yes'
695                             document.getElementById('inlineSavedSearch').style.display = '';
696
697                             document.getElementById('up_down_img').src='".SugarThemeRegistry::current()->getImageURL('basic_search.gif')."';
698
699                         }else{
700
701                             document.getElementById('up_down_img').src='".SugarThemeRegistry::current()->getImageURL('advanced_search.gif')."';
702
703                             document.getElementById('showSSDIV').value = 'no';
704                             document.getElementById('inlineSavedSearch').style.display = 'none';
705                         }
706                     }
707
708
709                 </script>
710             ";
711         return $str;
712     }
713 }
714
715 ?>