custom fields (create and update)

This commit is contained in:
Jonathan Labreuille
2015-11-05 19:43:02 +01:00
parent b12f7f29de
commit 1ce4b16327
4 changed files with 185 additions and 230 deletions

View File

@ -45,4 +45,33 @@ class CustomField extends Model {
'subscriber_id'
)->select_expr(MP_SUBSCRIBER_CUSTOM_FIELD_TABLE.'.value');
}
static function createOrUpdate($data = array()) {
$custom_field = false;
if(isset($data['id']) && (int)$data['id'] > 0) {
$custom_field = self::findOne((int)$data['id']);
}
// set name as label by default
if(empty($data['params']['label'])) {
$data['params']['label'] = $data['name'];
}
if($custom_field === false) {
$custom_field = self::create();
$custom_field->hydrate($data);
} else {
unset($data['id']);
$custom_field->set($data);
}
try {
$custom_field->save();
return $custom_field;
} catch(Exception $e) {
return $custom_field->getValidationErrors();
}
return false;
}
}

View File

@ -29,21 +29,33 @@ class CustomFields {
wp_send_json($result);
}
function save($data = array()) {
$custom_field = CustomField::createOrUpdate($data);
if($custom_field === false) {
$result = array(
'result' => false,
'errors' => array(
__('The custom field could not be created.')
)
);
} else {
$errors = $custom_field->getValidationErrors();
if(!empty($errors)) {
$result = array(
'result' => false,
'errors' => $errors
);
} else {
$result = array(
'result' => true,
'field' => $custom_field->asArray()
);
}
}
wp_send_json($result);
}
function get($id) {
$custom_field = CustomField::findOne($id);
@ -54,111 +66,4 @@ class CustomFields {
wp_send_json($custom_field);
}
}
function create() {
// create new form
$custom_field_data = array(
'name' => __('New form'),
'body' => array(
array(
'name' => __('Email'),
'type' => 'input',
'field' => 'email',
'static' => true,
'params' => array(
'label' => __('Email'),
'required' => true
)
),
array(
'name' => __('Submit'),
'type' => 'submit',
'field' => 'submit',
'static' => true,
'params' => array(
'label' => __('Subscribe!')
)
)
),
'settings' => array(
'on_success' => 'message',
'success_message' => __('Check your inbox or spam folder now to confirm your subscription.'),
'segments' => null,
'segments_selected_by' => 'admin'
)
);
$custom_field = CustomField::createOrUpdate($custom_field_data);
if($custom_field !== false && $custom_field->id()) {
wp_send_json(
admin_url('admin.php?page=mailpoet-form-editor&id='.$custom_field->id())
);
} else {
wp_send_json(false);
}
}
function save($data = array()) {
$custom_field = CustomField::createOrUpdate($data);
if($custom_field !== false && $custom_field->id()) {
wp_send_json($custom_field->id());
} else {
wp_send_json($custom_field);
}
}
function restore($id) {
$result = false;
$custom_field = CustomField::findOne($id);
if($custom_field !== false) {
$result = $custom_field->restore();
}
wp_send_json($result);
}
function trash($id) {
$result = false;
$custom_field = CustomField::findOne($id);
if($custom_field !== false) {
$result = $custom_field->trash();
}
wp_send_json($result);
}
}
}

View File

@ -200,69 +200,60 @@
<script type="text/javascript">
var mailpoet_segments = <%= json_encode(segments) %>;
var mailpoet_default_fields = [
{
id: 'divider',
name: "<%= __('Divider') %>",
type: 'divider',
multiple: true,
readonly: true
},
{
id: "first_name",
name: "<%= __('First name') %>",
type: 'input',
params: {
label: "<%= __('First name') %>"
},
readonly: true
},
{
id: "last_name",
name: "<%= __('Last name') %>",
type: 'input',
params: {
label: "<%= __('Last name') %>"
},
readonly: true
},
{
id: "segments",
name: "<%= __('List selection') %>",
type: 'segment',
params: {
label: "<%= __('Select list(s):') %>"
},
readonly: true
},
{
id: 'html',
name: "<%= __('Random text or HTML') %>",
type: 'html',
params: {
text: "<%= __('Subscribe to our newsletter and join our [mailpoet_subscribers_count] subscribers.') %>"
},
multiple: true,
readonly: true
}
];
jQuery(function($) {
// enable code mirror editor on styles textarea
MailPoet.CodeEditor = CodeMirror.fromTextArea(document.getElementById('mailpoet_form_styles'), {
lineNumbers: true,
tabMode: "indent",
matchBrackets: true,
theme: 'neo',
mode: 'css'
});
var default_fields = [
{
id: 'divider',
name: "<%= __('Divider') %>",
type: 'divider',
multiple: true,
readonly: true
},
{
id: "first_name",
name: "<%= __('First name') %>",
type: 'input',
params: {
label: "<%= __('First name') %>"
},
readonly: true
},
{
id: "last_name",
name: "<%= __('Last name') %>",
type: 'input',
params: {
label: "<%= __('Last name') %>"
},
readonly: true
},
{
id: "segments",
name: "<%= __('List selection') %>",
type: 'segment',
params: {
label: "<%= __('Select list(s):') %>"
},
readonly: true
},
{
id: 'html',
name: "<%= __('Random text or HTML') %>",
type: 'html',
params: {
text: "<%= __('Subscribe to our newsletter and join our [mailpoet_subscribers_count] subscribers.') %>"
},
multiple: true,
readonly: true
}
];
function mailpoet_form_fields() {
// form editor: default fields
var template = Handlebars.compile(jQuery('#form_template_fields').html());
var data = {
fields: default_fields
fields: mailpoet_default_fields
};
return MailPoet.Ajax.post({
@ -276,6 +267,16 @@
jQuery('#mailpoet_toolbar_fields').html(template(data));
});
}
window.mailpoet_form_fields = mailpoet_form_fields;
// enable code mirror editor on styles textarea
MailPoet.CodeEditor = CodeMirror.fromTextArea(document.getElementById('mailpoet_form_styles'), {
lineNumbers: true,
tabMode: "indent",
matchBrackets: true,
theme: 'neo',
mode: 'css'
});
// toolbar sections
$(document).on('click', '.mailpoet_toolbar_section.closed', function() {
@ -478,15 +479,26 @@
// edit field
$(document).on('click', '.mailpoet_form_field_edit', function() {
var field = $(this).data('field');
var id = $(this).data('id');
MailPoet.Modal.popup({
title: "<%= __('Edit field') %>",
template: $('#form_template_field_new').html(),
//url: mailpoet_api_url('subscriber_get_meta.php?field='+field),
onSuccess: function(data) {
MailPoet.Ajax.post({
endpoint: 'customFields',
action: 'get',
data: id
}).done(function(response) {
if(response !== false) {
MailPoet.Modal.popup({
title: "<%= __('Edit field') %>",
template: $('#form_template_field_new').html(),
data: response
});
}
});
/*onSuccess: function(data) {
console.log('TODO: field->edit');
/*mailpoet_get_json('subscriber_get_meta.php', { field: data.field }, function(data) {
mailpoet_get_json('subscriber_get_meta.php', { field: data.field }, function(data) {
// update field in form
var block_id = $('.mailpoet_form_block[wysija_field="'+field+'"]');
if(block_id.length > 0) {
@ -497,14 +509,13 @@
}
// toggle widgets
WysijaForm.toggleWidgets();
});*/
}
});
});
}*/
});
// delete field
$(document).on('click', '.mailpoet_form_field_delete', function() {
var id = $(this).data('id');
var item = $(this).parent();
var name = $(this).siblings('.mailpoet_form_field').attr('wysija_name');
@ -577,9 +588,18 @@
<%= partial('form_template_textarea', 'form/templates/blocks/textarea.hbs') %>
<%= partial('form_template_html', 'form/templates/blocks/html.hbs') %>
<%= partial('form_template_date', 'form/templates/blocks/date.hbs') %>
<%= partial('form_template_date_years', 'form/templates/blocks/date_years.hbs') %>
<%= partial('form_template_date_months', 'form/templates/blocks/date_months.hbs') %>
<%= partial('form_template_date_days', 'form/templates/blocks/date_days.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'
) %>
<!-- field settings -->
<%= partial('form_template_field_settings', 'form/templates/settings/field.hbs') %>

View File

@ -1,9 +1,9 @@
<form id="form_field_new" name="form_field_new" action="" method="post">
{{#if field}}<input type="hidden" id="field" name="field" value="{{ field }}" />{{/if}}
{{#if id}}<input type="hidden" id="field_id" name="id" value="{{ id }}" />{{/if}}
<p>
<label for="type"><%= __('Select a field type:') %></label>
<select id="type" name="type">
<label for="field_type"><%= __('Select a field type:') %></label>
<select id="field_type" name="type">
<option value="">--</option>
<option
{{#ifCond type '==' 'input'}}selected="selected"{{/ifCond}}
@ -51,56 +51,57 @@
loadFieldForm();
});
$('#form_field_new').on('submit', function() {
$('#form_field_new').on('submit', function(e) {
e.preventDefault();
// get data
var data = $(this).serializeObject();
MailPoet.Ajax.post({
endpoint: 'customFields',
action: 'save',
data: data
}).done(function(response) {
if(response.result === true) {
mailpoet_form_fields();
if(data.id) {
MailPoet.Notice.success(
"<%= __('Updated custom field “"+data.name+"“') %>"
);
} else {
MailPoet.Notice.success(
"<%= __('Added custom field “"+data.name+"“') %>"
);
}
// hide errors
$('.mailpoet_error').hide();
console.log(('TODO: subscriber->meta->edit'));
console.log(data);
// mailpoet_post_json('subscriber_extend.php', data, function(response) {
// if(response.error !== undefined) {
// // there's an error!
// $('.mailpoet_error[data-error="'+response.error+'"]').show();
// } else {
// // we should get the fields list in the response
// if(response.fields !== undefined) {
// // get fields (defaults to empty array)
// var fields = response.fields || [];
// // refresh toolbar
// mailpoet_form_fields({
// fields: $.merge(fields, default_fields)
// });
// }
} else {
if(response.errors.length > 0) {
for(error in response.errors) {
MailPoet.Notice.error(error);
}
}
}
});
// close popup
MailPoet.Modal.success();
// }
// });
return false;
return MailPoet.Modal.success();
});
$('#form_field_new #type').on('change', function() {
if($(this).val() !== '') {
$('.mailpoet_error[data-error="type_required"]').hide();
}
// load template based on type
$('#form_field_new #field_type').on('change', function() {
loadFieldForm($(this).val());
});
function loadFieldForm(type) {
type = (type === undefined) ? $('#form_field_new #type').val() : type;
type = (type === undefined) ? $('#form_field_new #field_type').val() : type;
if(type !== '') {
var template = Handlebars.compile($('#form_template_field_'+type).html()),
data = {type: type},
field = $('#form_field_new #field').val();
field_id = $('#form_field_new #field_id').val();
if(field !== undefined && field.length > 0) {
var params = $('.mailpoet_form_field[wysija_field="'+field+'"]').attr('wysija_params');
data.params = JSON.parse(params);
if(field_id !== undefined && field_id.length > 0) {
var params = $('.mailpoet_form_field[wysija_id="'+field_id+'"]').attr('wysija_params');
if(params !== undefined) {
data.params = JSON.parse(params);
}
}
// render field template
$('#form_field_new .field_type_form').html(template(data));