From 3f21743672a327a16df7d683fbaadcbc076bd9ce Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Sun, 3 Dec 2017 19:49:50 -0200 Subject: [PATCH] [dev-interface] display items metadata --- .../entities/class-tainacan-collection.php | 8 +- .../class-tainacan-collections.php | 5 + .../class-tainacan-repository.php | 9 +- .../class-tainacan-dev-interface.php | 98 +++++++++++++++++++ tests/test-item-metadata.php | 43 ++++++++ 5 files changed, 159 insertions(+), 4 deletions(-) diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index ec7c09640..b93824c67 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -21,6 +21,12 @@ class Collection extends Entity { * @var string */ protected $repository = 'Tainacan_Collections'; + + /** + * Prefix used to create the db_identifier + * @var string + */ + static $db_identifier_prefix = 'tnc_col_'; /** * Create an instance of Collection @@ -196,7 +202,7 @@ class Collection extends Entity { * @return string */ function get_db_identifier() { - return $this->get_id() ? 'tnc_col_' . $this->get_id() : false; + return $this->get_id() ? Collection::$db_identifier_prefix . $this->get_id() : false; } /** diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 78399961e..0e852e1b9 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -197,4 +197,9 @@ class Collections extends Repository { return $this->fetch_output($wp_query, $output); } } + + // TODO: Implement this method + public fetch_by_db_identifier($db_identifier) { + + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 5c2fe7271..a6ab43cd3 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -132,9 +132,12 @@ abstract class Repository { $result = []; if ( $WP_Query->have_posts() ){ - while ( $WP_Query->have_posts() ) { - $WP_Query->the_post(); - $result[] = new $this->entities_type( get_the_ID() ); + /** + * Using WordPress Loop here would cause problems + * @see https://core.trac.wordpress.org/ticket/18408 + */ + foreach ($WP_Query->get_posts() as $p) { + $result[] = new $this->entities_type( $p->ID ); } } diff --git a/src/dev-interface/class-tainacan-dev-interface.php b/src/dev-interface/class-tainacan-dev-interface.php index 9c48cad8c..3c1d998ba 100644 --- a/src/dev-interface/class-tainacan-dev-interface.php +++ b/src/dev-interface/class-tainacan-dev-interface.php @@ -47,6 +47,21 @@ class DevInterface { } + global $Tainacan_Collections; + $collections = $Tainacan_Collections->fetch([], 'OBJECT'); + + foreach ($collections as $col) { + add_meta_box( + $col->get_db_identifier() . '_metadata', + __('Metadata', 'tainacan'), + array(&$this, 'metadata_metabox'), + $col->get_db_identifier(), //post type + 'normal' + + ); + } + + } function properties_metabox_Collections() { @@ -128,6 +143,78 @@ class DevInterface { } + + + function metadata_metabox() { + global $Tainacan_Collections, $Tainacan_Item_Metadata, $pagenow, $typenow, $post; + + $collections = $Tainacan_Collections->fetch([], 'OBJECT'); + + // get current collection + $current_collection = false; + foreach ($collections as $col) { + if ($col->get_db_identifier() == $typenow) { + $current_collection = $col; + break; + } + } + + if (false === $current_collection) + return; + + $entity = new \Tainacan\Entities\Item($post); + + //for new Items + if (!$entity->get_collection_id()) + $entity->set_collection($current_collection); + + $metadata = $Tainacan_Item_Metadata->fetch($entity, 'OBJECT'); + + wp_nonce_field( 'save_metadata_'.$typenow, $typenow.'_metadata_noncename' ); + + ?> + + + +
+ + + + + + + + + + + + + + get_value(); + if (is_array($value)) $value = json_encode($value); + ?> + + + + + + + + + +
+
+ get_metadata()->get_description(); ?> +
+ +
+
+ fetch([], 'OBJECT'); @@ -199,9 +286,20 @@ class DevInterface { } } } + // TODO: display validation errors somehow // TODO: Actually we will replace it saving via ajax using API } + //die; + } else { + + // TODO properly handle Items metadata + + if (isset($_POST['tnc_prop_collection_id'])) { + update_post_meta($post_id, 'collection_id', $_POST['tnc_prop_collection_id']); + } + + } } diff --git a/tests/test-item-metadata.php b/tests/test-item-metadata.php index ded8ae0f0..be0ed99b8 100644 --- a/tests/test-item-metadata.php +++ b/tests/test-item-metadata.php @@ -167,4 +167,47 @@ class Item_Metadata extends \WP_UnitTestCase { $n_item_metadata->set_value($value); $this->assertFalse($n_item_metadata->validate()); } + + function teste_fetch(){ + global $Tainacan_Collections, $Tainacan_Metadatas, $Tainacan_Item_Metadata; + + $collection = new \Tainacan\Entities\Collection(); + $metadata = new \Tainacan\Entities\Metadata(); + $type = new \Tainacan\Field_Types\Text(); + + $collection->set_name('teste'); + $collection->validate(); + $collection = $Tainacan_Collections->insert($collection); + + //setando os valores na classe do metadado + $metadata->set_name('metadado'); + $metadata->set_description('descricao'); + $metadata->set_collection( $collection ); + $metadata->set_status('publish'); + $metadata->set_field_type_object( $type ); + + //inserindo o metadado + $metadata->validate(); + $metadata = $Tainacan_Metadatas->insert($metadata); + + //$test = $Tainacan_Metadatas->fetch($metadata->get_id()); + + $i = new \Tainacan\Entities\Item(); + + $i->set_title('item teste'); + $i->set_description('adasdasdsa'); + $i->set_collection($collection); + + global $Tainacan_Items; + $i->validate(); + $item = $Tainacan_Items->insert($i); + + + $ItemMetadatas = $Tainacan_Item_Metadata->fetch($item, 'OBJECT'); + + $this->assertTrue(is_array($ItemMetadatas)); + $this->assertEquals(1, sizeof($ItemMetadatas)); + $this->assertEquals('metadado', $ItemMetadatas[0]->get_metadata()->get_name()); + + } } \ No newline at end of file