category field type backend

This commit is contained in:
Leo Germani 2018-03-04 14:41:03 -03:00
parent e69f003de7
commit fdd6203df2
6 changed files with 63 additions and 13 deletions

View File

@ -370,7 +370,7 @@ class Field extends Entity {
$fto = $this->get_field_type_object(); $fto = $this->get_field_type_object();
if (is_object($fto)) { if (is_object($fto)) {
$is_valid = $fto->validate_options($this->get_field_type_options()); $is_valid = $fto->validate_options($this);
} }
if (true === $is_valid) if (true === $is_valid)

View File

@ -60,18 +60,49 @@ class Category extends Field_Type {
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
</td>
<td>
<label><?php echo __('Allow creation of new terms','tainacan'); ?></label><br/>
<small><?php echo __('If checked, users may create new terms for this category, otherwise they can only selected from existing terms.','tainacan'); ?></small>
</td>
<td>
<input type="checkbox" name="allow_new_terms" <?php checked(true, $this->get_option('allow_new_terms')); ?> >
<label>Allow</label>
</td> </td>
</tr> </tr>
<?php <?php
} }
public function validate_options(Array $options) { public function validate_options(\Tainacan\Entities\Field $field) {
if ( !in_array($field->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
if (empty($this->get_option('taxonomy_id')))
return ['taxonomy_id' => __('Please select a category', 'tainacan')];
global $Tainacan_Fields;
$category_fields = $Tainacan_Fields->fetch([
'collection_id' => $field->get_collection_id(),
'field_type' => 'Tainacan\\Field_Types\\Category'
], 'OBJECT');
$category_fields = array_map(function ($field) {
$fto = $field->get_field_type_object();
return $fto->get_option('taxonomy_id');
}, $category_fields);
if (in_array($this->get_option('taxonomy_id'), $category_fields)) {
return ['taxonomy_id' => __('You can not have 2 Category Fields using the same category in a collection', 'tainacan')];
}
return true; return true;
// TODO validate required and unique taxonomy
} }
/** /**
* Validate item based on field type categores options * Validate item based on field type categories options
* *
* @param TainacanEntitiesItem_Metadata_Entity $item_metadata * @param TainacanEntitiesItem_Metadata_Entity $item_metadata
* @return bool Valid or not * @return bool Valid or not
@ -88,6 +119,10 @@ class Category extends Field_Type {
if (false === $this->get_option('allow_new_terms')) { if (false === $this->get_option('allow_new_terms')) {
$terms = $item_metadata->get_value(); $terms = $item_metadata->get_value();
if (false === $terms)
return true;
if (!is_array($terms)) if (!is_array($terms))
$terms = array($terms); $terms = array($terms);

View File

@ -145,10 +145,10 @@ abstract class Field_Type {
* *
* This method should be declared by each field type sub classes * This method should be declared by each field type sub classes
* *
* @param Array $options Options to be saved as field_type_optins * @param \Tainacan\Entities\Field $field The field object that is beeing validated
* @return true|Array True if optinos are valid. If invalid, returns an array where keys are the field keys and values are error messages. * @return true|Array True if optinos are valid. If invalid, returns an array where keys are the field keys and values are error messages.
*/ */
public function validate_options(Array $options) { public function validate_options(\Tainacan\Entities\Field $field) {
return true; return true;
} }

View File

@ -98,9 +98,9 @@ class Relationship extends Field_Type {
<?php <?php
} }
public function validate_options(Array $options) { public function validate_options(\Tainacan\Entities\Field $field) {
// TODO: This is just a sample validation to test validation workflow for field types. Must redo it // TODO: This is just a sample validation to test validation workflow for field types. Must redo it
if (isset($options['collection_id']) && !is_numeric($options['collection_id'])) { if (!empty($this->get_option('collection_id')) && !is_numeric($this->get_option('collection_id'))) {
return [ return [
'collection_id' => 'Collection ID invalid' 'collection_id' => 'Collection ID invalid'
]; ];

View File

@ -286,7 +286,9 @@ class Fields extends Repository {
'posts_per_page' => -1, 'posts_per_page' => -1,
'post_status' => 'publish' 'post_status' => 'publish'
], $args); ], $args);
$args = $this->parse_fetch_args($args);
$args['post_type'] = Entities\Field::get_post_type(); $args['post_type'] = Entities\Field::get_post_type();
$wp_query = new \WP_Query($args); $wp_query = new \WP_Query($args);

View File

@ -40,10 +40,11 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
$field = $this->tainacan_entity_factory->create_entity( $field = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
'name' => 'metadado', 'name' => 'meta',
'description' => 'title', 'description' => 'description',
'collection' => $collection, 'collection' => $collection,
'field_type' => 'Tainacan\Field_Types\Category', 'field_type' => 'Tainacan\Field_Types\Category',
'status' => 'publish',
'field_type_options' => [ 'field_type_options' => [
'taxonomy_id' => $tax->get_db_identifier(), 'taxonomy_id' => $tax->get_db_identifier(),
'allow_new_terms' => false 'allow_new_terms' => false
@ -55,10 +56,11 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
$field2 = $this->tainacan_entity_factory->create_entity( $field2 = $this->tainacan_entity_factory->create_entity(
'field', 'field',
array( array(
'name' => 'metadado_desc', 'name' => 'meta2',
'description' => 'description', 'description' => 'description',
'collection' => $collection, 'collection' => $collection,
'field_type' => 'Tainacan\Field_Types\Category' 'field_type' => 'Tainacan\Field_Types\Category',
'status' => 'draft',
), ),
true true
); );
@ -111,6 +113,17 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $field); $check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $field);
$this->assertEquals('WP_Term', get_class($check_item_metadata->get_value())); $this->assertEquals('WP_Term', get_class($check_item_metadata->get_value()));
// test 2 fields with same category
$field2->set_field_type_options([
'taxonomy_id' => $tax->get_db_identifier(),
]);
$field2->set_status('publish');
$this->assertFalse($field2->validate(), 'Category Field should not validate when using a category in use by another field in the same collection');
$errors = $field2->get_errors();
$this->assertInternalType('array', $errors);
$this->assertArrayHasKey('taxonomy_id', $errors[0]);
} }
} }