Yaron Koren has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/99447


Change subject: Removed defaulting for date inputs, which allows for "partial" 
dates
......................................................................

Removed defaulting for date inputs, which allows for "partial" dates

"Partial" dates includes year only, or year and month but no day.

Change-Id: Id1979bdc50d593cb38939392ce0cf4ea941dc82c
---
M includes/SF_FormPrinter.php
M includes/forminputs/SF_DateInput.php
2 files changed, 61 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticForms 
refs/changes/47/99447/1

diff --git a/includes/SF_FormPrinter.php b/includes/SF_FormPrinter.php
index 8804da6..6ddf9fa 100644
--- a/includes/SF_FormPrinter.php
+++ b/includes/SF_FormPrinter.php
@@ -1096,24 +1096,44 @@
                                                                        if ( 
isset( $cur_value['second'] ) ) $second = $cur_value['second'];
                                                                        if ( 
isset( $cur_value['ampm24h'] ) ) $ampm24h = $cur_value['ampm24h'];
                                                                        if ( 
isset( $cur_value['timezone'] ) ) $timezone = $cur_value['timezone'];
-                                                                       if ( 
$month !== '' && $day !== '' && $year !== '' ) {
+                                                                       //if ( 
$month !== '' && $day !== '' && $year !== '' ) {
+                                                                       // We 
can accept either year, or year + month, or year + month + day.
+                                                                       //if ( 
$month !== '' && $day !== '' && $year !== '' ) {
+                                                                       if ( 
$year !== '' ) {
                                                                                
// special handling for American dates - otherwise, just
                                                                                
// the standard year/month/day (where month is a number)
                                                                                
global $wgAmericanDates;
-                                                                               
if ( $wgAmericanDates == true ) {
-                                                                               
        $cur_value_in_template = "$month $day, $year";
+
+                                                                               
if ( $month == '' ) {
+                                                                               
        $cur_value_in_template = $year;
+                                                                               
} elseif ( $day == '' ) {
+                                                                               
        $cur_value_in_template = $year;
+                                                                               
        if ( $wgAmericanDates == true ) {
+                                                                               
                $cur_value_in_template = "$month $year";
+                                                                               
        } else {
+                                                                               
                $cur_value_in_template = "$year/$month";
+                                                                               
        }
                                                                                
} else {
-                                                                               
        $cur_value_in_template = "$year/$month/$day";
+                                                                               
        if ( $wgAmericanDates == true ) {
+                                                                               
                $cur_value_in_template = "$month $day, $year";
+                                                                               
        } else {
+                                                                               
                $cur_value_in_template = "$year/$month/$day";
+                                                                               
        }
+                                                                               
        // If there's a day, include whatever time information
+                                                                               
        // we have.
+                                                                               
        if ( ! is_null( $hour ) ) {
+                                                                               
                $cur_value_in_template .= " " . str_pad( intval( substr( $hour, 
0, 2 ) ), 2, '0', STR_PAD_LEFT ) . ":" . str_pad( intval( substr( $minute, 0, 2 
) ), 2, '0', STR_PAD_LEFT );
+                                                                               
        }
+                                                                               
        if ( ! is_null( $second ) ) {
+                                                                               
                $cur_value_in_template .= ":" . str_pad( intval( substr( 
$second, 0, 2 ) ), 2, '0', STR_PAD_LEFT );
+                                                                               
        }
+                                                                               
        if ( ! is_null( $ampm24h ) ) {
+                                                                               
                $cur_value_in_template .= " $ampm24h";
+                                                                               
        }
+                                                                               
        if ( ! is_null( $timezone ) ) {
+                                                                               
                $cur_value_in_template .= " $timezone";
+                                                                               
        }
                                                                                
}
-                                                                               
// include whatever time information we have
-                                                                               
if ( ! is_null( $hour ) )
-                                                                               
        $cur_value_in_template .= " " . str_pad( intval( substr( $hour, 0, 2 ) 
), 2, '0', STR_PAD_LEFT ) . ":" . str_pad( intval( substr( $minute, 0, 2 ) ), 
2, '0', STR_PAD_LEFT );
-                                                                               
if ( ! is_null( $second ) )
-                                                                               
        $cur_value_in_template .= ":" . str_pad( intval( substr( $second, 0, 2 
) ), 2, '0', STR_PAD_LEFT );
-                                                                               
if ( ! is_null( $ampm24h ) )
-                                                                               
        $cur_value_in_template .= " $ampm24h";
-                                                                               
if ( ! is_null( $timezone ) )
-                                                                               
        $cur_value_in_template .= " $timezone";
                                                                        } else {
                                                                                
$cur_value_in_template = "";
                                                                        }
diff --git a/includes/forminputs/SF_DateInput.php 
b/includes/forminputs/SF_DateInput.php
index e8585fa..ade9656 100644
--- a/includes/forminputs/SF_DateInput.php
+++ b/includes/forminputs/SF_DateInput.php
@@ -25,11 +25,17 @@
 
                $optionsText = '';
                $month_names = SFFormUtils::getMonthNames();
+               // Add a "null" value at the beginning.
+               array_unshift( $month_names, null );
                foreach ( $month_names as $i => $name ) {
-                       // pad out month to always be two digits
-                       $month_value = ( $wgAmericanDates == true ) ? $name : 
str_pad( $i + 1, 2, '0', STR_PAD_LEFT );
+                       if ( is_null( $name ) ) {
+                               $month_value = null;
+                       } else {
+                               // Pad out month to always be two digits.
+                               $month_value = ( $wgAmericanDates == true ) ? 
$name : str_pad( $i, 2, '0', STR_PAD_LEFT );
+                       }
                        $optionAttrs = array ( 'value' => $month_value );
-                       if ( $name == $cur_month || ( $i + 1 ) == $cur_month ) {
+                       if ( $name == $cur_month || $i == $cur_month ) {
                                $optionAttrs['selected'] = 'selected';
                        }
                        $optionsText .= Html::element( 'option', $optionAttrs, 
$name );
@@ -48,6 +54,8 @@
 
        public static function getMainHTML( $date, $input_name, $is_mandatory, 
$is_disabled, $other_args ) {
                global $sfgTabIndex, $wgAmericanDates;
+
+               $year = $month = $day = null;
 
                if ( $date ) {
                        // Can show up here either as an array or a string,
@@ -78,14 +86,25 @@
                                if ( $year < 0 ) {
                                        $year = ( $year * - 1 + 1 ) . ' BC';
                                }
-                               $month = $actual_date->getMonth();
-                               $day = $actual_date->getDay();
+                               // Use precision of the date to determine
+                               // whether we should also set the month and
+                               // day.
+                               if ( method_exists( 
$actual_date->getDataItem(), 'getPrecision' ) ) {
+                                       $precision = 
$actual_date->getDataItem()->getPrecision();
+                                       if ( $precision > SMWDITime::PREC_Y ) {
+                                               $month = 
$actual_date->getMonth();
+                                       }
+                                       if ( $precision > SMWDITime::PREC_YM ) {
+                                               $day = $actual_date->getDay();
+                                       }
+                               } else {
+                                       // There's some sort of error - make
+                                       // everything blank.
+                                       $year = null;
+                               }
                        }
                } else {
-                       $cur_date = getdate();
-                       $year = $cur_date['year'];
-                       $month = $cur_date['month'];
-                       $day = null; // no need for day
+                       // Just keep everything at null.
                }
                $text = "";
                $disabled_text = ( $is_disabled ) ? 'disabled' : '';

-- 
To view, visit https://gerrit.wikimedia.org/r/99447
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id1979bdc50d593cb38939392ce0cf4ea941dc82c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticForms
Gerrit-Branch: master
Gerrit-Owner: Yaron Koren <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to