Skip to content

Commit

Permalink
Merge branch 'date-custom-fields' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dregad committed Feb 21, 2021
2 parents ee6ff2e + 04772f6 commit e5cef5a
Show file tree
Hide file tree
Showing 10 changed files with 1,041 additions and 808 deletions.
77 changes: 77 additions & 0 deletions admin/check/check_customfields_inc.php
@@ -0,0 +1,77 @@
<?php
# MantisBT - A PHP based bugtracking system

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.

/**
* Custom Fields Checks
* @package MantisBT
* @copyright Copyright (C) 2021 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link https://mantisbt.org
*
* @uses check_api.php
* @uses config_api.php
* @uses constant_inc.php
*/

if( !defined( 'CHECK_CUSTOMFIELDS_INC_ALLOW' ) ) {
return;
}

# MantisBT Check API
require_once( 'check_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );

check_print_section_header_row( 'Custom Fields' );

# Check for deprecated usage of {} in Date CF default values
$t_date_cf_with_bracket = array();
foreach( custom_field_get_ids() as $t_id ) {
/**
* @var int $v_id
* @var string $v_name
* @var int $v_type
* @var string|int $v_default_value
*/
extract( custom_field_get_definition( $t_id ), EXTR_PREFIX_ALL, 'v');

if( $v_type != CUSTOM_FIELD_TYPE_DATE ) {
continue;
}
if( preg_match( '/^{(.*)}$/', $v_default_value, $t_matches ) ) {
$t_date_cf_with_bracket[$v_name] = array( $v_id, $t_matches[1] );
}
}
if( $t_date_cf_with_bracket ) {
$t_manage_cf_link = '<a href="'
. helper_mantis_url( 'manage_custom_field_edit_page.php' )
. '?field_id=%d">Edit the Custom Field</a>';
ksort( $t_date_cf_with_bracket );
foreach( $t_date_cf_with_bracket as $t_name => list( $t_id, $t_new_value ) ) {
check_print_test_warn_row(
"Date Custom Field '$t_name' specifies its Default Value with deprecated curly brackets format.",
false,
array( false => "Use the same format, but without the '{}', i.e. '$t_new_value'. "
. sprintf( $t_manage_cf_link, $t_id )
)
);
}
} else {
check_print_test_warn_row(
'Deprecated usage of curly brackets in Date Custom Fields default value',
true,
''
);
}
7 changes: 6 additions & 1 deletion admin/check/index.php
Expand Up @@ -184,6 +184,11 @@ function mode_url( $p_all, $p_errors ) {
include( 'check_display_inc.php' );
}

if( !$g_failed_test ) {
define( 'CHECK_CUSTOMFIELDS_INC_ALLOW', true );
include( 'check_customfields_inc.php' );
}

if( !$g_failed_test ) {
define( 'CHECK_PLUGINS_INC_ALLOW', true );
include( 'check_plugins_inc.php' );
Expand All @@ -199,7 +204,7 @@ function mode_url( $p_all, $p_errors ) {

<?php if( $g_failed_test ) { ?>
<div class="alert alert-danger" id="check-notice-failed">
Some tests failed. Please review and correct these failed tests before using MantisBT.
Some tests failed. Please review, correct them and run the checks again before using MantisBT.
</div>
<?php } else if( $g_passed_test_with_warnings ) { ?>
<div class="alert alert-warning" id="check-notice-warnings">
Expand Down
76 changes: 48 additions & 28 deletions core/cfdefs/cfdef_standard.php
Expand Up @@ -18,6 +18,8 @@
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
* @package MantisBT
*
* @noinspection PhpUnused
*/

$g_custom_field_type_definition[CUSTOM_FIELD_TYPE_STRING] = array (
Expand Down Expand Up @@ -227,8 +229,7 @@ function cfdef_print_float( $p_value ) {
* @return int The numeric value.
*/
function cfdef_prepare_numeric( $p_value ) {
$t_value = (int)$p_value;
return $t_value;
return (int)$p_value;
}

/**
Expand All @@ -237,8 +238,7 @@ function cfdef_prepare_numeric( $p_value ) {
* @return float The float value.
*/
function cfdef_prepare_float( $p_value ) {
$t_value = (float)$p_value;
return $t_value;
return (float)$p_value;
}

/**
Expand Down Expand Up @@ -290,33 +290,38 @@ function cfdef_prepare_date_value_for_email( $p_value ) {
}

/**
* Translates the default date value entered by the creator of the custom
* field into a date value. For example, translate '=tomorrow' to tomorrow's
* date.
* Translates the default date value into a timestamp.
*
* Default date can be any supported Date format
* {@see https://www.php.net/manual/en/datetime.formats.php}.
* The legacy style with format wrapped in curly bracket (e.g. {tomorrow}) is
* still supported for backwards compatibility.
*
* @param string $p_value The default date string.
* @return string The calculated default date value if $p_value starts with '=', otherwise, returns $p_value.
*
* @return int Calculated default date's timestamp, or 0 if format is invalid.
*/
function cfdef_prepare_date_default( $p_value ) {
if( is_blank( $p_value ) ) {
return '';
return 0;
}

$t_value = trim( $p_value );
$t_value_length = mb_strlen( $t_value );

# We are expanding {tomorrow}, {yesterday}, {+3 days}, {-7 days}, {next week}
# See strtotime() for more details about supported formats.
if( $t_value_length >= 3 && $t_value[0] == '{' && $t_value[$t_value_length - 1] == '}' ) {
$t_value = mb_substr( $t_value, 1, $t_value_length - 2 );
$t_value = @strtotime( $t_value );
# Allow legacy "{xxx}" format for dynamic dates
if( preg_match( '/^{(.*)}$/', $t_value, $t_matches ) ) {
$t_value = $t_matches[1];
}

# Different versions of PHP return different values in case of error.
if( $t_value == -1 || $t_value === false ) {
return '';
}
# Check default date format and calculate actual date
try {
$t_date = new DateTimeImmutable( $t_value );
}
catch( Exception $e ) {
return 0;
}

return $t_value;
return $t_date->getTimestamp();
}

/**
Expand Down Expand Up @@ -344,10 +349,8 @@ function cfdef_prepare_email_value( $p_value ) {
* @return string
*/
function cfdef_prepare_date_value( $p_value ) {
if( $p_value != null ) {
if( is_numeric( $p_value ) ) {
return date( config_get( 'short_date_format' ), $p_value );
}
if( $p_value && is_numeric( $p_value ) ) {
return date( config_get( 'short_date_format' ), $p_value );
}

return '';
Expand Down Expand Up @@ -393,8 +396,10 @@ function cfdef_input_list( array $p_field_def, $p_custom_field_value, $p_require
* print_custom_field_input
* @param array $p_field_def Custom field definition.
* @param mixed $p_custom_field_value Custom field value.
* @param string $p_required The "required" attribute to add to the field
* @param string $p_required (Unused) The "required" attribute to add to the field
* @return void
*
* @noinspection PhpUnusedParameterInspection
*/
function cfdef_input_checkbox( array $p_field_def, $p_custom_field_value, $p_required = '' ) {
$t_values = explode( '|', custom_field_prepare_possible_values( $p_field_def['possible_values'] ) );
Expand All @@ -403,7 +408,10 @@ function cfdef_input_checkbox( array $p_field_def, $p_custom_field_value, $p_req
$t_input_id = 'custom_field_' . $p_field_def['id'] . '_value_' . $i;
$t_input_name = 'custom_field_' . $p_field_def['id'] . '[]';
echo '<label for="' . $t_input_id . '">' . "\n";
echo '<input class="ace" id="' . $t_input_id . '" ' . helper_get_tab_index() . ' type="checkbox" name="' . $t_input_name . '" value="' . string_attribute( $t_values[$i] ) . '"';
echo '<input class="ace" id="' . $t_input_id . '" '
. helper_get_tab_index()
. ' type="checkbox" name="' . $t_input_name
. '" value="' . string_attribute( $t_values[$i] ) . '"';
check_checked( $t_checked_values, $t_values[$i] );
echo " />\n";
echo '<span class="lbl">&#160;' . string_display_line( $t_values[$i] ) . '</label>' . "\n";
Expand Down Expand Up @@ -491,13 +499,25 @@ function cfdef_input_textarea( array $p_field_def, $p_custom_field_value, $p_req
/**
* Prints the controls for the date selector.
*
* @param string $p_field_def The custom field definition.
* @param array $p_field_def The custom field definition.
* @param string $p_custom_field_value The custom field value to print.
* @param string $p_required The "required" attribute to add to the field
* @return void
*/
function cfdef_input_date( $p_field_def, $p_custom_field_value, $p_required = '' ) {
print_date_selection_set( 'custom_field_' . $p_field_def['id'], config_get( 'short_date_format' ), $p_custom_field_value, false, true, 0, 0, 'input-sm', $p_required );
if( !is_numeric( $p_custom_field_value ) ) {
$p_custom_field_value = 0;
}
print_date_selection_set( 'custom_field_' . $p_field_def['id'],
config_get( 'short_date_format' ),
$p_custom_field_value,
false,
true,
0,
0,
'input-sm',
$p_required
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions core/constant_inc.php
Expand Up @@ -350,6 +350,7 @@
define( 'ERROR_CUSTOM_FIELD_INVALID_DEFINITION', 1304 );
define( 'ERROR_CUSTOM_FIELD_NOT_LINKED_TO_PROJECT', 1305 );
define( 'ERROR_CUSTOM_FIELD_INVALID_PROPERTY', 1306 );
define( 'ERROR_CUSTOM_FIELD_NAME_INVALID', 1307 );

# ERROR_LDAP_*
define( 'ERROR_LDAP_AUTH_FAILED', 1400 );
Expand Down

0 comments on commit e5cef5a

Please sign in to comment.