custom fields (create and update)
This commit is contained in:
@ -45,4 +45,33 @@ class CustomField extends Model {
|
|||||||
'subscriber_id'
|
'subscriber_id'
|
||||||
)->select_expr(MP_SUBSCRIBER_CUSTOM_FIELD_TABLE.'.value');
|
)->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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -29,21 +29,33 @@ class CustomFields {
|
|||||||
wp_send_json($result);
|
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) {
|
function get($id) {
|
||||||
$custom_field = CustomField::findOne($id);
|
$custom_field = CustomField::findOne($id);
|
||||||
@ -54,111 +66,4 @@ class CustomFields {
|
|||||||
wp_send_json($custom_field);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -200,17 +200,7 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var mailpoet_segments = <%= json_encode(segments) %>;
|
var mailpoet_segments = <%= json_encode(segments) %>;
|
||||||
|
|
||||||
jQuery(function($) {
|
var mailpoet_default_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'
|
|
||||||
});
|
|
||||||
|
|
||||||
var default_fields = [
|
|
||||||
{
|
{
|
||||||
id: 'divider',
|
id: 'divider',
|
||||||
name: "<%= __('Divider') %>",
|
name: "<%= __('Divider') %>",
|
||||||
@ -257,12 +247,13 @@
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
jQuery(function($) {
|
||||||
function mailpoet_form_fields() {
|
function mailpoet_form_fields() {
|
||||||
// form editor: default fields
|
// form editor: default fields
|
||||||
var template = Handlebars.compile(jQuery('#form_template_fields').html());
|
var template = Handlebars.compile(jQuery('#form_template_fields').html());
|
||||||
|
|
||||||
var data = {
|
var data = {
|
||||||
fields: default_fields
|
fields: mailpoet_default_fields
|
||||||
};
|
};
|
||||||
|
|
||||||
return MailPoet.Ajax.post({
|
return MailPoet.Ajax.post({
|
||||||
@ -276,6 +267,16 @@
|
|||||||
jQuery('#mailpoet_toolbar_fields').html(template(data));
|
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
|
// toolbar sections
|
||||||
$(document).on('click', '.mailpoet_toolbar_section.closed', function() {
|
$(document).on('click', '.mailpoet_toolbar_section.closed', function() {
|
||||||
@ -478,15 +479,26 @@
|
|||||||
|
|
||||||
// edit field
|
// edit field
|
||||||
$(document).on('click', '.mailpoet_form_field_edit', function() {
|
$(document).on('click', '.mailpoet_form_field_edit', function() {
|
||||||
var field = $(this).data('field');
|
var id = $(this).data('id');
|
||||||
|
|
||||||
|
MailPoet.Ajax.post({
|
||||||
|
endpoint: 'customFields',
|
||||||
|
action: 'get',
|
||||||
|
data: id
|
||||||
|
}).done(function(response) {
|
||||||
|
if(response !== false) {
|
||||||
MailPoet.Modal.popup({
|
MailPoet.Modal.popup({
|
||||||
title: "<%= __('Edit field') %>",
|
title: "<%= __('Edit field') %>",
|
||||||
template: $('#form_template_field_new').html(),
|
template: $('#form_template_field_new').html(),
|
||||||
//url: mailpoet_api_url('subscriber_get_meta.php?field='+field),
|
data: response
|
||||||
onSuccess: function(data) {
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/*onSuccess: function(data) {
|
||||||
console.log('TODO: field->edit');
|
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
|
// update field in form
|
||||||
var block_id = $('.mailpoet_form_block[wysija_field="'+field+'"]');
|
var block_id = $('.mailpoet_form_block[wysija_field="'+field+'"]');
|
||||||
if(block_id.length > 0) {
|
if(block_id.length > 0) {
|
||||||
@ -497,14 +509,13 @@
|
|||||||
}
|
}
|
||||||
// toggle widgets
|
// toggle widgets
|
||||||
WysijaForm.toggleWidgets();
|
WysijaForm.toggleWidgets();
|
||||||
});*/
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
}*/
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// delete field
|
// delete field
|
||||||
$(document).on('click', '.mailpoet_form_field_delete', function() {
|
$(document).on('click', '.mailpoet_form_field_delete', function() {
|
||||||
|
|
||||||
var id = $(this).data('id');
|
var id = $(this).data('id');
|
||||||
var item = $(this).parent();
|
var item = $(this).parent();
|
||||||
var name = $(this).siblings('.mailpoet_form_field').attr('wysija_name');
|
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_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', 'form/templates/blocks/date.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_years',
|
||||||
<%= partial('form_template_date_months', 'form/templates/blocks/date_months.hbs') %>
|
'form/templates/blocks/date_years.hbs',
|
||||||
<%= partial('form_template_date_days', 'form/templates/blocks/date_days.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 -->
|
<!-- field settings -->
|
||||||
<%= partial('form_template_field_settings', 'form/templates/settings/field.hbs') %>
|
<%= partial('form_template_field_settings', 'form/templates/settings/field.hbs') %>
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
|
|
||||||
<form id="form_field_new" name="form_field_new" action="" method="post">
|
<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>
|
<p>
|
||||||
<label for="type"><%= __('Select a field type:') %></label>
|
<label for="field_type"><%= __('Select a field type:') %></label>
|
||||||
<select id="type" name="type">
|
<select id="field_type" name="type">
|
||||||
<option value="">--</option>
|
<option value="">--</option>
|
||||||
<option
|
<option
|
||||||
{{#ifCond type '==' 'input'}}selected="selected"{{/ifCond}}
|
{{#ifCond type '==' 'input'}}selected="selected"{{/ifCond}}
|
||||||
@ -51,57 +51,58 @@
|
|||||||
loadFieldForm();
|
loadFieldForm();
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#form_field_new').on('submit', function() {
|
$('#form_field_new').on('submit', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
// get data
|
// get data
|
||||||
var data = $(this).serializeObject();
|
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
|
} else {
|
||||||
$('.mailpoet_error').hide();
|
if(response.errors.length > 0) {
|
||||||
|
for(error in response.errors) {
|
||||||
console.log(('TODO: subscriber->meta->edit'));
|
MailPoet.Notice.error(error);
|
||||||
|
}
|
||||||
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)
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// close popup
|
|
||||||
MailPoet.Modal.success();
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#form_field_new #type').on('change', function() {
|
// close popup
|
||||||
if($(this).val() !== '') {
|
return MailPoet.Modal.success();
|
||||||
$('.mailpoet_error[data-error="type_required"]').hide();
|
});
|
||||||
}
|
|
||||||
// load template based on type
|
$('#form_field_new #field_type').on('change', function() {
|
||||||
loadFieldForm($(this).val());
|
loadFieldForm($(this).val());
|
||||||
});
|
});
|
||||||
|
|
||||||
function loadFieldForm(type) {
|
function loadFieldForm(type) {
|
||||||
type = (type === undefined) ? $('#form_field_new #type').val() : type;
|
type = (type === undefined) ? $('#form_field_new #field_type').val() : type;
|
||||||
if(type !== '') {
|
if(type !== '') {
|
||||||
var template = Handlebars.compile($('#form_template_field_'+type).html()),
|
var template = Handlebars.compile($('#form_template_field_'+type).html()),
|
||||||
data = {type: type},
|
data = {type: type},
|
||||||
field = $('#form_field_new #field').val();
|
field_id = $('#form_field_new #field_id').val();
|
||||||
|
|
||||||
if(field !== undefined && field.length > 0) {
|
if(field_id !== undefined && field_id.length > 0) {
|
||||||
var params = $('.mailpoet_form_field[wysija_field="'+field+'"]').attr('wysija_params');
|
var params = $('.mailpoet_form_field[wysija_id="'+field_id+'"]').attr('wysija_params');
|
||||||
|
if(params !== undefined) {
|
||||||
data.params = JSON.parse(params);
|
data.params = JSON.parse(params);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// render field template
|
// render field template
|
||||||
$('#form_field_new .field_type_form').html(template(data));
|
$('#form_field_new .field_type_form').html(template(data));
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user