fix: change call action to update/insert control metadatum item #114
This commit is contained in:
parent
4b42e48b59
commit
7b55a3686d
|
@ -405,7 +405,7 @@ class Item extends Entity {
|
|||
$is_valid = false;
|
||||
}
|
||||
|
||||
$arrayItemMetadata = $this->get_metadata(['parent'=>'any']);
|
||||
$arrayItemMetadata = $this->get_metadata(['parent'=>'any', 'include_control_metadata_types' => 'true']);
|
||||
if ( $arrayItemMetadata ) {
|
||||
foreach ( $arrayItemMetadata as $itemMetadata ) {
|
||||
|
||||
|
|
|
@ -653,11 +653,12 @@ class Metadata extends Repository {
|
|||
|
||||
do_action( 'register_metadata_types' );
|
||||
|
||||
return array_filter($this->metadata_types, function($metadata_type) use ($output) {
|
||||
if ( $metadata_type != 'Tainacan\Metadata_Types\Control') {
|
||||
return $output === 'NAME' ? str_replace( 'Tainacan\Metadata_Types\\', '', $metadata_type ) : $metadata_type;
|
||||
}
|
||||
});
|
||||
return array_map(
|
||||
function($metadata_type) use ($output) {
|
||||
return $output === 'NAME' ? str_replace( 'Tainacan\\Metadata_Types\\', '', $metadata_type ) : $metadata_type;
|
||||
},
|
||||
array_filter($this->metadata_types, function($metadata_type) { return $metadata_type != 'Tainacan\Metadata_Types\Control';})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -10,33 +10,42 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Class TainacanMetadatumType
|
||||
*/
|
||||
class Control extends Metadata_Type {
|
||||
class MetadataTypeControlHelperHook {
|
||||
private static $instance = null;
|
||||
private function __construct() { }
|
||||
|
||||
function __construct(){
|
||||
// call metadatum type constructor
|
||||
parent::__construct();
|
||||
$this->set_primitive_type('control');
|
||||
$this->set_component('tainacan-text');
|
||||
$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_default_options([
|
||||
'control_metadatum_options' => ['document_type', 'collection_id'],
|
||||
'control_metadatum' => 'document_type'
|
||||
]);
|
||||
add_action( 'tainacan-insert-tainacan-item', array( $this, 'update_control_metadatum' ), 10, 1 );
|
||||
public static function get_instance() {
|
||||
if ( ! isset( self::$instance ) ) {
|
||||
self::$instance = new self();
|
||||
add_action( 'tainacan-insert-tainacan-item', array(self::$instance, 'update_control_metadatum'), 10, 1 );
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
function update_control_metadatum( $item ) {
|
||||
static function update_control_metadatum( $item ) {
|
||||
if ( $item instanceof \Tainacan\Entities\Item ) {
|
||||
$metadata = $item->get_metadata();
|
||||
$item_metadata_repositories = \Tainacan\Repositories\Item_Metadata::get_instance();
|
||||
$item_metadata_repositories->disable_logs();
|
||||
$collection = $item->get_collection();
|
||||
$args = [
|
||||
'include_control_metadata_types' => true,
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => 'metadata_type',
|
||||
'value' => 'Tainacan\Metadata_Types\Control',
|
||||
'compare' => 'IN'
|
||||
]
|
||||
]
|
||||
];
|
||||
$metadatum_repository = \Tainacan\Repositories\Metadata::get_instance();
|
||||
$metadata = $metadatum_repository->fetch_by_collection( $collection, $args );
|
||||
foreach ($metadata as $item_metadatum) {
|
||||
if ( $item_metadatum->get_metadatum()->get_metadata_type_object() instanceof \Tainacan\Metadata_Types\Control &&
|
||||
$item_metadatum->get_metadatum()->get_metadata_type_options()['control_metadatum'] == $this->get_option('control_metadatum')) {
|
||||
|
||||
$update_item_metadatum = new \Tainacan\Entities\Item_Metadata_Entity( $item, $item_metadatum->get_metadatum() );
|
||||
switch ( $this->get_option('control_metadatum') ) {
|
||||
if ( $item_metadatum->get_metadata_type_object() instanceof \Tainacan\Metadata_Types\Control) {
|
||||
$update_item_metadatum = new \Tainacan\Entities\Item_Metadata_Entity( $item, $item_metadatum );
|
||||
switch ( $item_metadatum->get_metadata_type_object()->get_option('control_metadatum') ) {
|
||||
case 'document_type':
|
||||
$update_item_metadatum->set_value( $item->get_document_type() );
|
||||
$document_type = $item->get_document_type() == 'attachment' ? get_post_mime_type($item->get_document()) : $item->get_document_type();
|
||||
$update_item_metadatum->set_value( $document_type !== false ? $document_type : 'attachment' );
|
||||
break;
|
||||
|
||||
case 'collection_id':
|
||||
|
@ -49,15 +58,38 @@ class Control extends Metadata_Type {
|
|||
}
|
||||
|
||||
if ( $update_item_metadatum->validate() )
|
||||
\Tainacan\Repositories\Item_Metadata::get_instance()->insert( $update_item_metadatum );
|
||||
$item_metadata_repositories->insert( $update_item_metadatum );
|
||||
else
|
||||
$errors[] = $update_item_metadatum->get_errors();
|
||||
|
||||
break; // Ends foreach in case we already found the related metadata
|
||||
}
|
||||
}
|
||||
$item_metadata_repositories->enable_logs();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class TainacanMetadatumType
|
||||
*/
|
||||
class Control extends Metadata_Type {
|
||||
|
||||
private $metadataTypeControlHelperHook;
|
||||
|
||||
function __construct() {
|
||||
// call metadatum type constructor
|
||||
parent::__construct();
|
||||
$this->set_primitive_type('control');
|
||||
$this->set_component('tainacan-text');
|
||||
$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_default_options([
|
||||
'control_metadatum_options' => ['document_type', 'collection_id'],
|
||||
'control_metadatum' => 'document_type'
|
||||
]);
|
||||
$metadataTypeControlHelperHook = MetadataTypeControlHelperHook::get_instance();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function validate_options( Metadatum $metadatum ) {
|
||||
if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
|
||||
|
|
|
@ -380,7 +380,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
|
|||
$data = $response->get_data()['items'];
|
||||
|
||||
$this->assertEquals( 2, sizeof($data) );
|
||||
$this->assertEquals( 7, sizeof($data[0]['metadata']) );
|
||||
$this->assertEquals( 5, sizeof($data[0]['metadata']) );
|
||||
|
||||
// Fetch only as admin
|
||||
$request = new \WP_REST_Request(
|
||||
|
|
|
@ -136,7 +136,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
|||
|
||||
$data = $response_defaults->get_data();
|
||||
|
||||
$this->assertCount(3, $data);
|
||||
$this->assertCount(1, $data);
|
||||
|
||||
$this->assertEquals('default', $data[0]['collection_id']);
|
||||
$this->assertEquals('Data 2', $data[0]['name']);
|
||||
|
@ -764,12 +764,10 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
|||
|
||||
$response = $this->server->dispatch($requestC);
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals(5, count($data));
|
||||
$this->assertEquals(3, count($data));
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data[0]['id']);
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data[1]['id']);
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data[2]['id']);
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data[3]['id']);
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data[4]['id']);
|
||||
}
|
||||
|
||||
public function test_private_meta_ids_not_in_metadata_order(){
|
||||
|
@ -827,7 +825,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
|||
$response = $this->server->dispatch($request);
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals(6, count($data['metadata_order']));
|
||||
$this->assertEquals(4, count($data['metadata_order']));
|
||||
|
||||
wp_logout();
|
||||
wp_set_current_user(0);
|
||||
|
@ -837,12 +835,10 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
|||
$response = $this->server->dispatch($request);
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals(5, count($data['metadata_order']));
|
||||
$this->assertEquals(3, count($data['metadata_order']));
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data['metadata_order'][0]['id']);
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data['metadata_order'][1]['id']);
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data['metadata_order'][2]['id']);
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data['metadata_order'][3]['id']);
|
||||
$this->assertNotEquals($metadatumB->get_id(), $data['metadata_order'][4]['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -502,25 +502,25 @@ class Capabilities extends TAINACAN_UnitTestCase {
|
|||
wp_set_current_user($this->subscriber2->ID);
|
||||
|
||||
$meta = tainacan_metadata()->fetch_by_collection($this->public_collection);
|
||||
$this->AssertEquals(6, sizeof($meta));
|
||||
$this->AssertEquals(4, sizeof($meta));
|
||||
$meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection);
|
||||
$this->AssertEquals(6, sizeof($meta));
|
||||
$this->AssertEquals(4, sizeof($meta));
|
||||
|
||||
$this->subscriber2->add_cap( 'tnc_col_' . $this->public_collection->get_id() . '_read_private_metadata' );
|
||||
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
||||
|
||||
$meta = tainacan_metadata()->fetch_by_collection($this->public_collection);
|
||||
$this->AssertEquals(7, sizeof($meta));
|
||||
$this->AssertEquals(5, sizeof($meta));
|
||||
$meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection);
|
||||
$this->AssertEquals(7, sizeof($meta));
|
||||
$this->AssertEquals(5, sizeof($meta));
|
||||
|
||||
$this->subscriber2->add_cap( 'tnc_rep_read_private_metadata' );
|
||||
$current_user = $this->subscriber2; // force update current user object with new capabilities
|
||||
|
||||
$meta = tainacan_metadata()->fetch_by_collection($this->public_collection);
|
||||
$this->AssertEquals(8, sizeof($meta));
|
||||
$this->AssertEquals(6, sizeof($meta));
|
||||
$meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection);
|
||||
$this->AssertEquals(8, sizeof($meta));
|
||||
$this->AssertEquals(6, sizeof($meta));
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -240,7 +240,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
|
|||
$this->assertTrue(is_array($item_metadatas));
|
||||
|
||||
// notice for repository metadata
|
||||
$this->assertEquals(5, sizeof($item_metadatas));
|
||||
$this->assertEquals(3, sizeof($item_metadatas));
|
||||
//first 2 metadata are repository metadata
|
||||
$this->assertTrue( in_array('metadado', $names) );
|
||||
|
||||
|
|
|
@ -165,8 +165,8 @@ class Metadata extends TAINACAN_UnitTestCase {
|
|||
$retrieve_metadata_ids = $Tainacan_Metadata->fetch_ids_by_collection( $collection_son, [] );
|
||||
|
||||
// should return 6
|
||||
$this->assertEquals( 8, sizeof( $retrieve_metadata ) );
|
||||
$this->assertEquals( 8, sizeof( $retrieve_metadata_ids ) );
|
||||
$this->assertEquals( 6, sizeof( $retrieve_metadata ) );
|
||||
$this->assertEquals( 6, sizeof( $retrieve_metadata_ids ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -197,7 +197,7 @@ class Metadata extends TAINACAN_UnitTestCase {
|
|||
*/
|
||||
function test_metadata_metadata_type(){
|
||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||
$this->assertEquals( 10, sizeof( $Tainacan_Metadata->fetch_metadata_types() ) );
|
||||
$this->assertEquals( 9, sizeof( $Tainacan_Metadata->fetch_metadata_types() ) );
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue