Custom fields

- added listing of custom fields in form editor
- ability to delete a custom field
- mades changes to the form editor in order to accomodate for the new custom fields system
- fix form editor bugs
- cleanup
This commit is contained in:
Jonathan Labreuille
2015-11-05 18:35:12 +01:00
parent 5473f94e24
commit b12f7f29de
16 changed files with 394 additions and 202 deletions

View File

@ -196,7 +196,7 @@ var WysijaHistory = {
// check if the field is unique // check if the field is unique
if(parseInt(clone.readAttribute('wysija_unique'), 10) === 1) { if(parseInt(clone.readAttribute('wysija_unique'), 10) === 1) {
// check if the field is already in the queue // check if the field is already in the queue
$(WysijaHistory.container).select('[wysija_field="' + clone.readAttribute('wysija_field') + '"]').invoke('remove'); $(WysijaHistory.container).select('[wysija_name="' + clone.readAttribute('wysija_name') + '"]').invoke('remove');
} }
// check history size // check history size
@ -225,7 +225,7 @@ var WysijaHistory = {
$(WysijaHistory.container).innerHTML = ''; $(WysijaHistory.container).innerHTML = '';
}, },
remove: function(field) { remove: function(field) {
$(WysijaHistory.container).select('[wysija_field="' + field + '"]').invoke('remove'); $(WysijaHistory.container).select('[wysija_name="' + field + '"]').invoke('remove');
} }
}; };
@ -376,8 +376,8 @@ var WysijaForm = {
// get basic field data // get basic field data
var data = { var data = {
type: element.readAttribute('wysija_type'), type: element.readAttribute('wysija_type'),
field: element.readAttribute('wysija_field'),
name: element.readAttribute('wysija_name'), name: element.readAttribute('wysija_name'),
id: element.readAttribute('wysija_id'),
unique: parseInt(element.readAttribute('wysija_unique') || 0, 10), unique: parseInt(element.readAttribute('wysija_unique') || 0, 10),
static: parseInt(element.readAttribute('wysija_static') || 0, 10), static: parseInt(element.readAttribute('wysija_static') || 0, 10),
element: element, element: element,
@ -394,16 +394,16 @@ var WysijaForm = {
$$('a[wysija_unique="1"]').invoke('removeClassName', 'disabled'); $$('a[wysija_unique="1"]').invoke('removeClassName', 'disabled');
// loop through each unique field already inserted in the editor and disable its toolbar equivalent // loop through each unique field already inserted in the editor and disable its toolbar equivalent
$$('#' + WysijaForm.options.editor + ' [wysija_unique="1"]').each(function(element) { $$('#' + WysijaForm.options.editor + ' [wysija_unique="1"]').map(function(element) {
var field = $$('#' + WysijaForm.options.toolbar + ' [wysija_field="' + element.readAttribute('wysija_field') + '"]').first(); var field = $$('#' + WysijaForm.options.toolbar + ' [wysija_id="' + element.readAttribute('wysija_id') + '"]');
if(field !== undefined) { if(field.length > 0) {
field.addClassName('disabled'); field.first().addClassName('disabled');
} }
}); });
// hide list selection if a list widget has been dragged into the editor // hide list selection if a list widget has been dragged into the editor
$('mailpoet_settings_segment_selection')[ $('mailpoet_settings_segment_selection')[
(($$('#' + WysijaForm.options.editor + ' [wysija_field="segments"]').length > 0) === true) (($$('#' + WysijaForm.options.editor + ' [wysija_id="segments"]').length > 0) === true)
? 'hide' : 'show' ? 'hide' : 'show'
](); ]();
}, },
@ -958,12 +958,10 @@ WysijaForm.Widget = Class.create(WysijaForm.Block, {
}, },
getData: function() { getData: function() {
var data = WysijaForm.getFieldData(this.element); var data = WysijaForm.getFieldData(this.element);
// decode params // decode params
if(data.params.length > 0) { if(data.params.length > 0) {
data.params = JSON.parse(data.params); data.params = JSON.parse(data.params);
} }
return data; return data;
}, },
getControls: function() { getControls: function() {

View File

@ -3,6 +3,7 @@ namespace MailPoet\Config;
use \MailPoet\Models\Segment; use \MailPoet\Models\Segment;
use \MailPoet\Models\Setting; use \MailPoet\Models\Setting;
use \MailPoet\Models\Form; use \MailPoet\Models\Form;
use \MailPoet\Form\Block;
use \MailPoet\Form\Renderer as FormRenderer; use \MailPoet\Form\Renderer as FormRenderer;
use \MailPoet\Settings\Hosts; use \MailPoet\Settings\Hosts;
use \MailPoet\Settings\Pages; use \MailPoet\Settings\Pages;
@ -214,7 +215,9 @@ class Menu {
'form' => $form, 'form' => $form,
'pages' => Pages::getAll(), 'pages' => Pages::getAll(),
'segments' => Segment::findArray(), 'segments' => Segment::findArray(),
'styles' => FormRenderer::getStyles($form) 'styles' => FormRenderer::getStyles($form),
'date_types' => Block\Date::getDateTypes(),
'date_formats' => Block\Date::getDateFormats()
); );
echo $this->renderer->render('form/editor.html', $data); echo $this->renderer->render('form/editor.html', $data);

View File

@ -125,7 +125,8 @@ class Migrator {
'segment_id mediumint(9) NOT NULL,', 'segment_id mediumint(9) NOT NULL,',
'created_at TIMESTAMP NOT NULL DEFAULT 0,', 'created_at TIMESTAMP NOT NULL DEFAULT 0,',
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,', 'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
'PRIMARY KEY (id)' 'PRIMARY KEY (id),',
'UNIQUE KEY subscriber_segment (subscriber_id,segment_id)'
); );
return $this->sqlify(__FUNCTION__, $attributes); return $this->sqlify(__FUNCTION__, $attributes);
} }
@ -137,7 +138,8 @@ class Migrator {
'segment_id mediumint(9) NOT NULL,', 'segment_id mediumint(9) NOT NULL,',
'created_at TIMESTAMP NOT NULL DEFAULT 0,', 'created_at TIMESTAMP NOT NULL DEFAULT 0,',
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,', 'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
'PRIMARY KEY (id)' 'PRIMARY KEY (id),',
'UNIQUE KEY newsletter_segment (newsletter_id,segment_id)'
); );
return $this->sqlify(__FUNCTION__, $attributes); return $this->sqlify(__FUNCTION__, $attributes);
} }
@ -147,6 +149,7 @@ class Migrator {
'id mediumint(9) NOT NULL AUTO_INCREMENT,', 'id mediumint(9) NOT NULL AUTO_INCREMENT,',
'name varchar(90) NOT NULL,', 'name varchar(90) NOT NULL,',
'type varchar(90) NOT NULL,', 'type varchar(90) NOT NULL,',
'params longtext NOT NULL,',
'created_at TIMESTAMP NOT NULL DEFAULT 0,', 'created_at TIMESTAMP NOT NULL DEFAULT 0,',
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,', 'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
'PRIMARY KEY (id),', 'PRIMARY KEY (id),',

View File

@ -9,12 +9,12 @@ abstract class Base {
protected static function getInputValidationRules($block) { protected static function getInputValidationRules($block) {
$rules = array(); $rules = array();
if($block['field'] === 'email') { if($block['name'] === 'email') {
$rules[] = 'required'; $rules[] = 'required';
$rules[] = 'custom[email]'; $rules[] = 'custom[email]';
} }
if($block['field'] === 'list') { if($block['name'] === 'list') {
$rules[] = 'required'; $rules[] = 'required';
} }
@ -83,7 +83,7 @@ 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['field']; return $block['id'];
} }
protected static function getFieldLabel($block = array()) { protected static function getFieldLabel($block = array()) {

View File

@ -18,7 +18,7 @@ class Export {
// generate url to load iframe's content // generate url to load iframe's content
$iframe_url = add_query_arg(array( $iframe_url = add_query_arg(array(
'mailpoet_page' => 'mailpoet_form_iframe', 'mailpoet_page' => 'mailpoet_form_iframe',
'mailpoet_form' => $form['form'] 'mailpoet_form' => $form['id']
), site_url()); ), site_url());
// generate iframe // generate iframe
@ -30,7 +30,7 @@ class Export {
'class="mailpoet_form_iframe" '. 'class="mailpoet_form_iframe" '.
'vspace="0" '. 'vspace="0" '.
'tabindex="0" '. 'tabindex="0" '.
//'style="position: static; top: 0pt; margin: 0px; border-style: none; height: 330px; left: 0pt; visibility: visible;" '. // TODO: need to find a solution for Height. 'onload="javascript:(this.style.height = this.contentWindow.document.body.scrollHeight + \'px\');"'.
'marginwidth="0" '. 'marginwidth="0" '.
'marginheight="0" '. 'marginheight="0" '.
'hspace="0" '. 'hspace="0" '.
@ -40,7 +40,7 @@ class Export {
case 'php': case 'php':
$output = array( $output = array(
'$form_widget = new \MailPoet\Form\Widget();', '$form_widget = new \MailPoet\Form\Widget();',
'echo $form_widget->widget(array(\'form\' => '.(int)$form['form'].', \'form_type\' => \'php\'));' 'echo $form_widget->widget(array(\'form\' => '.(int)$form['id'].', \'form_type\' => \'php\'));'
); );
return join("\n", $output); return join("\n", $output);
break; break;
@ -77,14 +77,14 @@ class Export {
$form_widget = new Widget(); $form_widget = new Widget();
$output[] = $form_widget->widget(array( $output[] = $form_widget->widget(array(
'form' => (int)$form['form'], 'form' => (int)$form['id'],
'form_type' => 'php' 'form_type' => 'php'
)); ));
return join("\n", $output); return join("\n", $output);
break; break;
case 'shortcode': case 'shortcode':
return '[mailpoet_form id="'.(int)$form['form'].'"]'; return '[mailpoet_form id="'.(int)$form['id'].'"]';
break; break;
} }
} }

View File

@ -6,6 +6,7 @@ use \MailPoet\Models\Segment;
use \MailPoet\Models\Setting; use \MailPoet\Models\Setting;
use \MailPoet\Models\Subscriber; use \MailPoet\Models\Subscriber;
use \MailPoet\Form\Renderer as FormRenderer; use \MailPoet\Form\Renderer as FormRenderer;
use \MailPoet\Form\Util;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
@ -160,27 +161,27 @@ class Widget extends \WP_Widget {
'title' => $title, 'title' => $title,
'styles' => FormRenderer::renderStyles($form), 'styles' => FormRenderer::renderStyles($form),
'html' => FormRenderer::renderHTML($form), 'html' => FormRenderer::renderHTML($form),
'before_widget' => $before_widget, 'before_widget' => (!empty($before_widget) ? $before_widget : ''),
'after_widget' => $after_widget, 'after_widget' => (!empty($after_widget) ? $after_widget : ''),
'before_title' => $before_title, 'before_title' => (!empty($before_title) ? $before_title : ''),
'after_title' => $after_title 'after_title' => (!empty($after_title) ? $after_title : '')
); );
/*if(isset($_GET['mailpoet_form']) && (int)$_GET['mailpoet_form'] === $form['id']) { // if(isset($_GET['mailpoet_form']) && (int)$_GET['mailpoet_form'] === $form['id']) {
// form messages (success / error) // // form messages (success / error)
$output .= '<div class="mailpoet_message">'; // $output .= '<div class="mailpoet_message">';
// success message // // success message
if(isset($_GET['mailpoet_success'])) { // if(isset($_GET['mailpoet_success'])) {
$output .= '<p class="mailpoet_validate_success">'.strip_tags(urldecode($_GET['mailpoet_success']), '<a><strong><em><br><p>').'</p>'; // $output .= '<p class="mailpoet_validate_success">'.strip_tags(urldecode($_GET['mailpoet_success']), '<a><strong><em><br><p>').'</p>';
} // }
// error message // // error message
if(isset($_GET['mailpoet_error'])) { // if(isset($_GET['mailpoet_error'])) {
$output .= '<p class="mailpoet_validate_error">'.strip_tags(urldecode($_GET['mailpoet_error']), '<a><strong><em><br><p>').'</p>'; // $output .= '<p class="mailpoet_validate_error">'.strip_tags(urldecode($_GET['mailpoet_error']), '<a><strong><em><br><p>').'</p>';
} // }
$output .= '</div>'; // $output .= '</div>';
} else { // } else {
$output .= '<div class="mailpoet_message"></div>'; // $output .= '<div class="mailpoet_message"></div>';
}*/ // }
// render form // render form
$renderer = new Renderer(); $renderer = new Renderer();
@ -226,7 +227,9 @@ if(isset($_GET['mailpoet_page']) && strlen(trim($_GET['mailpoet_page'])) > 0) {
if($form !== false) { if($form !== false) {
// render form // render form
print FormRenderer::getExport('html', $form->asArray()); $output = Util\Export::get('html', $form->asArray());
// $output = do_shortcode($output);
print $output;
exit; exit;
} }
break; break;

View File

@ -16,6 +16,27 @@ class CustomField extends Model {
)); ));
} }
function asArray() {
$model = parent::asArray();
$model['params'] = (
is_serialized($this->params)
? unserialize($this->params)
: $this->params
);
return $model;
}
function save() {
$this->set('params', (
is_serialized($this->params)
? $this->params
: serialize($this->params)
));
return parent::save();
}
function subscribers() { function subscribers() {
return $this->has_many_through( return $this->has_many_through(
__NAMESPACE__ . '\Subscriber', __NAMESPACE__ . '\Subscriber',

164
lib/Router/CustomFields.php Normal file
View File

@ -0,0 +1,164 @@
<?php
namespace MailPoet\Router;
use \MailPoet\Models\CustomField;
if(!defined('ABSPATH')) exit;
class CustomFields {
function __construct() {
}
function getAll() {
$collection = CustomField::findMany();
$custom_fields = array_map(function($custom_field) {
return $custom_field->asArray();
}, $collection);
wp_send_json($custom_fields);
}
function delete($id) {
$result = false;
$custom_field = CustomField::findOne($id);
if($custom_field !== false) {
$custom_field->delete();
$result = true;
}
wp_send_json($result);
}
function get($id) {
$custom_field = CustomField::findOne($id);
if($custom_field === false) {
wp_send_json(false);
} else {
$custom_field = $custom_field->asArray();
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

@ -61,9 +61,9 @@ class Forms {
'name' => __('New form'), 'name' => __('New form'),
'body' => array( 'body' => array(
array( array(
'id' => 'email',
'name' => __('Email'), 'name' => __('Email'),
'type' => 'input', 'type' => 'input',
'field' => 'email',
'static' => true, 'static' => true,
'params' => array( 'params' => array(
'label' => __('Email'), 'label' => __('Email'),
@ -71,9 +71,9 @@ class Forms {
) )
), ),
array( array(
'id' => 'submit',
'name' => __('Submit'), 'name' => __('Submit'),
'type' => 'submit', 'type' => 'submit',
'field' => 'submit',
'static' => true, 'static' => true,
'params' => array( 'params' => array(
'label' => __('Subscribe!') 'label' => __('Subscribe!')
@ -109,27 +109,32 @@ class Forms {
} }
} }
function preview($id) { function previewEditor($data = array()) {
$form = Form::findOne($id);
if($form === false) {
wp_send_json(false);
} else {
$form = $form->asArray();
// html // html
$html = FormRenderer::renderHTML($form); $html = FormRenderer::renderHTML($data);
// convert shortcodes // convert shortcodes
$html = do_shortcode($html); $html = do_shortcode($html);
// styles // styles
$css = new Util\Styles(FormRenderer::getStyles($form)); $css = new Util\Styles(FormRenderer::getStyles($data));
wp_send_json(array( wp_send_json(array(
'html' => $html, 'html' => $html,
'css' => $css->render() 'css' => $css->render()
)); ));
} }
function exportsEditor($id) {
$exports = false;
$form = Form::findOne($id);
if($form !== false) {
$exports = Util\Export::getAll($form->asArray());
}
wp_send_json($exports);
} }
function saveEditor($data = array()) { function saveEditor($data = array()) {
@ -192,7 +197,7 @@ class Forms {
'result' => ($form !== false), 'result' => ($form !== false),
'is_widget' => $is_widget 'is_widget' => $is_widget
)); ));
} }
function restore($id) { function restore($id) {
$result = false; $result = false;
@ -242,15 +247,6 @@ class Forms {
wp_send_json($result); wp_send_json($result);
} }
function itemAction($data = array()) {
$item_action = new Listing\ItemAction(
'\MailPoet\Models\Form',
$data
);
wp_send_json($item_action->apply());
}
function bulkAction($data = array()) { function bulkAction($data = array()) {
$bulk_action = new Listing\BulkAction( $bulk_action = new Listing\BulkAction(
'\MailPoet\Models\Form', '\MailPoet\Models\Form',

View File

@ -204,14 +204,6 @@ class Newsletters {
wp_send_json($listing_data); wp_send_json($listing_data);
} }
function itemAction($data = array()) {
$item_action = new Listing\ItemAction(
'\MailPoet\Models\Newsletter',
$data
);
wp_send_json($item_action->apply());
}
function bulkAction($data = array()) { function bulkAction($data = array()) {
$bulk_action = new Listing\BulkAction( $bulk_action = new Listing\BulkAction(
'\MailPoet\Models\Newsletter', '\MailPoet\Models\Newsletter',

View File

@ -88,11 +88,14 @@ class Subscribers {
$errors[] = __('You need to select a list'); $errors[] = __('You need to select a list');
} }
$subscriber = false;
if(!empty($errors)) { if(!empty($errors)) {
wp_send_json(array('errors' => $errors)); wp_send_json(array('errors' => $errors));
} else { } else {
if(!empty($data['email'])) {
$subscriber = Subscriber::where('email', $data['email'])->findOne(); $subscriber = Subscriber::where('email', $data['email'])->findOne();
} }
}
$signup_confirmation = Setting::getValue('signup_confirmation', array()); $signup_confirmation = Setting::getValue('signup_confirmation', array());
@ -180,14 +183,17 @@ class Subscribers {
? unserialize($form->settings) : null ? unserialize($form->settings) : null
); );
if($subscriber !== null && empty($errors)) { if(!empty($errors)) {
$result = true; wp_send_json(array(
$message = $form_settings['success_message']; 'result' => false,
'errors' => $errors
));
} else { } else {
$result = false; $result = true;
} }
if($form_settings !== null) { if($form_settings !== null) {
$message = $form_settings['success_message'];
// url params for non ajax requests // url params for non ajax requests
if($doing_ajax === false) { if($doing_ajax === false) {

View File

@ -121,8 +121,11 @@
<div> <div>
<!-- Form export links --> <!-- Form export links -->
<p> <p>
<%= __("You can easily add this form to your theme's in the %sWidgets area%s") <%= __("You can easily add this form to your theme's in the [link]Widgets area[/link]")
| format('<a href="widgets.php" target="_blank">', '</a>') | replace({
'[link]': '<a href="widgets.php" target="_blank">',
'[/link]': '</a>'
})
| raw | raw
%> %>
</p> </p>
@ -198,34 +201,26 @@
var mailpoet_segments = <%= json_encode(segments) %>; var mailpoet_segments = <%= json_encode(segments) %>;
jQuery(function($) { jQuery(function($) {
function mailpoet_form_fields(data) {
var template = Handlebars.compile(jQuery('#form_template_fields').html());
// render toolbar
jQuery('#mailpoet_toolbar_fields').html(template(data));
// enable code mirror editor on styles textarea // enable code mirror editor on styles textarea
MailPoet.CodeEditor = CodeMirror.fromTextArea(document.getElementById('mailpoet_form_styles'), { MailPoet.CodeEditor = CodeMirror.fromTextArea(document.getElementById('mailpoet_form_styles'), {
lineNumbers: true, lineNumbers: true,
tabMode: "indent", tabMode: "indent",
matchBrackets: true, matchBrackets: true,
theme: 'neo', theme: 'neo',
mode: 'css', mode: 'css'
}); });
}
// form editor: default fields
var default_fields = [ var default_fields = [
{ {
id: 'divider',
name: "<%= __('Divider') %>", name: "<%= __('Divider') %>",
field: 'divider',
type: 'divider', type: 'divider',
multiple: true, multiple: true,
readonly: true readonly: true
}, },
{ {
id: "first_name",
name: "<%= __('First name') %>", name: "<%= __('First name') %>",
field: 'first_name',
type: 'input', type: 'input',
params: { params: {
label: "<%= __('First name') %>" label: "<%= __('First name') %>"
@ -233,8 +228,8 @@
readonly: true readonly: true
}, },
{ {
id: "last_name",
name: "<%= __('Last name') %>", name: "<%= __('Last name') %>",
field: 'last_name',
type: 'input', type: 'input',
params: { params: {
label: "<%= __('Last name') %>" label: "<%= __('Last name') %>"
@ -242,8 +237,8 @@
readonly: true readonly: true
}, },
{ {
id: "segments",
name: "<%= __('List selection') %>", name: "<%= __('List selection') %>",
field: 'segments',
type: 'segment', type: 'segment',
params: { params: {
label: "<%= __('Select list(s):') %>" label: "<%= __('Select list(s):') %>"
@ -251,8 +246,8 @@
readonly: true readonly: true
}, },
{ {
id: 'html',
name: "<%= __('Random text or HTML') %>", name: "<%= __('Random text or HTML') %>",
field: 'html',
type: 'html', type: 'html',
params: { params: {
text: "<%= __('Subscribe to our newsletter and join our [mailpoet_subscribers_count] subscribers.') %>" text: "<%= __('Subscribe to our newsletter and join our [mailpoet_subscribers_count] subscribers.') %>"
@ -262,6 +257,26 @@
} }
]; ];
function mailpoet_form_fields() {
// form editor: default fields
var template = Handlebars.compile(jQuery('#form_template_fields').html());
var data = {
fields: default_fields
};
return MailPoet.Ajax.post({
endpoint: 'customFields',
action: 'getAll',
}).done(function(response) {
if(response !== false) {
data.fields = $.merge(response, data.fields);
}
// render toolbar
jQuery('#mailpoet_toolbar_fields').html(template(data));
});
}
// toolbar sections // toolbar sections
$(document).on('click', '.mailpoet_toolbar_section.closed', function() { $(document).on('click', '.mailpoet_toolbar_section.closed', function() {
// close currently opened section // close currently opened section
@ -272,7 +287,8 @@
}); });
// toolbar: open default section // toolbar: open default section
$('.mailpoet_toolbar_section[data-section="shortcodes"]').removeClass('closed'); // TODO: data-section="settings"
$('.mailpoet_toolbar_section[data-section="fields"]').removeClass('closed');
// form: edit name (in place editor) // form: edit name (in place editor)
$('#mailpoet_form_edit_name').on('click', function() { $('#mailpoet_form_edit_name').on('click', function() {
@ -350,20 +366,22 @@
// save form // save form
$('#mailpoet_form_save').on('click', function() { $('#mailpoet_form_save').on('click', function() {
mailpoet_form_save(); mailpoet_form_save();
mailpoet_form_export();
return false; return false;
}); });
// 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);
mailpoet_form_preview();
return false; return false;
}); });
function mailpoet_form_preview() { function mailpoet_form_preview() {
MailPoet.Ajax.post({ MailPoet.Ajax.post({
endpoint: 'forms', endpoint: 'forms',
action: 'preview', action: 'previewEditor',
data: $('#mailpoet_form_id').val() data: WysijaForm.save()
}).done(function(response) { }).done(function(response) {
MailPoet.Modal.popup({ MailPoet.Modal.popup({
title: "<%= __('Form preview') %>", title: "<%= __('Form preview') %>",
@ -373,8 +391,6 @@
}) })
} }
mailpoet_form_export(<%= exports | json_encode | raw %>);
function mailpoet_form_save(callback) { function mailpoet_form_save(callback) {
var form = WysijaForm.save(); var form = WysijaForm.save();
form.id = $('#mailpoet_form_id').val(); form.id = $('#mailpoet_form_id').val();
@ -429,7 +445,7 @@
var template = Handlebars.compile($('#form_template_exports').html()); var template = Handlebars.compile($('#form_template_exports').html());
MailPoet.Ajax.post({ MailPoet.Ajax.post({
endpoint: 'forms', endpoint: 'forms',
action: 'getExports', action: 'exportsEditor',
data: $('#mailpoet_form_id').val() data: $('#mailpoet_form_id').val()
}).done(function(response) { }).done(function(response) {
if(response !== false) { if(response !== false) {
@ -437,6 +453,7 @@
} }
}); });
} }
mailpoet_form_export();
$(document).on('click', '.mailpoet_form_export_toggle', function() { $(document).on('click', '.mailpoet_form_export_toggle', function() {
$('.mailpoet_form_export_output').hide(); $('.mailpoet_form_export_output').hide();
@ -487,27 +504,27 @@
// delete field // delete field
$(document).on('click', '.mailpoet_form_field_delete', function() { $(document).on('click', '.mailpoet_form_field_delete', function() {
var field = $(this).data('field');
if(window.confirm("<%= __('Do you really want to delete this custom field?') %>")) { var id = $(this).data('id');
// delete field var item = $(this).parent();
console.log('TODO subscriber->meta->delete'); var name = $(this).siblings('.mailpoet_form_field').attr('wysija_name');
/*mailpoet_post_json('subscriber_delete_meta.php', { field: field }, function(response) {
var fields = (response.fields !== undefined) ? (response.fields || []) : [];
// refresh toolbar if(window.confirm(
mailpoet_form_fields({ "<%= __('Do you really want to delete this custom field?') %>"
fields: $.merge(fields, default_fields) )) {
}); MailPoet.Ajax.post({
endpoint: 'customFields',
// delete field in form action: 'delete',
// remove any instance of the field present in the form data: id
var blocks = $$('#mailpoet_form_body .mailpoet_form_block[wysija_field="'+field+'"]'); }).done(function(response) {
if(blocks.length > 0) { if(response === true) {
var block = WysijaForm.get(blocks[0]); item.remove();
block.removeBlock(); mailpoet_form_fields();
MailPoet.Notice.success(
"<%= __('Removed custom field “"+name+"“') %>"
);
} }
});*/ });
} }
}); });
@ -516,11 +533,14 @@
// pop last element off the history // pop last element off the history
WysijaHistory.dequeue(); WysijaHistory.dequeue();
// init wysija form
WysijaForm.init();
return false; return false;
}); });
// get form fields
mailpoet_form_fields().done(function() {
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 %>;
@ -535,19 +555,6 @@
} }
} }
}).select2('val', <%= form.settings.segments | json_encode | raw %>); }).select2('val', <%= form.settings.segments | json_encode | raw %>);
// subscriber meta fields
var meta_fields = [
// {"name":"CF text input (mandatory + numbers only)","field":"627e1c8d4fe97712836484e0f2377abd","type":"input","params":{"required":"1","validate":"onlyNumberSp","label":"CF text input (mandatory + numbers only)"}},
// {"name":"CF text area (no validation)","field":"6607e6e447b89384c59adc124e979d6b","type":"textarea","params":{"required":"","validate":"","label":"CF text area (no validation)"}},
// {"name":"CF text area (letters only)","field":"47a9cae88f8b3b3e23a16990922e7905","type":"textarea","params":{"required":"","validate":"onlyLetterSp","label":"CF text area (letters only)"}},
// {"name":"CF radio (mandatory)","field":"5f01b4ccc146fdd6b889bab63dc1b5cd","type":"radio","params":{"values":[{"value":"un"},{"is_checked":"1","value":"deux"},{"value":"trois"}],"required":"1","label":"CF radio (mandatory)"}}
];
// toolbar: fiels
mailpoet_form_fields({
fields: $.merge(meta_fields, default_fields)
});
}); });
}); });
</script> </script>
@ -586,8 +593,16 @@
<%= partial('field_settings_submit', 'form/templates/settings/submit.hbs', '_settings_submit') %> <%= 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_values_item', 'form/templates/settings/values_item.hbs') %>
<%= partial('field_settings_date_formats', 'form/templates/settings/date_formats.hbs') %> <%= partial(
<%= partial('field_settings_date_type', 'form/templates/settings/date_types.hbs') %> '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_item', 'form/templates/settings/segment_selection_item.hbs') %>
<%= partial('field_settings_segment_selection', 'form/templates/settings/segment_selection.hbs', '_settings_segment_selection') %> <%= partial('field_settings_segment_selection', 'form/templates/settings/segment_selection.hbs', '_settings_segment_selection') %>

View File

@ -1,8 +1,8 @@
<div class="mailpoet_form_block" <div class="mailpoet_form_block"
wysija_type="{{ type }}" wysija_type="{{ type }}"
wysija_field="{{ field }}" {{#if id}}wysija_id="{{ id }}"{{/if}}
wysija_name="{{ name }}" wysija_name="{{ name }}"
wysija_unique="{{#if multiple}}0{{else}}1{{/if}}" wysija_unique="{{ unique }}"
wysija_static="{{#ifCond static '==' 1}}1{{else}}0{{/ifCond}}" wysija_static="{{#ifCond static '==' 1}}1{{else}}0{{/ifCond}}"
wysija_params="{{#if params}}{{ json_encode params }}{{/if}}"> wysija_params="{{#if params}}{{ json_encode params }}{{/if}}">
{{#ifCond type '!==' 'divider'}} {{#ifCond type '!==' 'divider'}}

View File

@ -31,22 +31,10 @@
</option> </option>
</select> </select>
</p> </p>
<p class="mailpoet_error" data-error="type_required">
<%= __('You need to select a type.') %>
</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 }}" />
</p> </p>
<p class="mailpoet_error" data-error="name_required">
<%= __('You need to specify a name.') %>
</p>
<p class="mailpoet_error" data-error="name_already_exists">
<%= __('This name is already used.') %>
</p>
<hr /> <hr />
<div class="field_type_form"></div> <div class="field_type_form"></div>
@ -71,6 +59,8 @@
$('.mailpoet_error').hide(); $('.mailpoet_error').hide();
console.log(('TODO: subscriber->meta->edit')); console.log(('TODO: subscriber->meta->edit'));
console.log(data);
// mailpoet_post_json('subscriber_extend.php', data, function(response) { // mailpoet_post_json('subscriber_extend.php', data, function(response) {
// if(response.error !== undefined) { // if(response.error !== undefined) {
// // there's an error! // // there's an error!

View File

@ -1,8 +1,8 @@
{{#each fields}} {{#each fields}}
<li> <li>
<a class="mailpoet_form_field" <a class="mailpoet_form_field"
wysija_field="{{ field }}"
wysija_name="{{ name }}" wysija_name="{{ name }}"
{{#if id }}wysija_id="{{ id }}"{{/if}}
wysija_unique="{{#if multiple}}0{{else}}1{{/if}}" wysija_unique="{{#if multiple}}0{{else}}1{{/if}}"
wysija_type="{{ type }}" wysija_type="{{ type }}"
{{#if params}}wysija_params="{{ json_encode params }}"{{/if}}> {{#if params}}wysija_params="{{ json_encode params }}"{{/if}}>
@ -13,13 +13,13 @@
<a class="mailpoet_form_field_edit settings" <a class="mailpoet_form_field_edit settings"
title="<%= __('Edit field') %>" title="<%= __('Edit field') %>"
href="javascript:;" href="javascript:;"
data-field="{{ field }}"> data-id="{{ id }}">
<span class="dashicons dashicons-admin-generic"></span> <span class="dashicons dashicons-admin-generic"></span>
</a> </a>
<a class="mailpoet_form_field_delete delete" <a class="mailpoet_form_field_delete delete"
title="<?php _e('Delete field'); ?>" title="<%= __('Delete field') %>"
href="javascript:;" href="javascript:;"
data-field="{{ field }}"> data-id="{{ id }}">
<span class="dashicons dashicons-dismiss"></span> <span class="dashicons dashicons-dismiss"></span>
</a> </a>
{{/unless}} {{/unless}}

View File

@ -17,10 +17,11 @@
<input type="hidden" name="form_id" value="<%= form.id %>" /> <input type="hidden" name="form_id" value="<%= form.id %>" />
<% if not(settings.segments_selected_by == 'user') %> <% if not(form.settings.segments_selected_by == 'user') %>
<input type="hidden" name="segments" value="<%= settings.segments | join(',') %>" /> <% for segment in form.settings.segments %>
<input type="hidden" name="segments[]" value="<%= segment %>" />
<% endfor %>
<% endif %> <% endif %>
<%= html | raw %> <%= html | raw %>
</form> </form>
</div> </div>