Page 1 of 2

DATETIME custom Field type + Search on custom fields

Posted: 30 Sep 2005, 15:54
by MTW
Hi all

I was in need for DATETIME data type in my project so I have added it to custom data types

I have also added support for simple text search on custom fields

In the meanwhile I have modified the build of an incorrect sql join statement that bring MySQL to exec the infinite query when using more custom fields and filtering on them. (see #6297)

If anyone is interested i will post some code (but where?)

Byez
MTW

Datetime custom fields!

Posted: 11 Oct 2005, 12:22
by Salala
Hi!

Could you tell me what to do to have datetime custom fields?
Explanation or some code? Just I could know where to start!

Bye

DATETIME data type

Posted: 11 Oct 2005, 13:30
by MTW
Sure, here it is:

1) in your main mantis dir (not core) create a new file named custom_constant_inc.php and add following lines to it
<?php
define( 'CUSTOM_FIELD_TYPE_DATETIME', 9 );
?>

2) in config_inc.php add the new type to enum string
$g_custom_field_type_enum_string = '0:string,1:numeric,2:float,3:enum,4:email,5:checkbox,6:list,7:multiselection list,8:date,9:datetime';

if english is not your main language, in your main mantis dir (not core) create a new file named custom_strings_inc.php and add to it following lines with your local translation (in the example italian)
<?php
$s_custom_field_type_enum_string = "0:Stringa,1:Numerico,2:Virgola mobile,3:Valori prestabiliti,4:Email,5:Checkbox,6:Lista,7:Lista a scelta multipla,8:Data,9:Data e ora";
?>

in core\custom_field_api.php
in function print_custom_field_input add to the big switch this code

case CUSTOM_FIELD_TYPE_DATETIME:
print_date_selection_set("custom_field_" . $t_id, config_get('normal_date_format'), $t_custom_field_value, false, true, date( "Y" )-1) ;
break ;

in function string_custom_field_value add to the big switch this code
case CUSTOM_FIELD_TYPE_DATETIME:
if ($t_custom_field_value != null) {
return date( config_get( 'normal_date_format'), $t_custom_field_value) ;
}
break ;

in function print_custom_field_value add to the big switch this code
case CUSTOM_FIELD_TYPE_DATETIME:
if ($p_value != null) {
return date( config_get( 'normal_date_format' ), $p_value) ;
}
break ;

in core\date_api.php add these two functions near similar functions for month and day
function print_hour_option_list( $p_hour = 0 ) {
for ($i=0; $i<=23; $i++) {
if ( $i+1 == $p_hour ) {
PRINT "<option value=\"$i\" selected=\"selected\"> $i </option>";
} else {
PRINT "<option value=\"$i\"> $i </option>";
}
}
}

function print_minute_option_list( $p_minute = 0 ) {
for ($i=0; $i<=59; $i++) {
if ( $i+1 == $p_minute ) {
PRINT "<option value=\"$i\" selected=\"selected\"> $i </option>";
} else {
PRINT "<option value=\"$i\"> $i </option>";
}
}
}

and modify the function print_date_selection_set as follow

old code

if ( $p_date != 0 ) {
$t_date = preg_split('/-/', date( 'Y-m-d', $p_date), -1, PREG_SPLIT_NO_EMPTY) ;
} else {
$t_date = array( 0, 0, 0 );
}

new code
if ( $p_date != 0 ) {
$t_date = preg_split('/-| |:/', date( 'Y-m-d H:i', $p_date), -1, PREG_SPLIT_NO_EMPTY) ;
} else {
$t_date = array( 0, 0, 0, 0, 0 );
}

in same function, inside the foreach loop, after month day and year code, add
if (strcasecmp( $t_char, "H") == 0) {
echo "<select name=\"" . $p_name . "_hour\" $t_disable>" ;
print_hour_option_list( $t_date[3] ) ;
echo "</select>\n" ;
}

if (strcasecmp( $t_char, "i") == 0) {
echo "<select name=\"" . $p_name . "_minute\" $t_disable>" ;
print_minute_option_list( $t_date[4] ) ;
echo "</select>\n" ;
}

if you like you could add this line just after the foreach loop
echo "($p_format)";


in core\gpc_api.php modify function gpc_get_custom_field as follow

old code

case CUSTOM_FIELD_TYPE_DATE:
$t_day = gpc_get_int( $p_var_name . "_day", 0) ;
$t_month = gpc_get_int( $p_var_name . "_month", 0) ;
$t_year = gpc_get_int( $p_var_name . "_year", 0) ;
if (($t_year == 0) || ($t_month == 0) || ($t_day == 0)) {
if ($p_default == null) {
return '' ;
} else {
return $p_default ;
}
} else {
return strtotime($t_year . "-" . $t_month . "-" . $t_day) ;
}
break ;

new code
case CUSTOM_FIELD_TYPE_DATE:
case CUSTOM_FIELD_TYPE_DATETIME:
$t_minute = gpc_get_int( $p_var_name . "_minute", 0) ;
$t_hour = gpc_get_int( $p_var_name . "_hour", 0) ;
$t_day = gpc_get_int( $p_var_name . "_day", 0) ;
$t_month = gpc_get_int( $p_var_name . "_month", 0) ;
$t_year = gpc_get_int( $p_var_name . "_year", 0) ;
if (($t_year == 0) || ($t_month == 0) || ($t_day == 0)) {
if ($p_default == null) {
return '' ;
} else {
return $p_default ;
}
} else {
return strtotime($t_year . "-" . $t_month . "-" . $t_day . " " . $t_hour . ":" . $t_minute) ;
}
break ;

This should works well for you as it does for me, give it a try
(The post strip off duobled spaces, so adjust the indentation)

:)

MTW

Datetime

Posted: 12 Oct 2005, 07:02
by slala
Hi!

Thanks I'll try this code

Bye

Error?

Posted: 17 Oct 2005, 07:29
by Slala
Hi!

You wrote this:

in function print_custom_field_value add to the big switch this code
case CUSTOM_FIELD_TYPE_DATETIME:
if ($p_value != null) {
return date( config_get( 'normal_date_format' ), $p_value) ;
}
break ;

but in function print_custom_field_value there is no switch, so should I add this somewhere else (string_custom_field_value_for_email) or it is caused by diferences in versions becouse I use 1.0.0rc1?? And I don't see difference with or without this code!

Regards

Bye

Posted: 17 Oct 2005, 07:40
by MTW
You are right!

lol

the right function to modify is string_custom_field_value_for_email and not function print_custom_field_value

Sorry Cut&Paste error :)

look also here for some information
http://bugs.mantisbt.org/view.php?id=6325

Bye,
MTW

A bug??

Posted: 17 Oct 2005, 07:58
by Slala
Hi again ;-)!

You also wrote something like this:
I date_api.php:

print_hour(minute)_list_option:
if( $i +1 == $p_hour(p_minute )
PRINT ... ;
else
PRINT ... ;

I think there should be:
if( $i == $p_hour(p_minute) ) ...
Becouse every time I go to Update Bug page the Hour and minute is one hour (minute) less!
Is it wrong or it is for any particular reason $i + 1 ??

Regards

Posted: 17 Oct 2005, 08:12
by MTW
Again right!

in my early code I had the option list starting with empty option " " like date, month and year

This is the reason for that +1, after I removed the first empty option but I forgot to reset the loop index

Thank you for your attention!

MTW

Datetime Code Thanks!

Posted: 17 Oct 2005, 08:16
by Slala
Hey!

Just wanted to say thank you for your code it was very helpful!

Bye

Posted: 17 Oct 2005, 08:19
by MTW
:)

You are welcome

I'm glad to help

MTW

Re: DATETIME custom Field type + Search on custom fields

Posted: 10 Jan 2006, 13:55
by jerhinesmith
MTW wrote:Hi all

I was in need for DATETIME data type in my project so I have added it to custom data types

I have also added support for simple text search on custom fields

In the meanwhile I have modified the build of an incorrect sql join statement that bring MySQL to exec the infinite query when using more custom fields and filtering on them. (see #6297)

If anyone is interested i will post some code (but where?)

Byez
MTW
Could you perhaps post the code for searching on Custom Fields??

Thanks!

Custom fields search

Posted: 21 Feb 2006, 10:36
by Guest
Hi, here is my code for searchable custom fields.

download filter_api from:

http://bugs.mantisbt.org/view.php?id=6325

Bye,
MTW

Re:

Posted: 17 Sep 2009, 13:01
by Leonardo Colombi
Hello,
first of all thanks for your suggestions and code :)
I'm trying it in Mantis 1.1.8 but there are some problems: I've defined a custom field of datetime type, I set it up when reporting issue but Mantis doesn't save the value.
I always see that field empty.
What I'm missing?

Re: DATETIME custom Field type + Search on custom fields

Posted: 22 Jun 2010, 21:31
by tetsunami
but thas to much work i think this must be a upgrade on next mantis version 8O

Re: DATETIME custom Field type + Search on custom fields

Posted: 22 Jun 2010, 21:52
by atrol
tetsunami wrote:but thas to much work i think this must be a upgrade on next mantis version 8O
AFAIK this is available in current version of MantisBT (1.2.1)