From 1ce4b16327a7a4de52ccab60a356cf9da66e21c0 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Thu, 5 Nov 2015 19:43:02 +0100 Subject: [PATCH] custom fields (create and update) --- lib/Models/CustomField.php | 29 ++++ lib/Router/CustomFields.php | 147 ++++-------------- views/form/editor.html | 162 +++++++++++--------- views/form/templates/settings/field_new.hbs | 77 +++++----- 4 files changed, 185 insertions(+), 230 deletions(-) diff --git a/lib/Models/CustomField.php b/lib/Models/CustomField.php index 0e81a12539..d44dc16bcc 100644 --- a/lib/Models/CustomField.php +++ b/lib/Models/CustomField.php @@ -45,4 +45,33 @@ class CustomField extends Model { 'subscriber_id' )->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; + } } \ No newline at end of file diff --git a/lib/Router/CustomFields.php b/lib/Router/CustomFields.php index a9a79b319a..92aa693f58 100644 --- a/lib/Router/CustomFields.php +++ b/lib/Router/CustomFields.php @@ -29,21 +29,33 @@ class CustomFields { 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) { $custom_field = CustomField::findOne($id); @@ -54,111 +66,4 @@ class CustomFields { 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); - } - - -} +} \ No newline at end of file diff --git a/views/form/editor.html b/views/form/editor.html index 69725d71b1..78f4a0e4df 100644 --- a/views/form/editor.html +++ b/views/form/editor.html @@ -200,69 +200,60 @@