Merge pull request #261 from mailpoet/form_editor_round_1

Form editor round 1
This commit is contained in:
Marco
2015-12-09 14:07:33 +01:00
25 changed files with 242 additions and 154 deletions

View File

@ -5,13 +5,15 @@
@require 'common' @require 'common'
@require 'modal' @require 'modal'
@require 'notice' @require 'notice'
@require 'parsley'
@require 'form_editor' @require 'form_editor'
@require 'listing' @require 'listing'
@require 'box' @require 'box'
@require 'breadcrumb' @require 'breadcrumb'
@require 'form' @require 'form'
@require 'parsley'
@require 'form_validation'
@require 'settings' @require 'settings'
@require 'progress_bar' @require 'progress_bar'

View File

@ -4,6 +4,9 @@
icons = '../img/form_editor_icons.png' icons = '../img/form_editor_icons.png'
handle_icon = '../img/handle.png' handle_icon = '../img/handle.png'
#mailpoet_form_name
font-size: 23px
#mailpoet_form_history #mailpoet_form_history
display: none display: none
@ -99,6 +102,7 @@ handle_icon = '../img/handle.png'
/* MailPoet Form wrapper */ /* MailPoet Form wrapper */
#mailpoet_form_wrapper #mailpoet_form_wrapper
position: relative position: relative
margin: 20px 0 0 0
/* MailPoet Form container */ /* MailPoet Form container */
#mailpoet_form_container #mailpoet_form_container

View File

@ -0,0 +1,6 @@
.parsley-errors-list
margin-top: 8px
.parsley-required
.parsley-custom-error-message
color: #b94a48

View File

@ -1,3 +1,4 @@
@import 'nib' @import 'nib'
@require 'parsley' @require 'parsley'
@require 'form_validation'

View File

@ -231,7 +231,7 @@ var WysijaHistory = {
/* MailPoet Form */ /* MailPoet Form */
var WysijaForm = { var WysijaForm = {
version: '0.6', version: '0.7',
options: { options: {
container: 'mailpoet_form_container', container: 'mailpoet_form_container',
editor: 'mailpoet_form_editor', editor: 'mailpoet_form_editor',
@ -317,6 +317,7 @@ var WysijaForm = {
save: function() { save: function() {
var position = 1, var position = 1,
data = { data = {
'name': $F('mailpoet_form_name'),
'settings': $('mailpoet_form_settings').serialize(true), 'settings': $('mailpoet_form_settings').serialize(true),
'body': [], 'body': [],
'styles': (MailPoet.CodeEditor !== undefined) ? MailPoet.CodeEditor.getValue() : null 'styles': (MailPoet.CodeEditor !== undefined) ? MailPoet.CodeEditor.getValue() : null

View File

@ -20,7 +20,10 @@ function(
$('form.mailpoet_form').each(function() { $('form.mailpoet_form').each(function() {
var form = $(this); var form = $(this);
form.parsley().on('form:submit', function(parsley) { form.parsley({
errorsWrapper: '<p></p>',
errorTemplate: '<span></span>'
}).on('form:submit', function(parsley) {
var data = form.serializeObject() || {}; var data = form.serializeObject() || {};

View File

@ -42,11 +42,7 @@ define(
return ( return (
<div> <div>
<h2 className="title"> <h2 className="title">
Segment <a Segment
href="javascript:;"
className="add-new-h2"
onClick={ this.history.goBack }
>Back to list</a>
</h2> </h2>
<Form <Form

View File

@ -70,11 +70,7 @@ define(
return ( return (
<div> <div>
<h2 className="title"> <h2 className="title">
Subscriber <a Subscriber
href="javascript:;"
className="add-new-h2"
onClick={ this.history.goBack }
>Back to list</a>
</h2> </h2>
<Form <Form

View File

@ -28,6 +28,7 @@ class Initializer {
$this->setupChangelog(); $this->setupChangelog();
$this->setupPublicAPI(); $this->setupPublicAPI();
$this->runQueueSupervisor(); $this->runQueueSupervisor();
$this->setupShortcodes();
$this->setupHooks(); $this->setupHooks();
$this->setupImages(); $this->setupImages();
} }
@ -125,6 +126,10 @@ class Initializer {
$changelog->init(); $changelog->init();
} }
function setupShortcodes() {
$shortcodes = new Shortcodes();
$shortcodes->init();
}
function setupHooks() { function setupHooks() {
$hooks = new Hooks(); $hooks = new Hooks();
$hooks->init(); $hooks->init();

View File

@ -374,7 +374,8 @@ class Menu {
->findArray(), ->findArray(),
'styles' => FormRenderer::getStyles($form), 'styles' => FormRenderer::getStyles($form),
'date_types' => Block\Date::getDateTypes(), 'date_types' => Block\Date::getDateTypes(),
'date_formats' => Block\Date::getDateFormats() 'date_formats' => Block\Date::getDateFormats(),
'month_names' => Block\Date::getMonthNames()
); );
echo $this->renderer->render('form/editor.html', $data); echo $this->renderer->render('form/editor.html', $data);

26
lib/Config/Shortcodes.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace MailPoet\Config;
class Shortcodes {
function __construct() {
}
function init() {
// form widget shortcode
add_shortcode('mailpoet_form', array($this, 'formWidget'));
add_shortcode('wysija_form', array($this, 'formWidget'));
}
function formWidget($params = array()) {
// IMPORTANT: fixes conflict with MagicMember
remove_shortcode('user_list');
if(isset($params['id']) && (int)$params['id'] > 0) {
$form_widget = new \MailPoet\Form\Widget();
return $form_widget->widget(array(
'form' => (int)$params['id'],
'form_type' => 'shortcode'
));
}
}
}

View File

@ -29,7 +29,13 @@ abstract class Base {
} }
} }
$validation = ''; if($block['type'] === 'radio') {
$rules['group'] = 'custom_field_'.$block['id'];
$rules['errors-container'] = '.mailpoet_error_'.$block['id'];
$rules['required-message'] = __('You need to select at least one option.');
}
$validation = array();
if(!empty($rules)) { if(!empty($rules)) {
$rules = array_unique($rules); $rules = array_unique($rules);
@ -37,10 +43,10 @@ abstract class Base {
if(is_bool($value)) { if(is_bool($value)) {
$value = ($value) ? 'true' : 'false'; $value = ($value) ? 'true' : 'false';
} }
$validation .= 'data-parsley-'.$rule.'="'.$value.'"'; $validation[] = 'data-parsley-'.$rule.'="'.$value.'"';
} }
} }
return $validation; return join(' ', $validation);
} }
protected static function renderLabel($block) { protected static function renderLabel($block) {
@ -86,7 +92,11 @@ abstract class Base {
// return field name depending on block data // return field name depending on block data
protected static function getFieldName($block = array()) { protected static function getFieldName($block = array()) {
return $block['id']; if((int)$block['id'] > 0) {
return 'cf_'.$block['id'];
} else {
return $block['id'];
}
} }
protected static function getFieldLabel($block = array()) { protected static function getFieldLabel($block = array()) {
@ -98,6 +108,6 @@ abstract class Base {
protected static function getFieldValue($block = array()) { protected static function getFieldValue($block = array()) {
return (isset($block['params']['value']) return (isset($block['params']['value'])
&& strlen(trim($block['params']['value'])) > 0) && strlen(trim($block['params']['value'])) > 0)
? trim($block['params']['value']) : ''; ? esc_attr(trim($block['params']['value'])) : '';
} }
} }

View File

@ -9,14 +9,12 @@ class Radio extends Base {
$field_name = static::getFieldName($block); $field_name = static::getFieldName($block);
$field_validation = static::getInputValidation($block); $field_validation = static::getInputValidation($block);
// TODO: check if it still makes sense
// create hidden default value
// $html .= '<input type="hidden"name="'.$field_name.'" value="0" '.static::getInputValidation($block).'/>';
$html .= '<p class="mailpoet_paragraph">'; $html .= '<p class="mailpoet_paragraph">';
$html .= static::renderLabel($block); $html .= static::renderLabel($block);
$html .= '<span class="mailpoet_error_'.$block['id'].'"></span>';
foreach($block['params']['values'] as $option) { foreach($block['params']['values'] as $option) {
$html .= '<label class="mailpoet_radio_label">'; $html .= '<label class="mailpoet_radio_label">';
@ -24,13 +22,13 @@ class Radio extends Base {
$html .= 'name="'.$field_name.'" '; $html .= 'name="'.$field_name.'" ';
$html .= 'value="'.$option['value'].'" '; $html .= 'value="'.esc_attr($option['value']).'" ';
$html .= (isset($option['is_checked']) && $option['is_checked']) $html .= (isset($option['is_checked']) && $option['is_checked'])
? 'checked="checked"' : ''; ? 'checked="checked"' : '';
$html .= $field_validation; $html .= $field_validation;
$html .= ' />&nbsp;'.$option['value']; $html .= ' />&nbsp;'.esc_attr($option['value']);
$html .= '</label>'; $html .= '</label>';
} }

View File

@ -36,9 +36,9 @@ class Widget extends \WP_Widget {
return parent::__construct( return parent::__construct(
'mailpoet_form', 'mailpoet_form',
__("MailPoet Subscription Form"), __('MailPoet Form'),
array( array(
'title' => __("Newsletter subscription form"), 'description' => __('Add a newsletter subscription form.')
) )
); );
} }
@ -196,24 +196,6 @@ class Widget extends \WP_Widget {
} }
} }
// mailpoet shortcodes
// form shortcode
add_shortcode('mailpoet_form', 'mailpoet_form_shortcode');
add_shortcode('wysija_form', 'mailpoet_form_shortcode');
function mailpoet_form_shortcode($params = array()) {
// IMPORTANT: this is to make sure MagicMember won't scan our form and find [user_list] as a code they should replace.
remove_shortcode('user_list');
if(isset($params['id']) && (int)$params['id'] > 0) {
$form_widget = new \MailPoet\Form\Widget();
return $form_widget->widget(array(
'form' => (int)$params['id'],
'form_type' => 'shortcode'
));
}
}
// set the content filter to replace the shortcode // set the content filter to replace the shortcode
if(isset($_GET['mailpoet_page']) && strlen(trim($_GET['mailpoet_page'])) > 0) { if(isset($_GET['mailpoet_page']) && strlen(trim($_GET['mailpoet_page'])) > 0) {
switch($_GET['mailpoet_page']) { switch($_GET['mailpoet_page']) {

View File

@ -70,7 +70,7 @@ class MailPoet {
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'Authorization' => $this->auth() 'Authorization' => $this->auth()
), ),
'body' => json_encode($body) 'body' => $body
); );
} }
} }

View File

@ -139,6 +139,7 @@ class Forms {
function saveEditor($data = array()) { function saveEditor($data = array()) {
$form_id = (isset($data['id']) ? (int)$data['id'] : 0); $form_id = (isset($data['id']) ? (int)$data['id'] : 0);
$name = (isset($data['name']) ? $data['name'] : array());
$body = (isset($data['body']) ? $data['body'] : array()); $body = (isset($data['body']) ? $data['body'] : array());
$settings = (isset($data['settings']) ? $data['settings'] : array()); $settings = (isset($data['settings']) ? $data['settings'] : array());
$styles = (isset($data['styles']) ? $data['styles'] : array()); $styles = (isset($data['styles']) ? $data['styles'] : array());
@ -187,6 +188,7 @@ class Forms {
$form = Form::createOrUpdate(array( $form = Form::createOrUpdate(array(
'id' => $form_id, 'id' => $form_id,
'name' => $name,
'body' => $body, 'body' => $body,
'settings' => $settings, 'settings' => $settings,
'styles' => $styles 'styles' => $styles

View File

@ -4,6 +4,7 @@ namespace MailPoet\Router;
use MailPoet\Listing; use MailPoet\Listing;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment; use MailPoet\Models\SubscriberSegment;
use MailPoet\Models\SubscriberCustomField;
use MailPoet\Models\Segment; use MailPoet\Models\Segment;
use MailPoet\Models\Setting; use MailPoet\Models\Setting;
use MailPoet\Models\Form; use MailPoet\Models\Form;
@ -140,32 +141,37 @@ class Subscribers {
? 'unconfirmed' : 'subscribed' ? 'unconfirmed' : 'subscribed'
); );
// // set custom fields // custom fields
// $meta_fields = $mailpoet->getOption('mailpoet_subscriber_meta', array()); $custom_fields = array();
// if(!empty($meta_fields)) { foreach($data as $key => $value) {
// // loop through data to see if any meta field has been passed if(strpos($key, 'cf_') === 0) {
// foreach($meta_fields as $field => $field_data) { $custom_fields[substr($key, 3)] = $value;
// // check if it's a mandatory field unset($data[$key]);
// $is_required = (isset($field_data['params']['required']) && (bool)$field_data['params']['required'] === true); }
}
// if(array_key_exists($field, $data)) {
// // check if it's a mandatory field
// if($is_required === true && empty($data[$field])) {
// // if it's missing, throw an error
// $errors[] = sprintf(__('&quot;%s&quot; is required'), $field_data['name']);
// } else {
// // assign field to subscriber
// $subscriber[$field] = $data[$field];
// }
// }
// }
// }
// insert new subscriber // insert new subscriber
$subscriber = Subscriber::createOrUpdate($data); $subscriber = Subscriber::createOrUpdate($data);
if($subscriber === false || !$subscriber->id()) { if($subscriber === false || !$subscriber->id()) {
$errors = array_merge($errors, $subscriber->getValidationErrors()); $errors = array_merge($errors, $subscriber->getValidationErrors());
} else {
// add custom fields
if(!empty($custom_fields)) {
foreach($custom_fields as $custom_field_id => $value) {
if(is_array($value)) {
// date
$value = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
}
$subscriber_custom_field = SubscriberCustomField::create();
$subscriber_custom_field->hydrate(array(
'subscriber_id' => $subscriber->id(),
'custom_field_id' => $custom_field_id,
'value' => $value
));
$subscriber_custom_field->save();
}
}
} }
} else { } else {
$subscriber->set('status', ( $subscriber->set('status', (

View File

@ -2,17 +2,12 @@
<% block title %> <% block title %>
<h2 class="title"> <h2 class="title">
<span id="mailpoet_form_name"><%= form.name %></span> <input
<input id="mailpoet_form_name_input" type="text" value="" style="display:none;" /> type="text"
<a placeholder="<%= __('Click to change the form name!') %>"
id="mailpoet_form_edit_name" id="mailpoet_form_name"
class="add-new-h2" value="<%= form.name %>"
href="javascript:;" />
><%= __('Edit name' ) %></a>
<a
href="<%= admin_url('admin.php?page=mailpoet-forms') | raw %>"
class="add-new-h2"
><%= __('List of forms' ) %></a>
</h2> </h2>
<% endblock %> <% endblock %>
@ -122,29 +117,36 @@
<!-- Toolbar: Shortcodes / Export --> <!-- Toolbar: Shortcodes / Export -->
<div class="mailpoet_toolbar_section closed" data-section="shortcodes"> <div class="mailpoet_toolbar_section closed" data-section="shortcodes">
<a href="javascript:;" class="mailpoet_toggle"><br /></a> <a href="javascript:;" class="mailpoet_toggle"><br /></a>
<h3><%= __('Shortcodes') %></h3> <h3><%= __('Form Placement') %></h3>
<div> <div>
<!-- Form export links --> <!-- Form export links -->
<p> <p>
<%= __("You can easily add this form to your theme's in the [link]Widgets area[/link]") <%= __("Add this form to your sidebar or footer in the [link]Widgets[/link].")
| replace({ | replace({
'[link]': '<a href="widgets.php" target="_blank">', '[link]': '<a href="widgets.php" target="_blank">',
'[/link]': '</a>' '[/link]': '</a>'
}) })
| raw | raw
%> %>
</p> </p>
<p> <p>
<%= __('%sHTML%s, %sPHP%s, %siframe%s and %sshortcode%s versions are also available.', 'wysija-newsletters') <%= __("Copy and paste this [link]shortcode[/link] into a post or page alternatively.")
| replace({
'[link]': '<a href="javascript:;" class="mailpoet_form_export_toggle" data-type="shortcode">',
'[/link]': '</a>'
})
| raw
%>
</p>
<p>
<%= __('%sHTML%s, %sPHP%s and %siframe%s versions are also available.', 'wysija-newsletters')
| format( | format(
'<a href="javascript:;" class="mailpoet_form_export_toggle" data-type="html">', '<a href="javascript:;" class="mailpoet_form_export_toggle" data-type="html">',
'</a>', '</a>',
'<a href="javascript:;" class="mailpoet_form_export_toggle" data-type="php">', '<a href="javascript:;" class="mailpoet_form_export_toggle" data-type="php">',
'</a>', '</a>',
'<a href="javascript:;" class="mailpoet_form_export_toggle" data-type="iframe">', '<a href="javascript:;" class="mailpoet_form_export_toggle" data-type="iframe">',
'</a>',
'<a href="javascript:;" class="mailpoet_form_export_toggle" data-type="shortcode">',
'</a>' '</a>'
) )
| raw | raw
@ -254,6 +256,14 @@
]; ];
jQuery(function($) { jQuery(function($) {
function mailpoet_form_toggle_segments() {
// hide list selection if a list widget has been dragged into the editor
$('mailpoet_settings_segment_selection')[
(($$('#' + WysijaForm.options.editor + ' [wysija_id="segments"]').length > 0) === true)
? 'hide' : 'show'
]();
}
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());
@ -269,8 +279,13 @@
if(response !== false) { if(response !== false) {
data.fields = $.merge(response, data.fields); data.fields = $.merge(response, data.fields);
} }
// render toolbar // render toolbar
jQuery('#mailpoet_toolbar_fields').html(template(data)); jQuery('#mailpoet_toolbar_fields').html(template(data));
setTimeout(function() {
WysijaForm.init();
}, 1);
}); });
} }
window.mailpoet_form_fields = mailpoet_form_fields; window.mailpoet_form_fields = mailpoet_form_fields;
@ -299,7 +314,7 @@
}); });
// toolbar: open default section // toolbar: open default section
$('.mailpoet_toolbar_section[data-section="settings"]') $('.mailpoet_toolbar_section[data-section="fields"]')
.removeClass('closed'); .removeClass('closed');
// form: edit name (in place editor) // form: edit name (in place editor)
@ -385,6 +400,14 @@
return false; return false;
}); });
// edit name
$('#mailpoet_form_name').on('keyup', function(e) {
if(e.which === 13) {
$('#mailpoet_form_save').trigger('click');
this.blur();
}
});
// preview form // preview form
$(document).on('click', '#mailpoet_form_preview', function() { $(document).on('click', '#mailpoet_form_preview', function() {
//mailpoet_form_save(mailpoet_form_preview); //mailpoet_form_save(mailpoet_form_preview);
@ -465,7 +488,7 @@
action: 'exportsEditor', action: 'exportsEditor',
data: $('#mailpoet_form_id').val() data: $('#mailpoet_form_id').val()
}).done(function(response) { }).done(function(response) {
if(response !== false) { if(response.result !== false) {
$('#mailpoet_form_export').html(template({ exports: response })); $('#mailpoet_form_export').html(template({ exports: response }));
} }
}); });
@ -483,11 +506,7 @@
// open popup // open popup
MailPoet.Modal.popup({ MailPoet.Modal.popup({
title: "<%= __('Add new field') %>", title: "<%= __('Add new field') %>",
template: $('#form_template_field_new').html(), template: $('#form_template_field_new').html()
onSuccess: function(data) {
// toggle widgets
WysijaForm.toggleWidgets();
}
}); });
return false; return false;
@ -502,7 +521,7 @@
action: 'get', action: 'get',
data: id data: id
}).done(function(response) { }).done(function(response) {
if(response !== false) { if(response.result !== 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(),
@ -519,14 +538,14 @@
var name = $(this).siblings('.mailpoet_form_field').attr('wysija_name'); var name = $(this).siblings('.mailpoet_form_field').attr('wysija_name');
if(window.confirm( if(window.confirm(
"<%= __('Do you really want to delete this custom field?') %>" "<%= __('Deleting this field here will delete the data associated to it for all you users. Confirm you want to delete.') %>"
)) { )) {
MailPoet.Ajax.post({ MailPoet.Ajax.post({
endpoint: 'customFields', endpoint: 'customFields',
action: 'delete', action: 'delete',
data: id data: id
}).done(function(response) { }).done(function(result) {
if(response === true) { if(result === true) {
item.remove(); item.remove();
mailpoet_form_fields(); mailpoet_form_fields();
MailPoet.Notice.success( MailPoet.Notice.success(
@ -546,15 +565,13 @@
}); });
// get form fields // get form fields
mailpoet_form_fields().done(function() { mailpoet_form_fields();
WysijaForm.init();
});
// toolbar: segment selection // toolbar: segment selection
var selected_segments = <%= form.settings.segments | json_encode | raw %>; var selected_segments = <%= form.settings.segments | json_encode | raw %>;
// enable select2 for segment selection // enable select2 for segment selection
$('#mailpoet_form_segments').select2({ var select2 = $('#mailpoet_form_segments').select2({
width:'100%', width:'100%',
templateResult: function(item) { templateResult: function(item) {
if(item.element && item.element.selected) { if(item.element && item.element.selected) {
@ -563,7 +580,23 @@
return item.text; return item.text;
} }
} }
}).select2('val', <%= form.settings.segments | json_encode | raw %>); });
var hasRemoved = false;
select2.on('select2:unselecting', function(e) {
hasRemoved = true;
});
select2.on('select2:opening', function(e) {
if(hasRemoved === true) {
hasRemoved = false;
e.preventDefault();
}
});
// set selected values
$('#mailpoet_form_segments')
.val(<%= form.settings.segments | json_encode | raw %>)
.trigger('change');
}); });
}); });
</script> </script>

View File

@ -1,10 +1,10 @@
<% set currentDay = 'now'|date("d") %> <% set currentDay = 'now' | date('d') | number_format %>
<select id="{{ id }}_days"> <select id="{{ id }}_days">
<% for day in 1..31 %> <% for day in 1..31 %>
<option <option
{{#if params.is_default_today}} <% if(currentDay == day) %>
{{#ifCond day "==" "<%= currentDay %>"}}selected="selected"{{/ifCond}} {{#if params.is_default_today}}selected="selected"{{/if}}
{{/if}}><%= day %></option> <% endif %>
><%= day %></option>
<% endfor %> <% endfor %>
</select> </select>

View File

@ -1,11 +1,12 @@
<% set currentMonth = 'now'|date('n') %> <% set currentMonth = 'now'|date('n') %>
<select id="{{ id }}_months"> <select id="{{ id }}_months">
<% for month in 1..12 %> <% for month in 1..12 %>
<option <option
{{#if params.is_default_today}} <% if(currentMonth == month) %>
{{#ifCond month "==" "<%= currentMonth %>"}}selected="selected"{{/ifCond}} {{#if params.is_default_today}}selected="selected"{{/if}}
{{/if}} <% endif %>
><%= month |date('1984-' ~ month ~ '-01') |date('F') %></option> >
<%= month_names[month - 1] %>
</option>
<% endfor %> <% endfor %>
</select> </select>

View File

@ -4,8 +4,9 @@
<select id="{{ id }}_years"> <select id="{{ id }}_years">
<% for year in currentYear..minYear %> <% for year in currentYear..minYear %>
<option <option
{{#if params.is_default_today}} <% if(currentYear == year) %>
{{#ifCond year "==" "<%= year %>"}}selected="selected"{{/ifCond}} {{#if params.is_default_today}}selected="selected"{{/if}}
{{/if}}><%= year %></option> <% endif %>
><%= year %></option>
<% endfor %> <% endfor %>
</select> </select>

View File

@ -1,9 +1,7 @@
{{#if params.text}} {{#if params.text}}
<p> {{#ifCond params.nl2br '==' '1'}}
{{#if params.nl2br}} {{{ nl2br params.text }}}
{{{ nl2br params.text }}} {{else}}
{{else}} {{{ params.text }}}
{{{ params.text }}} {{/ifCond}}
{{/if}}
</p>
{{/if}} {{/if}}

View File

@ -1 +1 @@
<input type="submit" value="{{ params.label }}" /> <input type="submit" value="{{ params.label }}" class="button" />

View File

@ -1,9 +1,20 @@
<form id="form_field_new" name="form_field_new" action="" method="post"> <form
id="form_field_new"
name="form_field_new"
action=""
method="post"
data-parsley-validate="true"
>
{{#if id}}<input type="hidden" id="field_id" name="id" value="{{ id }}" />{{/if}} {{#if id}}<input type="hidden" id="field_id" name="id" value="{{ id }}" />{{/if}}
<p> <p>
<label for="field_type"><%= __('Select a field type:') %></label> <label for="field_type"><%= __('Select a field type:') %></label>
<select id="field_type" name="type"> <select
id="field_type"
name="type"
data-parsley-required="true"
data-parsley-required-message="<%= __('You need to specify a type') %>"
>
<option value="">--</option> <option value="">--</option>
<option <option
{{#ifCond type '==' 'input'}}selected="selected"{{/ifCond}} {{#ifCond type '==' 'input'}}selected="selected"{{/ifCond}}
@ -33,7 +44,13 @@
</p> </p>
<p> <p>
<label><%= __("Field's name:") %></label> <label><%= __("Field's name:") %></label>
<input type="text" name="name" value="{{ name }}" /> <input
type="text"
name="name"
value="{{ name }}"
data-parsley-required="true"
data-parsley-required-message="<%= __('You need to specify a name') %>"
/>
</p> </p>
<hr /> <hr />
@ -49,41 +66,41 @@
$(function() { $(function() {
loadFieldForm(); loadFieldForm();
});
$('#form_field_new').on('submit', function(e) { $('#form_field_new').parsley().on('form:submit', function(parsley) {
e.preventDefault(); // get data
var data = $(this.$element).serializeObject();
// get data // save custom field
var data = $(this).serializeObject(); MailPoet.Ajax.post({
MailPoet.Ajax.post({ endpoint: 'customFields',
endpoint: 'customFields', action: 'save',
action: 'save', data: data
data: data }).done(function(response) {
}).done(function(response) { if(response.result === true) {
if(response.result === true) { mailpoet_form_fields();
mailpoet_form_fields(); if(data.id) {
if(data.id) { MailPoet.Notice.success(
MailPoet.Notice.success( "<%= __('Updated custom field “"+data.name+"“') %>"
"<%= __('Updated custom field “"+data.name+"“') %>" );
); } else {
MailPoet.Notice.success(
"<%= __('Added custom field “"+data.name+"“') %>"
);
}
// close popup
MailPoet.Modal.success();
} else { } else {
MailPoet.Notice.success( if(response.errors.length > 0) {
"<%= __('Added custom field “"+data.name+"“') %>" $(response.errors).each(function(i, error) {
); MailPoet.Notice.error(error);
} });
} else {
if(response.errors.length > 0) {
for(error in response.errors) {
MailPoet.Notice.error(error);
} }
} }
} });
return false;
}); });
// close popup
return MailPoet.Modal.success();
}); });
$('#form_field_new #field_type').on('change', function() { $('#form_field_new #field_type').on('change', function() {

View File

@ -15,7 +15,6 @@
#> #>
class="mailpoet_form mailpoet_form_<%= form_type %>" class="mailpoet_form mailpoet_form_<%= form_type %>"
novalidate novalidate
data-parsley-validate
> >
<div class="mailpoet_message"></div> <div class="mailpoet_message"></div>