CustomFields

- renamed form block type "input" to "text" for consistency with React
- updated CustomFields router to comply with main router
- unit tests for CF router
This commit is contained in:
Jonathan Labreuille
2016-01-29 17:14:01 +01:00
parent 24f96d9d7d
commit bb1cc997cc
11 changed files with 146 additions and 28 deletions

View File

@ -53,10 +53,6 @@ define(
var custom_fields = window.mailpoet_custom_fields || [];
custom_fields.map(custom_field => {
if(custom_field.type === 'input') {
custom_field.type = 'text';
}
let field = {
name: 'cf_' + custom_field.id,
label: custom_field.name,

View File

@ -1,7 +1,7 @@
<?php
namespace MailPoet\Form\Block;
class Input extends Base {
class Text extends Base {
static function render($block) {
$type = 'text';
@ -15,7 +15,7 @@ class Input extends Base {
$html .= static::renderLabel($block);
$html .= '<input type="'.$type.'" class="mailpoet_input" ';
$html .= '<input type="'.$type.'" class="mailpoet_text" ';
$html .= 'name="'.static::getFieldName($block).'" ';

View File

@ -49,7 +49,7 @@ class Renderer {
private static function renderBlock($block = array()) {
$html = '';
switch ($block['type']) {
switch($block['type']) {
case 'html':
$html .= Block\Html::render($block);
break;
@ -78,8 +78,8 @@ class Renderer {
$html .= Block\Select::render($block);
break;
case 'input':
$html .= Block\Input::render($block);
case 'text':
$html .= Block\Text::render($block);
break;
case 'textarea':

View File

@ -54,7 +54,7 @@ class CustomField extends Model {
}
// set name as label by default
if(empty($data['params']['label'])) {
if(empty($data['params']['label']) && isset($data['name'])) {
$data['params']['label'] = $data['name'];
}

View File

@ -14,22 +14,20 @@ class CustomFields {
return $custom_field->asArray();
}, $collection);
wp_send_json($custom_fields);
return $custom_fields;
}
function delete($id) {
$custom_field = CustomField::findOne($id);
if($custom_field === false or !$custom_field->id()) {
wp_send_json(array(
'result' => false
));
return array('result' => false);
} else {
$custom_field->delete();
wp_send_json(array(
return array(
'result' => true,
'field' => $custom_field->asArray()
));
);
}
}
@ -58,16 +56,15 @@ class CustomFields {
}
}
wp_send_json($result);
return $result;
}
function get($id) {
$custom_field = CustomField::findOne($id);
if($custom_field === false) {
wp_send_json(false);
return false;
} else {
$custom_field = $custom_field->asArray();
wp_send_json($custom_field);
return $custom_field->asArray();
}
}
}

View File

@ -63,7 +63,7 @@ class Forms {
array(
'id' => 'email',
'name' => __('Email'),
'type' => 'input',
'type' => 'text',
'static' => true,
'params' => array(
'label' => __('Email'),

View File

@ -0,0 +1,125 @@
<?php
use \MailPoet\Router\CustomFields;
use \MailPoet\Models\CustomField;
class CustomFieldsCest {
private $custom_fields = array(
array(
'name' => 'CF: text',
'type' => 'text',
'params' => array(
'required' => '1',
'validate' => '',
'label' => 'CF: text'
)
),
array(
'name' => 'CF: textarea',
'type' => 'textarea',
'params' => array(
'required' => '1',
'validate' => '',
'label' => 'CF: text area'
)
),
array(
'name' => 'CF: radio',
'type' => 'radio',
'params' => array(
'values' =>
array(
array('value' => 'one'),
array('value' => 'two'),
array('value' => 'three')
),
'required' => '1',
'label' => 'CF: radio'
)
),
array(
'name' => 'CF: date',
'type' => 'date',
'params' => array(
'required' => '1',
'date_type' => 'year_month_day',
'date_format' => '',
'label' => 'CF: date'
)
)
);
function _before() {
foreach($this->custom_fields as $custom_field) {
CustomField::createOrUpdate($custom_field);
}
}
function itCanGetAllCustomFields() {
$router = new CustomFields();
$response = $router->getAll();
expect($response)->count(count($this->custom_fields));
foreach($response as $custom_field) {
expect($custom_field['name'])->notEmpty();
expect($custom_field['type'])->notEmpty();
expect($custom_field['params'])->notEmpty();
}
}
function itCanDeleteACustomField() {
$custom_field = CustomField::where('type', 'date')->findOne();
$router = new CustomFields();
$response = $router->delete($custom_field->id());
expect($response['result'])->true();
$custom_field = CustomField::where('type', 'date')->findOne();
expect($custom_field)->false();
}
function itCanSaveACustomField() {
$new_custom_field = array(
'name' => 'New custom field',
'type' => 'text'
);
$router = new CustomFields();
$response = $router->save($new_custom_field);
expect($response['result'])->true();
// missing type
$response = $router->save(array('name' => 'New custom field'));
expect($response['result'])->false();
expect($response['errors'][0])->equals('You need to specify a type.');
// missing name
$response = $router->save(array('type' => 'text'));
expect($response['result'])->false();
expect($response['errors'][0])->equals('You need to specify a name.');
// missing data
$response = $router->save();
expect($response['result'])->false();
expect($response['errors'][0])->equals('You need to specify a name.');
expect($response['errors'][1])->equals('You need to specify a type.');
}
function itCanGetACustomField() {
$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('not_an_id');
expect($response)->false();
}
function _after() {
ORM::forTable(CustomField::$_table)->deleteMany();
}
}

View File

@ -219,7 +219,7 @@
{
id: "first_name",
name: "<%= __('First name') %>",
type: 'input',
type: 'text',
params: {
label: "<%= __('First name') %>"
},
@ -228,7 +228,7 @@
{
id: "last_name",
name: "<%= __('Last name') %>",
type: 'input',
type: 'text',
params: {
label: "<%= __('Last name') %>"
},
@ -620,7 +620,7 @@
<!-- block templates -->
<%= partial('form_template_block', 'form/templates/blocks/container.hbs') %>
<%= partial('form_template_divider', 'form/templates/blocks/divider.hbs') %>
<%= partial('form_template_input', 'form/templates/blocks/input.hbs') %>
<%= partial('form_template_text', 'form/templates/blocks/text.hbs') %>
<%= partial('form_template_submit', 'form/templates/blocks/submit.hbs') %>
<%= partial('form_template_segment', 'form/templates/blocks/segment.hbs') %>
<%= partial('form_template_radio', 'form/templates/blocks/radio.hbs') %>
@ -705,7 +705,7 @@
) %>
<!-- field settings depending on field type -->
<script id="form_template_field_input" type="text/x-handlebars-template">
<script id="form_template_field_text" type="text/x-handlebars-template">
{{> _settings_required }}
{{> _settings_validate }}
</script>

View File

@ -3,7 +3,7 @@
{{> _settings_label }}
{{/ifCond}}
{{#ifCond type '==' 'input'}}
{{#ifCond type '==' 'text'}}
{{> _settings_label }}
{{> _settings_label_within }}
{{#ifCond id 'in' 'first_name,last_name' }}

View File

@ -17,8 +17,8 @@
>
<option value="">--</option>
<option
{{#ifCond type '==' 'input'}}selected="selected"{{/ifCond}}
value="input"><%= __('Text Input') %>
{{#ifCond type '==' 'text'}}selected="selected"{{/ifCond}}
value="text"><%= __('Text Input') %>
</option>
<option
{{#ifCond type '==' 'textarea'}}selected="selected"{{/ifCond}}