feat: add option `only_repository` in control metadata #114

This commit is contained in:
vnmedeiros 2020-10-30 20:12:18 -03:00
parent c2d060950c
commit 386dceeb83
5 changed files with 43 additions and 19 deletions

View File

@ -382,6 +382,12 @@ class REST_Filters_Controller extends REST_Controller {
} else { } else {
$collection = $this->collection_repository->fetch($request['collection_id']); $collection = $this->collection_repository->fetch($request['collection_id']);
$filters = $this->filter_repository->fetch_by_collection($collection, $args); $filters = $this->filter_repository->fetch_by_collection($collection, $args);
$filters = array_filter($filters, function($filter) {
return ( $filter->get_metadatum() !== null && (
!isset($filter->get_metadatum()->get_metadata_type_options()['only_repository']) ||
$filter->get_metadatum()->get_metadata_type_options()['only_repository'] == 'no' )
);
});
} }
$response = []; $response = [];

View File

@ -387,6 +387,10 @@ class REST_Metadata_Controller extends REST_Controller {
] ]
]; ];
if ($request['include_control_metadata_types'] === 'true') {
$args['include_control_metadata_types'] = true;
}
$result = $this->metadatum_repository->fetch( $args, 'OBJECT' ); $result = $this->metadatum_repository->fetch( $args, 'OBJECT' );
} }

View File

@ -110,7 +110,7 @@ class Cli_Control_Metadata {
$progress->finish(); $progress->finish();
$this->result_count['items'] += $this->result_count['items_collection']; $this->result_count['items'] += $this->result_count['items_collection'];
$msg = "\n" . $this->result_count['items_collection'] . " items recalculated in collection"; $msg = $this->result_count['items_collection'] . " items recalculated in collection";
\WP_CLI::success( $msg ); \WP_CLI::success( $msg );
} }

View File

@ -458,7 +458,9 @@ class Metadata extends Repository {
$results = $this->fetch( $args, 'OBJECT' ); $results = $this->fetch( $args, 'OBJECT' );
} }
$results = array_filter($results, function($meta) {
return ( !isset($meta->get_metadata_type_options()['only_repository']) || $meta->get_metadata_type_options()['only_repository'] == 'no' );
});
return $this->order_result( return $this->order_result(
$results, $results,
@ -793,22 +795,26 @@ class Metadata extends Repository {
return; return;
} }
$metadata = $this->fetch_by_collection( $collection, [ $metadata = $this->fetch( [
'meta_query' => [ 'meta_query' => [
[ [
'key' => 'metadata_type', 'key' => 'metadata_type',
'value' => [ 'Tainacan\Metadata_Types\Control' ], 'value' => 'Tainacan\Metadata_Types\Control',
'compare' => 'IN' 'compare' => '='
],[
'key' => 'collection_id',
'value' => 'default',
'compare' => '='
] ]
], ],
'include_disabled' => true, 'include_disabled' => true,
'include_control_metadata_types' => true 'include_control_metadata_types' => true
] ); ], 'OBJECT' );
$data_control_metadata = [ $data_control_metadata = [
'document_type' => [ 'document_type' => [
'name' => 'DocumentType', 'name' => __('Document type', 'tainacan'),
'description' => 'The item main document type', 'description' => __('The item main document type', 'tainacan'),
'collection_id' => 'default', 'collection_id' => 'default',
'metadata_type' => 'Tainacan\Metadata_Types\Control', 'metadata_type' => 'Tainacan\Metadata_Types\Control',
'status' => 'publish', 'status' => 'publish',
@ -816,22 +822,25 @@ class Metadata extends Repository {
'metadata_type_options' => [ 'control_metadatum' => 'document_type' ] 'metadata_type_options' => [ 'control_metadatum' => 'document_type' ]
], ],
'collection_id' => [ 'collection_id' => [
'name' => 'CollectionID', 'name' => __('Collection', 'tainacan'),
'description' => 'The item collection ID', 'description' => __('The item collection ID', 'tainacan'),
'collection_id' => 'default', 'collection_id' => 'default',
'metadata_type' => 'Tainacan\Metadata_Types\Control', 'metadata_type' => 'Tainacan\Metadata_Types\Control',
'status' => 'publish', 'status' => 'publish',
'display' => 'never', 'display' => 'never',
'metadata_type_options' => [ 'control_metadatum' => 'collection_id' ] 'metadata_type_options' => [
'control_metadatum' => 'collection_id',
'only_repository' => 'yes'
]
], ],
'have_thumbnail' => [ 'has_thumbnail' => [
'name' => 'HaveThumbnail', 'name' => __('Has thumbnail', 'tainacan'),
'description' => 'Does the item have a thumbnail?', 'description' => __('Does the item has a thumbnail set?', 'tainacan'),
'collection_id' => 'default', 'collection_id' => 'default',
'metadata_type' => 'Tainacan\Metadata_Types\Control', 'metadata_type' => 'Tainacan\Metadata_Types\Control',
'status' => 'publish', 'status' => 'publish',
'display' => 'never', 'display' => 'never',
'metadata_type_options' => [ 'control_metadatum' => 'have_thumbnail' ] 'metadata_type_options' => [ 'control_metadatum' => 'has_thumbnail' ]
], ],
]; ];

View File

@ -52,7 +52,7 @@ class MetadataTypeControlHelper {
$update_item_metadatum->set_value( $item->get_collection_id() ); $update_item_metadatum->set_value( $item->get_collection_id() );
break; break;
case 'have_thumbnail': case 'has_thumbnail':
$update_item_metadatum->set_value( !empty($item->get__thumbnail_id()) ? 'yes':'no' ); $update_item_metadatum->set_value( !empty($item->get__thumbnail_id()) ? 'yes':'no' );
break; break;
@ -87,8 +87,9 @@ class Control extends Metadata_Type {
$this->set_name( __('Control Type', 'tainacan') ); $this->set_name( __('Control Type', 'tainacan') );
$this->set_description( __('A special metadata type, used to map certain item properties such as collection id and document type to metadata in order to easily create filters.', 'tainacan') ); $this->set_description( __('A special metadata type, used to map certain item properties such as collection id and document type to metadata in order to easily create filters.', 'tainacan') );
$this->set_default_options([ $this->set_default_options([
'control_metadatum_options' => ['document_type', 'collection_id', 'have_thumbnail'], 'control_metadatum_options' => ['document_type', 'collection_id', 'has_thumbnail'],
'control_metadatum' => 'document_type' 'control_metadatum' => 'document_type',
'only_repository' => 'no'
]); ]);
$metadataTypeControlHelper = MetadataTypeControlHelper::get_instance(); $metadataTypeControlHelper = MetadataTypeControlHelper::get_instance();
} }
@ -121,7 +122,7 @@ class Control extends Metadata_Type {
$return = ($format == 'html' ? $this->get_collection_as_html( $value ) : $this->get_collection_as_string( $value )); $return = ($format == 'html' ? $this->get_collection_as_html( $value ) : $this->get_collection_as_string( $value ));
break; break;
case 'have_thumbnail': case 'has_thumbnail':
$return = ( $value == 'yes' ? __( 'yes', 'tainacan' ) : __( 'no', 'tainacan' ) ); $return = ( $value == 'yes' ? __( 'yes', 'tainacan' ) : __( 'no', 'tainacan' ) );
break; break;
@ -174,6 +175,10 @@ class Control extends Metadata_Type {
return __( 'URL', 'tainacan' ); return __( 'URL', 'tainacan' );
break; break;
case 'empty':
return __( 'Empty', 'tainacan' );
break;
default: default:
return $value; return $value;
} }