feat: add option to edit properties of the metadata section #184
This commit is contained in:
parent
c5ec733c2c
commit
6fe458b1e3
|
@ -622,7 +622,7 @@ class REST_Collections_Controller extends REST_Controller {
|
|||
if ( !is_array($val) ) {
|
||||
return false;
|
||||
}
|
||||
if ( !isset($val['id']) || (!is_numeric($val['id']) && $val['id'] != 'default_section' ) ) {
|
||||
if ( !isset($val['id']) || (!is_numeric($val['id']) && $val['id'] != \Tainacan\Entities\Metadata_Section::$default_section_slug ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( !isset($val['enabled']) || !is_bool($val['enabled']) ) {
|
||||
|
@ -646,7 +646,7 @@ class REST_Collections_Controller extends REST_Controller {
|
|||
*/
|
||||
public function update_metadata_order( $request ) {
|
||||
$collection_id = $request['collection_id'];
|
||||
$metadata_section_id = isset($request['metadata_section_id']) ? $request['metadata_section_id'] : 'default_section';
|
||||
$metadata_section_id = isset($request['metadata_section_id']) ? $request['metadata_section_id'] : \Tainacan\Entities\Metadata_Section::$default_section_slug;
|
||||
|
||||
$body = json_decode($request->get_body(), true);
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
|
|||
* @throws \Exception
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_section_id>[\d]+)',
|
||||
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_section_id>[\d|default_section]+)',
|
||||
array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
|
@ -85,7 +85,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
|
|||
'schema' => [$this, 'get_schema']
|
||||
)
|
||||
);
|
||||
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_section_id>[\d]+)/metadata',
|
||||
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_section_id>[\d|default_section]+)/metadata',
|
||||
array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
|
@ -149,7 +149,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
|
|||
$item_arr['enabled'] = $item->get_enabled_for_collection();
|
||||
}
|
||||
|
||||
$metadata_list = $item->get_id() == 'default_section'
|
||||
$metadata_list = $item->get_id() == \Tainacan\Entities\Metadata_Section::$default_section_slug
|
||||
? $this->metadata_sections_repository->get_default_section_metadata_object_list($item->get_collection())
|
||||
: $item->get_metadata_object_list();
|
||||
$item_arr['metadata_object_list'] = [];
|
||||
|
@ -434,13 +434,16 @@ class REST_Metadata_Sections_Controller extends REST_Controller {
|
|||
|
||||
if(!empty($body)){
|
||||
$metadata_section_id = $request['metadata_section_id'];
|
||||
$metadata_section = $this->metadata_sections_repository->fetch($metadata_section_id);
|
||||
|
||||
if ( $collection_id != $metadata_section->get_collection_id() ) {
|
||||
return new \WP_REST_Response( [
|
||||
'error_message' => __('This metadata section not found in collection', 'tainacan'),
|
||||
'metadata_section_id' => $metadata_section_id
|
||||
] );
|
||||
if($metadata_section_id == \Tainacan\Entities\Metadata_Section::$default_section_slug) {
|
||||
$metadata_section = $this->metadata_sections_repository->get_default_section($collection_id);
|
||||
} else {
|
||||
$metadata_section = $this->metadata_sections_repository->fetch($metadata_section_id);
|
||||
if ( $collection_id != $metadata_section->get_collection_id() ) {
|
||||
return new \WP_REST_Response( [
|
||||
'error_message' => __('This metadata section not found in collection', 'tainacan'),
|
||||
'metadata_section_id' => $metadata_section_id
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
if ($metadata_section) {
|
||||
|
|
|
@ -604,6 +604,17 @@ class Collection extends Entity {
|
|||
return $this->get_mapped_property( 'submission_use_recaptcha' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default metadata section properties.
|
||||
*
|
||||
* @param [string] $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function get_default_metadata_section_properties( ) {
|
||||
return $this->get_mapped_property( 'default_metadata_section_properties' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the collection name
|
||||
*
|
||||
|
@ -866,6 +877,17 @@ class Collection extends Entity {
|
|||
return $this->set_mapped_property( 'submission_use_recaptcha', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default metadata section properties.
|
||||
*
|
||||
* @param [string] $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function set_default_metadata_section_properties( $value ) {
|
||||
return $this->set_mapped_property( 'default_metadata_section_properties', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Collection
|
||||
*
|
||||
|
|
|
@ -1079,7 +1079,7 @@ class Item extends Entity {
|
|||
if (is_array($args['metadata_sections__in'])) {
|
||||
$post__in[] = -1; // If metadata_sections__in is an empty array, this forces empty result
|
||||
foreach ($args['metadata_sections__in'] as $metadata_section) {
|
||||
if (is_numeric($metadata_section) || $metadata_section === 'default_section') {
|
||||
if (is_numeric($metadata_section) || $metadata_section === \Tainacan\Entities\Metadata_Section::$default_section_slug) {
|
||||
$post__in[] = $metadata_section;
|
||||
} elseif (is_string($metadata_section)) {
|
||||
$post__name_in[] = $metadata_section;
|
||||
|
@ -1088,7 +1088,7 @@ class Item extends Entity {
|
|||
}
|
||||
if (is_array($args['metadata_sections__not_in'])) {
|
||||
foreach ($args['metadata_sections__not_in'] as $metadata_section) {
|
||||
if (is_integer($metadata_section) || $metadata_section === 'default_section') {
|
||||
if (is_integer($metadata_section) || $metadata_section === \Tainacan\Entities\Metadata_Section::$default_section_slug) {
|
||||
$post__not_in[] = $metadata_section;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,8 @@ class Metadata_Section extends Entity {
|
|||
use \Tainacan\Traits\Entity_Collection_Relation;
|
||||
|
||||
static $post_type = 'tainacan-metasection';
|
||||
static $default_section_slug = 'default_section';
|
||||
|
||||
protected
|
||||
$name,
|
||||
$slug,
|
||||
|
@ -38,7 +40,7 @@ class Metadata_Section extends Entity {
|
|||
*/
|
||||
public function get_id() {
|
||||
$id = $this->get_mapped_property('id');
|
||||
return isset($id) ? $id : 'default_section';
|
||||
return isset($id) ? $id : static::$default_section_slug;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,7 +88,7 @@ class Metadata_Section extends Entity {
|
|||
$tainacan_metadata_sections = \Tainacan\Repositories\Metadata_Sections::get_instance();
|
||||
$metadata_section_id = $this->get_id();
|
||||
|
||||
if ($metadata_section_id == 'default_section')
|
||||
if ($metadata_section_id == static::$default_section_slug)
|
||||
return $tainacan_metadata_sections->get_default_section_metadata_object_list($this->get_collection());
|
||||
|
||||
return $tainacan_metadata_sections->get_metadata_object_list($this->get_id());
|
||||
|
|
|
@ -245,7 +245,13 @@ class Collections extends Repository {
|
|||
'on_error' => __( 'Value should be yes or no', 'tainacan' ),
|
||||
'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no
|
||||
],
|
||||
|
||||
'default_metadata_section_properties' => [
|
||||
'map' => 'meta',
|
||||
'title' => __( 'Default metadata section properties', 'tainacan' ),
|
||||
'type' => 'object',
|
||||
'items' => [ 'name' => 'string', 'description' => 'string', 'description_bellow_name' => 'string' ],
|
||||
'description' => __( 'The default metadata section properties', 'tainacan' ),
|
||||
],
|
||||
] );
|
||||
}
|
||||
|
||||
|
|
|
@ -128,18 +128,36 @@ class Metadata_Sections extends Repository {
|
|||
register_post_type( Entities\Metadata_Section::get_post_type(), $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default metadata section of the collection
|
||||
*
|
||||
* @param \Tainacan\Entities\Collection|int the collection of the metadata section
|
||||
*
|
||||
* @return \Tainacan\Entities\Metadata_Section|false return de the default metadata section or false otherwise.
|
||||
*/
|
||||
public function get_default_section ($collection) {
|
||||
if($collection instanceof Entities\Collection) {
|
||||
$collection_id = $collection->get_id();
|
||||
} else if (is_int($collection)) {
|
||||
$collection_id = $collection;
|
||||
$collection = \tainacan_collections()->fetch($collection, 'OBJECT');
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
$name = __('Metadata', 'tainacan');
|
||||
$description = __('Metadata section', 'tainacan');
|
||||
$description_bellow_name = 'no';
|
||||
$default_metadata_section_properties = $collection->get_default_metadata_section_properties();
|
||||
if( !empty($default_metadata_section_properties) ) {
|
||||
$name = isset($default_metadata_section_properties['name']) ? $default_metadata_section_properties['name'] : $name;
|
||||
$description = isset($default_metadata_section_properties['description']) ? $default_metadata_section_properties['description'] : $description;
|
||||
$description_bellow_name = isset($default_metadata_section_properties['description_bellow_name']) ? $default_metadata_section_properties['description_bellow_name'] : $description_bellow_name;
|
||||
}
|
||||
$defaul_section = new Entities\Metadata_Section();
|
||||
$defaul_section->set_slug('default_section');
|
||||
$defaul_section->set_name(__('Metadata', 'tainacan'));
|
||||
$defaul_section->set_description(__('Metadata section', 'tainacan'));
|
||||
$defaul_section->set_slug(\Tainacan\Entities\Metadata_Section::$default_section_slug);
|
||||
$defaul_section->set_name($name);
|
||||
$defaul_section->set_description($description);
|
||||
$defaul_section->set_description_bellow_name($description_bellow_name);
|
||||
$defaul_section->set_collection_id($collection_id);
|
||||
return $defaul_section;
|
||||
}
|
||||
|
@ -306,6 +324,22 @@ class Metadata_Sections extends Repository {
|
|||
* @throws \Exception
|
||||
*/
|
||||
public function update( $object, $new_values = null ) {
|
||||
if($object->get_id() == \Tainacan\Entities\Metadata_Section::$default_section_slug) {
|
||||
$collection = $object->get_collection();
|
||||
if($collection instanceof \Tainacan\Entities\Collection) {
|
||||
$properties = array(
|
||||
'name' => $object->get_name(),
|
||||
'description' => $object->get_description(),
|
||||
'description_bellow_name' => $object->get_description_bellow_name()
|
||||
);
|
||||
$collection->set_default_metadata_section_properties($properties);
|
||||
if($collection->validate()) {
|
||||
\tainacan_collections()->update($collection);
|
||||
return $object;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return $this->insert( $object );
|
||||
}
|
||||
|
||||
|
@ -350,7 +384,7 @@ class Metadata_Sections extends Repository {
|
|||
'relation' => 'OR',
|
||||
array(
|
||||
'key' => 'metadata_section_id',
|
||||
'value' => 'default_section',
|
||||
'value' => \Tainacan\Entities\Metadata_Section::$default_section_slug,
|
||||
'compare' => '='
|
||||
),
|
||||
array(
|
||||
|
|
|
@ -231,7 +231,7 @@ class Metadata extends Repository {
|
|||
'title' => __( 'Metadata section', 'tainacan' ),
|
||||
'type' => ['integer', 'string', 'array'],
|
||||
'description' => __( 'The metadata section ID', 'tainacan' ),
|
||||
'default' => 'default_section'
|
||||
'default' => \Tainacan\Entities\Metadata_Section::$default_section_slug
|
||||
],
|
||||
] );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue