- Allows setting empty value for date custom fields

This commit is contained in:
Vlad
2016-08-14 12:26:01 -04:00
parent 7f091d7188
commit c5a02c6136
5 changed files with 59 additions and 25 deletions

View File

@ -904,7 +904,7 @@ define(
subscribersClone.subscribers[0][matchedColumn.index] =
'<span class="mailpoet_data_match mailpoet_import_error" title="'
+ MailPoet.I18n.t('noDateFieldMatch') + '">'
+ MailPoet.I18n.t('emptyDate')
+ MailPoet.I18n.t('emptyFirstRowDate')
+ '</span>';
preventNextStep = true;
}
@ -923,10 +923,10 @@ define(
}
}
jQuery.map(subscribersClone.subscribers, function (data, index) {
if (index === fillerPosition) return;
var rowData = data[matchedColumn.index];
if (index === fillerPosition || rowData.trim() === '') return;
var date = Moment(rowData, testedFormat, true);
// validate date:
// validate date
if (date.isValid()) {
data[matchedColumn.index] +=
'<span class="mailpoet_data_match" title="'

View File

@ -206,46 +206,67 @@ class Date extends Base {
$month_position = array_search('MM', $parsed_date_format);
$day_position = array_search('DD', $parsed_date_format);
if(count($parsed_date) === 3) {
// create date from any combination of mm, dd and yyyy
// create date from any combination of month, day and year
$parsed_date = array(
'year' => $parsed_date[$year_position],
'month' => $parsed_date[$month_position],
'day' => $parsed_date[$day_position]
);
} else if(count($parsed_date) === 2) {
// create date from any combination of mm and dd
// create date from any combination of month and year
$parsed_date = array(
'year' => $parsed_date[$year_position],
'month' => $parsed_date[$month_position],
'day' => '01'
);
} else if($date_format === 'MM' && count($parsed_date) === 1) {
// create date from mm
$parsed_date = array(
'month' => $parsed_date[$month_position],
'day' => '01',
'year' => date('Y')
);
// create date from month
if((int)$parsed_date[$month_position] === 0) {
$datetime = '';
$parsed_date = false;
} else {
$parsed_date = array(
'month' => $parsed_date[$month_position],
'day' => '01',
'year' => date('Y')
);
}
} else if($date_format === 'YYYY' && count($parsed_date) === 1) {
// create date from dd
$parsed_date = array(
'year' => $parsed_date[$year_position],
'month' => '01',
'day' => '01'
);
// create date from year
if((int)$parsed_date[$year_position] === 0) {
$datetime = '';
$parsed_date = false;
} else {
$parsed_date = array(
'year' => $parsed_date[$year_position],
'month' => '01',
'day' => '01'
);
}
} else {
$parsed_date = false;
}
if($parsed_date) {
$datetime = sprintf(
'%s-%s-%s 00:00:00',
$parsed_date['year'],
$parsed_date['month'],
$parsed_date['day']
);
$year = $parsed_date['year'];
$month = $parsed_date['month'];
$day = $parsed_date['day'];
// if all date parts are set to 0, date value is empty
if((int)$year === 0 && (int)$month === 0 && (int)$day === 0) {
$datetime = '';
} else {
if((int)$year === 0) $year = date('Y');
if((int)$month === 0) $month = date('m');
if((int)$day === 0) $day = date('d');
$datetime = sprintf(
'%s-%s-%s 00:00:00',
$year,
$month,
$day
);
}
}
}
if($datetime) {
if($datetime !== false && !empty($datetime)) {
try {
$datetime = Carbon::parse($datetime)->toDateTimeString();
} catch(\Exception $e) {

View File

@ -127,6 +127,7 @@ class Import {
if($custom_field->type === 'date') {
$data = array_map(
function($index, $date) use($validation_rule, &$invalid_records) {
if (empty($date)) return $date;
$date = Date::convertDateToDatetime($date, $validation_rule);
if(!$date) {
$invalid_records[] = $index;

View File

@ -42,4 +42,16 @@ class DateTest extends MailPoetTest {
->equals('2016-05-10 00:00:00');
}
function testItCanClearDate() {
expect(Date::convertDateToDatetime('0/10/5', 'YYYY/MM/DD'))
->equals(date('Y') . '-10-05 00:00:00');
expect(Date::convertDateToDatetime('0/0/5', 'YYYY/MM/DD'))
->equals(date('Y') . '-' . date('m') . '-05 00:00:00');
expect(Date::convertDateToDatetime('0/0/0', 'YYYY/MM/DD'))
->equals('');
expect(Date::convertDateToDatetime('0', 'YYYY'))
->equals('');
expect(Date::convertDateToDatetime('0', 'MM'))
->equals('');
}
}

View File

@ -66,7 +66,7 @@
'november': __('November'),
'december': __('December'),
'noDateFieldMatch': __("Do not match as a 'date field' if most of the rows for that field return the same error"),
'emptyDate': __('Date cannot be empty'),
'emptyFirstRowDate': __('First row date cannot be empty'),
'verifyDateMatch': __('Verify that the date in blue matches the original date'),
'pm': __('PM'),
'am': __('AM'),