diff --git a/lib/API/Endpoints/CustomFields.php b/lib/API/Endpoints/CustomFields.php index 5bdfcc4502..1dcfa908b4 100644 --- a/lib/API/Endpoints/CustomFields.php +++ b/lib/API/Endpoints/CustomFields.php @@ -1,34 +1,32 @@ 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()); } } } \ No newline at end of file diff --git a/tests/unit/API/CustomFieldsTest.php b/tests/unit/API/CustomFieldsTest.php index 4f646cb613..f0faec7d40 100644 --- a/tests/unit/API/CustomFieldsTest.php +++ b/tests/unit/API/CustomFieldsTest.php @@ -1,6 +1,7 @@ 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() { diff --git a/views/form/editor.html b/views/form/editor.html index 416a057f8e..32b573fd98 100644 --- a/views/form/editor.html +++ b/views/form/editor.html @@ -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('
')); - } 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 - }); + MailPoet.Modal.popup({ + title: "<%= __('Edit field') %>", + template: $('#form_template_field_form').html(), + 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 - }).done(function(response) { - if(response.result === true) { - item.remove(); - - if(response.field !== undefined) { - WysijaForm.removeBlock(response.field, function() { - mailpoet_form_save(false); - }); - } - - mailpoet_form_fields(); - MailPoet.Notice.success( - "<%= __('Removed custom field “"+name+"“') %>" - ); + data: { + id: id } + }).done(function(response) { + item.remove(); + + WysijaForm.removeBlock(id, function() { + mailpoet_form_save(false); + }); + + mailpoet_form_fields(); + MailPoet.Notice.success( + "<%= __('Removed custom field “"+name+"“') %>" + ); }); } }); diff --git a/views/form/templates/settings/field_form.hbs b/views/form/templates/settings/field_form.hbs index 1ac51dea8e..d315e17508 100644 --- a/views/form/templates/settings/field_form.hbs +++ b/views/form/templates/settings/field_form.hbs @@ -78,32 +78,31 @@ action: 'save', data: data }).done(function(response) { - if(response.result === true) { - // close popup - MailPoet.Modal.close(); + // close popup + MailPoet.Modal.close(); - if(WysijaForm.updateBlock(response.field) === true) { - // trigger save, if a block has been updated - mailpoet_form_save(false); - } + if(WysijaForm.updateBlock(response.data) === true) { + // trigger save, if a block has been updated + mailpoet_form_save(false); + } - mailpoet_form_fields(); + mailpoet_form_fields(); - if(data.id) { - MailPoet.Notice.success( - "<%= __('Updated custom field “"+data.name+"“') %>" - ); - } else { - MailPoet.Notice.success( - "<%= __('Added custom field “"+data.name+"“') %>" - ); - } + if(data.id) { + MailPoet.Notice.success( + "<%= __('Updated custom field “"+data.name+"“') %>" + ); } else { - if(response.errors.length > 0) { - $(response.errors).each(function(i, error) { - MailPoet.Notice.error(error, {positionAfter: '#field_name'}); - }); - } + MailPoet.Notice.success( + "<%= __('Added custom field “"+data.name+"“') %>" + ); + } + }).fail(function(response) { + if(response.errors.length > 0) { + MailPoet.Notice.error( + response.errors.map(function(error) { return error.message; }), + { positionAfter: '#field_name' } + ); } }); return false;