I just took a look how to do this in Sugar5, so, based on Anna's solution above, you have to do the following steps:
1. add date from and date to to your search from
Edit modules/module/metadata/searchdefs.php
for me the new fields will be:
PHP Code:
array('name' => 'start_date', 'type'=>'date', 'label' => 'LBL_DATA_FROM', 'displayParams'=>array('showFormats'=>true)),
array('name' => 'end_date', 'type'=>'date', 'label' => 'LBL_DATA_TO', 'displayParams'=>array('showFormats'=>true)),
2. tell Sugar how to deal with the new fields
Edit modules/module/metadata/SearchFields.php
PHP Code:
'start_date' => array( 'query_type'=>'default', 'operator' => '>=', 'db_field'=>array('data_custom')),
'end_date' => array( 'query_type'=>'default', 'operator' => '<=', 'db_field'=>array('data_custom')),
for me data_custom is the actual name of the field in the database.
The operators above will not do anything yet, see further step.
3. tell Sugar some more about your date fields
All we need to tell that these are date fields, but for this we have to edit
modules/module/vardefs.php
PHP Code:
'start_date' =>
array (
'name' => 'start_date',
'type' => 'date',
'source'=>'non-db',
),
'end_date' =>
array (
'name' => 'end_date',
'type' => 'date',
'source'=>'non-db',
),
4. patch Sugar to be able to use the new operators
Edit include/SearchForm/SearchForm2.php
around line 522
PHP Code:
if($GLOBALS['db']->dbType == 'mysql') {
if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
$field_value = $timedate->to_db_date($field_value, false);
//$operator = '=';
} else {
//$operator = 'db_date';
}
//adam@dri - stay with operator db_date
$operator = 'db_date';
and around line 573
PHP Code:
case 'db_date':
//adam@dri, have a chance to modify operator here
$realop = ($parms['operator']) ? $parms['operator'] : '=';
if(preg_match('/^\d{4}.\d{1,2}$/', $field_value) == 0) {
$where .= $db_field . " $realop '$field_value'";
} else {
// Create correct date_format conversion String
if($GLOBALS['db']->dbType == 'oci8') {
$where .= db_convert($db_field,'date_format',array("'YYYY-MM'")) . " $realop '" . $field_value . "'";
} else {
$where .= db_convert($db_field,'date_format',array("'%Y-%m'")) . " $realop '" . $field_value . "'";
}
}
break;
Bookmarks