From 6fe458b1e322227b87a610ccc9aa94b28461c6c5 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 12 Aug 2022 11:40:30 -0300 Subject: [PATCH] feat: add option to edit properties of the metadata section #184 --- ...s-tainacan-rest-collections-controller.php | 4 +- ...acan-rest-metadata-sections-controller.php | 23 +++++----- .../entities/class-tainacan-collection.php | 22 ++++++++++ src/classes/entities/class-tainacan-item.php | 4 +- .../class-tainacan-metadata-section.php | 6 ++- .../class-tainacan-collections.php | 8 +++- .../class-tainacan-metadata-sections.php | 42 +++++++++++++++++-- .../repositories/class-tainacan-metadata.php | 2 +- 8 files changed, 89 insertions(+), 22 deletions(-) diff --git a/src/classes/api/endpoints/class-tainacan-rest-collections-controller.php b/src/classes/api/endpoints/class-tainacan-rest-collections-controller.php index e6deb9bea..91f436e11 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-collections-controller.php @@ -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); diff --git a/src/classes/api/endpoints/class-tainacan-rest-metadata-sections-controller.php b/src/classes/api/endpoints/class-tainacan-rest-metadata-sections-controller.php index 87835fcb2..bda7dcdc0 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-metadata-sections-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-metadata-sections-controller.php @@ -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[\d]+)/' . $this->rest_base . '/(?P[\d]+)', + register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base . '/(?P[\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[\d]+)/' . $this->rest_base . '/(?P[\d]+)/metadata', + register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base . '/(?P[\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) { diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index ff61362cd..3ab6d06fd 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -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 * diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index 2bb4bf2da..072f5d26b 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -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; } } diff --git a/src/classes/entities/class-tainacan-metadata-section.php b/src/classes/entities/class-tainacan-metadata-section.php index ed66181d0..963a1ebd9 100644 --- a/src/classes/entities/class-tainacan-metadata-section.php +++ b/src/classes/entities/class-tainacan-metadata-section.php @@ -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()); diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index a923c4a7d..ef965d8e4 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -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' ), + ], ] ); } diff --git a/src/classes/repositories/class-tainacan-metadata-sections.php b/src/classes/repositories/class-tainacan-metadata-sections.php index 39c650dfe..e590df17f 100644 --- a/src/classes/repositories/class-tainacan-metadata-sections.php +++ b/src/classes/repositories/class-tainacan-metadata-sections.php @@ -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( diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index dd8901a99..66a36fbb2 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -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 ], ] ); }