feat: add validate to conditional metadata section #736

This commit is contained in:
vnmedeiros 2023-02-08 12:11:22 -03:00
parent 47ea0d6a16
commit a69b8c66a8
3 changed files with 95 additions and 1 deletions

View File

@ -18,7 +18,9 @@ class Metadata_Section extends Entity {
$name,
$slug,
$description,
$description_bellow_name;
$description_bellow_name,
$is_conditional_section,
$conditional_section_rules;
/**
* {@inheritDoc}
@ -94,6 +96,34 @@ class Metadata_Section extends Entity {
return $tainacan_metadata_sections->get_metadata_object_list($this->get_id(), $args);
}
/**
* Get if section is conditional.
*
* @return string "yes"|"no"
*/
function get_is_conditional_section() {
return $this->get_mapped_property( 'is_conditional_section' );
}
/**
* get the rules of the conditional section
*
* @return array|object
*/
function get_conditional_section_rules() {
return $this->get_mapped_property( 'conditional_section_rules' );
}
/**
* Checks if section is conditional
*
* @return boolean
*/
function is_conditional_section() {
return $this->get_is_conditional_section() == 'yes';
}
/**
* Set the metadata section name
*
@ -141,6 +171,25 @@ class Metadata_Section extends Entity {
}
/**
* set if section is conditional.
*
* @return void
*/
function set_is_conditional_section($value) {
return $this->set_mapped_property( 'is_conditional_section', $value );
}
/**
* set the rules of the conditional section
*
* @return void
*/
function set_conditional_section_rules($value) {
return $this->set_mapped_property( 'conditional_section_rules', $value );
}
/**
* Transient property used to store the status of the metadatum section for a particular collection
*
@ -168,6 +217,25 @@ class Metadata_Section extends Entity {
$name = $this->get_name();
$collection = $this->get_collection();
if ($this->is_conditional_section()) {
$metadata_section_id = $this->get_id();
if ($metadata_section_id == static::$default_section_slug) {
$this->add_error($this->get_id(), __("conditional section cannot be enabled in default section", 'tainacan'));
$no_errors = false;
} else {
$metadata_list = $this->get_metadata_object_list();
$required_metadata_list = array_filter($metadata_list, function($m) {
return $m->is_required();
});
if ( count($required_metadata_list) ) {
$no_errors = false;
foreach($required_metadata_list as $metadata) {
$this->add_error($metadata->get_id(), __("metadata cannot be required", 'tainacan'));
}
}
}
}
if( empty($collection) ) {
$this->add_error($this->get_id(), __("collection is required", 'tainacan'));
$no_errors = false;

View File

@ -533,6 +533,17 @@ class Metadatum extends Entity {
return false;
}
}
if( $this->is_required() ) {
$meta_section_id = $this->get_metadata_section_id();
if($meta_section_id != \Tainacan\Entities\Metadata_Section::$default_section_slug) {
$meta_section = new \Tainacan\Entities\Metadata_Section($meta_section_id);
if($meta_section->is_conditional_section()) {
$this->add_error($this->get_id(), __('Metadata cannot be required into conditional section', 'tainacan'));
return false;
}
}
}
// You cant have a taxonomy metadatum inside a multiple compound metadatum
if ( $this->get_parent() > 0 && $this->get_metadata_type_object()->get_primitive_type() == 'term' ) {

View File

@ -76,6 +76,21 @@ class Metadata_Sections extends Repository {
'title' => __( 'Collection', 'tainacan' ),
'type' => ['integer', 'string'],
'description' => __( 'The collection ID', 'tainacan' ),
],
'is_conditional_section' => [
'map' => 'meta',
'title' => __( 'Enable conditional section', 'tainacan' ),
'type' => 'string',
'description' => __( 'Enable conditional section', 'tainacan' ),
'on_error' => __( 'Value should be yes or no', 'tainacan' ),
'validation' => v::stringType()->in( [ 'yes', 'no' ] ),
'default' => 'no'
],
'conditional_section_rules' => [
'map' => 'meta',
'title' => __( 'Conditional section rules', 'tainacan' ),
'type' => ['object', 'array'],
'description' => __( 'Conditional section rules', 'tainacan' ),
]
] );
}