From 907fe585de74d180a58772b01dff7e40356a8661 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Fri, 30 Oct 2015 18:12:07 +0100 Subject: [PATCH] add Form renderer and fixed Newsletter saving issue --- lib/Config/Menu.php | 12 +- lib/Form/Block/base.php | 100 ++++++ lib/Form/Block/checkbox.php | 43 +++ lib/Form/Block/date.php | 158 ++++++++++ lib/Form/Block/divider.php | 9 + lib/Form/Block/html.php | 23 ++ lib/Form/Block/input.php | 31 ++ lib/Form/Block/lists.php | 37 +++ lib/Form/Block/radio.php | 42 +++ lib/Form/Block/select.php | 36 +++ lib/Form/Block/submit.php | 17 + lib/Form/Block/textarea.php | 28 ++ lib/Form/Renderer.php | 97 ++++++ lib/Form/Util/export.php | 91 ++++++ lib/Form/Util/styles.php | 47 +++ lib/Form/Widget.php | 612 ++++++++++++++++++++++++++++++++---- lib/Models/Newsletter.php | 21 +- lib/Router/Newsletters.php | 25 +- views/form/editor.html | 13 +- webpack.config.js | 3 + 20 files changed, 1346 insertions(+), 99 deletions(-) create mode 100644 lib/Form/Block/base.php create mode 100644 lib/Form/Block/checkbox.php create mode 100644 lib/Form/Block/date.php create mode 100644 lib/Form/Block/divider.php create mode 100644 lib/Form/Block/html.php create mode 100644 lib/Form/Block/input.php create mode 100644 lib/Form/Block/lists.php create mode 100644 lib/Form/Block/radio.php create mode 100644 lib/Form/Block/select.php create mode 100644 lib/Form/Block/submit.php create mode 100644 lib/Form/Block/textarea.php create mode 100644 lib/Form/Renderer.php create mode 100644 lib/Form/Util/export.php create mode 100644 lib/Form/Util/styles.php diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index 3728188fc4..df10fb9fa0 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -87,8 +87,8 @@ class Menu { function registered_pages() { global $_registered_pages; $pages = array( - //'mailpoet-form-editor' => 'formEditor', - 'mailpoet-newsletter-editor' => array($this, 'newletterForm') + 'mailpoet-form-editor' => array($this, 'formEditor'), + 'mailpoet-newsletter-editor' => array($this, 'newletterEditor') ); foreach($pages as $menu_slug => $callback) { $hookname = get_plugin_page_hookname($menu_slug, null); @@ -193,11 +193,17 @@ class Menu { echo $this->renderer->render('newsletters.html', $data); } - function newletterForm() { + function newletterEditor() { $data = array(); wp_enqueue_media(); wp_enqueue_script('tinymce-wplink', includes_url('js/tinymce/plugins/wplink/plugin.js')); wp_enqueue_style('editor', includes_url('css/editor.css')); echo $this->renderer->render('newsletter/form.html', $data); } + + function formEditor() { + $data = array(); + $data['segments'] = Segment::findArray(); + echo $this->renderer->render('form/editor.html', $data); + } } diff --git a/lib/Form/Block/base.php b/lib/Form/Block/base.php new file mode 100644 index 0000000000..3214d230e4 --- /dev/null +++ b/lib/Form/Block/base.php @@ -0,0 +1,100 @@ + 0) { + // handle single validation rule + $rules[] = 'custom['.$block['params']['validate'].']'; + } + } + + // generate string if there is at least one rule to validate against + if(empty($rules)) { + return ''; + } else { + // make sure rules are not duplicated + $rules = array_unique($rules); + return 'validate['.join(',', $rules).']'; + } + } + + protected static function renderLabel($block) { + $html = ''; + // if the label is displayed as a placeholder, we don't display a label outside + if(isset($block['params']['label_within']) + && $block['params']['label_within']) { + return $html; + } + if(isset($block['params']['label']) + && strlen(trim($block['params']['label'])) > 0) { + $html .= ''; + } + return $html; + } + + protected static function renderInputPlaceholder($block) { + $html = ''; + // if the label is displayed as a placeholder, + if(isset($block['params']['label_within']) && $block['params']['label_within']) { + // display only label + $html .= ' placeholder="'; + $html .= static::getFieldLabel($block); + // add an asterisk if it's a required field + if(isset($block['params']['required']) && $block['params']['required']) { + $html .= ' *'; + } + $html .= '" '; + } + return $html; + } + + // return field name depending on block data + protected static function getFieldName($block = array()) { + return $block['field']; + } + + protected static function getFieldLabel($block = array()) { + return (isset($block['params']['label']) + && strlen(trim($block['params']['label'])) > 0) + ? trim($block['params']['label']) : ''; + } + + protected static function getFieldValue($block = array()) { + return (isset($block['params']['value']) + && strlen(trim($block['params']['value'])) > 0) + ? trim($block['params']['value']) : ''; + } +} \ No newline at end of file diff --git a/lib/Form/Block/checkbox.php b/lib/Form/Block/checkbox.php new file mode 100644 index 0000000000..5b446b19f9 --- /dev/null +++ b/lib/Form/Block/checkbox.php @@ -0,0 +1,43 @@ +'; + + $html .= '

'; + + $html .= static::renderLabel($block); + + foreach($block['params']['values'] as $option) { + $html .= '

'; + + return $html; + } +} diff --git a/lib/Form/Block/input.php b/lib/Form/Block/input.php new file mode 100644 index 0000000000..096e59632f --- /dev/null +++ b/lib/Form/Block/input.php @@ -0,0 +1,31 @@ +'; + + $html .= static::renderLabel($block); + + $html .= ''; + + $html .= '

'; + + $html .= static::renderLabel($block); + + foreach($block['params']['values'] as $option) { + $html .= '

'; + + return $html; + } +} \ No newline at end of file diff --git a/lib/Form/Block/submit.php b/lib/Form/Block/submit.php new file mode 100644 index 0000000000..085dd7c577 --- /dev/null +++ b/lib/Form/Block/submit.php @@ -0,0 +1,17 @@ +'; + + return $html; + } +} \ No newline at end of file diff --git a/lib/Form/Block/textarea.php b/lib/Form/Block/textarea.php new file mode 100644 index 0000000000..8cd36ffda9 --- /dev/null +++ b/lib/Form/Block/textarea.php @@ -0,0 +1,28 @@ +'; + + $html .= static::renderLabel($block); + + $lines = (isset($block['params']['lines']) ? (int)$block['params']['lines'] : 1); + + $html .= '