updated Custom Field endpoint + Unit tests + form editor update
This commit is contained in:
@@ -1,34 +1,32 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\API\Endpoints;
|
namespace MailPoet\API\Endpoints;
|
||||||
|
use \MailPoet\API\Endpoint as APIEndpoint;
|
||||||
|
use \MailPoet\API\Error as APIError;
|
||||||
use \MailPoet\Models\CustomField;
|
use \MailPoet\Models\CustomField;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class CustomFields {
|
class CustomFields extends APIEndpoint {
|
||||||
function __construct() {
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAll() {
|
function getAll() {
|
||||||
$collection = CustomField::findMany();
|
$collection = CustomField::findMany();
|
||||||
$custom_fields = array_map(function($custom_field) {
|
$custom_fields = array_map(function($custom_field) {
|
||||||
return $custom_field->asArray();
|
return $custom_field->asArray();
|
||||||
}, $collection);
|
}, $collection);
|
||||||
|
|
||||||
return $custom_fields;
|
return $this->successResponse($custom_fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
function delete($id) {
|
function delete($data = array()) {
|
||||||
|
$id = (isset($data['id']) ? (int)$data['id'] : null);
|
||||||
$custom_field = CustomField::findOne($id);
|
$custom_field = CustomField::findOne($id);
|
||||||
if($custom_field === false or !$custom_field->id()) {
|
if($custom_field === false) {
|
||||||
return array('result' => false);
|
return $this->errorResponse(array(
|
||||||
|
APIError::NOT_FOUND => __('This custom field does not exist.')
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
$custom_field->delete();
|
$custom_field->delete();
|
||||||
|
|
||||||
return array(
|
return $this->successResponse($custom_field->asArray());
|
||||||
'result' => true,
|
|
||||||
'field' => $custom_field->asArray()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,24 +35,23 @@ class CustomFields {
|
|||||||
$errors = $custom_field->getErrors();
|
$errors = $custom_field->getErrors();
|
||||||
|
|
||||||
if(!empty($errors)) {
|
if(!empty($errors)) {
|
||||||
return array(
|
return $this->badRequest($errors);
|
||||||
'result' => false,
|
|
||||||
'errors' => $errors
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
return array(
|
return $this->successResponse(
|
||||||
'result' => true,
|
CustomField::findOne($custom_field->id)->asArray()
|
||||||
'field' => $custom_field->asArray()
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function get($id) {
|
function get($data = array()) {
|
||||||
|
$id = (isset($data['id']) ? (int)$data['id'] : null);
|
||||||
$custom_field = CustomField::findOne($id);
|
$custom_field = CustomField::findOne($id);
|
||||||
if($custom_field === false) {
|
if($custom_field === false) {
|
||||||
return false;
|
return $this->errorResponse(array(
|
||||||
|
APIError::NOT_FOUND => __('This custom field does not exist.')
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
return $custom_field->asArray();
|
return $this->successResponse($custom_field->asArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use \MailPoet\API\Endpoints\CustomFields;
|
use \MailPoet\API\Endpoints\CustomFields;
|
||||||
|
use \MailPoet\API\Response as APIResponse;
|
||||||
use \MailPoet\Models\CustomField;
|
use \MailPoet\Models\CustomField;
|
||||||
|
|
||||||
class CustomFieldsTest extends MailPoetTest {
|
class CustomFieldsTest extends MailPoetTest {
|
||||||
@@ -58,9 +59,10 @@ class CustomFieldsTest extends MailPoetTest {
|
|||||||
function testItCanGetAllCustomFields() {
|
function testItCanGetAllCustomFields() {
|
||||||
$router = new CustomFields();
|
$router = new CustomFields();
|
||||||
$response = $router->getAll();
|
$response = $router->getAll();
|
||||||
expect($response)->count(count($this->custom_fields));
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
expect($response->data)->count(count($this->custom_fields));
|
||||||
|
|
||||||
foreach($response as $custom_field) {
|
foreach($response->data as $custom_field) {
|
||||||
expect($custom_field['name'])->notEmpty();
|
expect($custom_field['name'])->notEmpty();
|
||||||
expect($custom_field['type'])->notEmpty();
|
expect($custom_field['type'])->notEmpty();
|
||||||
expect($custom_field['params'])->notEmpty();
|
expect($custom_field['params'])->notEmpty();
|
||||||
@@ -72,14 +74,14 @@ class CustomFieldsTest extends MailPoetTest {
|
|||||||
$custom_field_id = $custom_field->id();
|
$custom_field_id = $custom_field->id();
|
||||||
|
|
||||||
$router = new CustomFields();
|
$router = new CustomFields();
|
||||||
$response = $router->delete($custom_field_id);
|
$response = $router->delete(array('id' => $custom_field_id));
|
||||||
expect($response['result'])->true();
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
|
||||||
$custom_field = CustomField::where('type', 'date')->findOne();
|
$custom_field = CustomField::where('type', 'date')->findOne();
|
||||||
expect($custom_field)->false();
|
expect($custom_field)->false();
|
||||||
|
|
||||||
$response = $router->delete($custom_field_id);
|
$response = $router->delete(array('id' => $custom_field_id));
|
||||||
expect($response['result'])->false();
|
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanSaveACustomField() {
|
function testItCanSaveACustomField() {
|
||||||
@@ -90,37 +92,37 @@ class CustomFieldsTest extends MailPoetTest {
|
|||||||
|
|
||||||
$router = new CustomFields();
|
$router = new CustomFields();
|
||||||
$response = $router->save($new_custom_field);
|
$response = $router->save($new_custom_field);
|
||||||
expect($response['result'])->true();
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
|
||||||
// missing type
|
// missing type
|
||||||
$response = $router->save(array('name' => 'New custom field'));
|
$response = $router->save(array('name' => 'New custom field'));
|
||||||
expect($response['result'])->false();
|
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||||
expect($response['errors'][0])->equals('Please specify a type');
|
expect($response->errors[0]['message'])->equals('Please specify a type');
|
||||||
|
|
||||||
// missing name
|
// missing name
|
||||||
$response = $router->save(array('type' => 'text'));
|
$response = $router->save(array('type' => 'text'));
|
||||||
expect($response['result'])->false();
|
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||||
expect($response['errors'][0])->equals('Please specify a name');
|
expect($response->errors[0]['message'])->equals('Please specify a name');
|
||||||
|
|
||||||
// missing data
|
// missing data
|
||||||
$response = $router->save();
|
$response = $router->save();
|
||||||
expect($response['result'])->false();
|
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||||
expect($response['errors'][0])->equals('Please specify a name');
|
expect($response->errors[0]['message'])->equals('Please specify a name');
|
||||||
expect($response['errors'][1])->equals('Please specify a type');
|
expect($response->errors[1]['message'])->equals('Please specify a type');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanGetACustomField() {
|
function testItCanGetACustomField() {
|
||||||
$custom_field = CustomField::where('name', 'CF: text')->findOne();
|
$custom_field = CustomField::where('name', 'CF: text')->findOne();
|
||||||
|
|
||||||
$router = new CustomFields();
|
$router = new CustomFields();
|
||||||
$response = $router->get($custom_field->id());
|
$response = $router->get(array('id' => $custom_field->id()));
|
||||||
expect($response)->notEmpty();
|
|
||||||
expect($response['name'])->equals('CF: text');
|
|
||||||
expect($response['type'])->equals('text');
|
|
||||||
expect($response['params'])->notEmpty();
|
|
||||||
|
|
||||||
$response = $router->get('not_an_id');
|
expect($response->data['name'])->equals('CF: text');
|
||||||
expect($response)->false();
|
expect($response->data['type'])->equals('text');
|
||||||
|
expect($response->data['params'])->notEmpty();
|
||||||
|
|
||||||
|
$response = $router->get(array('id' => 'not_an_id'));
|
||||||
|
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
|
@@ -282,9 +282,7 @@
|
|||||||
endpoint: 'customFields',
|
endpoint: 'customFields',
|
||||||
action: 'getAll',
|
action: 'getAll',
|
||||||
}).done(function(response) {
|
}).done(function(response) {
|
||||||
if(response !== false) {
|
data.fields = $.merge(response.data, 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));
|
||||||
@@ -489,18 +487,6 @@
|
|||||||
action: 'saveEditor',
|
action: 'saveEditor',
|
||||||
data: form
|
data: form
|
||||||
}).done(function(response) {
|
}).done(function(response) {
|
||||||
if(response.result === false) {
|
|
||||||
if(response.errors.length > 0) {
|
|
||||||
MailPoet.Notice.error(response.errors.join('<br />'));
|
|
||||||
} else {
|
|
||||||
MailPoet.Notice.error(
|
|
||||||
"<%= __('An error occurred. Please reload the page and try again.') %>"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(callback !== false) {
|
if(callback !== false) {
|
||||||
var message = null;
|
var message = null;
|
||||||
|
|
||||||
@@ -523,6 +509,13 @@
|
|||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}).fail(function(response) {
|
||||||
|
if (response.errors.length > 0) {
|
||||||
|
MailPoet.Notice.error(
|
||||||
|
response.errors.map(function(error) { return error.message; }),
|
||||||
|
{ scroll: true }
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -579,14 +572,21 @@
|
|||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
endpoint: 'customFields',
|
endpoint: 'customFields',
|
||||||
action: 'get',
|
action: 'get',
|
||||||
data: id
|
data: {
|
||||||
|
id: id
|
||||||
|
}
|
||||||
}).done(function(response) {
|
}).done(function(response) {
|
||||||
if(response.result !== false) {
|
|
||||||
MailPoet.Modal.popup({
|
MailPoet.Modal.popup({
|
||||||
title: "<%= __('Edit field') %>",
|
title: "<%= __('Edit field') %>",
|
||||||
template: $('#form_template_field_form').html(),
|
template: $('#form_template_field_form').html(),
|
||||||
data: response
|
data: response.data
|
||||||
});
|
});
|
||||||
|
}).fail(function(response) {
|
||||||
|
if (response.errors.length > 0) {
|
||||||
|
MailPoet.Notice.error(
|
||||||
|
response.errors.map(function(error) { return error.message; }),
|
||||||
|
{ scroll: true }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -603,22 +603,20 @@
|
|||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
endpoint: 'customFields',
|
endpoint: 'customFields',
|
||||||
action: 'delete',
|
action: 'delete',
|
||||||
data: id
|
data: {
|
||||||
|
id: id
|
||||||
|
}
|
||||||
}).done(function(response) {
|
}).done(function(response) {
|
||||||
if(response.result === true) {
|
|
||||||
item.remove();
|
item.remove();
|
||||||
|
|
||||||
if(response.field !== undefined) {
|
WysijaForm.removeBlock(id, function() {
|
||||||
WysijaForm.removeBlock(response.field, function() {
|
|
||||||
mailpoet_form_save(false);
|
mailpoet_form_save(false);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
mailpoet_form_fields();
|
mailpoet_form_fields();
|
||||||
MailPoet.Notice.success(
|
MailPoet.Notice.success(
|
||||||
"<%= __('Removed custom field “"+name+"“') %>"
|
"<%= __('Removed custom field “"+name+"“') %>"
|
||||||
);
|
);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@@ -78,11 +78,10 @@
|
|||||||
action: 'save',
|
action: 'save',
|
||||||
data: data
|
data: data
|
||||||
}).done(function(response) {
|
}).done(function(response) {
|
||||||
if(response.result === true) {
|
|
||||||
// close popup
|
// close popup
|
||||||
MailPoet.Modal.close();
|
MailPoet.Modal.close();
|
||||||
|
|
||||||
if(WysijaForm.updateBlock(response.field) === true) {
|
if(WysijaForm.updateBlock(response.data) === true) {
|
||||||
// trigger save, if a block has been updated
|
// trigger save, if a block has been updated
|
||||||
mailpoet_form_save(false);
|
mailpoet_form_save(false);
|
||||||
}
|
}
|
||||||
@@ -98,12 +97,12 @@
|
|||||||
"<%= __('Added custom field “"+data.name+"“') %>"
|
"<%= __('Added custom field “"+data.name+"“') %>"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
}).fail(function(response) {
|
||||||
if(response.errors.length > 0) {
|
if(response.errors.length > 0) {
|
||||||
$(response.errors).each(function(i, error) {
|
MailPoet.Notice.error(
|
||||||
MailPoet.Notice.error(error, {positionAfter: '#field_name'});
|
response.errors.map(function(error) { return error.message; }),
|
||||||
});
|
{ positionAfter: '#field_name' }
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user