[dev-interface] display items metadata

This commit is contained in:
Leo Germani 2017-12-03 19:49:50 -02:00
parent 764db51e97
commit 3f21743672
5 changed files with 159 additions and 4 deletions

View File

@ -22,6 +22,12 @@ class Collection extends Entity {
*/ */
protected $repository = 'Tainacan_Collections'; protected $repository = 'Tainacan_Collections';
/**
* Prefix used to create the db_identifier
* @var string
*/
static $db_identifier_prefix = 'tnc_col_';
/** /**
* Create an instance of Collection * Create an instance of Collection
* @param integer|\WP_Post optional $which Collection ID or a WP_Post object for existing collections. Leave empty to create a new collection. * @param integer|\WP_Post optional $which Collection ID or a WP_Post object for existing collections. Leave empty to create a new collection.
@ -196,7 +202,7 @@ class Collection extends Entity {
* @return string * @return string
*/ */
function get_db_identifier() { 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;
} }
/** /**

View File

@ -197,4 +197,9 @@ class Collections extends Repository {
return $this->fetch_output($wp_query, $output); return $this->fetch_output($wp_query, $output);
} }
} }
// TODO: Implement this method
public fetch_by_db_identifier($db_identifier) {
}
} }

View File

@ -132,9 +132,12 @@ abstract class Repository {
$result = []; $result = [];
if ( $WP_Query->have_posts() ){ if ( $WP_Query->have_posts() ){
while ( $WP_Query->have_posts() ) { /**
$WP_Query->the_post(); * Using WordPress Loop here would cause problems
$result[] = new $this->entities_type( get_the_ID() ); * @see https://core.trac.wordpress.org/ticket/18408
*/
foreach ($WP_Query->get_posts() as $p) {
$result[] = new $this->entities_type( $p->ID );
} }
} }

View File

@ -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() { 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' );
?>
<input type="hidden" name="tnc_prop_collection_id" value="<?php echo $current_collection->get_id(); ?>" />
<div id="postcustomstuff">
<table>
<thead>
<tr>
<th class="left"><?php _e('Metadata', 'tainacan'); ?></th>
<th><?php _e('Value', 'tainacan'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($metadata as $item_meta): ?>
<?php
$value = $item_meta->get_value();
if (is_array($value)) $value = json_encode($value);
?>
<tr>
<td>
<label><?php echo $item_meta->get_metadata()->get_name(); ?></label><br/>
<small><?php echo $item_meta->get_metadata()->get_description(); ?></small>
</td>
<td>
<textarea name="tnc_metadata_<?php echo $item_meta->get_metadata()->get_id(); ?>"><?php echo htmlspecialchars($value); ?></textarea>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
function collections_dropdown($selected) { function collections_dropdown($selected) {
global $Tainacan_Collections; global $Tainacan_Collections;
$collections = $Tainacan_Collections->fetch([], 'OBJECT'); $collections = $Tainacan_Collections->fetch([], 'OBJECT');
@ -199,9 +286,20 @@ class DevInterface {
} }
} }
} }
// TODO: display validation errors somehow // TODO: display validation errors somehow
// TODO: Actually we will replace it saving via ajax using API // 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']);
}
} }
} }

View File

@ -167,4 +167,47 @@ class Item_Metadata extends \WP_UnitTestCase {
$n_item_metadata->set_value($value); $n_item_metadata->set_value($value);
$this->assertFalse($n_item_metadata->validate()); $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());
}
} }