DATETIME custom Field type + Search on custom fields

Post about your customizations to share with others.

Moderators: Contributor, Developer

MTW
Posts: 13
Joined: Sep 02, 2005 3:44 am
Contact:

DATETIME custom Field type + Search on custom fields

Post by MTW » Sep 30, 2005 10:54 am

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

Salala

Datetime custom fields!

Post by Salala » Oct 11, 2005 7:22 am

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

MTW
Posts: 13
Joined: Sep 02, 2005 3:44 am
Contact:

DATETIME data type

Post by MTW » Oct 11, 2005 8:30 am

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

slala

Datetime

Post by slala » Oct 12, 2005 2:02 am

Hi!

Thanks I'll try this code

Bye

Slala

Error?

Post by Slala » Oct 17, 2005 2:29 am

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

MTW
Posts: 13
Joined: Sep 02, 2005 3:44 am
Contact:

Post by MTW » Oct 17, 2005 2:40 am

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

Slala

A bug??

Post by Slala » Oct 17, 2005 2:58 am

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

MTW
Posts: 13
Joined: Sep 02, 2005 3:44 am
Contact:

Post by MTW » Oct 17, 2005 3:12 am

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

Slala

Datetime Code Thanks!

Post by Slala » Oct 17, 2005 3:16 am

Hey!

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

Bye

MTW
Posts: 13
Joined: Sep 02, 2005 3:44 am
Contact:

Post by MTW » Oct 17, 2005 3:19 am

:)

You are welcome

I'm glad to help

MTW

jerhinesmith
Posts: 7
Joined: Sep 09, 2005 2:06 pm

Re: DATETIME custom Field type + Search on custom fields

Post by jerhinesmith » Jan 10, 2006 8:55 am

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!

Guest

Custom fields search

Post by Guest » Feb 21, 2006 5:36 am

Hi, here is my code for searchable custom fields.

download filter_api from:

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

Bye,
MTW

Leonardo Colombi
Posts: 5
Joined: Sep 17, 2009 2:04 am
Contact:

Re:

Post by Leonardo Colombi » Sep 17, 2009 8:01 am

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?

tetsunami
Posts: 26
Joined: Apr 21, 2010 9:45 am

Re: DATETIME custom Field type + Search on custom fields

Post by tetsunami » Jun 22, 2010 4:31 pm

but thas to much work i think this must be a upgrade on next mantis version 8O

atrol
Site Admin
Posts: 7394
Joined: Mar 26, 2008 4:37 pm
Location: Germany

Re: DATETIME custom Field type + Search on custom fields

Post by atrol » Jun 22, 2010 4:52 pm

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)
Please use Search before posting and read the Manual

Post Reply