diff --git a/assets/js/src/listing/listing.jsx b/assets/js/src/listing/listing.jsx index 7aef70d8d5..5503221f32 100644 --- a/assets/js/src/listing/listing.jsx +++ b/assets/js/src/listing/listing.jsx @@ -74,10 +74,11 @@ define( ); } - var custom_actions = this.props.item_actions; - var item_actions = false; + const custom_actions = this.props.item_actions; + let item_actions = false; if(custom_actions.length > 0) { + let is_first = true; item_actions = custom_actions.map(function(action, index) { if(action.display !== undefined) { if(action.display(this.props.item) === false) { @@ -85,10 +86,12 @@ define( } } + let custom_action = null; + if(action.name === 'trash') { - return ( + custom_action = ( - {(index > 0) ? ' | ' : ''} + {(!is_first) ? ' | ' : ''} ); } else if(action.refresh) { - return ( + custom_action = ( - {(index > 0) ? ' | ' : ''} + {(!is_first) ? ' | ' : ''} { action.link(this.props.item) } ); } else if(action.link) { - return ( + custom_action = ( - {(index > 0) ? ' | ' : ''} + {(!is_first) ? ' | ' : ''} { action.link(this.props.item) } ); } else { - return ( + custom_action = ( - {(index > 0) ? ' | ' : ''} + {(!is_first) ? ' | ' : ''} ); } + + if(custom_action !== null && is_first === true) { + is_first = false; + } + + return custom_action; }.bind(this)); } else { item_actions = ( diff --git a/assets/js/src/segments/form.jsx b/assets/js/src/segments/form.jsx index 04ac4d8cb6..ca7b2526a7 100644 --- a/assets/js/src/segments/form.jsx +++ b/assets/js/src/segments/form.jsx @@ -12,7 +12,7 @@ define( Form ) { - var fields = [ + let fields = [ { name: 'name', label: 'Name', @@ -25,7 +25,7 @@ define( } ]; - var messages = { + const messages = { onUpdate: function() { MailPoet.Notice.success('Segment successfully updated!'); }, @@ -34,7 +34,7 @@ define( } }; - var SegmentForm = React.createClass({ + const SegmentForm = React.createClass({ mixins: [ Router.History ], diff --git a/assets/js/src/segments/list.jsx b/assets/js/src/segments/list.jsx index 456b782deb..6574e62d8d 100644 --- a/assets/js/src/segments/list.jsx +++ b/assets/js/src/segments/list.jsx @@ -96,6 +96,9 @@ const item_actions = [ return ( Edit ); + }, + display: function(segment) { + return (segment.type !== 'wp_users'); } }, { diff --git a/lib/Form/Renderer.php b/lib/Form/Renderer.php index de81214ec0..51ba032906 100644 --- a/lib/Form/Renderer.php +++ b/lib/Form/Renderer.php @@ -13,9 +13,11 @@ class Renderer { return $html; } - static function renderStyles($form = array()) { + static function renderStyles($form = array(), $prefix = null) { + $styles = new Util\Styles(static::getStyles($form)); + $html = ''; return $html; diff --git a/lib/Form/Util/Styles.php b/lib/Form/Util/Styles.php index bb36c1bc29..e5a71e9873 100644 --- a/lib/Form/Util/Styles.php +++ b/lib/Form/Util/Styles.php @@ -101,7 +101,7 @@ EOL; } private function stripComments($stylesheet) { - // remove comments + // remove comments return preg_replace('!/\*.*?\*/!s', '', $stylesheet); } @@ -111,7 +111,6 @@ EOL; private function setStyles($styles) { $this->_styles = $styles; - return $this; } diff --git a/lib/Form/Widget.php b/lib/Form/Widget.php index dea2158a09..2dfff48ac3 100644 --- a/lib/Form/Widget.php +++ b/lib/Form/Widget.php @@ -154,12 +154,14 @@ class Widget extends \WP_Widget { $output = ''; if(!empty($body)) { + $form_id = $this->id_base.'_'.$this->number; + $data = array( - 'form_id' => $this->id_base.'_'.$this->number, + 'form_id' => $form_id, 'form_type' => $form_type, 'form' => $form, 'title' => $title, - 'styles' => FormRenderer::renderStyles($form), + 'styles' => FormRenderer::renderStyles($form, '#'.$form_id), 'html' => FormRenderer::renderHTML($form), 'before_widget' => (!empty($before_widget) ? $before_widget : ''), 'after_widget' => (!empty($after_widget) ? $after_widget : ''), diff --git a/lib/Models/Model.php b/lib/Models/Model.php index 33a59b37c1..a18f14f273 100644 --- a/lib/Models/Model.php +++ b/lib/Models/Model.php @@ -41,7 +41,24 @@ class Model extends \Sudzy\ValidModel { } catch(\Sudzy\ValidationException $e) { $this->setError($e->getValidationErrors()); } catch(\PDOException $e) { - $this->setError($e->getMessage()); + switch($e->getCode()) { + case 23000: + preg_match("/for key \'(.*?)\'/i", $e->getMessage(), $matches); + if(isset($matches[1])) { + $column = $matches[1]; + $this->setError( + sprintf( + __('Another record already exists. Please specify a different "%1$s".'), + $column + ) + ); + } else { + $this->setError($e->getMessage()); + } + break; + default: + $this->setError($e->getMessage()); + } } catch(\Exception $e) { $this->setError($e->getMessage()); } diff --git a/lib/Models/Segment.php b/lib/Models/Segment.php index 615983bdac..41ef874e1a 100644 --- a/lib/Models/Segment.php +++ b/lib/Models/Segment.php @@ -122,6 +122,7 @@ class Segment extends Model { ->group_by(self::$_table.'.id') ->group_by(self::$_table.'.name') ->where(self::$_table.'.type', 'default') + ->whereNull(self::$_table.'.deleted_at') ->findArray(); } diff --git a/tests/unit/Models/SegmentCest.php b/tests/unit/Models/SegmentCest.php index 21788f2785..b5bd8ce031 100644 --- a/tests/unit/Models/SegmentCest.php +++ b/tests/unit/Models/SegmentCest.php @@ -56,7 +56,9 @@ class SegmentCest { $errors = $result->getErrors(); expect(is_array($errors))->true(); - expect($errors[0])->contains('Duplicate'); + expect($errors[0])->equals( + 'Another record already exists. Please specify a different "name".' + ); } function itCanHaveDescription() { diff --git a/tests/unit/Router/SegmentsCest.php b/tests/unit/Router/SegmentsCest.php index 6113321c08..e7fd485c10 100644 --- a/tests/unit/Router/SegmentsCest.php +++ b/tests/unit/Router/SegmentsCest.php @@ -61,7 +61,9 @@ class SegmentsCest { $router = new Segments(); $response = $router->save($duplicate_entry); expect($response['result'])->false(); - expect($response['errors'][0])->contains('Duplicate'); + expect($response['errors'][0])->equals( + 'Another record already exists. Please specify a different "name".' + ); } function itCanRestoreASegment() { diff --git a/views/form/editor.html b/views/form/editor.html index 991ed07b62..5717d364a5 100644 --- a/views/form/editor.html +++ b/views/form/editor.html @@ -50,6 +50,8 @@ name="segments" data-placeholder="<%= __('Choose a list') %>" multiple + data-parsley-required="true" + data-parsley-required-message="<%= __('You need to select a list.') %>" > <% for segment in segments %> @@ -303,19 +305,46 @@ ); // toolbar sections - $(document).on('click', '.mailpoet_toolbar_section.closed', function() { + $(document).on( + 'click', + '.mailpoet_toolbar_section.closed', + mailpoet_toolbar_tab + ); + + function mailpoet_toolbar_tab(tab, callback) { + var section = null; + + if($.type(tab) === 'string') { + section = $('.mailpoet_toolbar_section[data-section="'+tab+'"]'); + } else { + section = $(this); + } + + var current_section = $('.mailpoet_toolbar_section:not(.closed)'); + + if(current_section.data('section') === section.data('section')) { + if(callback !== undefined && $.type(callback) === 'function') { + callback(); + } + } else { // close currently opened section $('.mailpoet_toolbar_section:not(.closed)').addClass('closed'); + // open selected section after a mini delay setTimeout(function() { - $(this).removeClass('closed'); - }.bind(this), 250); - return false; - }); + $(section).removeClass('closed'); + if(callback !== undefined && $.type(callback) === 'function') { + setTimeout(function() { + callback(); + }, 151); + } + }.bind(this), 150); + } + return false; + } // toolbar: open default section - $('.mailpoet_toolbar_section[data-section="fields"]') - .removeClass('closed'); + mailpoet_toolbar_tab('settings'); // form: edit name (in place editor) $('#mailpoet_form_edit_name').on('click', function() { @@ -328,7 +357,7 @@ mailpoet_edit_form_name(); return false; } - }) + }); function mailpoet_edit_form_name() { var is_editing = $('#mailpoet_form_name') @@ -433,46 +462,52 @@ var form = WysijaForm.save(); form.id = $('#mailpoet_form_id').val(); - MailPoet.Ajax.post({ - endpoint: 'forms', - action: 'saveEditor', - data: form - }).done(function(response) { - if(response.result === false) { - if(response.errors.length > 0) { - MailPoet.Notice.error(response.errors.join('
')); - } else { - MailPoet.Notice.error( - "<%= __('An error occured, please reload the page and try again.') %>" - ); + if(jQuery('#mailpoet_form_settings').parsley().isValid() === false) { + mailpoet_toolbar_tab('settings', function() { + jQuery('#mailpoet_form_settings').parsley().validate(); + }); + } else { + MailPoet.Ajax.post({ + endpoint: 'forms', + action: 'saveEditor', + data: form + }).done(function(response) { + if(response.result === false) { + if(response.errors.length > 0) { + MailPoet.Notice.error(response.errors.join('
')); + } else { + MailPoet.Notice.error( + "<%= __('An error occured, please reload the page and try again.') %>" + ); + } + + return false; } - return false; - } + if(callback !== false) { + var message = null; - if(callback !== false) { - var message = null; + if(response.is_widget === true) { + message = "<%= __('Saved! The changes are already active in your widget.') %>"; + } else { + message = "<%= __('Saved! Add this form to %1$sa widget%2$s.') | format("
", '') | raw %>"; + } - if(response.is_widget === true) { - message = "<%= __('Saved! The changes are already active in your widget.') %>"; - } else { - message = "<%= __('Saved! Add this form to %1$sa widget%2$s.') | format("", '') | raw %>"; + if(message !== null) { + MailPoet.Notice.hide(); + MailPoet.Notice.success(message, { + scroll: true, + static: (response.is_widget === false) + }); + } + + // if there is a callback, call it! + if(callback !== undefined) { + callback(); + } } - - if(message !== null) { - MailPoet.Notice.hide(); - MailPoet.Notice.success(message, { - scroll: true, - static: (response.is_widget === false) - }); - } - - // if there is a callback, call it! - if(callback !== undefined) { - callback(); - } - } - }); + }); + } } window.mailpoet_form_save = mailpoet_form_save; @@ -546,7 +581,7 @@ var name = $(this).siblings('.mailpoet_form_field').attr('wysija_name'); if(window.confirm( - "<%= __('Deleting this field here will delete the data associated to it for all you users. Confirm you want to delete.') %>" + "<%= __('Delete this field for all your subscribers?') %>" )) { MailPoet.Ajax.post({ endpoint: 'customFields', diff --git a/views/form/widget.html b/views/form/widget.html index 1f08ccb1eb..8593cd09a8 100644 --- a/views/form/widget.html +++ b/views/form/widget.html @@ -5,10 +5,9 @@

<%= title | raw %>

<% endif %> -
+
<%= styles | raw %>