Begins fronend implementation of metadata sections. Refactor part of the backend terminology to use metadata instead of metadatum #184.

This commit is contained in:
mateuswetah 2022-04-13 15:38:17 -03:00
parent 3880668464
commit 71202eb247
14 changed files with 872 additions and 200 deletions

View File

@ -6,7 +6,7 @@ use \Tainacan\API\REST_Controller;
use Tainacan\Entities; use Tainacan\Entities;
use Tainacan\Repositories; use Tainacan\Repositories;
class REST_Metadata_Section_Controller extends REST_Controller { class REST_Metadata_Sections_Controller extends REST_Controller {
public function __construct() { public function __construct() {
parent::__construct(); parent::__construct();
$this->rest_base = 'metadata-sections'; $this->rest_base = 'metadata-sections';
@ -19,8 +19,8 @@ class REST_Metadata_Section_Controller extends REST_Controller {
* @throws \Exception * @throws \Exception
*/ */
public function init_objects() { public function init_objects() {
$this->metadatum_section_repository = Repositories\Metadata_Section::get_instance(); $this->metadata_sections_repository = Repositories\Metadata_Sections::get_instance();
$this->metadatum_repository = Repositories\Metadata::get_instance(); $this->metadata_repository = Repositories\Metadata::get_instance();
} }
/** /**
@ -84,23 +84,23 @@ class REST_Metadata_Section_Controller extends REST_Controller {
'schema' => [$this, 'get_schema'] 'schema' => [$this, 'get_schema']
) )
); );
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_section_id>[\d]+)/metadatum', register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<metadata_section_id>[\d]+)/metadata',
array( array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_metadatum_list'), 'callback' => array($this, 'get_metadata_list'),
'permission_callback' => array($this, 'get_items_permissions_check'), 'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE), 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE),
), ),
array( array(
'methods' => \WP_REST_Server::CREATABLE, 'methods' => \WP_REST_Server::CREATABLE,
'callback' => array($this, 'add_metadatum'), 'callback' => array($this, 'add_metadata'),
'permission_callback' => array($this, 'update_item_permissions_check'), 'permission_callback' => array($this, 'update_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE),
), ),
array( array(
'methods' => \WP_REST_Server::DELETABLE, 'methods' => \WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_metadatum'), 'callback' => array($this, 'delete_metadata'),
'permission_callback' => array($this, 'update_item_permissions_check'), 'permission_callback' => array($this, 'update_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE), 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE),
), ),
@ -125,7 +125,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
$number = $request['number']; $number = $request['number'];
} }
$result = $this->metadatum_section_repository->fetch($metadatum_id, 'OBJECT'); $result = $this->metadata_sections_repository->fetch($metadatum_id, 'OBJECT');
if (! $result instanceof Entities\Metadatum) { if (! $result instanceof Entities\Metadatum) {
return new \WP_REST_Response([ return new \WP_REST_Response([
@ -144,7 +144,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
* @throws \Exception * @throws \Exception
*/ */
public function get_item_permissions_check( $request ) { public function get_item_permissions_check( $request ) {
$metadatum = $this->metadatum_section_repository->fetch($request['metadatum_id']); $metadatum = $this->metadata_sections_repository->fetch($request['metadatum_id']);
if ( $metadatum instanceof Entities\Metadatum ) { if ( $metadatum instanceof Entities\Metadatum ) {
return $metadatum->can_read(); return $metadatum->can_read();
@ -165,15 +165,15 @@ class REST_Metadata_Section_Controller extends REST_Controller {
if($collection_id == null) { if($collection_id == null) {
throw new \InvalidArgumentException('You need provide a collection id'); throw new \InvalidArgumentException('You need provide a collection id');
} }
$metadatum_section = new Entities\Metadatum_Section(); $metadata_section = new Entities\Metadata_Section();
$meta = json_decode( $request, true ); $meta = json_decode( $request, true );
foreach ( $meta as $key => $value ) { foreach ( $meta as $key => $value ) {
$set_ = 'set_' . $key; $set_ = 'set_' . $key;
$metadatum_section->$set_( $value ); $metadata_section->$set_( $value );
} }
$collection = new Entities\Collection( $collection_id ); $collection = new Entities\Collection( $collection_id );
$metadatum_section->set_collection( $collection ); $metadata_section->set_collection( $collection );
return $metadatum_section; return $metadata_section;
} }
/** /**
@ -193,8 +193,8 @@ class REST_Metadata_Section_Controller extends REST_Controller {
} }
if($prepared->validate()) { if($prepared->validate()) {
$metadatum_section = $this->metadatum_section_repository->insert($prepared); $metadata_section = $this->metadata_sections_repository->insert($prepared);
$response = $this->prepare_item_for_response($metadatum_section, $request); $response = $this->prepare_item_for_response($metadata_section, $request);
return new \WP_REST_Response($response, 201); return new \WP_REST_Response($response, 201);
} else { } else {
return new \WP_REST_Response([ return new \WP_REST_Response([
@ -251,12 +251,12 @@ class REST_Metadata_Section_Controller extends REST_Controller {
// $item_arr['enabled'] = $item->get_enabled_for_collection(); // $item_arr['enabled'] = $item->get_enabled_for_collection();
} }
if( !empty($item_arr['metadatum_list']) ) { if( !empty($item_arr['metadata_list']) ) {
$metadatum_list = $item_arr['metadatum_list']; $metadata_list = $item_arr['metadata_list'];
$item_arr['metadatum_object_list'] = []; $item_arr['metadata_object_list'] = [];
foreach($metadatum_list as $metadatum_id) { foreach($metadata_list as $metadatum_id) {
$meta = $this->metadatum_repository->fetch($metadatum_id, 'OBJECT'); $meta = $this->metadata_repository->fetch($metadatum_id, 'OBJECT');
$item_arr['metadatum_object_list'][] = $meta->_toArray(); $item_arr['metadata_object_list'][] = $meta->_toArray();
} }
} }
@ -267,7 +267,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
* *
* Also take care to do any permissions verification before exposing the data * Also take care to do any permissions verification before exposing the data
*/ */
$extra_metadata = apply_filters('tainacan-api-response-metadatum-section-meta', [], $request); $extra_metadata = apply_filters('tainacan-api-response-metadata-section-meta', [], $request);
foreach ($extra_metadata as $extra_meta) { foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true); $item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
@ -295,7 +295,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
$collection = new Entities\Collection( $collection_id ); $collection = new Entities\Collection( $collection_id );
$result = $this->metadatum_section_repository->fetch_by_collection( $collection, $args ); $result = $this->metadata_sections_repository->fetch_by_collection( $collection, $args );
} }
$prepared_item = []; $prepared_item = [];
@ -341,7 +341,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
public function delete_item( $request ) { public function delete_item( $request ) {
$metadatum_id = $request['metadatum_id']; $metadatum_id = $request['metadatum_id'];
$metadatum = $this->metadatum_section_repository->fetch($metadatum_id); $metadatum = $this->metadata_sections_repository->fetch($metadatum_id);
if (! $metadatum instanceof Entities\Metadatum) { if (! $metadatum instanceof Entities\Metadatum) {
return new \WP_REST_Response([ return new \WP_REST_Response([
@ -350,7 +350,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
], 400); ], 400);
} }
$metadatum_trashed = $this->metadatum_section_repository->trash($metadatum); $metadatum_trashed = $this->metadata_sections_repository->trash($metadatum);
$prepared = $this->prepare_item_for_response($metadatum_trashed, $request); $prepared = $this->prepare_item_for_response($metadatum_trashed, $request);
@ -364,7 +364,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
* @throws \Exception * @throws \Exception
*/ */
public function delete_item_permissions_check( $request ) { public function delete_item_permissions_check( $request ) {
$metadatum = $this->metadatum_section_repository->fetch($request['metadatum_id']); $metadatum = $this->metadata_sections_repository->fetch($request['metadatum_id']);
if ($metadatum instanceof Entities\Metadatum) { if ($metadatum instanceof Entities\Metadatum) {
return $metadatum->can_delete(); return $metadatum->can_delete();
@ -396,7 +396,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
$attributes[$att] = $value; $attributes[$att] = $value;
} }
$metadatum = $this->metadatum_section_repository->fetch($metadatum_id); $metadatum = $this->metadata_sections_repository->fetch($metadatum_id);
$error_message = __('Metadata with this ID was not found', 'tainacan'); $error_message = __('Metadata with this ID was not found', 'tainacan');
@ -425,7 +425,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
$prepared_metadata = $this->prepare_item_for_updating($metadatum, $attributes); $prepared_metadata = $this->prepare_item_for_updating($metadatum, $attributes);
if($prepared_metadata->validate()){ if($prepared_metadata->validate()){
$updated_metadata = $this->metadatum_section_repository->update($prepared_metadata); $updated_metadata = $this->metadata_sections_repository->update($prepared_metadata);
$response = $this->prepare_item_for_response($updated_metadata, $request); $response = $this->prepare_item_for_response($updated_metadata, $request);
@ -455,17 +455,17 @@ class REST_Metadata_Section_Controller extends REST_Controller {
if( !empty($request->get_body()) && isset($request['metadata_section_id']) ){ if( !empty($request->get_body()) && isset($request['metadata_section_id']) ){
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
$metadata_section_id = $request['metadata_section_id']; $metadata_section_id = $request['metadata_section_id'];
$metadatum_list = $body['metadatum_list']; $metadata_list = $body['metadata_list'];
try { try {
$metadatum_section = $this->metadatum_section_repository->add_metadatum($metadata_section_id, $metadatum_list); $metadata_section = $this->metadata_sections_repository->add_metadatum($metadata_section_id, $metadata_list);
if($metadatum_section == false) { if($metadata_section == false) {
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'item' => $request->get_body() 'item' => $request->get_body()
], 400); ], 400);
} }
$response = $this->prepare_item_for_response($metadatum_section, $request); $response = $this->prepare_item_for_response($metadata_section, $request);
return new \WP_REST_Response($response, 201); return new \WP_REST_Response($response, 201);
} catch (\Exception $exception) { } catch (\Exception $exception) {
return new \WP_REST_Response($exception->getMessage(), 400); return new \WP_REST_Response($exception->getMessage(), 400);
@ -481,17 +481,17 @@ class REST_Metadata_Section_Controller extends REST_Controller {
if( !empty($request->get_body()) && isset($request['metadata_section_id']) ){ if( !empty($request->get_body()) && isset($request['metadata_section_id']) ){
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
$metadata_section_id = $request['metadata_section_id']; $metadata_section_id = $request['metadata_section_id'];
$metadatum_list = $body['metadatum_list']; $metadata_list = $body['metadata_list'];
try { try {
$metadatum_section = $this->metadatum_section_repository->delete_metadatum($metadata_section_id, $metadatum_list); $metadata_section = $this->metadata_sections_repository->delete_metadatum($metadata_section_id, $metadata_list);
if($metadatum_section == false) { if($metadata_section == false) {
return new \WP_REST_Response([ return new \WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'item' => $request->get_body() 'item' => $request->get_body()
], 400); ], 400);
} }
$response = $this->prepare_item_for_response($metadatum_section, $request); $response = $this->prepare_item_for_response($metadata_section, $request);
return new \WP_REST_Response($response, 201); return new \WP_REST_Response($response, 201);
} catch (\Exception $exception) { } catch (\Exception $exception) {
return new \WP_REST_Response($exception->getMessage(), 400); return new \WP_REST_Response($exception->getMessage(), 400);
@ -503,12 +503,12 @@ class REST_Metadata_Section_Controller extends REST_Controller {
], 400); ], 400);
} }
public function get_metadatum_list( $request ) { public function get_metadata_list( $request ) {
if(isset($request['metadata_section_id']) ){ if(isset($request['metadata_section_id']) ){
$metadata_section_id = $request['metadata_section_id']; $metadata_section_id = $request['metadata_section_id'];
try { try {
$result = $this->metadatum_section_repository->get_metadatum_list($metadata_section_id); $result = $this->metadata_sections_repository->get_metadata_list($metadata_section_id);
$prepared_item = []; $prepared_item = [];
foreach ( $result as $item ) { foreach ( $result as $item ) {
$prepared_item[] = $item->_toArray(); $prepared_item[] = $item->_toArray();
@ -532,7 +532,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
*/ */
public function update_item_permissions_check( $request ) { public function update_item_permissions_check( $request ) {
return true; return true;
$metadatum = $this->metadatum_section_repository->fetch($request['metadatum_id']); $metadatum = $this->metadata_sections_repository->fetch($request['metadatum_id']);
if ($metadatum instanceof Entities\Metadatum) { if ($metadatum instanceof Entities\Metadatum) {
return $metadatum->can_edit(); return $metadatum->can_edit();
@ -575,7 +575,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
parent::get_wp_query_params() parent::get_wp_query_params()
); );
} elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) { } elseif ($method === \WP_REST_Server::CREATABLE || $method === \WP_REST_Server::EDITABLE) {
$map = $this->metadatum_section_repository->get_map(); $map = $this->metadata_sections_repository->get_map();
foreach ($map as $mapped => $value){ foreach ($map as $mapped => $value){
$set_ = 'set_'. $mapped; $set_ = 'set_'. $mapped;
@ -599,7 +599,7 @@ class REST_Metadata_Section_Controller extends REST_Controller {
'type' => 'object' 'type' => 'object'
]; ];
$main_schema = parent::get_repository_schema( $this->metadatum_section_repository ); $main_schema = parent::get_repository_schema( $this->metadata_sections_repository );
$permissions_schema = parent::get_permissions_schema(); $permissions_schema = parent::get_permissions_schema();
// $item_metadata_scheme = parent::get_repository_schema( $this->item_metadata_repository ); // $item_metadata_scheme = parent::get_repository_schema( $this->item_metadata_repository );

View File

@ -22,7 +22,7 @@ $rest_oaipmh_expose_controller = new \Tainacan\API\EndPoints\REST_Oaipmh_
$rest_item_metadata_controller = new \Tainacan\API\EndPoints\REST_Item_Metadata_Controller(); $rest_item_metadata_controller = new \Tainacan\API\EndPoints\REST_Item_Metadata_Controller();
$rest_sequence_edit_controller = new \Tainacan\API\EndPoints\REST_Sequence_Edit_Controller(); $rest_sequence_edit_controller = new \Tainacan\API\EndPoints\REST_Sequence_Edit_Controller();
$rest_metadata_types_controller = new \Tainacan\API\EndPoints\REST_Metadata_Types_Controller(); $rest_metadata_types_controller = new \Tainacan\API\EndPoints\REST_Metadata_Types_Controller();
$rest_metadata_section_controller = new \Tainacan\API\EndPoints\REST_Metadata_Section_Controller(); $rest_metadata_sections_controller = new \Tainacan\API\EndPoints\REST_Metadata_Sections_Controller();
$rest_metadatum_mappers_controller = new \Tainacan\API\EndPoints\REST_Metadatum_Mappers_Controller(); $rest_metadatum_mappers_controller = new \Tainacan\API\EndPoints\REST_Metadatum_Mappers_Controller();
$rest_background_processes_controller = new \Tainacan\API\EndPoints\REST_Background_Processes_Controller(); $rest_background_processes_controller = new \Tainacan\API\EndPoints\REST_Background_Processes_Controller();
// Add here other endpoints imports // Add here other endpoints imports

View File

@ -7,7 +7,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/** /**
* Represents the Entity Metadatum * Represents the Entity Metadatum
*/ */
class Metadatum_Section extends Entity { class Metadata_Section extends Entity {
// Collection getter and setter declared here // Collection getter and setter declared here
use \Tainacan\Traits\Entity_Collection_Relation; use \Tainacan\Traits\Entity_Collection_Relation;
@ -16,7 +16,7 @@ class Metadatum_Section extends Entity {
$name, $name,
$slug, $slug,
$description, $description,
$metadatum_list; $metadata_list;
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -26,11 +26,11 @@ class Metadatum_Section extends Entity {
protected $repository = 'Metadata_Section'; protected $repository = 'Metadata_Section';
public function __toString() { public function __toString() {
return apply_filters("tainacan-metadatum-section-to-string", $this->get_name(), $this); return apply_filters("tainacan-metadata-section-to-string", $this->get_name(), $this);
} }
/** /**
* Return the metadatum section name * Return the metadata section name
* *
* @return string * @return string
*/ */
@ -39,7 +39,7 @@ class Metadatum_Section extends Entity {
} }
/** /**
* Get metadatum section slug * Get metadata section slug
* *
* @return string * @return string
*/ */
@ -48,7 +48,7 @@ class Metadatum_Section extends Entity {
} }
/** /**
* Return the metadatum section description * Return the metadata section description
* *
* @return string * @return string
*/ */
@ -57,16 +57,16 @@ class Metadatum_Section extends Entity {
} }
/** /**
* Return the metadatum_list of section * Return the metadata_list of section
* *
* @return [int] * @return [int]
*/ */
function get_metadatum_list() { function get_metadata_list() {
return $this->get_mapped_property('metadatum_list'); return $this->get_mapped_property('metadata_list');
} }
/** /**
* Set the metadatum section name * Set the metadata section name
* *
* @param [string] $value * @param [string] $value
* @return void * @return void
@ -76,9 +76,9 @@ class Metadatum_Section extends Entity {
} }
/** /**
* Set the metadatum section slug * Set the metadata section slug
* *
* If you dont set the metadatum slug, it will be set automatically based on the name and * If you dont set the metadata slug, it will be set automatically based on the name and
* following WordPress default behavior of creating slugs for posts. * following WordPress default behavior of creating slugs for posts.
* *
* If you set the slug for an existing one, WordPress will append a number at the end of in order * If you set the slug for an existing one, WordPress will append a number at the end of in order
@ -92,7 +92,7 @@ class Metadatum_Section extends Entity {
} }
/** /**
* Set metadatum section description * Set metadata section description
* *
* @param [string] $value The text description * @param [string] $value The text description
* @return void * @return void
@ -103,26 +103,26 @@ class Metadatum_Section extends Entity {
/** /**
* Set metadatum list of the section * Set metadata list of the section
* *
* @param [string|int] $value The array of list metadatum * @param [string|int] $value The array of metadata in this section
* @return void * @return void
*/ */
function set_metadatum_list($value) { function set_metadata_list($value) {
$this->set_mapped_property('metadatum_list', array_unique($value)); $this->set_mapped_property('metadata_list', array_unique($value));
} }
/** /**
* {@inheritdoc } * {@inheritdoc }
* *
* Also validates the metadatum, calling the validate_options callback of the Metadatum Type * Also validates the metadata, calling the validate_options callback of the Metadatum Type
* *
* @return bool valid or not * @return bool valid or not
* @throws \Exception * @throws \Exception
*/ */
public function validate() { public function validate() {
$no_errors = true; $no_errors = true;
$metadatum_list = $this->get_metadatum_list(); $metadata_list = $this->get_metadata_list();
$name = $this->get_name(); $name = $this->get_name();
$collection = $this->get_collection(); $collection = $this->get_collection();
@ -135,8 +135,8 @@ class Metadatum_Section extends Entity {
$this->add_error($this->get_id(), __("name is required", 'tainacan')); $this->add_error($this->get_id(), __("name is required", 'tainacan'));
$no_errors = false; $no_errors = false;
} }
if( !empty($metadatum_list) ) { if( !empty($metadata_list) ) {
foreach($metadatum_list as $metadatum_id) { foreach($metadata_list as $metadatum_id) {
if(get_post_type($metadatum_id) != \Tainacan\Entities\Metadatum::$post_type ) { if(get_post_type($metadatum_id) != \Tainacan\Entities\Metadatum::$post_type ) {
$this->add_error($this->get_id(), __("is not a valid metadata", 'tainacan')); $this->add_error($this->get_id(), __("is not a valid metadata", 'tainacan'));
$no_errors = false; $no_errors = false;

View File

@ -25,7 +25,7 @@ class Metadatum extends Entity {
$default_value, $default_value,
$metadata_type, $metadata_type,
$metadata_type_options, $metadata_type_options,
$metadatum_section_id; $metadata_section_id;
// Collection getter and setter declared here // Collection getter and setter declared here
use \Tainacan\Traits\Entity_Collection_Relation; use \Tainacan\Traits\Entity_Collection_Relation;
@ -254,12 +254,12 @@ class Metadatum extends Entity {
} }
/** /**
* Return the metadatum_section_id * Return the metadata_section_id
* *
* @return string * @return string
*/ */
function get_metadatum_section_id(){ function get_metadata_section_id(){
return $this->get_mapped_property('metadatum_section_id'); return $this->get_mapped_property('metadata_section_id');
} }
/** /**
@ -450,8 +450,8 @@ class Metadatum extends Entity {
* @param [string] $value * @param [string] $value
* @return void * @return void
*/ */
function set_metadatum_section_id( $value) { function set_metadata_section_id( $value) {
return $this->set_mapped_property('metadatum_section_id', $value); return $this->set_mapped_property('metadata_section_id', $value);
} }
/** /**

View File

@ -11,9 +11,9 @@ use \Respect\Validation\Validator as v;
/** /**
* Class Metadata * Class Metadata
*/ */
class Metadata_Section extends Repository { class Metadata_Sections extends Repository {
public $entities_type = '\Tainacan\Entities\Metadatum_Section'; public $entities_type = '\Tainacan\Entities\Metadata_Section';
private static $instance = null; private static $instance = null;
protected function __construct() { protected function __construct() {
@ -59,7 +59,7 @@ class Metadata_Section extends Repository {
'map' => 'post_content', 'map' => 'post_content',
'title' => __( 'Description', 'tainacan' ), 'title' => __( 'Description', 'tainacan' ),
'type' => 'string', 'type' => 'string',
'description' => __( 'The metadatum section description.', 'tainacan' ), 'description' => __( 'The metadata section description.', 'tainacan' ),
'default' => '', 'default' => '',
], ],
'collection_id' => [ 'collection_id' => [
@ -68,14 +68,14 @@ class Metadata_Section extends Repository {
'type' => ['integer', 'string'], 'type' => ['integer', 'string'],
'description' => __( 'The collection ID', 'tainacan' ), 'description' => __( 'The collection ID', 'tainacan' ),
], ],
'metadatum_list' => [ 'metadata_list' => [
'map' => 'meta', 'map' => 'meta',
'title' => __( 'Metadatum list', 'tainacan' ), 'title' => __( 'Metadata list', 'tainacan' ),
'type' => 'array', 'type' => 'array',
'items' => [ 'items' => [
'type' => 'integer' 'type' => 'integer'
], ],
'description' => __( 'The metadatum ID list', 'tainacan' ), 'description' => __( 'The metadata ID list', 'tainacan' ),
] ]
] ); ] );
} }
@ -87,7 +87,7 @@ class Metadata_Section extends Repository {
*/ */
public function get_cpt_labels() { public function get_cpt_labels() {
return array( return array(
'name' => __( 'Metadata Section', 'tainacan' ), 'name' => __( 'Metadata Sections', 'tainacan' ),
'singular_name' => __( 'Metadata Section', 'tainacan' ), 'singular_name' => __( 'Metadata Section', 'tainacan' ),
'add_new' => __( 'Add new', 'tainacan' ), 'add_new' => __( 'Add new', 'tainacan' ),
'add_new_item' => __( 'Add new Metadata Section', 'tainacan' ), 'add_new_item' => __( 'Add new Metadata Section', 'tainacan' ),
@ -125,24 +125,24 @@ class Metadata_Section extends Repository {
'page-attributes' 'page-attributes'
] ]
); );
register_post_type( Entities\Metadatum_Section::get_post_type(), $args ); register_post_type( Entities\Metadata_Section::get_post_type(), $args );
} }
/** /**
* fetch metadatum section based on ID or WP_Query args * fetch metadata section based on ID or WP_Query args
* *
* metadatum section are stored as posts. Check WP_Query docs * metadata section are stored as posts. Check WP_Query docs
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/) * to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the * You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
* appropriate WP_Query argument * appropriate WP_Query argument
* *
* If a number is passed to $args, it will return a \Tainacan\Entities\Metadatum_Section object. But if the post is not found or * If a number is passed to $args, it will return a \Tainacan\Entities\Metadata_Section object. But if the post is not found or
* does not match the entity post type, it will return an empty array * does not match the entity post type, it will return an empty array
* *
* @param array $args WP_Query args || int $args the metadatum section id * @param array $args WP_Query args || int $args the metadata section id
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
* *
* @return Entities\Metadatum_Section|\WP_Query|Array an instance of wp query OR array of entities; * @return Entities\Metadata_Section|\WP_Query|Array an instance of wp query OR array of entities;
* @throws \Exception * @throws \Exception
*/ */
public function fetch( $args, $output = null ) { public function fetch( $args, $output = null ) {
@ -151,7 +151,7 @@ class Metadata_Section extends Repository {
$existing_post = get_post( $args ); $existing_post = get_post( $args );
if ( $existing_post instanceof \WP_Post ) { if ( $existing_post instanceof \WP_Post ) {
try { try {
return new Entities\Metadatum_Section( $existing_post ); return new Entities\Metadata_Section( $existing_post );
} catch (\Exception $e) { } catch (\Exception $e) {
return []; return [];
} }
@ -164,7 +164,7 @@ class Metadata_Section extends Repository {
], $args ); ], $args );
$args = $this->parse_fetch_args( $args ); $args = $this->parse_fetch_args( $args );
$args['post_type'] = Entities\Metadatum_Section::get_post_type(); $args['post_type'] = Entities\Metadata_Section::get_post_type();
$args = apply_filters( 'tainacan_fetch_args', $args, 'metadata-section' ); $args = apply_filters( 'tainacan_fetch_args', $args, 'metadata-section' );
$wp_query = new \WP_Query( $args ); $wp_query = new \WP_Query( $args );
@ -193,12 +193,12 @@ class Metadata_Section extends Repository {
} }
/** /**
* fetch metadatum section by collection * fetch metadata section by collection
* *
* @param Entities\Collection $collection * @param Entities\Collection $collection
* @param array $args WP_Query args * @param array $args WP_Query args
* *
* @return array Entities\Metadatum_Section * @return array Entities\Metadata_Section
* @throws \Exception * @throws \Exception
*/ */
public function fetch_by_collection( Entities\Collection $collection, $args = [] ) { public function fetch_by_collection( Entities\Collection $collection, $args = [] ) {
@ -271,19 +271,19 @@ class Metadata_Section extends Repository {
} }
/** /**
* @param \Tainacan\Entities\Metadatum_Section $metadatum_section * @param \Tainacan\Entities\Metadata_Section $metadata_section
* *
* @return \Tainacan\Entities\Metadatum_Section * @return \Tainacan\Entities\Metadata_Section
* {@inheritDoc} * {@inheritDoc}
* @see \Tainacan\Repositories\Repository::insert() * @see \Tainacan\Repositories\Repository::insert()
*/ */
public function insert( $metadatum_section ) { public function insert( $metadata_section ) {
$new_metadatum_section = parent::insert( $metadatum_section ); $new_metadata_section = parent::insert( $metadata_section );
return $new_metadatum_section; return $new_metadata_section;
} }
/** /**
* @param \Tainacan\Entities\Metadatum_Section $object * @param \Tainacan\Entities\Metadata_Section $object
* @param $new_values * @param $new_values
* *
* @return mixed|string|Entities\Entity * @return mixed|string|Entities\Entity
@ -293,12 +293,12 @@ class Metadata_Section extends Repository {
return $this->insert( $object ); return $this->insert( $object );
} }
public function add_metadatum($metadata_section_id, $metadatum_list) { public function add_metadatum($metadata_section_id, $metadata_list) {
$metadata_section = $this->fetch($metadata_section_id); $metadata_section = $this->fetch($metadata_section_id);
if ($metadata_section) { if ($metadata_section) {
$list = $metadata_section->get_metadatum_list(); $list = $metadata_section->get_metadata_list();
$metadatum_list = array_merge($list, $metadatum_list); $metadata_list = array_merge($list, $metadata_list);
$metadata_section->set_metadatum_list($metadatum_list); $metadata_section->set_metadata_list($metadata_list);
if($metadata_section->validate()) { if($metadata_section->validate()) {
$metadata_section = $this->update($metadata_section); $metadata_section = $this->update($metadata_section);
return $metadata_section; return $metadata_section;
@ -307,11 +307,11 @@ class Metadata_Section extends Repository {
return false; return false;
} }
public function delete_metadatum($metadata_section_id, $metadatum_list) { public function delete_metadatum($metadata_section_id, $metadata_list) {
$metadata_section = $this->fetch($metadata_section_id); $metadata_section = $this->fetch($metadata_section_id);
$list = $metadata_section->get_metadatum_list(); $list = $metadata_section->get_metadata_list();
$list = array_diff($list, $metadatum_list); $list = array_diff($list, $metadata_list);
$metadata_section->set_metadatum_list($list); $metadata_section->set_metadata_list($list);
if($metadata_section->validate()) { if($metadata_section->validate()) {
$metadata_section = $this->update($metadata_section); $metadata_section = $this->update($metadata_section);
return $metadata_section; return $metadata_section;
@ -319,13 +319,13 @@ class Metadata_Section extends Repository {
return false; return false;
} }
public function get_metadatum_list($metadata_section_id) { public function get_metadata_list($metadata_section_id) {
$metadata_section = $this->fetch($metadata_section_id); $metadata_section = $this->fetch($metadata_section_id);
$list = $metadata_section->get_metadatum_list(); $list = $metadata_section->get_metadata_list();
$args = array('post__in' => $list); $args = array('post__in' => $list);
$metadata_repository = \Tainacan\Repositories\Metadata::get_instance(); $metadata_repository = \Tainacan\Repositories\Metadata::get_instance();
$metadatum_list = $metadata_repository->fetch($args, 'OBJECT'); $metadata_list = $metadata_repository->fetch($args, 'OBJECT');
return $metadatum_list; return $metadata_list;
} }
/** /**

View File

@ -225,7 +225,7 @@ class Metadata extends Repository {
// yes or no. It cant be multiple if its collection_key // yes or no. It cant be multiple if its collection_key
'default' => 'no' 'default' => 'no'
], ],
'metadatum_section_id' => [ 'metadata_section_id' => [
'map' => 'meta', 'map' => 'meta',
'title' => __( 'Metadatum section', 'tainacan' ), 'title' => __( 'Metadatum section', 'tainacan' ),
'type' => ['integer', 'string', 'array'], 'type' => ['integer', 'string', 'array'],
@ -657,7 +657,7 @@ class Metadata extends Repository {
$this->update_taxonomy_metadatum( $new_metadatum ); $this->update_taxonomy_metadatum( $new_metadatum );
$this->update_metadata_type_index( $new_metadatum ); $this->update_metadata_type_index( $new_metadatum );
$this->update_metadatum_section( $new_metadatum ); $this->update_metadata_section( $new_metadatum );
return $new_metadatum; return $new_metadatum;
} }
@ -1592,7 +1592,7 @@ class Metadata extends Repository {
*/ */
public function delete( Entities\Entity $entity, $permanent = true ) { public function delete( Entities\Entity $entity, $permanent = true ) {
$this->delete_taxonomy_metadatum($entity); $this->delete_taxonomy_metadatum($entity);
$this->update_metadatum_section($entity, true); $this->update_metadata_section($entity, true);
return parent::delete($entity, $permanent); return parent::delete($entity, $permanent);
} }
@ -1724,12 +1724,12 @@ class Metadata extends Repository {
} }
public function update_metadatum_section( Entities\Metadatum $metadatum, $remove = false ) { public function update_metadata_section( Entities\Metadatum $metadatum, $remove = false ) {
$metadatum_section_repository = Metadata_Section::get_instance(); $metadata_section_repository = Metadata_Section::get_instance();
if (!$remove) { if (!$remove) {
$metadatum_section_repository->add_metadatum($metadatum->get_metadatum_section_id(), [$metadatum->get_id()]); $metadata_section_repository->add_metadatum($metadatum->get_metadata_section_id(), [$metadatum->get_id()]);
} else { } else {
$metadatum_section_repository->delete_metadatum($metadatum->get_metadatum_section_id(), [$metadatum->get_id()]); $metadata_section_repository->delete_metadatum($metadatum->get_metadata_section_id(), [$metadatum->get_id()]);
} }
} }

View File

@ -0,0 +1,382 @@
<template>
<form
id="metadataSectionEditForm"
@submit.prevent="saveEdition(form)"
autofocus="true"
tabindex="-1"
role="dialog"
aria-modal>
<div
v-if="form && Object.keys(form).length"
class="tainacan-modal-content">
<div class="tainacan-modal-title">
<h2 v-html="form.name ? ($i18n.get('instruction_configure_the_metadata_section') + ' <em>' + form.name + '</em>') : $i18n.get('instruction_configure_new_metadata_section')" />
<hr>
</div>
<div class="tainacan-form">
<div class="options-columns">
<b-field
:addons="false"
:type="formErrors['name'] != undefined ? 'is-danger' : ''"
:message="formErrors['name'] != undefined ? formErrors['name'] : ''">
<label class="label is-inline">
{{ $i18n.get('label_name') }}
<span
class="required-metadata-section-asterisk"
:class="formErrors['name'] != undefined ? 'is-danger' : ''">*</span>
<help-button
:title="$i18n.getHelperTitle('metadata-sections', 'name')"
:message="$i18n.getHelperMessage('metadata-sections', 'name')"
:extra-classes="isRepositoryLevel ? 'tainacan-repository-tooltip' : ''" />
</label>
<b-input
v-model="form.name"
name="name"
@focus="clearErrors('name')"/>
</b-field>
<!-- Hook for extra Form options -->
<template
v-if="hasBeginLeftForm">
<form
id="form-metadataSection-begin-left"
class="form-hook-region"
v-html="getBeginLeftForm"/>
</template>
<b-field
:addons="false"
:type="formErrors['description'] != undefined ? 'is-danger' : ''"
:message="formErrors['description'] != undefined ? formErrors['description'] : ''">
<label class="label is-inline">
{{ $i18n.get('label_description') }}
<help-button
:title="$i18n.getHelperTitle('metadata-sections', 'description')"
:message="$i18n.getHelperMessage('metadata-sections', 'description')"
:extra-classes="isRepositoryLevel ? 'tainacan-repository-tooltip' : ''" />
</label>
<b-input
type="textarea"
name="description"
rows="3"
v-model="form.description"
@focus="clearErrors('description')"/>
</b-field>
<b-field
:addons="false"
:label="$i18n.getHelperTitle('metadata-sections', 'description_bellow_name')"
:type="formErrors['description_bellow_name'] != undefined ? 'is-danger' : ''"
:message="formErrors['description_bellow_name'] != undefined ? formErrors['description_bellow_name'] : ''">
&nbsp;
<b-switch
size="is-small"
@input="clearErrors('description_bellow_name')"
v-model="form.description_bellow_name"
true-value="yes"
false-value="no"
name="description_bellow_name">
<help-button
:title="$i18n.getHelperTitle('metadata-sections', 'description_bellow_name')"
:message="$i18n.getHelperMessage('metadata-sections', 'description_bellow_name')"
:extra-classes="isRepositoryLevel ? 'tainacan-repository-tooltip' : ''" />
</b-switch>
</b-field>
<b-field
v-if="form.parent == 0"
:addons="false"
:type="formErrors['status'] != undefined ? 'is-danger' : ''"
:message="formErrors['status'] != undefined ? formErrors['status'] : ''">
<label class="label is-inline">
{{ $i18n.get('label_status') }}
<help-button
:title="$i18n.getHelperTitle('metadata-sections', 'status')"
:message="$i18n.getHelperMessage('metadata-sections', 'status')"
:extra-classes="isRepositoryLevel ? 'tainacan-repository-tooltip' : ''" />
</label>
<div class="is-flex is-justify-content-space-between">
<b-radio
@focus="clearErrors('label_status')"
id="tainacan-select-status-publish"
name="status"
v-model="form.status"
native-value="publish">
<span class="icon has-text-gray3">
<i class="tainacan-icon tainacan-icon-public"/>
</span>
{{ $i18n.get('status_public') }}
</b-radio>
<b-radio
@focus="clearErrors('label_status')"
id="tainacan-select-status-private"
name="status"
v-model="form.status"
native-value="private">
<span class="icon has-text-gray3">
<i class="tainacan-icon tainacan-icon-private"/>
</span>
{{ $i18n.get('status_private') }}
</b-radio>
</div>
</b-field>
</div>
<!-- Hook for extra Form options -->
<template v-if="hasEndLeftForm" >
<form
id="form-metadataSection-end-left"
class="form-hook-region"
v-html="getEndLeftForm"/>
</template>
</div>
</div>
<div class="field is-grouped form-submit">
<div class="control">
<button
type="button"
class="button is-outlined"
@click.prevent="cancelEdition()"
slot="trigger">{{ $i18n.get('cancel') }}
</button>
</div>
<p class="help is-danger">{{ formErrorMessage }}</p>
<div class="control">
<b-button
:loading="isLoading"
class="button is-success"
native-type="submit">
{{ $i18n.get('save') }}
</b-button>
</div>
</div>
</form>
</template>
<script>
import { mapActions } from 'vuex';
import { formHooks } from "../../js/mixins";
export default {
name: 'MetadataSectionEditionForm',
mixins: [ formHooks ],
props: {
index: '',
originalMetadataSection: Object,
collectionId: '',
isInsideImporterFlow: false
},
data() {
return {
form: {},
formErrors: {},
formErrorMessage: '',
closedByForm: false,
entityName: 'metadataSection',
isUpdating: false
}
},
created() {
this.form = JSON.parse(JSON.stringify(this.originalMetadataSection));
if (this.form.status == 'auto-draft')
this.form.status = 'publish';
this.formErrors = this.form.formErrors != undefined ? this.form.formErrors : {};
this.formErrorMessage = this.form.formErrors != undefined ? this.form.formErrorMessage : '';
},
mounted() {
// Fills hook forms with it's real values
this.$nextTick()
.then(() => {
this.updateExtraFormData(this.form);
});
},
methods: {
...mapActions('metadata', [
'updateMetadataSection'
]),
saveEdition(metadataSection) {
if ( (metadataSection.metadata_type_object && metadataSection.metadata_type_object.form_component) || metadataSection.edit_form == '') {
this.fillExtraFormData(this.form);
this.isUpdating = true;
this.updateMetadataSection({
collectionId: this.collectionId,
metadataSectionId: metadataSection.id,
index: this.index,
options: this.form
})
.then(() => {
this.form = {};
this.formErrors = {};
this.formErrorMessage = '';
this.isUpdating = false;
this.closedByForm = true;
this.$emit('onEditionFinished');
})
.catch((errors) => {
this.isUpdating = false;
for (let error of errors.errors) {
for (let attribute of Object.keys(error))
this.formErrors[attribute] = error[attribute];
}
this.formErrorMessage = errors.error_message;
this.form.formErrors = this.formErrors;
this.form.formErrorMessage = this.formErrorMessage;
});
} else {
let formElement = document.getElementById('metadataSectionEditForm');
let formData = new FormData(formElement);
let formObj = {};
for (let [key, value] of formData.entries()) {
formObj[key] = value;
}
this.fillExtraFormData(formObj);
this.isUpdating = true;
this.updateMetadataSection({
collectionId: this.collectionId,
metadataSectionId: metadataSection.id,
index: this.index,
options: formObj
})
.then(() => {
this.form = {};
this.formErrors = {};
this.formErrorMessage = '';
this.isUpdating = false;
this.closedByForm = true;
this.$emit('onEditionFinished');
})
.catch((errors) => {
this.isUpdating = false;
for (let error of errors.errors) {
for (let attribute of Object.keys(error))
this.formErrors[attribute] = error[attribute];
}
this.formErrorMessage = errors.error_message;
this.$emit('onErrorFound');
this.form.formErrors = this.formErrors;
this.form.formErrorMessage = this.formErrorMessage;
});
}
},
clearErrors(attribute) {
this.formErrors[attribute] = undefined;
},
cancelEdition() {
this.closedByForm = true;
this.$emit('onEditionCanceled');
},
}
}
</script>
<style lang="scss" scoped>
form#metadataSectionEditForm {
.options-columns {
-moz-column-count: 2;
-moz-column-gap: 0;
-moz-column-rule: 1px solid var(--tainacan-gray1);
-webkit-column-count: 2;
-webkit-column-gap: 0;
-webkit-column-rule: 1px solid var(--tainacan-gray1);
column-count: 2;
column-gap: 4em;
column-rule: 1px solid var(--tainacan-gray1);
padding-left: 0.25em;
padding-right: 0.25em;
padding-bottom: 0.5em;
&>.field, &>section {
-webkit-column-break-inside: avoid;
page-break-inside: avoid;
break-inside: avoid;
}
.field > .field:not(:last-child) {
margin-bottom: 0em;
}
/deep/ .field {
-webkit-column-break-inside: avoid;
page-break-inside: avoid;
break-inside: avoid;
}
section {
display: grid;
}
.field:first-child {
-webkit-column-span: all;
column-span: all;
}
.tainacan-help-tooltip-trigger {
font-size: 1.25em;
}
}
.tainacan-form .field:not(:last-child) {
margin-bottom: 1em;
}
.tainacan-form /deep/ .control-label {
white-space: normal;
}
.metadata-form-section {
margin: 1.5em 0 0.5em -1.5em;
position: relative;
cursor: pointer;
.icon {
background: var(--tainacan-background-color);
z-index: 1;
position: relative;
}
strong {
background: var(--tainacan-background-color);
color: var(--tainacan-gray4);
font-size: 0.875em;
z-index: 1;
position: relative;
padding-right: 12px;
}
hr {
position: absolute;
top: -0.75em;
width: calc(100% - 42px);
height: 1px;
background-color: var(--tainacan-gray2);
margin-left: 42px;
}
}
@media screen and (max-width: 768px) {
.options-columns {
-moz-column-count: 1;
-webkit-column-count: 1;
column-count: 1;
}
}
}
.form-submit {
background-color: var(--tainacan-gray1);
position: sticky;
bottom: 0;
padding: 16px 4.166666667vw;
display: flex;
justify-content: space-between;
z-index: 2;
font-size: 1.125em;
}
</style>

View File

@ -273,9 +273,9 @@ export const updateMetadatumMappers = ({commit}, metadatumMappers) => {
}; };
// METADATA SECTIONS // METADATA SECTIONS
export const fetchMetadataSections = ({commit}, collectionId) => { export const fetchMetadataSections = ({commit}, {collectionId}) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios.tainacan.get('/collection/' + collectionId + '/metadatasection') axios.tainacan.get('/collection/' + collectionId + '/metadata-sections')
.then((res) => { .then((res) => {
let metadataSections = res.data; let metadataSections = res.data;
commit('setMetadataSections', metadataSections); commit('setMetadataSections', metadataSections);
@ -287,6 +287,85 @@ export const fetchMetadataSections = ({commit}, collectionId) => {
}); });
} }
export const sendMetadataSection = ({commit}, { collectionId, name, status, newIndex }) => {
return new Promise((resolve, reject) => {
let endpoint = '/collection/' + collectionId + '/metadata-sections/';
endpoint += '?context=edit';
axios.tainacan.post(endpoint, {
name: name,
status: status,
})
.then(res => {
let metadataSection = res.data;
commit('setSingleMetadataSection', { metadataSection: metadataSection, index: newIndex });
resolve(metadataSection);
})
.catch(error => {
reject(error.response);
});
});
};
export const updateMetadataSection = ({commit}, {collectionId, metadataSectionId, index, options }) => {
return new Promise((resolve, reject) => {
let endpoint = '/collection/' + collectionId + '/metadata/' + metadataSectionId;
endpoint += '?context=edit';
axios.tainacan.put(endpoint, options)
.then(res => {
let metadataSection = res.data;
commit('setSingleMetadataSection', { metadataSection: metadataSection, index: index });
resolve(metadataSection);
})
.catch(error => {
reject({
error_message: error['response']['data'].error_message,
errors: error['response']['data'].errors
});
});
});
};
export const deleteMetadataSection = ({commit}, { collectionId, metadataSectionId }) => {
let endpoint = '/collection/' + collectionId + '/metadata-sections/' + metadataSectionId;
return new Promise((resolve, reject) => {
axios.tainacan.delete(endpoint)
.then(res => {
const metadataSection = res.data;
commit('deleteMetadataSection', metadataSection);
resolve(res.data);
}).catch((error) => {
console.log(error);
reject(error);
});
});
};
export const updateMetadataSections = ({commit}, metadataSections) => {
commit('setMetadataSections', metadataSections);
};
export const updateCollectionMetadataSectionsOrder = ({ commit }, {collectionId, metadataSectionsOrder}) => {
return new Promise((resolve, reject) => {
axios.tainacan.patch('/collections/' + collectionId + '/metadata_sections_order?context=edit', {
metadata_sections_order: metadataSectionsOrder
}).then(res => {
commit('collection/setCollection', res.data, { root: true });
commit('updateMetadataSectionsOrderFromCollection', res.data.metadata_sections_order);
resolve(res.data);
}).catch(error => {
reject(error.response);
});
});
}
export const cleanMetadataSections = ({commit}) => { export const cleanMetadataSections = ({commit}) => {
commit('cleanMetadataSections'); commit('cleanMetadataSections');
}; };

View File

@ -92,7 +92,26 @@ export const cleanMetadata = (state) => {
} }
export const setMetadataSections = (state, metadataSections) => { export const setMetadataSections = (state, metadataSections) => {
state.metadataSections = metadatumSections; state.metadataSections = metadataSections;
}
export const setSingleMetadataSection = (state, { metadataSection, index }) => {
if (index != undefined && index != null)
Vue.set( state.metadataSections, index, metadataSection);
else {
const existingIndex = state.metadataSections.findIndex((aMetadataSection) => aMetadataSection.id == metadataSection.id);
if (existingIndex >= 0)
Vue.set( state.metadataSections, existingIndex, metadataSection)
}
}
export const deleteMetadataSection = ( state, metadataSection ) => {
let index = state.metadataSection.findIndex(deletedMetadataSection => deletedMetadataSection.id == metadataSection.id);
if (index >= 0)
state.metadataSection.splice(index, 1);
} }
export const cleanMetadataSections = (state) => { export const cleanMetadataSections = (state) => {

View File

@ -295,6 +295,22 @@
@onEditionCanceled="onEditionCanceled()" @onEditionCanceled="onEditionCanceled()"
:index="index" /> :index="index" />
</b-modal> </b-modal>
<b-modal
@close="onSectionEditionCanceled()"
:active="openedMetadataSectionId == metadataSection.id"
trap-focus
aria-modal
aria-role="dialog"
custom-class="tainacan-modal"
:close-button-aria-label="$i18n.get('close')">
<metadata-section-edition-form
:collection-id="collectionId"
:original-metadata-section="metadataSection"
@onEditionFinished="onSectionEditionFinished()"
@onEditionCanceled="onSectionEditionCanceled()"
:index="sectionIndex" />
</b-modal>
</div> </div>
</draggable> </draggable>
</div> </div>
@ -358,6 +374,59 @@
v-if="hightlightedMetadatum == metadatum.name"/> v-if="hightlightedMetadatum == metadatum.name"/>
</div> </div>
</draggable> </draggable>
<draggable
v-if="!isRepositoryLevel"
v-model="availableMetadataSectionsList"
:sort="false"
:group="{ name:'metadata', pull: 'clone', put: false, revertClone: true }"
drag-class="sortable-drag">
<div
:id="metadataSection.id"
@click.prevent="addMetadataSectionViaButton(metadatum)"
class="available-metadata-section-item"
v-for="(metadataSection, index) in availableMetadataSectionsList"
:key="index">
<span
v-tooltip="{
content: $i18n.get('instruction_click_or_drag_metadatum_create'),
autoHide: true,
popperClass: ['tainacan-tooltip', 'tooltip', isRepositoryLevel ? 'tainacan-repository-tooltip' : ''],
placement: 'auto-start'
}"
class="icon grip-icon">
<!-- <i class="tainacan-icon tainacan-icon-1-25em tainacan-icon-drag"/> -->
<svg
xmlns="http://www.w3.org/2000/svg"
height="24px"
viewBox="0 0 24 24"
width="24px"
fill="currentColor">
<path
d="M0 0h24v24H0V0z"
fill="transparent"/>
<path d="M11 18c0 1.1-.9 2-2 2s-2-.9-2-2 .9-2 2-2 2 .9 2 2zm-2-8c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0-6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm6 4c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"/>
</svg>
</span>
<span class="metadatum-name">
{{ metadataSection.label }}
<span
v-tooltip="{
popperClass: ['tainacan-tooltip', 'tooltip', isRepositoryLevel ? 'tainacan-repository-tooltip' : '', 'metadata-type-preview-tooltip'],
content: '',//getPreviewTemplateContent(metadatum),
html: true,
delay: {
shown: 0,
hide: 100,
},
placement: 'top',
}"
class="icon preview-help-icon has-text-secondary">
<i class="tainacan-icon tainacan-icon-help"/>
</span>
</span>
</div>
</draggable>
</div> </div>
</div> </div>
</div> </div>
@ -387,6 +456,7 @@
<script> <script>
import MetadataMappingList from '../../components/lists/metadata-mapping-list.vue'; import MetadataMappingList from '../../components/lists/metadata-mapping-list.vue';
import MetadatumEditionForm from '../../components/edition/metadatum-edition-form.vue'; import MetadatumEditionForm from '../../components/edition/metadatum-edition-form.vue';
import MetadataSectionEditionForm from '../../components/edition/metadata-section-edition-form.vue';
import MetadatumDetails from '../../components/other/metadatum-details.vue'; import MetadatumDetails from '../../components/other/metadatum-details.vue';
import ChildMetadataList from '../../components/metadata-types/compound/child-metadata-list.vue'; import ChildMetadataList from '../../components/metadata-types/compound/child-metadata-list.vue';
import CustomDialog from '../../components/other/custom-dialog.vue'; import CustomDialog from '../../components/other/custom-dialog.vue';
@ -397,6 +467,7 @@ export default {
components: { components: {
MetadataMappingList, MetadataMappingList,
MetadatumEditionForm, MetadatumEditionForm,
MetadataSectionEditionForm,
ChildMetadataList, ChildMetadataList,
MetadatumDetails MetadatumDetails
}, },
@ -407,14 +478,21 @@ export default {
collectionId: '', collectionId: '',
isLoadingMetadatumTypes: true, isLoadingMetadatumTypes: true,
isLoadingMetadata: false, isLoadingMetadata: false,
isLoadingMetadataSections: false,
isUpdatingMetadataOrder: false, isUpdatingMetadataOrder: false,
openedMetadatumId: '', openedMetadatumId: '',
openedMetadataSectionId: '',
hightlightedMetadatum: '', hightlightedMetadatum: '',
collapses: {}, collapses: {},
columnsTopY: 0, columnsTopY: 0,
collapseAll: false, collapseAll: false,
metadataNameFilterString: '', metadataNameFilterString: '',
metadataTypeFilterOptions: [] metadataTypeFilterOptions: [],
availableMetadataSectionsList: [{
label: this.$i18n.get('label_add_new_section'),
id: 'metadataSectionCreator'
}],
isUpdatingMetadataSectionsOrder: false
} }
}, },
computed: { computed: {
@ -439,6 +517,14 @@ export default {
set(value) { set(value) {
this.updateMetadata(value); this.updateMetadata(value);
} }
},
activeMetadataSectionsList: {
get() {
return this.getMetadataSections();
},
set(value) {
this.updateMetadataSections(value);
}
} }
}, },
watch: { watch: {
@ -448,6 +534,10 @@ export default {
let existingMetadataIndex = this.activeMetadatumList.findIndex((metadatum) => metadatum && (metadatum.id == newQuery.edit)); let existingMetadataIndex = this.activeMetadatumList.findIndex((metadatum) => metadatum && (metadatum.id == newQuery.edit));
if (existingMetadataIndex >= 0) if (existingMetadataIndex >= 0)
this.editMetadatum(this.activeMetadatumList[existingMetadataIndex]) this.editMetadatum(this.activeMetadatumList[existingMetadataIndex])
} else if (newQuery.sectionEdit != undefined) {
let existingMetadataSectionIndex = this.activeMetadataSectionsList.findIndex((metadataSection) => metadataSection && (metadataSection.id == newQuery.sectionEdit));
if (existingMetadataSectionIndex >= 0)
this.editMetadataSection(this.activeMetadataSectionsList[existingMetadataSectionIndex])
} }
}, },
immediate: true immediate: true
@ -469,6 +559,7 @@ export default {
}); });
this.cleanMetadata(); this.cleanMetadata();
this.cleanMetadataSections();
this.isLoadingMetadatumTypes = true; this.isLoadingMetadatumTypes = true;
this.fetchMetadatumTypes() this.fetchMetadatumTypes()
@ -486,14 +577,30 @@ export default {
.catch(() => { .catch(() => {
this.isLoadingMetadatumTypes = false; this.isLoadingMetadatumTypes = false;
}); });
if (this.isRepositoryLevel) {
this.collectionId = 'default';
this.refreshMetadata(); this.refreshMetadata();
} else {
this.collectionId = this.$route.params.collectionId;
this.isLoadingMetadataSections = true;
this.fetchMetadataSections({ collectionId: this.collectionId })
.then(() => {
this.activeMetadataSectionsList.forEach((aMetadataSection) => {
this.refreshMetadata(aMetadataSection);
});
this.isLoadingMetadataSections = false;
})
.catch((error) => {
this.$console.error(error);
this.isLoadingMetadataSections = false;
});
}
}, },
beforeDestroy() { beforeDestroy() {
// Cancels previous Request // Cancels previous Request
if (this.metadataSearchCancel != undefined) if (this.metadataSearchCancel != undefined)
this.metadataSearchCancel.cancel('Metadata search Canceled.'); this.metadataSearchCancel.cancel('Metadata search Canceled.');
}, },
methods: { methods: {
...mapGetters('collection', [ ...mapGetters('collection', [
@ -504,10 +611,12 @@ export default {
'updateMetadatumTypes', 'updateMetadatumTypes',
'fetchMetadata', 'fetchMetadata',
'sendMetadatum', 'sendMetadatum',
'sendMetadataSection',
'deleteMetadatum', 'deleteMetadatum',
'updateMetadata', 'updateMetadata',
'updateCollectionMetadataOrder', 'updateCollectionMetadataOrder',
'cleanMetadata', 'cleanMetadata',
'updateMetadataSections',
'fetchMetadataSections', 'fetchMetadataSections',
'cleanMetadataSections' 'cleanMetadataSections'
]), ]),
@ -537,6 +646,17 @@ export default {
.then(() => this.isUpdatingMetadataOrder = false) .then(() => this.isUpdatingMetadataOrder = false)
.catch(() => this.isUpdatingMetadataOrder = false); .catch(() => this.isUpdatingMetadataOrder = false);
}, },
updateMetadataSectionsOrder() {
let metadataSectionsOrder = [];
for (let metadataSection of this.activeMetadataSectionsList)
if (metadataSection != undefined)
metadataSectionsOrder.push({ 'id': metadataSection.id, 'enabled': metadataSection.enabled });
this.isUpdatingMetadataSectionsOrder = true;
this.updateCollectionMetadataSectionsOrder({ collectionId: this.collectionId, metadataSectionsOrder: metadataSectionsOrder })
.then(() => this.isUpdatingMetadataSectionsOrder = false)
.catch(() => this.isUpdatingMetadataSectionsOrder = false);
},
onChangeEnable($event, index) { onChangeEnable($event, index) {
let metadataOrder = []; let metadataOrder = [];
for (let metadatum of this.activeMetadatumList) for (let metadatum of this.activeMetadatumList)
@ -553,9 +673,13 @@ export default {
let lastIndex = this.activeMetadatumList.length; let lastIndex = this.activeMetadatumList.length;
this.addNewMetadatum(metadatumType, lastIndex); this.addNewMetadatum(metadatumType, lastIndex);
// Higlights the clicker metadatum // Higlights the clicked metadatum
this.hightlightedMetadatum = metadatumType.name; this.hightlightedMetadatum = metadatumType.name;
}, },
addMetadataSectionViaButton() {
let lastIndex = this.activeMetadataSectionsList.length;
this.addNewMetadataSection(lastIndex);
},
addNewMetadatum(newMetadatum, newIndex) { addNewMetadatum(newMetadatum, newIndex) {
this.sendMetadatum({ this.sendMetadatum({
collectionId: this.collectionId, collectionId: this.collectionId,
@ -578,6 +702,21 @@ export default {
this.$console.error(error); this.$console.error(error);
}); });
}, },
addNewMetadataSection(newIndex) {
this.sendMetadataSection({
collectionId: this.collectionId,
name: '',
status: 'auto-draft',
newIndex: newIndex
})
.then((metadataSection) => {
this.updateMetadataSectionOrder();
this.toggleMetadataSectionEdition(metadataSection)
})
.catch((error) => {
this.$console.error(error);
});
},
removeMetadatum(removedMetadatum) { removeMetadatum(removedMetadatum) {
this.$buefy.modal.open({ this.$buefy.modal.open({
parent: this, parent: this,
@ -602,12 +741,41 @@ export default {
closeButtonAriaLabel: this.$i18n.get('close') closeButtonAriaLabel: this.$i18n.get('close')
}); });
}, },
removeMetadataSection(removedMetadataSection) {
this.$buefy.modal.open({
parent: this,
component: CustomDialog,
props: {
icon: 'alert',
title: this.$i18n.get('label_warning'),
message: this.$i18n.get('info_warning_metadata_section_delete'),
onConfirm: () => {
this.deleteMetadataSection({ collectionId: this.collectionId, metadataSectionId: removedMetadataSection.id })
.then(() => {
this.updateMetadataSectionOrder();
})
.catch(() => {
this.$console.log("Error deleting metadata section.")
});
}
},
trapFocus: true,
customClass: 'tainacan-modal',
closeButtonAriaLabel: this.$i18n.get('close')
});
},
toggleMetadatumEdition(metadatum) { toggleMetadatumEdition(metadatum) {
this.$router.push({ query: { edit: metadatum.id } }); this.$router.push({ query: { edit: metadatum.id } });
}, },
toggleMetadataSectionEdition(metadataSection) {
this.$router.push({ query: { sectionEdit: metadataSection.id } });
},
editMetadatum(metadatum) { editMetadatum(metadatum) {
this.openedMetadatumId = metadatum.id; this.openedMetadatumId = metadatum.id;
}, },
editMetadataSection(metadataSection) {
this.openedMetadataSectionId = metadataSection.id;
},
onEditionFinished() { onEditionFinished() {
this.openedMetadatumId = ''; this.openedMetadatumId = '';
this.$router.push({ query: {}}); this.$router.push({ query: {}});
@ -616,18 +784,22 @@ export default {
this.openedMetadatumId = ''; this.openedMetadatumId = '';
this.$router.push({ query: {}}); this.$router.push({ query: {}});
}, },
refreshMetadata() { onSectionEditionFinished() {
this.openedMetadataSectionId = '';
this.$router.push({ query: {}});
},
onSectionEditionCanceled() {
this.openedMetadataSectionId = '';
this.$router.push({ query: {}});
},
refreshMetadata(metadataSection) {
console.log(metadataSection)
this.isLoadingMetadata = true; this.isLoadingMetadata = true;
// Cancels previous Request // Cancels previous Request
if (this.metadataSearchCancel != undefined) if (this.metadataSearchCancel != undefined)
this.metadataSearchCancel.cancel('Metadata search Canceled.'); this.metadataSearchCancel.cancel('Metadata search Canceled.');
if (this.isRepositoryLevel)
this.collectionId = 'default';
else
this.collectionId = this.$route.params.collectionId;
this.fetchMetadata({ this.fetchMetadata({
collectionId: this.collectionId, collectionId: this.collectionId,
isRepositoryLevel: this.isRepositoryLevel, isRepositoryLevel: this.isRepositoryLevel,
@ -1003,7 +1175,9 @@ export default {
margin: 1em 0em 1em 0em !important; margin: 1em 0em 1em 0em !important;
} }
.available-metadatum-item::before, .available-metadatum-item::before,
.available-metadatum-item::after { .available-metadatum-item::after,
.available-metadata-section-item::before,
.available-metadata-section-item::after {
display: none !important; display: none !important;
} }
} }
@ -1012,7 +1186,8 @@ export default {
margin: 0.875em 0em 1em 0em; margin: 0.875em 0em 1em 0em;
} }
.available-metadatum-item { .available-metadatum-item,
.available-metadata-section-item {
padding: 0.6em; padding: 0.6em;
margin: 4px 4px 4px 1.2em; margin: 4px 4px 4px 1.2em;
background-color: var(--tainacan-white); background-color: var(--tainacan-white);
@ -1074,6 +1249,15 @@ export default {
} }
} }
.available-metadata-section-item {
margin-top: 2em;
color: var(--tainacan-secondary);
border-color: var(--tainacan-secondary);
&::before {
border-color: transparent var(--tainacan-secondary) transparent transparent;
}
}
.sortable-drag { .sortable-drag {
opacity: 1 !important; opacity: 1 !important;
} }
@ -1151,7 +1335,8 @@ export default {
animation-iteration-count: 2; animation-iteration-count: 2;
} }
} }
.available-metadatum-item:hover { .available-metadatum-item:hover,
.available-metadata-section-item::hover {
background-color: var(--tainacan-turquoise1); background-color: var(--tainacan-turquoise1);
border-color: var(--tainacan-turquoise2); border-color: var(--tainacan-turquoise2);
position: relative; position: relative;
@ -1182,7 +1367,8 @@ export default {
color: var(--tainacan-blue5) !important; color: var(--tainacan-blue5) !important;
} }
} }
&.available-metadatum-item:hover { &.available-metadatum-item:hover,
&.available-metadata-section-item::hover {
background-color: var(--tainacan-blue1) !important; background-color: var(--tainacan-blue1) !important;
border-color: var(--tainacan-blue2) !important; border-color: var(--tainacan-blue2) !important;

View File

@ -256,6 +256,7 @@ class Admin {
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$Tainacan_Metadata_Sections = \Tainacan\Repositories\Metadata_Sections::get_instance();
$Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance(); $Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance();
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
@ -265,6 +266,7 @@ class Admin {
$entities_labels = [ $entities_labels = [
'collections' => $Tainacan_Collections->get_cpt_labels(), 'collections' => $Tainacan_Collections->get_cpt_labels(),
'metadata' => $Tainacan_Metadata->get_cpt_labels(), 'metadata' => $Tainacan_Metadata->get_cpt_labels(),
'metadata-sections' => $Tainacan_Metadata_Sections->get_cpt_labels(),
'filters' => $Tainacan_Filters->get_cpt_labels(), 'filters' => $Tainacan_Filters->get_cpt_labels(),
'items' => $Tainacan_Items->get_cpt_labels(), 'items' => $Tainacan_Items->get_cpt_labels(),
'taxonomies' => $Tainacan_Taxonomies->get_cpt_labels(), 'taxonomies' => $Tainacan_Taxonomies->get_cpt_labels(),

View File

@ -624,6 +624,7 @@ return apply_filters( 'tainacan-i18n', [
'label_metadata_and_sections' => __( 'Metadata and Sections', 'tainacan' ), 'label_metadata_and_sections' => __( 'Metadata and Sections', 'tainacan' ),
'label_view_activity_logs' => __( 'View activity logs', 'tainacan' ), 'label_view_activity_logs' => __( 'View activity logs', 'tainacan' ),
'label_item_activities' => __( 'Item activities', 'tainacan' ), 'label_item_activities' => __( 'Item activities', 'tainacan' ),
'label_add_new_section' => __( 'Add new section', 'tainacan' ),
// Instructions. More complex sentences to guide user and placeholders // Instructions. More complex sentences to guide user and placeholders
'instruction_delete_selected_collections' => __( 'Delete selected collections', 'tainacan' ), 'instruction_delete_selected_collections' => __( 'Delete selected collections', 'tainacan' ),
@ -660,6 +661,8 @@ return apply_filters( 'tainacan-i18n', [
'instruction_select_metadatum_type' => __( 'Select a metadatum type', 'tainacan' ), 'instruction_select_metadatum_type' => __( 'Select a metadatum type', 'tainacan' ),
'instruction_configure_new_metadatum' => __( 'Configure new metadatum', 'tainacan' ), 'instruction_configure_new_metadatum' => __( 'Configure new metadatum', 'tainacan' ),
'instruction_configure_the_metadatum' => __( 'Configure the metadatum', 'tainacan' ), 'instruction_configure_the_metadatum' => __( 'Configure the metadatum', 'tainacan' ),
'instruction_configure_new_metadata_section' => __( 'Configure new metadata section', 'tainacan' ),
'instruction_configure_the_metadata_section' => __( 'Configure the metadata section', 'tainacan' ),
'instruction_insert_mapper_metadatum_info' => __( 'Insert the new mapper\'s metadatum info', 'tainacan' ), 'instruction_insert_mapper_metadatum_info' => __( 'Insert the new mapper\'s metadatum info', 'tainacan' ),
'instruction_select_max_options_to_show' => __( 'Select maximum options to show', 'tainacan' ), 'instruction_select_max_options_to_show' => __( 'Select maximum options to show', 'tainacan' ),
'instruction_select_collection_fetch_items' => __( 'Select a collection to fetch items', 'tainacan' ), 'instruction_select_collection_fetch_items' => __( 'Select a collection to fetch items', 'tainacan' ),
@ -756,6 +759,7 @@ return apply_filters( 'tainacan-i18n', [
'info_warning_remove_item_from_trash' => __( 'Do you really want to remove this item from trash?', 'tainacan' ), 'info_warning_remove_item_from_trash' => __( 'Do you really want to remove this item from trash?', 'tainacan' ),
'info_warning_item_trash' => __( 'Do you really want to trash this item?', 'tainacan' ), 'info_warning_item_trash' => __( 'Do you really want to trash this item?', 'tainacan' ),
'info_warning_metadatum_delete' => __( 'Do you really want to permanently delete this metadatum?', 'tainacan' ), 'info_warning_metadatum_delete' => __( 'Do you really want to permanently delete this metadatum?', 'tainacan' ),
'info_warning_metadata_section_delete' => __( 'Do you really want to permanently delete this metadata section?', 'tainacan' ),
'info_warning_taxonomy_delete' => __( 'Do you really want to delete this taxonomy?', 'tainacan' ), 'info_warning_taxonomy_delete' => __( 'Do you really want to delete this taxonomy?', 'tainacan' ),
'info_warning_selected_collections_delete' => __( 'Do you really want to permanently delete the selected collections?', 'tainacan' ), 'info_warning_selected_collections_delete' => __( 'Do you really want to permanently delete the selected collections?', 'tainacan' ),
'info_warning_selected_collections_trash' => __( 'Do you really want to trash the selected collections?', 'tainacan' ), 'info_warning_selected_collections_trash' => __( 'Do you really want to trash the selected collections?', 'tainacan' ),

View File

@ -58,8 +58,8 @@ class Entity_Factory {
$this->repository_type = "\Tainacan\Repositories\\$type".'es'; $this->repository_type = "\Tainacan\Repositories\\$type".'es';
} elseif($type == 'Metadatum'){ } elseif($type == 'Metadatum'){
$this->repository_type = "\Tainacan\Repositories\Metadata"; $this->repository_type = "\Tainacan\Repositories\Metadata";
} elseif($type == 'Metadatum_Section'){ } elseif($type == 'Metadata_Section'){
$this->repository_type = "\Tainacan\Repositories\Metadata_Section"; $this->repository_type = "\Tainacan\Repositories\Metadata_Sections";
} }
else { else {
$this->repository_type = "\Tainacan\Repositories\\$type".'s'; $this->repository_type = "\Tainacan\Repositories\\$type".'s';

View File

@ -5,9 +5,9 @@ namespace Tainacan\Tests;
/** /**
* @group api * @group api
*/ */
class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase { class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCase {
public function test_create_empty_metadatum_section() { public function test_create_empty_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadatum = json_encode( $metadatum = json_encode(
@ -26,13 +26,13 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadatum_section_added = $response->get_data(); $metadata_section_added = $response->get_data();
$this->assertTrue(is_array($metadatum_section_added) && array_key_exists('name', $metadatum_section_added), sprintf('cannot create metadatum section, response: %s', print_r($metadatum_section_added, true))); $this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true)));
$this->assertEquals('Dados Pessoais', $metadatum_section_added['name']); $this->assertEquals('Dados Pessoais', $metadata_section_added['name']);
$this->assertTrue(empty($metadatum_section_added['metadatum_list'])); $this->assertTrue(empty($metadata_section_added['metadata_list']));
} }
public function test_create_fill_metadatum_section() { public function test_create_fill_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadatum_1 = $this->tainacan_entity_factory->create_entity(
@ -57,12 +57,12 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
true true
); );
$metadatum_section = json_encode( $metadata_section = json_encode(
array( array(
'name' => 'Dados Pessoais', 'name' => 'Dados Pessoais',
'description' => 'Informações e detalhes.', 'description' => 'Informações e detalhes.',
'collection_id' => $collection->get_id(), 'collection_id' => $collection->get_id(),
'metadatum_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_2->get_id()] 'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_2->get_id()]
) )
); );
@ -70,17 +70,17 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
'POST', 'POST',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections' $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections'
); );
$request->set_body($metadatum_section); $request->set_body($metadata_section);
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadatum_section_added = $response->get_data(); $metadata_section_added = $response->get_data();
$this->assertTrue(is_array($metadatum_section_added) && array_key_exists('name', $metadatum_section_added), sprintf('cannot create metadatum section, response: %s', print_r($metadatum_section_added, true))); $this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true)));
$this->assertEquals('Dados Pessoais', $metadatum_section_added['name']); $this->assertEquals('Dados Pessoais', $metadata_section_added['name']);
$this->assertEquals(2, count($metadatum_section_added['metadatum_list'])); $this->assertEquals(2, count($metadata_section_added['metadata_list']));
} }
public function test_add_metadata_metadatum_section() { public function test_add_metadata_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadatum_1 = $this->tainacan_entity_factory->create_entity(
@ -119,38 +119,38 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
true true
); );
$metadatum_section = $this->tainacan_entity_factory->create_entity( $metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadatum_Section', 'Metadata_Section',
array( array(
'name' => 'Section', 'name' => 'Section',
'description' => 'Section Description', 'description' => 'Section Description',
'collection' => $collection, 'collection' => $collection,
'status' => 'publish', 'status' => 'publish',
'metadatum_list' => [$metadatum_1->get_id(), $metadatum_1->get_id()] 'metadata_list' => [$metadatum_1->get_id(), $metadatum_1->get_id()]
), ),
true true
); );
$metadatum_list = json_encode( $metadata_list = json_encode(
array( array(
'metadatum_list' => [$metadatum_2->get_id(), $metadatum_3->get_id()] 'metadata_list' => [$metadatum_2->get_id(), $metadatum_3->get_id()]
) )
); );
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'POST', 'POST',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadatum_section->get_id() . '/metadatum' $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadata_section->get_id() . '/metadata'
); );
$request->set_body($metadatum_list); $request->set_body($metadata_list);
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadatum_section_added = $response->get_data(); $metadata_section_added = $response->get_data();
$this->assertTrue(is_array($metadatum_section_added) && array_key_exists('name', $metadatum_section_added), sprintf('cannot create metadatum section, response: %s', print_r($metadatum_section_added, true))); $this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true)));
$this->assertEquals('Section', $metadatum_section_added['name']); $this->assertEquals('Section', $metadata_section_added['name']);
$this->assertEquals(3, count($metadatum_section_added['metadatum_list'])); $this->assertEquals(3, count($metadata_section_added['metadata_list']));
$this->assertContains($metadatum_1->get_id(), $metadatum_section_added['metadatum_list']); $this->assertContains($metadatum_1->get_id(), $metadata_section_added['metadata_list']);
$this->assertContains($metadatum_2->get_id(), $metadatum_section_added['metadatum_list']); $this->assertContains($metadatum_2->get_id(), $metadata_section_added['metadata_list']);
$this->assertContains($metadatum_3->get_id(), $metadatum_section_added['metadatum_list']); $this->assertContains($metadatum_3->get_id(), $metadata_section_added['metadata_list']);
$metadatum = $this->tainacan_entity_factory->create_entity( $metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum', 'metadatum',
@ -168,7 +168,7 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
$values = json_encode([ $values = json_encode([
'name' => 'Dia/Mês/Ano', 'name' => 'Dia/Mês/Ano',
'description' => 'Continua descrevendo o dado do campo.', 'description' => 'Continua descrevendo o dado do campo.',
'metadatum_section_id' => $metadatum_section->get_id() 'metadata_section_id' => $metadata_section->get_id()
]); ]);
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
@ -182,11 +182,11 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
$this->assertEquals($metadatum->get_id(), $data['id']); $this->assertEquals($metadatum->get_id(), $data['id']);
$this->assertEquals('Dia/Mês/Ano', $data['name']); $this->assertEquals('Dia/Mês/Ano', $data['name']);
$this->assertEquals($metadatum_section->get_id(), $data['metadatum_section_id']); $this->assertEquals($metadata_section->get_id(), $data['metadata_section_id']);
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'GET', 'GET',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadatum_section->get_id() . '/metadatum' $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadata_section->get_id() . '/metadata'
); );
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadata_list = $response->get_data(); $metadata_list = $response->get_data();
@ -197,7 +197,7 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
$this->assertNotNull(array_search($metadatum->get_id(), array_column($metadata_list, "id"))); $this->assertNotNull(array_search($metadatum->get_id(), array_column($metadata_list, "id")));
} }
public function test_delete_metadata_metadatum_section() { public function test_delete_metadata_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadatum_1 = $this->tainacan_entity_factory->create_entity(
@ -233,41 +233,41 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
true true
); );
$metadatum_section = $this->tainacan_entity_factory->create_entity( $metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadatum_Section', 'Metadata_Section',
array( array(
'name' => 'Section', 'name' => 'Section',
'description' => 'Section Description', 'description' => 'Section Description',
'collection' => $collection, 'collection' => $collection,
'metadatum_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_3->get_id()] 'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_3->get_id()]
), ),
true true
); );
$metadatum_list = json_encode( $metadata_list = json_encode(
array( array(
'metadatum_list' => [$metadatum_1->get_id(), $metadatum_3->get_id()] 'metadata_list' => [$metadatum_1->get_id(), $metadatum_3->get_id()]
) )
); );
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'DELETE', 'DELETE',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadatum_section->get_id() . '/metadatum' $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadata_section->get_id() . '/metadata'
); );
$request->set_body($metadatum_list); $request->set_body($metadata_list);
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadatum_section_added = $response->get_data(); $metadata_section_added = $response->get_data();
$this->assertTrue(is_array($metadatum_section_added) && array_key_exists('name', $metadatum_section_added), sprintf('cannot create metadatum section, response: %s', print_r($metadatum_section_added, true))); $this->assertTrue(is_array($metadata_section_added) && array_key_exists('name', $metadata_section_added), sprintf('cannot create metadata section, response: %s', print_r($metadata_section_added, true)));
$this->assertEquals('Section', $metadatum_section_added['name']); $this->assertEquals('Section', $metadata_section_added['name']);
$this->assertEquals(1, count($metadatum_section_added['metadatum_list'])); $this->assertEquals(1, count($metadata_section_added['metadata_list']));
$this->assertNotContains($metadatum_1->get_id(), $metadatum_section_added['metadatum_list']); $this->assertNotContains($metadatum_1->get_id(), $metadata_section_added['metadata_list']);
$this->assertContains($metadatum_2->get_id(), $metadatum_section_added['metadatum_list']); $this->assertContains($metadatum_2->get_id(), $metadata_section_added['metadata_list']);
$this->assertNotContains($metadatum_3->get_id(), $metadatum_section_added['metadatum_list']); $this->assertNotContains($metadatum_3->get_id(), $metadata_section_added['metadata_list']);
} }
public function test_get_metadata_metadatum_section() { public function test_get_metadata_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadatum_1 = $this->tainacan_entity_factory->create_entity(
@ -306,20 +306,20 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
true true
); );
$metadatum_section = $this->tainacan_entity_factory->create_entity( $metadata_section = $this->tainacan_entity_factory->create_entity(
'Metadatum_Section', 'Metadata_Section',
array( array(
'name' => 'Section', 'name' => 'Section',
'description' => 'Section Description', 'description' => 'Section Description',
'collection' => $collection, 'collection' => $collection,
'metadatum_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_3->get_id()] 'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id(), $metadatum_3->get_id()]
), ),
true true
); );
$request = new \WP_REST_Request( $request = new \WP_REST_Request(
'GET', 'GET',
$this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadatum_section->get_id() . '/metadatum' $this->namespace . '/collection/' . $collection->get_id() . '/metadata-sections/' . $metadata_section->get_id() . '/metadata'
); );
$response = $this->server->dispatch($request); $response = $this->server->dispatch($request);
$metadata_list = $response->get_data(); $metadata_list = $response->get_data();
@ -327,7 +327,7 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
$this->assertEquals(3, count($metadata_list)); $this->assertEquals(3, count($metadata_list));
} }
public function test_get_metadatum_section() { public function test_get_metadata_section() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true); $collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$metadatum_1 = $this->tainacan_entity_factory->create_entity( $metadatum_1 = $this->tainacan_entity_factory->create_entity(
@ -379,24 +379,24 @@ class TAINACAN_REST_Metadata_Section_Controller extends TAINACAN_UnitApiTestCase
); );
$this->tainacan_entity_factory->create_entity( $this->tainacan_entity_factory->create_entity(
'Metadatum_Section', 'Metadata_Section',
array( array(
'name' => 'Section', 'name' => 'Section',
'description' => 'Section Description', 'description' => 'Section Description',
'collection' => $collection, 'collection' => $collection,
'metadatum_list' => [$metadatum_1->get_id(), $metadatum_2->get_id()] 'metadata_list' => [$metadatum_1->get_id(), $metadatum_2->get_id()]
), ),
true, true,
true true
); );
$this->tainacan_entity_factory->create_entity( $this->tainacan_entity_factory->create_entity(
'Metadatum_Section', 'Metadata_Section',
array( array(
'name' => 'Section', 'name' => 'Section',
'description' => 'Section Description', 'description' => 'Section Description',
'collection' => $collection, 'collection' => $collection,
'metadatum_list' => [$metadatum_3->get_id(), $metadatum_4->get_id()] 'metadata_list' => [$metadatum_3->get_id(), $metadatum_4->get_id()]
), ),
true, true,
true true