Merge pull request #582 from mailpoet/custom_field_fix
Custom field fix
This commit is contained in:
@ -127,37 +127,12 @@ define([
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const dateType = this.props.field.params.date_type;
|
const dateTime = Moment(value);
|
||||||
const dateParts = value.split('-');
|
|
||||||
let year = '';
|
|
||||||
let month = '';
|
|
||||||
let day = '';
|
|
||||||
|
|
||||||
switch(dateType) {
|
|
||||||
case 'year_month_day':
|
|
||||||
year = ~~(dateParts[0]);
|
|
||||||
month = ~~(dateParts[1]);
|
|
||||||
day = ~~(dateParts[2]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'year_month':
|
|
||||||
year = ~~(dateParts[0]);
|
|
||||||
month = ~~(dateParts[1]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'month':
|
|
||||||
month = ~~(dateParts[0]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'year':
|
|
||||||
year = ~~(dateParts[0]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
year: year,
|
year: dateTime.format('YYYY'),
|
||||||
month: month,
|
month: dateTime.format('M'),
|
||||||
day: day
|
day: dateTime.format('D')
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
formatValue() {
|
formatValue() {
|
||||||
@ -228,7 +203,7 @@ define([
|
|||||||
|
|
||||||
const fields = dateSelects.map(type => {
|
const fields = dateSelects.map(type => {
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case 'yyyy':
|
case 'YYYY':
|
||||||
return (<FormFieldDateYear
|
return (<FormFieldDateYear
|
||||||
onValueChange={ this.onValueChange.bind(this) }
|
onValueChange={ this.onValueChange.bind(this) }
|
||||||
ref={ 'year' }
|
ref={ 'year' }
|
||||||
@ -239,7 +214,7 @@ define([
|
|||||||
/>);
|
/>);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'mm':
|
case 'MM':
|
||||||
return (<FormFieldDateMonth
|
return (<FormFieldDateMonth
|
||||||
onValueChange={ this.onValueChange.bind(this) }
|
onValueChange={ this.onValueChange.bind(this) }
|
||||||
ref={ 'month' }
|
ref={ 'month' }
|
||||||
@ -251,7 +226,7 @@ define([
|
|||||||
/>);
|
/>);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'dd':
|
case 'DD':
|
||||||
return (<FormFieldDateDay
|
return (<FormFieldDateDay
|
||||||
onValueChange={ this.onValueChange.bind(this) }
|
onValueChange={ this.onValueChange.bind(this) }
|
||||||
ref={ 'day' }
|
ref={ 'day' }
|
||||||
|
@ -158,7 +158,7 @@ define(
|
|||||||
window.location.href = response.data.exportFileURL;
|
window.location.href = response.data.exportFileURL;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.error(function (error) {
|
.fail(function (error) {
|
||||||
MailPoet.Modal.loading(false);
|
MailPoet.Modal.loading(false);
|
||||||
MailPoet.Notice.error(
|
MailPoet.Notice.error(
|
||||||
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
||||||
|
@ -51,6 +51,12 @@ define(
|
|||||||
* STEP 1 (upload or copy/paste)
|
* STEP 1 (upload or copy/paste)
|
||||||
*/
|
*/
|
||||||
router.on('route:step1', function () {
|
router.on('route:step1', function () {
|
||||||
|
// set or reset temporary validation rule on all columns
|
||||||
|
mailpoetColumns = jQuery.map(mailpoetColumns, function (column, columnIndex) {
|
||||||
|
column.validation_rule = false;
|
||||||
|
return column;
|
||||||
|
});
|
||||||
|
|
||||||
if (typeof (importData.step1) !== 'undefined') {
|
if (typeof (importData.step1) !== 'undefined') {
|
||||||
showCurrentStep();
|
showCurrentStep();
|
||||||
return;
|
return;
|
||||||
@ -211,7 +217,7 @@ define(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
MailPoet.Modal.loading(false);
|
MailPoet.Modal.loading(false);
|
||||||
}).error(function (error) {
|
}).fail(function (error) {
|
||||||
MailPoet.Modal.loading(false);
|
MailPoet.Modal.loading(false);
|
||||||
MailPoet.Notice.error(
|
MailPoet.Notice.error(
|
||||||
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
||||||
@ -242,7 +248,7 @@ define(
|
|||||||
MailPoet.Notice.error(response.errors);
|
MailPoet.Notice.error(response.errors);
|
||||||
}
|
}
|
||||||
MailPoet.Modal.loading(false);
|
MailPoet.Modal.loading(false);
|
||||||
}).error(function () {
|
}).fail(function () {
|
||||||
MailPoet.Modal.loading(false);
|
MailPoet.Modal.loading(false);
|
||||||
MailPoet.Notice.error(
|
MailPoet.Notice.error(
|
||||||
MailPoet.I18n.t('serverError') + result.statusText.toLowerCase() + '.'
|
MailPoet.I18n.t('serverError') + result.statusText.toLowerCase() + '.'
|
||||||
@ -455,7 +461,7 @@ define(
|
|||||||
null,
|
null,
|
||||||
new Array(subscribers.subscribers[0].length)
|
new Array(subscribers.subscribers[0].length)
|
||||||
).map(String.prototype.valueOf, filler),
|
).map(String.prototype.valueOf, filler),
|
||||||
fillterPosition;
|
fillerPosition;
|
||||||
|
|
||||||
showCurrentStep();
|
showCurrentStep();
|
||||||
|
|
||||||
@ -631,7 +637,7 @@ define(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.error(function (error) {
|
.fail(function (error) {
|
||||||
MailPoet.Modal.close();
|
MailPoet.Modal.close();
|
||||||
MailPoet.Notice.error(
|
MailPoet.Notice.error(
|
||||||
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
||||||
@ -707,7 +713,7 @@ define(
|
|||||||
// display filler data (e.g., ellipsis) if we've reached the maximum number of rows and
|
// display filler data (e.g., ellipsis) if we've reached the maximum number of rows and
|
||||||
// subscribers count is greater than the maximum number of rows we're displaying
|
// subscribers count is greater than the maximum number of rows we're displaying
|
||||||
if (index === maxRowsToShow && subscribers.subscribersCount > (maxRowsToShow + 1)) {
|
if (index === maxRowsToShow && subscribers.subscribersCount > (maxRowsToShow + 1)) {
|
||||||
fillterPosition = index;
|
fillerPosition = index;
|
||||||
return filler;
|
return filler;
|
||||||
}
|
}
|
||||||
// if we're on the last line, show the total count of subscribers data
|
// if we're on the last line, show the total count of subscribers data
|
||||||
@ -745,70 +751,31 @@ define(
|
|||||||
.on('select2:selecting', function (selectEvent) {
|
.on('select2:selecting', function (selectEvent) {
|
||||||
var selectElement = this,
|
var selectElement = this,
|
||||||
selectedOptionId = selectEvent.params.args.data.id;
|
selectedOptionId = selectEvent.params.args.data.id;
|
||||||
|
// CREATE CUSTOM FIELD
|
||||||
if (selectedOptionId === 'create') {
|
if (selectedOptionId === 'create') {
|
||||||
selectEvent.preventDefault();
|
selectEvent.preventDefault();
|
||||||
jQuery(selectElement).select2('close');
|
jQuery(selectElement).select2('close');
|
||||||
MailPoet.Modal.popup({
|
MailPoet.Modal.popup({
|
||||||
title: MailPoet.I18n.t('addNewColumn'),
|
title: MailPoet.I18n.t('addNewField'),
|
||||||
template: jQuery('#new_column_template').html()
|
template: jQuery('#form_template_field_form').html()
|
||||||
});
|
});
|
||||||
jQuery('#new_column_name').keypress(function (e) {
|
jQuery('#form_field_new').parsley().on('form:submit', function(parsley) {
|
||||||
if (e.which == 13) {
|
// get data
|
||||||
jQuery('#new_column_process').click();
|
var data = jQuery(this.$element).serializeObject();
|
||||||
}
|
|
||||||
});
|
// save custom field
|
||||||
jQuery('#new_column_process').click(function () {
|
MailPoet.Ajax.post({
|
||||||
var name = jQuery('#new_column_name').val().trim(),
|
endpoint: 'customFields',
|
||||||
type = jQuery('#new_column_type').val().trim(),
|
action: 'save',
|
||||||
columnNames = mailpoetColumns.map(function (el) {
|
data: data
|
||||||
return el.name.toLowerCase();
|
}).done(function(response) {
|
||||||
});
|
if(response.result === true) {
|
||||||
isDuplicateColumnName =
|
|
||||||
(name && columnNames.indexOf(name.toLowerCase()) > -1)
|
|
||||||
? true
|
|
||||||
: false;
|
|
||||||
if (name === '') {
|
|
||||||
jQuery('.mailpoet_validation_error[data-error="name_required"]')
|
|
||||||
.show();
|
|
||||||
} else {
|
|
||||||
jQuery('.mailpoet_validation_error[data-error="name_required"]')
|
|
||||||
.hide();
|
|
||||||
}
|
|
||||||
if (type === '') {
|
|
||||||
jQuery('.mailpoet_validation_error[data-error="type_required"]')
|
|
||||||
.show();
|
|
||||||
} else {
|
|
||||||
jQuery('.mailpoet_validation_error[data-error="type_required"]')
|
|
||||||
.hide();
|
|
||||||
}
|
|
||||||
if (isDuplicateColumnName) {
|
|
||||||
jQuery('.mailpoet_validation_error[data-error="name_not_unique"]')
|
|
||||||
.show();
|
|
||||||
} else {
|
|
||||||
jQuery('.mailpoet_validation_error[data-error="name_not_unique"]')
|
|
||||||
.hide();
|
|
||||||
}
|
|
||||||
// create new field
|
|
||||||
if (name && type && !isDuplicateColumnName) {
|
|
||||||
MailPoet.Modal
|
|
||||||
.close()
|
|
||||||
.loading(true);
|
|
||||||
MailPoet.Ajax
|
|
||||||
.post({
|
|
||||||
endpoint: 'ImportExport',
|
|
||||||
action: 'addCustomField',
|
|
||||||
data: {
|
|
||||||
name: name,
|
|
||||||
type: type
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.done(function (response) {
|
|
||||||
if (response.result === true) {
|
|
||||||
var new_column_data = {
|
var new_column_data = {
|
||||||
'id': response.customField.id,
|
'id': response.field.id,
|
||||||
'name': name,
|
'name': response.field.name,
|
||||||
'type': type,
|
'type': response.field.type,
|
||||||
'custom': true,
|
'params': response.field.params,
|
||||||
|
'custom': true
|
||||||
};
|
};
|
||||||
// if this is the first custom column, create an "optgroup"
|
// if this is the first custom column, create an "optgroup"
|
||||||
if (mailpoetColumnsSelect2.length === 2) {
|
if (mailpoetColumnsSelect2.length === 2) {
|
||||||
@ -836,23 +803,20 @@ define(
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
jQuery(selectElement).data('column-id', new_column_data.id);
|
jQuery(selectElement).data('column-id', new_column_data.id);
|
||||||
|
jQuery(selectElement).data('validation-rule', false);
|
||||||
filterSubscribers();
|
filterSubscribers();
|
||||||
|
// close popup
|
||||||
|
MailPoet.Modal.close();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
MailPoet.Notice.error(MailPoet.I18n.t('customFieldCreateError'));
|
if(response.errors.length > 0) {
|
||||||
}
|
jQuery(response.errors).each(function(i, error) {
|
||||||
MailPoet.Modal.loading(false);
|
MailPoet.Notice.error(error, {positionAfter: '#field_name'});
|
||||||
})
|
|
||||||
.error(function (error) {
|
|
||||||
MailPoet.Modal.loading(false);
|
|
||||||
MailPoet.Notice.error(
|
|
||||||
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
jQuery('#new_column_cancel').click(function () {
|
return false;
|
||||||
MailPoet.Modal.close();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// CHANGE COLUMN
|
// CHANGE COLUMN
|
||||||
@ -890,22 +854,22 @@ define(
|
|||||||
.remove();
|
.remove();
|
||||||
var subscribersClone = jQuery.extend(true, {}, subscribers),
|
var subscribersClone = jQuery.extend(true, {}, subscribers),
|
||||||
preventNextStep = false,
|
preventNextStep = false,
|
||||||
displayedColumnsIds = jQuery.map(
|
displayedColumns = jQuery.map(
|
||||||
jQuery('.mailpoet_subscribers_column_data_match'), function (data) {
|
jQuery('.mailpoet_subscribers_column_data_match'), function (element, elementIndex) {
|
||||||
var columnId = jQuery(data).data('column-id');
|
var columnId = jQuery(element).data('column-id');
|
||||||
jQuery(data).val(columnId).trigger('change');
|
var validationRule = jQuery(element).data('validation-rule');
|
||||||
return columnId;
|
jQuery(element).val(columnId).trigger('change');
|
||||||
|
return { id: columnId, index: elementIndex, validationRule: validationRule, element: element };
|
||||||
});
|
});
|
||||||
// iterate through the object of mailpoet columns
|
// iterate through the object of mailpoet columns
|
||||||
jQuery.map(mailpoetColumns, function (column) {
|
jQuery.map(mailpoetColumns, function (column, columnIndex) {
|
||||||
// check if the column id matches the selected id of one of the
|
// check if the column id matches the selected id of one of the
|
||||||
// subscriber's data columns
|
// subscriber's data columns
|
||||||
var matchedColumn = jQuery.inArray(column.id, displayedColumnsIds);
|
var matchedColumn = _.find(displayedColumns, function(data) { return data.id === column.id; });
|
||||||
|
// EMAIL filter: if the first value in the column doesn't have a valid
|
||||||
// EMAIL filter: if the last value in the column doesn't have a valid
|
|
||||||
// email, hide the next button
|
// email, hide the next button
|
||||||
if (column.id === "email") {
|
if (column.id === 'email') {
|
||||||
if (!emailRegex.test(subscribersClone.subscribers[0][matchedColumn])) {
|
if (!emailRegex.test(subscribersClone.subscribers[0][matchedColumn.index])) {
|
||||||
preventNextStep = true;
|
preventNextStep = true;
|
||||||
if (!jQuery('[data-id="notice_invalidEmail"]').length) {
|
if (!jQuery('[data-id="notice_invalidEmail"]').length) {
|
||||||
MailPoet.Notice.error(MailPoet.I18n.t('columnContainsInvalidElement'), {
|
MailPoet.Notice.error(MailPoet.I18n.t('columnContainsInvalidElement'), {
|
||||||
@ -921,35 +885,63 @@ define(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// DATE filter: if column type is date, check if we can recognize it
|
// DATE filter: if column type is date, check if we can recognize it
|
||||||
if (column.type === 'date' && matchedColumn !== -1) {
|
if (column.type === 'date' && matchedColumn) {
|
||||||
jQuery.map(subscribersClone.subscribers, function (data, position) {
|
var allowedDateFormats = [
|
||||||
var rowData = data[matchedColumn];
|
Moment.ISO_8601,
|
||||||
if (position !== fillterPosition) {
|
'YYYY/MM/DD',
|
||||||
|
'MM/DD/YYYY',
|
||||||
|
'DD/MM/YYYY',
|
||||||
|
'YYYY/MM/DD',
|
||||||
|
'YYYY/DD/MM',
|
||||||
|
'MM/YYYY',
|
||||||
|
'YYYY/MM',
|
||||||
|
'YYYY'
|
||||||
|
];
|
||||||
|
var firstRowData = subscribersClone.subscribers[0][matchedColumn.index];
|
||||||
|
var validationRule = false;
|
||||||
// check if date exists
|
// check if date exists
|
||||||
if (rowData.trim() === '') {
|
if (firstRowData.trim() === '') {
|
||||||
data[matchedColumn] =
|
subscribersClone.subscribers[0][matchedColumn.index] =
|
||||||
'<span class="mailpoet_data_match mailpoet_import_error" title="'
|
'<span class="mailpoet_data_match mailpoet_import_error" title="'
|
||||||
+ MailPoet.I18n.t('noDateFieldMatch') + '">'
|
+ MailPoet.I18n.t('noDateFieldMatch') + '">'
|
||||||
+ MailPoet.I18n.t('emptyDate')
|
+ MailPoet.I18n.t('emptyFirstRowDate')
|
||||||
+ '</span>';
|
+ '</span>';
|
||||||
preventNextStep = true;
|
preventNextStep = true;
|
||||||
return;
|
|
||||||
}
|
|
||||||
// check if date is valid and is before today
|
|
||||||
if (Moment(rowData).isValid() && Moment(rowData).isBefore(Moment())) {
|
|
||||||
data[matchedColumn] +=
|
|
||||||
'<span class="mailpoet_data_match" title="'
|
|
||||||
+ MailPoet.I18n.t('verifyDateMatch') + '">'
|
|
||||||
+ MailPoet.Date.format(rowData) + '</span>';
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
data[matchedColumn] +=
|
for (var format in allowedDateFormats) {
|
||||||
|
var testedFormat = allowedDateFormats[format]
|
||||||
|
if (Moment(firstRowData, testedFormat, true).isValid()) {
|
||||||
|
var validationRule = (typeof(testedFormat) === 'function') ?
|
||||||
|
'datetime' :
|
||||||
|
testedFormat
|
||||||
|
// set validation on the column element
|
||||||
|
jQuery(matchedColumn.element).data('validation-rule', validationRule);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (validationRule === 'datetime') validationRule = Moment.ISO_8601;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jQuery.map(subscribersClone.subscribers, function (data, index) {
|
||||||
|
var rowData = data[matchedColumn.index];
|
||||||
|
if (index === fillerPosition || rowData.trim() === '') return;
|
||||||
|
var date = Moment(rowData, testedFormat, true);
|
||||||
|
// validate date
|
||||||
|
if (date.isValid()) {
|
||||||
|
data[matchedColumn.index] +=
|
||||||
|
'<span class="mailpoet_data_match" title="'
|
||||||
|
+ MailPoet.I18n.t('verifyDateMatch') + '">'
|
||||||
|
+ MailPoet.Date.format(date)
|
||||||
|
+ '</span>';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
data[matchedColumn.index] +=
|
||||||
'<span class="mailpoet_data_match mailpoet_import_error" title="'
|
'<span class="mailpoet_data_match mailpoet_import_error" title="'
|
||||||
+ MailPoet.I18n.t('noDateFieldMatch') + '">'
|
+ MailPoet.I18n.t('noDateFieldMatch') + '">'
|
||||||
+ MailPoet.I18n.t('dateMatchError') + '</span>';
|
+ MailPoet.I18n.t('dateMatchError')
|
||||||
|
+ '</span>';
|
||||||
preventNextStep = true;
|
preventNextStep = true;
|
||||||
}
|
};
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (preventNextStep && !jQuery('.mailpoet_invalidDate').length) {
|
if (preventNextStep && !jQuery('.mailpoet_invalidDate').length) {
|
||||||
MailPoet.Notice.error(MailPoet.I18n.t('columnContainsInvalidDate'), {
|
MailPoet.Notice.error(MailPoet.I18n.t('columnContainsInvalidDate'), {
|
||||||
@ -1019,10 +1011,11 @@ define(
|
|||||||
_.each(jQuery('select.mailpoet_subscribers_column_data_match'),
|
_.each(jQuery('select.mailpoet_subscribers_column_data_match'),
|
||||||
function (column, columnIndex) {
|
function (column, columnIndex) {
|
||||||
var columnId = jQuery(column).data('column-id');
|
var columnId = jQuery(column).data('column-id');
|
||||||
|
var validationRule = jQuery(column).data('validation-rule');
|
||||||
if (columnId === 'ignore') {
|
if (columnId === 'ignore') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
columns[columnId] = columnIndex;
|
columns[columnId] = { index: columnIndex, validation_rule: validationRule };
|
||||||
});
|
});
|
||||||
|
|
||||||
_.each(subscribers, function () {
|
_.each(subscribers, function () {
|
||||||
@ -1051,7 +1044,7 @@ define(
|
|||||||
}
|
}
|
||||||
queue.run();
|
queue.run();
|
||||||
})
|
})
|
||||||
.error(function (error) {
|
.fail(function (error) {
|
||||||
importResults.errors.push(
|
importResults.errors.push(
|
||||||
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
MailPoet.I18n.t('serverError') + error.statusText.toLowerCase() + '.'
|
||||||
);
|
);
|
||||||
|
@ -32,22 +32,6 @@ class ImportExport {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addCustomField($data) {
|
|
||||||
$customField = CustomField::create();
|
|
||||||
$customField->hydrate($data);
|
|
||||||
$result = $customField->save();
|
|
||||||
return (
|
|
||||||
($result) ?
|
|
||||||
array(
|
|
||||||
'result' => true,
|
|
||||||
'customField' => $customField->asArray()
|
|
||||||
) :
|
|
||||||
array(
|
|
||||||
'result' => false
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function processImport($data) {
|
function processImport($data) {
|
||||||
$import = new \MailPoet\Subscribers\ImportExport\Import\Import(
|
$import = new \MailPoet\Subscribers\ImportExport\Import\Import(
|
||||||
json_decode($data, true)
|
json_decode($data, true)
|
||||||
|
@ -379,7 +379,12 @@ class Menu {
|
|||||||
function import() {
|
function import() {
|
||||||
$import = new ImportExportFactory('import');
|
$import = new ImportExportFactory('import');
|
||||||
$data = $import->bootstrap();
|
$data = $import->bootstrap();
|
||||||
$data['sub_menu'] = 'mailpoet-subscribers';
|
$data = array_merge($data, array(
|
||||||
|
'date_types' => Block\Date::getDateTypes(),
|
||||||
|
'date_formats' => Block\Date::getDateFormats(),
|
||||||
|
'month_names' => Block\Date::getMonthNames(),
|
||||||
|
'sub_menu' => 'mailpoet-subscribers'
|
||||||
|
));
|
||||||
echo $this->renderer->render('subscribers/importExport/import.html', $data);
|
echo $this->renderer->render('subscribers/importExport/import.html', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
class Date extends Base {
|
class Date extends Base {
|
||||||
|
|
||||||
static function render($block) {
|
static function render($block) {
|
||||||
@ -65,19 +67,19 @@ class Date extends Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach($date_selectors as $date_selector) {
|
foreach($date_selectors as $date_selector) {
|
||||||
if($date_selector === 'dd') {
|
if($date_selector === 'DD') {
|
||||||
$block['selected'] = $day;
|
$block['selected'] = $day;
|
||||||
$html .= '<select class="mailpoet_date_day" ';
|
$html .= '<select class="mailpoet_date_day" ';
|
||||||
$html .= 'name="'.$field_name.'[day]" placeholder="'.__('Day').'">';
|
$html .= 'name="'.$field_name.'[day]" placeholder="'.__('Day').'">';
|
||||||
$html .= static::getDays($block);
|
$html .= static::getDays($block);
|
||||||
$html .= '</select>';
|
$html .= '</select>';
|
||||||
} else if($date_selector === 'mm') {
|
} else if($date_selector === 'MM') {
|
||||||
$block['selected'] = $month;
|
$block['selected'] = $month;
|
||||||
$html .= '<select class="mailpoet_date_month" ';
|
$html .= '<select class="mailpoet_date_month" ';
|
||||||
$html .= 'name="'.$field_name.'[month]" placeholder="'.__('Month').'">';
|
$html .= 'name="'.$field_name.'[month]" placeholder="'.__('Month').'">';
|
||||||
$html .= static::getMonths($block);
|
$html .= static::getMonths($block);
|
||||||
$html .= '</select>';
|
$html .= '</select>';
|
||||||
} else if($date_selector === 'yyyy') {
|
} else if($date_selector === 'YYYY') {
|
||||||
$block['selected'] = $year;
|
$block['selected'] = $year;
|
||||||
$html .= '<select class="mailpoet_date_year" ';
|
$html .= '<select class="mailpoet_date_year" ';
|
||||||
$html .= 'name="'.$field_name.'[year]" placeholder="'.__('Year').'">';
|
$html .= 'name="'.$field_name.'[year]" placeholder="'.__('Year').'">';
|
||||||
@ -100,10 +102,10 @@ class Date extends Base {
|
|||||||
|
|
||||||
static function getDateFormats() {
|
static function getDateFormats() {
|
||||||
return array(
|
return array(
|
||||||
'year_month_day' => array('mm/dd/yyyy', 'dd/mm/yyyy', 'yyyy/mm/dd'),
|
'year_month_day' => array('MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD'),
|
||||||
'year_month' => array('mm/yyyy', 'yyyy/mm'),
|
'year_month' => array('MM/YYYY', 'YYYY/MM'),
|
||||||
'year' => array('yyyy'),
|
'year' => array('YYYY'),
|
||||||
'month' => array('mm')
|
'month' => array('MM')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
static function getMonthNames() {
|
static function getMonthNames() {
|
||||||
@ -192,4 +194,85 @@ class Date extends Base {
|
|||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function convertDateToDatetime($date, $date_format) {
|
||||||
|
$datetime = false;
|
||||||
|
if($date_format === 'datetime') {
|
||||||
|
$datetime = $date;
|
||||||
|
} else {
|
||||||
|
$parsed_date = explode('/', $date);
|
||||||
|
$parsed_date_format = explode('/', $date_format);
|
||||||
|
$year_position = array_search('YYYY', $parsed_date_format);
|
||||||
|
$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 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 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 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 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) {
|
||||||
|
$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 !== false && !empty($datetime)) {
|
||||||
|
try {
|
||||||
|
$datetime = Carbon::parse($datetime)->toDateTimeString();
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
$datetime = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $datetime;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Models;
|
namespace MailPoet\Models;
|
||||||
|
|
||||||
|
use MailPoet\Form\Block\Date;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class CustomField extends Model {
|
class CustomField extends Model {
|
||||||
@ -43,28 +45,27 @@ class CustomField extends Model {
|
|||||||
// format custom field data depending on type
|
// format custom field data depending on type
|
||||||
if(is_array($value) && $this->type === 'date' ) {
|
if(is_array($value) && $this->type === 'date' ) {
|
||||||
$custom_field_data = $this->asArray();
|
$custom_field_data = $this->asArray();
|
||||||
|
$date_format = $custom_field_data['params']['date_format'];
|
||||||
$date_type = (isset($custom_field_data['params']['date_type'])
|
$date_type = (isset($custom_field_data['params']['date_type'])
|
||||||
? $custom_field_data['params']['date_type']
|
? $custom_field_data['params']['date_type']
|
||||||
: 'year_month_day'
|
: 'year_month_day'
|
||||||
);
|
);
|
||||||
$date_parts = explode('_', $date_type);
|
$date_parts = explode('_', $date_type);
|
||||||
|
|
||||||
switch($date_type) {
|
switch($date_type) {
|
||||||
case 'year_month_day':
|
case 'year_month_day':
|
||||||
$value = sprintf(
|
$value = sprintf(
|
||||||
'%04d-%02d-%02d',
|
'%s/%s/%s',
|
||||||
$value['year'],
|
|
||||||
$value['month'],
|
$value['month'],
|
||||||
$value['day']
|
$value['day'],
|
||||||
|
$value['year']
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'year_month':
|
case 'year_month':
|
||||||
$value = sprintf(
|
$value = sprintf(
|
||||||
'%04d-%02d',
|
'%s/%s',
|
||||||
$value['year'],
|
$value['month'],
|
||||||
$value['month']
|
$value['year']
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -73,12 +74,23 @@ class CustomField extends Model {
|
|||||||
$value = '';
|
$value = '';
|
||||||
} else {
|
} else {
|
||||||
$value = sprintf(
|
$value = sprintf(
|
||||||
'%02d',
|
'%s',
|
||||||
$value['month']
|
$value['month']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'day':
|
||||||
|
if((int)$value['day'] === 0) {
|
||||||
|
$value = '';
|
||||||
|
} else {
|
||||||
|
$value = sprintf(
|
||||||
|
'%s',
|
||||||
|
$value['day']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case 'year':
|
case 'year':
|
||||||
if((int)$value['year'] === 0) {
|
if((int)$value['year'] === 0) {
|
||||||
$value = '';
|
$value = '';
|
||||||
@ -90,6 +102,10 @@ class CustomField extends Model {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!empty($value)) {
|
||||||
|
$value = Date::convertDateToDatetime($value, $date_format);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Subscribers\ImportExport\Import;
|
namespace MailPoet\Subscribers\ImportExport\Import;
|
||||||
|
|
||||||
|
use MailPoet\Form\Block\Date;
|
||||||
|
use MailPoet\Models\CustomField;
|
||||||
use MailPoet\Models\Newsletter;
|
use MailPoet\Models\Newsletter;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Models\SubscriberCustomField;
|
use MailPoet\Models\SubscriberCustomField;
|
||||||
@ -32,16 +34,32 @@ class Import {
|
|||||||
$this->subscriber_custom_fields = $this->getCustomSubscriberFields(
|
$this->subscriber_custom_fields = $this->getCustomSubscriberFields(
|
||||||
array_keys($data['columns'])
|
array_keys($data['columns'])
|
||||||
);
|
);
|
||||||
|
$this->subscriber_fields_validation_rules = $this->getSubscriberFieldsValidationRules(
|
||||||
|
$data['columns']
|
||||||
|
);
|
||||||
$this->subscribers_count = count(reset($this->subscribers_data));
|
$this->subscribers_count = count(reset($this->subscribers_data));
|
||||||
$this->created_at = date('Y-m-d H:i:s', (int)$data['timestamp']);
|
$this->created_at = date('Y-m-d H:i:s', (int)$data['timestamp']);
|
||||||
$this->updated_at = date('Y-m-d H:i:s', (int)$data['timestamp'] + 1);
|
$this->updated_at = date('Y-m-d H:i:s', (int)$data['timestamp'] + 1);
|
||||||
$this->profiler_start = microtime(true);
|
$this->profiler_start = microtime(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSubscriberFieldsValidationRules($subscriber_fields) {
|
||||||
|
$validation_rules = array();
|
||||||
|
foreach($subscriber_fields as $column => $field) {
|
||||||
|
$validation_rules[$column] = (!empty($field['validation_rule'])) ?
|
||||||
|
$field['validation_rule'] :
|
||||||
|
false;
|
||||||
|
}
|
||||||
|
return $validation_rules;
|
||||||
|
}
|
||||||
|
|
||||||
function process() {
|
function process() {
|
||||||
$subscriber_fields = $this->subscriber_fields;
|
$subscriber_fields = $this->subscriber_fields;
|
||||||
$subscriber_custom_fields = $this->subscriber_custom_fields;
|
$subscriber_custom_fields = $this->subscriber_custom_fields;
|
||||||
$subscribers_data = $this->subscribers_data;
|
$subscribers_data = $this->validateSubscribersFields(
|
||||||
|
$this->subscribers_data,
|
||||||
|
$this->subscriber_fields_validation_rules
|
||||||
|
);
|
||||||
list ($subscribers_data, $subscriber_fields) =
|
list ($subscribers_data, $subscriber_fields) =
|
||||||
$this->filterSubscriberStatus($subscribers_data, $subscriber_fields);
|
$this->filterSubscriberStatus($subscribers_data, $subscriber_fields);
|
||||||
$this->deleteExistingTrashedSubscribers($subscribers_data);
|
$this->deleteExistingTrashedSubscribers($subscribers_data);
|
||||||
@ -98,9 +116,39 @@ class Import {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateSubscribersFields($subscribers_data, $validation_rules) {
|
||||||
|
$invalid_records = array();
|
||||||
|
foreach($subscribers_data as $column => &$data) {
|
||||||
|
$validation_rule = $validation_rules[$column];
|
||||||
|
// if this is a custom column
|
||||||
|
if(in_array($column, $this->subscriber_custom_fields)) {
|
||||||
|
$custom_field = CustomField::findOne($column);
|
||||||
|
// validate date type
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return $date;
|
||||||
|
}, array_keys($data), $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if($invalid_records) {
|
||||||
|
foreach($subscribers_data as $column => &$data) {
|
||||||
|
$data = array_diff_key($data, array_flip($invalid_records));
|
||||||
|
$data = array_values($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $subscribers_data;
|
||||||
|
}
|
||||||
|
|
||||||
function transformSubscribersData($subscribers, $columns) {
|
function transformSubscribersData($subscribers, $columns) {
|
||||||
foreach($columns as $column => $index) {
|
foreach($columns as $column => $data) {
|
||||||
$transformed_subscribers[$column] = Helpers::arrayColumn($subscribers, $index);
|
$transformed_subscribers[$column] = Helpers::arrayColumn($subscribers, $data['index']);
|
||||||
}
|
}
|
||||||
return $transformed_subscribers;
|
return $transformed_subscribers;
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,7 @@ class ImportExportFactory {
|
|||||||
'id' => $field['id'],
|
'id' => $field['id'],
|
||||||
'name' => $field['name'],
|
'name' => $field['name'],
|
||||||
'type' => $field['type'],
|
'type' => $field['type'],
|
||||||
|
'params' => unserialize($field['params']),
|
||||||
'custom' => true
|
'custom' => true
|
||||||
);
|
);
|
||||||
}, $subscriber_custom_fields);
|
}, $subscriber_custom_fields);
|
||||||
|
57
tests/unit/Form/Block/DateTest.php
Normal file
57
tests/unit/Form/Block/DateTest.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use MailPoet\Form\Block\Date;
|
||||||
|
|
||||||
|
class DateTest extends MailPoetTest {
|
||||||
|
function testItCanConvertDateMonthYearFormatToDatetime() {
|
||||||
|
$date = array(
|
||||||
|
'MM/DD/YYYY' => '05/10/2016',
|
||||||
|
'DD/MM/YYYY' => '10/05/2016',
|
||||||
|
'YYYY/MM/DD' => '2016/05/10',
|
||||||
|
'YYYY/DD/MM' => '2016/10/05'
|
||||||
|
);
|
||||||
|
foreach($date as $date_format => $date) {
|
||||||
|
expect(Date::convertDateToDatetime($date, $date_format))
|
||||||
|
->equals('2016-05-10 00:00:00');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanConvertMonthYearFormatToDatetime() {
|
||||||
|
$date = array(
|
||||||
|
'MM/YYYY' => '05/2016',
|
||||||
|
'YYYY/MM' => '2016/05'
|
||||||
|
);
|
||||||
|
foreach($date as $date_format => $date) {
|
||||||
|
expect(Date::convertDATEToDatetime($date, $date_format))
|
||||||
|
->equals('2016-05-01 00:00:00');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanConvertMonthToDatetime() {
|
||||||
|
expect(Date::convertDateToDatetime('05', 'MM'))
|
||||||
|
->equals('2016-05-01 00:00:00');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanConvertYearToDatetime() {
|
||||||
|
expect(Date::convertDateToDatetime('2016', 'YYYY'))
|
||||||
|
->equals('2016-01-01 00:00:00');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanConvertDatetimeToDatetime() {
|
||||||
|
expect(Date::convertDateToDatetime('2016-05-10 00:00:00', 'datetime'))
|
||||||
|
->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('');
|
||||||
|
}
|
||||||
|
}
|
@ -169,7 +169,8 @@ class SubscriberTest extends MailPoetTest {
|
|||||||
'name' => 'Birthday',
|
'name' => 'Birthday',
|
||||||
'type' => 'date',
|
'type' => 'date',
|
||||||
'params' => array(
|
'params' => array(
|
||||||
'date_type' => 'year_month_day'
|
'date_type' => 'year_month_day',
|
||||||
|
'date_format' => 'MM/DD/YYYY'
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -177,7 +178,8 @@ class SubscriberTest extends MailPoetTest {
|
|||||||
'name' => 'Registered on',
|
'name' => 'Registered on',
|
||||||
'type' => 'date',
|
'type' => 'date',
|
||||||
'params' => array(
|
'params' => array(
|
||||||
'date_type' => 'year_month'
|
'date_type' => 'year_month',
|
||||||
|
'date_format' => 'MM/YYYY'
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -199,7 +201,7 @@ class SubscriberTest extends MailPoetTest {
|
|||||||
expect($subscriber->email)->equals('user.with.cf@mailpoet.com');
|
expect($subscriber->email)->equals('user.with.cf@mailpoet.com');
|
||||||
expect($subscriber->{'cf_'.$custom_field->id})->equals('Paris');
|
expect($subscriber->{'cf_'.$custom_field->id})->equals('Paris');
|
||||||
// date specified as array gets converted to string
|
// date specified as array gets converted to string
|
||||||
expect($subscriber->{'cf_'.$custom_field_2->id})->equals('1984-03-09');
|
expect($subscriber->{'cf_'.$custom_field_2->id})->equals('1984-03-09 00:00:00');
|
||||||
// date specified as string is stored as is
|
// date specified as string is stored as is
|
||||||
expect($subscriber->{'cf_'.$custom_field_3->id})->equals('2013-07');
|
expect($subscriber->{'cf_'.$custom_field_3->id})->equals('2013-07');
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use MailPoet\Models\CustomField;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\Models\SubscriberCustomField;
|
use MailPoet\Models\SubscriberCustomField;
|
||||||
@ -9,6 +10,13 @@ use MailPoet\Util\Helpers;
|
|||||||
|
|
||||||
class ImportTest extends MailPoetTest {
|
class ImportTest extends MailPoetTest {
|
||||||
function _before() {
|
function _before() {
|
||||||
|
$custom_field = CustomField::create();
|
||||||
|
$custom_field->name = 'country';
|
||||||
|
$custom_field->type = 'text';
|
||||||
|
$custom_field->save();
|
||||||
|
$this->subscriber_custom_fields = array((string)$custom_field->id);
|
||||||
|
$this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1'));
|
||||||
|
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2'));
|
||||||
$this->data = array(
|
$this->data = array(
|
||||||
'subscribers' => array(
|
'subscribers' => array(
|
||||||
array(
|
array(
|
||||||
@ -25,13 +33,13 @@ class ImportTest extends MailPoetTest {
|
|||||||
)
|
)
|
||||||
),
|
),
|
||||||
'columns' => array(
|
'columns' => array(
|
||||||
'first_name' => 0,
|
'first_name' => array('index' => 0),
|
||||||
'last_name' => 1,
|
'last_name' => array('index' => 1),
|
||||||
'email' => 2,
|
'email' => array('index' => 2),
|
||||||
777 => 3
|
(string)$custom_field->id => array('index' => 3)
|
||||||
),
|
),
|
||||||
'segments' => array(
|
'segments' => array(
|
||||||
195
|
$this->segment_1->id
|
||||||
),
|
),
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
'updateSubscribers' => true
|
'updateSubscribers' => true
|
||||||
@ -41,10 +49,6 @@ class ImportTest extends MailPoetTest {
|
|||||||
'last_name',
|
'last_name',
|
||||||
'email'
|
'email'
|
||||||
);
|
);
|
||||||
$this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1'));
|
|
||||||
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2'));
|
|
||||||
|
|
||||||
$this->subscriber_custom_fields = array(777);
|
|
||||||
$this->import = new Import($this->data);
|
$this->import = new Import($this->data);
|
||||||
$this->subscribers_data = $this->import->transformSubscribersData(
|
$this->subscribers_data = $this->import->transformSubscribersData(
|
||||||
$this->data['subscribers'],
|
$this->data['subscribers'],
|
||||||
@ -63,13 +67,14 @@ class ImportTest extends MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItCanTransformSubscribers() {
|
function testItCanTransformSubscribers() {
|
||||||
|
$custom_field = $this->subscriber_custom_fields[0];
|
||||||
expect($this->import->subscribers_data['first_name'][0])
|
expect($this->import->subscribers_data['first_name'][0])
|
||||||
->equals($this->data['subscribers'][0][0]);
|
->equals($this->data['subscribers'][0][0]);
|
||||||
expect($this->import->subscribers_data['last_name'][0])
|
expect($this->import->subscribers_data['last_name'][0])
|
||||||
->equals($this->data['subscribers'][0][1]);
|
->equals($this->data['subscribers'][0][1]);
|
||||||
expect($this->import->subscribers_data['email'][0])
|
expect($this->import->subscribers_data['email'][0])
|
||||||
->equals($this->data['subscribers'][0][2]);
|
->equals($this->data['subscribers'][0][2]);
|
||||||
expect($this->import->subscribers_data['777'][0])
|
expect($this->import->subscribers_data[$custom_field][0])
|
||||||
->equals($this->data['subscribers'][0][3]);
|
->equals($this->data['subscribers'][0][3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,6 +247,7 @@ class ImportTest extends MailPoetTest {
|
|||||||
|
|
||||||
function testItCanCreateOrUpdateCustomFields() {
|
function testItCanCreateOrUpdateCustomFields() {
|
||||||
$subscribers_data = $this->subscribers_data;
|
$subscribers_data = $this->subscribers_data;
|
||||||
|
$custom_field = $this->subscriber_custom_fields[0];
|
||||||
$this->import->createOrUpdateSubscribers(
|
$this->import->createOrUpdateSubscribers(
|
||||||
'create',
|
'create',
|
||||||
$subscribers_data,
|
$subscribers_data,
|
||||||
@ -266,8 +272,8 @@ class ImportTest extends MailPoetTest {
|
|||||||
$subscriber_custom_fields = SubscriberCustomField::findArray();
|
$subscriber_custom_fields = SubscriberCustomField::findArray();
|
||||||
expect(count($subscriber_custom_fields))->equals(2);
|
expect(count($subscriber_custom_fields))->equals(2);
|
||||||
expect($subscriber_custom_fields[0]['value'])
|
expect($subscriber_custom_fields[0]['value'])
|
||||||
->equals($subscribers_data[777][0]);
|
->equals($subscribers_data[$custom_field][0]);
|
||||||
$subscribers_data[777][1] = 'Rio';
|
$subscribers_data[$custom_field][1] = 'Rio';
|
||||||
$this->import->createOrUpdateCustomFields(
|
$this->import->createOrUpdateCustomFields(
|
||||||
'update',
|
'update',
|
||||||
$db_subscribers,
|
$db_subscribers,
|
||||||
@ -276,7 +282,7 @@ class ImportTest extends MailPoetTest {
|
|||||||
);
|
);
|
||||||
$subscriber_custom_fields = SubscriberCustomField::findArray();
|
$subscriber_custom_fields = SubscriberCustomField::findArray();
|
||||||
expect($subscriber_custom_fields[1]['value'])
|
expect($subscriber_custom_fields[1]['value'])
|
||||||
->equals($subscribers_data[777][1]);
|
->equals($subscribers_data[$custom_field][1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -345,6 +351,7 @@ class ImportTest extends MailPoetTest {
|
|||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
||||||
|
ORM::raw_execute('TRUNCATE ' . CustomField::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table);
|
ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table);
|
||||||
}
|
}
|
||||||
}
|
}
|
102
views/form/custom_fields.html
Normal file
102
views/form/custom_fields.html
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
<!-- date settings and block templates -->
|
||||||
|
<%= partial('form_template_date_years',
|
||||||
|
'form/templates/blocks/date_years.hbs',
|
||||||
|
'_settings_date_years'
|
||||||
|
) %>
|
||||||
|
<%= partial('form_template_date_months',
|
||||||
|
'form/templates/blocks/date_months.hbs',
|
||||||
|
'_settings_date_months'
|
||||||
|
) %>
|
||||||
|
<%= partial('form_template_date_days',
|
||||||
|
'form/templates/blocks/date_days.hbs',
|
||||||
|
'_settings_date_days'
|
||||||
|
) %>
|
||||||
|
<%= partial('form_template_date', 'form/templates/blocks/date.hbs') %>
|
||||||
|
|
||||||
|
<!-- field settings -->
|
||||||
|
<%= partial('form_template_field_settings', 'form/templates/settings/field.hbs') %>
|
||||||
|
|
||||||
|
<%= partial('field_settings_label',
|
||||||
|
'form/templates/settings/label.hbs',
|
||||||
|
'_settings_label'
|
||||||
|
) %>
|
||||||
|
<%= partial('field_settings_label_within',
|
||||||
|
'form/templates/settings/label_within.hbs',
|
||||||
|
'_settings_label_within'
|
||||||
|
) %>
|
||||||
|
<%= partial('field_settings_required',
|
||||||
|
'form/templates/settings/required.hbs',
|
||||||
|
'_settings_required'
|
||||||
|
) %>
|
||||||
|
<%= partial('field_settings_validate',
|
||||||
|
'form/templates/settings/validate.hbs',
|
||||||
|
'_settings_validate'
|
||||||
|
) %>
|
||||||
|
<%= partial('field_settings_values',
|
||||||
|
'form/templates/settings/values.hbs',
|
||||||
|
'_settings_values'
|
||||||
|
) %>
|
||||||
|
<%= partial('field_settings_date_default',
|
||||||
|
'form/templates/settings/date_default.hbs',
|
||||||
|
'_settings_date_default'
|
||||||
|
) %>
|
||||||
|
<%= partial('field_settings_submit',
|
||||||
|
'form/templates/settings/submit.hbs',
|
||||||
|
'_settings_submit'
|
||||||
|
) %>
|
||||||
|
|
||||||
|
<%= partial('field_settings_values_item',
|
||||||
|
'form/templates/settings/values_item.hbs') %>
|
||||||
|
<%= partial(
|
||||||
|
'field_settings_date_format',
|
||||||
|
'form/templates/settings/date_formats.hbs',
|
||||||
|
'_settings_date_format'
|
||||||
|
) %>
|
||||||
|
<%= partial(
|
||||||
|
'field_settings_date_type',
|
||||||
|
'form/templates/settings/date_types.hbs',
|
||||||
|
'_settings_date_type'
|
||||||
|
) %>
|
||||||
|
<%= partial('field_settings_segment_selection_item',
|
||||||
|
'form/templates/settings/segment_selection_item.hbs'
|
||||||
|
) %>
|
||||||
|
<%= partial('field_settings_segment_selection',
|
||||||
|
'form/templates/settings/segment_selection.hbs',
|
||||||
|
'_settings_segment_selection'
|
||||||
|
) %>
|
||||||
|
|
||||||
|
<!-- custom field: new -->
|
||||||
|
<%= partial('form_template_field_form',
|
||||||
|
'form/templates/settings/field_form.hbs'
|
||||||
|
) %>
|
||||||
|
|
||||||
|
<!-- field settings depending on field type -->
|
||||||
|
<script id="form_template_field_text" type="text/x-handlebars-template">
|
||||||
|
{{> _settings_required }}
|
||||||
|
{{> _settings_validate }}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script id="form_template_field_textarea" type="text/x-handlebars-template">
|
||||||
|
{{> _settings_required }}
|
||||||
|
{{> _settings_validate }}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script id="form_template_field_radio" type="text/x-handlebars-template">
|
||||||
|
{{> _settings_values }}
|
||||||
|
{{> _settings_required }}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script id="form_template_field_checkbox" type="text/x-handlebars-template">
|
||||||
|
{{> _settings_values }}
|
||||||
|
{{> _settings_required }}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script id="form_template_field_select" type="text/x-handlebars-template">
|
||||||
|
{{> _settings_values }}
|
||||||
|
{{> _settings_required }}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script id="form_template_field_date" type="text/x-handlebars-template">
|
||||||
|
{{> _settings_required }}
|
||||||
|
{{> _settings_date_type }}
|
||||||
|
</script>
|
@ -685,110 +685,12 @@
|
|||||||
<%= partial('form_template_checkbox', 'form/templates/blocks/checkbox.hbs') %>
|
<%= partial('form_template_checkbox', 'form/templates/blocks/checkbox.hbs') %>
|
||||||
<%= partial('form_template_textarea', 'form/templates/blocks/textarea.hbs') %>
|
<%= partial('form_template_textarea', 'form/templates/blocks/textarea.hbs') %>
|
||||||
<%= partial('form_template_html', 'form/templates/blocks/html.hbs') %>
|
<%= partial('form_template_html', 'form/templates/blocks/html.hbs') %>
|
||||||
<%= partial('form_template_date_years',
|
|
||||||
'form/templates/blocks/date_years.hbs',
|
|
||||||
'_settings_date_years'
|
|
||||||
) %>
|
|
||||||
<%= partial('form_template_date_months',
|
|
||||||
'form/templates/blocks/date_months.hbs',
|
|
||||||
'_settings_date_months'
|
|
||||||
) %>
|
|
||||||
<%= partial('form_template_date_days',
|
|
||||||
'form/templates/blocks/date_days.hbs',
|
|
||||||
'_settings_date_days'
|
|
||||||
) %>
|
|
||||||
<%= partial('form_template_date', 'form/templates/blocks/date.hbs') %>
|
|
||||||
|
|
||||||
<!-- field settings -->
|
<!-- custom field settings and templates -->
|
||||||
<%= partial('form_template_field_settings', 'form/templates/settings/field.hbs') %>
|
<% include 'form/custom_fields.html' %>
|
||||||
|
|
||||||
<%= partial('field_settings_label',
|
|
||||||
'form/templates/settings/label.hbs',
|
|
||||||
'_settings_label'
|
|
||||||
) %>
|
|
||||||
<%= partial('field_settings_label_within',
|
|
||||||
'form/templates/settings/label_within.hbs',
|
|
||||||
'_settings_label_within'
|
|
||||||
) %>
|
|
||||||
<%= partial('field_settings_required',
|
|
||||||
'form/templates/settings/required.hbs',
|
|
||||||
'_settings_required'
|
|
||||||
) %>
|
|
||||||
<%= partial('field_settings_validate',
|
|
||||||
'form/templates/settings/validate.hbs',
|
|
||||||
'_settings_validate'
|
|
||||||
) %>
|
|
||||||
<%= partial('field_settings_values',
|
|
||||||
'form/templates/settings/values.hbs',
|
|
||||||
'_settings_values'
|
|
||||||
) %>
|
|
||||||
<%= partial('field_settings_date_default',
|
|
||||||
'form/templates/settings/date_default.hbs',
|
|
||||||
'_settings_date_default'
|
|
||||||
) %>
|
|
||||||
<%= partial('field_settings_submit',
|
|
||||||
'form/templates/settings/submit.hbs',
|
|
||||||
'_settings_submit'
|
|
||||||
) %>
|
|
||||||
|
|
||||||
<%= partial('field_settings_values_item',
|
|
||||||
'form/templates/settings/values_item.hbs') %>
|
|
||||||
<%= partial(
|
|
||||||
'field_settings_date_format',
|
|
||||||
'form/templates/settings/date_formats.hbs',
|
|
||||||
'_settings_date_format'
|
|
||||||
) %>
|
|
||||||
<%= partial(
|
|
||||||
'field_settings_date_type',
|
|
||||||
'form/templates/settings/date_types.hbs',
|
|
||||||
'_settings_date_type'
|
|
||||||
) %>
|
|
||||||
<%= partial('field_settings_segment_selection_item',
|
|
||||||
'form/templates/settings/segment_selection_item.hbs'
|
|
||||||
) %>
|
|
||||||
<%= partial('field_settings_segment_selection',
|
|
||||||
'form/templates/settings/segment_selection.hbs',
|
|
||||||
'_settings_segment_selection'
|
|
||||||
) %>
|
|
||||||
|
|
||||||
<!-- custom field: new -->
|
|
||||||
<%= partial('form_template_field_form',
|
|
||||||
'form/templates/settings/field_form.hbs'
|
|
||||||
) %>
|
|
||||||
|
|
||||||
<!-- form preview -->
|
<!-- form preview -->
|
||||||
<%= partial('mailpoet_form_preview_template',
|
<%= partial('mailpoet_form_preview_template',
|
||||||
'form/templates/preview.hbs'
|
'form/templates/preview.hbs'
|
||||||
) %>
|
) %>
|
||||||
|
|
||||||
<!-- field settings depending on field type -->
|
|
||||||
<script id="form_template_field_text" type="text/x-handlebars-template">
|
|
||||||
{{> _settings_required }}
|
|
||||||
{{> _settings_validate }}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script id="form_template_field_textarea" type="text/x-handlebars-template">
|
|
||||||
{{> _settings_required }}
|
|
||||||
{{> _settings_validate }}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script id="form_template_field_radio" type="text/x-handlebars-template">
|
|
||||||
{{> _settings_values }}
|
|
||||||
{{> _settings_required }}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script id="form_template_field_checkbox" type="text/x-handlebars-template">
|
|
||||||
{{> _settings_values }}
|
|
||||||
{{> _settings_required }}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script id="form_template_field_select" type="text/x-handlebars-template">
|
|
||||||
{{> _settings_values }}
|
|
||||||
{{> _settings_required }}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script id="form_template_field_date" type="text/x-handlebars-template">
|
|
||||||
{{> _settings_required }}
|
|
||||||
{{> _settings_date_type }}
|
|
||||||
</script>
|
|
||||||
<% endblock %>
|
<% endblock %>
|
@ -22,5 +22,7 @@
|
|||||||
.find('option:selected')
|
.find('option:selected')
|
||||||
.data('format'));
|
.data('format'));
|
||||||
});
|
});
|
||||||
|
// set default format
|
||||||
|
$('select[name="params[date_type]"]').trigger('change');
|
||||||
});
|
});
|
||||||
<{{!}}/script>
|
<{{!}}/script>
|
@ -43,8 +43,9 @@
|
|||||||
</select>
|
</select>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label><%= __("Field name:") %></label>
|
<label for="field_name"><%= __("Field name:") %></label>
|
||||||
<input
|
<input
|
||||||
|
id="field_name"
|
||||||
type="text"
|
type="text"
|
||||||
name="name"
|
name="name"
|
||||||
value="{{ name }}"
|
value="{{ name }}"
|
||||||
@ -100,7 +101,7 @@
|
|||||||
} else {
|
} else {
|
||||||
if(response.errors.length > 0) {
|
if(response.errors.length > 0) {
|
||||||
$(response.errors).each(function(i, error) {
|
$(response.errors).each(function(i, error) {
|
||||||
MailPoet.Notice.error(error);
|
MailPoet.Notice.error(error, {positionAfter: '#field_name'});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,7 @@
|
|||||||
'showDetails': __('Show more details'),
|
'showDetails': __('Show more details'),
|
||||||
'segmentSelectionRequired': __('Please select at least one list'),
|
'segmentSelectionRequired': __('Please select at least one list'),
|
||||||
'addNewList': __('Add new list'),
|
'addNewList': __('Add new list'),
|
||||||
|
'addNewField': __('Add new field'),
|
||||||
'addNewColumuserColumnsn': __('Add new list'),
|
'addNewColumuserColumnsn': __('Add new list'),
|
||||||
'userColumns': __('User fields'),
|
'userColumns': __('User fields'),
|
||||||
'selectedValueAlreadyMatched': __('The selected value is already matched to another field'),
|
'selectedValueAlreadyMatched': __('The selected value is already matched to another field'),
|
||||||
@ -65,7 +66,7 @@
|
|||||||
'november': __('November'),
|
'november': __('November'),
|
||||||
'december': __('December'),
|
'december': __('December'),
|
||||||
'noDateFieldMatch': __("Do not match as a 'date field' if most of the rows for that field return the same error"),
|
'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'),
|
'verifyDateMatch': __('Verify that the date in blue matches the original date'),
|
||||||
'pm': __('PM'),
|
'pm': __('PM'),
|
||||||
'am': __('AM'),
|
'am': __('AM'),
|
||||||
|
@ -92,7 +92,7 @@
|
|||||||
{{#show_and_match_columns .}}
|
{{#show_and_match_columns .}}
|
||||||
{{#.}}
|
{{#.}}
|
||||||
<th>
|
<th>
|
||||||
<select class="mailpoet_subscribers_column_data_match" id="{{column_id}}" data-column-id="{{column_id}}" id="column_{{@index}}">
|
<select class="mailpoet_subscribers_column_data_match" data-column-id="{{column_id}}" data-validation-rule="false" id="column_{{@index}}">
|
||||||
</th>
|
</th>
|
||||||
{{/.}}
|
{{/.}}
|
||||||
{{/show_and_match_columns}}
|
{{/show_and_match_columns}}
|
||||||
@ -157,57 +157,7 @@
|
|||||||
</form>
|
</form>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- New column template -->
|
<!-- New custom field logic -->
|
||||||
<script id="new_column_template" type="text/x-handlebars-template">
|
<% include 'form/custom_fields.html' %>
|
||||||
<p>
|
|
||||||
<label><%= __('Field type') %>:</label>
|
|
||||||
<select id="new_column_type" name="type">
|
|
||||||
<option value="">--</option>
|
|
||||||
<option value="input">
|
|
||||||
<%= __('Text Input') %>
|
|
||||||
</option>
|
|
||||||
<option value="textarea">
|
|
||||||
<%= __('Text Area') %>
|
|
||||||
</option>
|
|
||||||
<option value="radio">
|
|
||||||
<%= __('Radio buttons') %>
|
|
||||||
</option>
|
|
||||||
<option value="checkbox">
|
|
||||||
<%= __('Checkbox') %>
|
|
||||||
</option>
|
|
||||||
<option value="select">
|
|
||||||
<%= __('Select') %>
|
|
||||||
</option>
|
|
||||||
<option value="date">
|
|
||||||
<%= __('Date') %>
|
|
||||||
</option>
|
|
||||||
</select>
|
|
||||||
</p>
|
|
||||||
<p class="mailpoet_validation_error" data-error="type_required">
|
|
||||||
<%= __('Please select a type') %>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<label><%= __('Field name') %>:</label>
|
|
||||||
<input id="new_column_name" type="text" name="name" value="{{ name }}"/>
|
|
||||||
</p>
|
|
||||||
<p class="mailpoet_validation_error" data-error="name_required">
|
|
||||||
<%= __('Please specify a name') %>
|
|
||||||
</p>
|
|
||||||
<p class="mailpoet_validation_error" data-error="name_not_unique">
|
|
||||||
<%= __('This name is already taken') %>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<hr/>
|
|
||||||
|
|
||||||
<p class="mailpoet_align_right">
|
|
||||||
<input type="submit" value="<%= __('Done') %>" id="new_column_process"
|
|
||||||
class="button-primary "/>
|
|
||||||
<input type="submit" value="<%= __('Cancel') %>" id="new_column_cancel"
|
|
||||||
class="button-primary"/>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
</form>
|
|
||||||
</script>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
Reference in New Issue
Block a user