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