Skip to content

Commit

Permalink
feat: add option to edit properties of the metadata section #184
Browse files Browse the repository at this point in the history
  • Loading branch information
vnmedeiros committed Aug 12, 2022
1 parent c5ec733 commit 6fe458b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ public function validate_metadata_section_order($value, $request, $param) {
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']) ) {
Expand All @@ -646,7 +646,7 @@ public function validate_metadata_section_order($value, $request, $param) {
*/
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function init_objects() {
* @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,
Expand Down Expand Up @@ -85,7 +85,7 @@ public function register_routes() {
'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,
Expand Down Expand Up @@ -149,7 +149,7 @@ public function prepare_item_for_response( $item, $request ) {
$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'] = [];
Expand Down Expand Up @@ -434,13 +434,16 @@ public function update_item( $request ) {

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) {
Expand Down
22 changes: 22 additions & 0 deletions src/classes/entities/class-tainacan-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,17 @@ function get_submission_use_recaptcha() {
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
*
Expand Down Expand Up @@ -866,6 +877,17 @@ function set_submission_use_recaptcha( $value ) {
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
*
Expand Down
4 changes: 2 additions & 2 deletions src/classes/entities/class-tainacan-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ public function get_metadata_sections_as_html($args = array()) {
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;
Expand All @@ -1088,7 +1088,7 @@ public function get_metadata_sections_as_html($args = array()) {
}
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;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/classes/entities/class-tainacan-metadata-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,7 +40,7 @@ public function __toString() {
*/
public function get_id() {
$id = $this->get_mapped_property('id');
return isset($id) ? $id : 'default_section';
return isset($id) ? $id : static::$default_section_slug;
}

/**
Expand Down Expand Up @@ -86,7 +88,7 @@ function get_metadata_object_list() {
$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());
Expand Down
8 changes: 7 additions & 1 deletion src/classes/repositories/class-tainacan-collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@ protected function _get_map() {
'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' ),
],
] );
}

Expand Down
42 changes: 38 additions & 4 deletions src/classes/repositories/class-tainacan-metadata-sections.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,36 @@ public function register_post_type() {
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;
}
Expand Down Expand Up @@ -306,6 +324,22 @@ public function insert( $metadata_section ) {
* @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 );
}

Expand Down Expand Up @@ -350,7 +384,7 @@ public function get_default_section_metadata_object_list (Entities\Collection $c
'relation' => 'OR',
array(
'key' => 'metadata_section_id',
'value' => 'default_section',
'value' => \Tainacan\Entities\Metadata_Section::$default_section_slug,
'compare' => '='
),
array(
Expand Down
2 changes: 1 addition & 1 deletion src/classes/repositories/class-tainacan-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ protected function _get_map() {
'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
],
] );
}
Expand Down

0 comments on commit 6fe458b

Please sign in to comment.