diff --git a/src/classes/Entities/Collection.php b/src/classes/Entities/Collection.php index deda05aa9..982aaf50c 100644 --- a/src/classes/Entities/Collection.php +++ b/src/classes/Entities/Collection.php @@ -49,6 +49,15 @@ class TainacanCollection extends Entity { return $this->get_mapped_property('itens_per_page'); } + // special Getters + // + + function get_db_identifier() { + global $TainacanCollections; + return $TainacanCollections->get_collection_db_identifier($this->get_id()); + } + + // Setters // diff --git a/src/classes/Entities/Item.php b/src/classes/Entities/Item.php index e69de29bb..e2bd14ffe 100644 --- a/src/classes/Entities/Item.php +++ b/src/classes/Entities/Item.php @@ -0,0 +1,97 @@ +repository = 'TainacanItems'; + + if (is_numeric($which) && $which > 0) { + $post = get_post($which); + if ($post instanceof WP_Post) { + $this->WP_Post = get_post($which); + } + + } elseif ($which instanceof WP_Post) { + $this->WP_Post = $which; + } else { + $this->WP_Post = new StdClass(); + } + + } + + // Getters + // + function get_id() { + return $this->get_mapped_property('ID'); + } + function get_title() { + return $this->get_mapped_property('title'); + } + function get_order() { + return $this->get_mapped_property('order'); + } + function get_parent() { + return $this->get_mapped_property('parent'); + } + function get_description() { + return $this->get_mapped_property('description'); + } + function get_collection_id() { + return $this->get_mapped_property('collection_id'); + } + + // sepecial Getters + function get_collection() { + if (isset($this->collection) && $this->collection instanceof TainacanCollection) + return $this->collection; + + if (is_numeric($this->get_collection_id())) { + $collection = get_post($this->get_collection_id()); + if ($collection instanceof WP_Post) { + $this->collection = new TainacanCollection($collection); + return $this->collection; + } + } + + return null; + + } + + // Setters + // + + function set_title($value) { + return $this->set_mapped_property('title', $value); + } + function set_order($value) { + return $this->set_mapped_property('order', $value); + } + function set_parent($value) { + return $this->set_mapped_property('parent', $value); + } + function set_description($value) { + return $this->set_mapped_property('description', $value); + } + function set_collection_id($value) { + return $this->set_mapped_property('collection_id', $value); + } + + // sepecial Setters + + function set_collection(TainacanCollection $collection) { + $this->collection = $collection; + $this->set_collection_id($collection->get_id()); + } + + + +} \ No newline at end of file diff --git a/src/classes/Entity.php b/src/classes/Entity.php index fa4d53d96..2429c96f2 100644 --- a/src/classes/Entity.php +++ b/src/classes/Entity.php @@ -15,8 +15,6 @@ class Entity { return $this->$prop; - //$map = $this->map_properties(); - if (!array_key_exists($prop, $map)) return null; diff --git a/src/classes/Repositories/Items.php b/src/classes/Repositories/Items.php index b7957cf4e..f777000dc 100644 --- a/src/classes/Repositories/Items.php +++ b/src/classes/Repositories/Items.php @@ -7,6 +7,14 @@ if ( ! defined( 'ABSPATH' ) ) { class TainacanItems { + var $map = [ + 'ID' => 'ID', + 'title' => 'post_title', + 'description' => 'post_content', + 'collection_id' => 'meta', + //'collection' => 'relation...' + ]; + function __construct() { add_action('init', array(&$this, 'register_post_types')); } @@ -65,10 +73,21 @@ class TainacanItems { } - /* + function insert(TainacanItem $item) { - // First iterate through the native post properties - $map = $item->map_properties(); + + + $map = $this->map; + + // get collection to determine post type + $collection = $item->get_collection(); + + if (!$collection) + return false; + + $cpt = $collection->get_db_identifier(); + + // iterate through the native post properties foreach ($map as $prop => $mapped) { if ($mapped != 'meta') { $item->WP_Post->$mapped = $item->get_mapped_property($prop); @@ -76,7 +95,7 @@ class TainacanItems { } // save post and geet its ID - $item->WP_Post->post_type = self::POST_TYPE; + $item->WP_Post->post_type = $cpt; $id = wp_insert_post($item->WP_Post); // Now run through properties stored as postmeta @@ -86,11 +105,16 @@ class TainacanItems { } } + // TODO - save item medatada + // get collection Metadata + // foreach metadata... + + return $id; } - */ + - function getItemById($id) { + function get_item_by_id($id) { return new TainacanItem($id); } diff --git a/src/test-tainacan.php b/src/test-tainacan.php index e035dccce..c8378cbf5 100644 --- a/src/test-tainacan.php +++ b/src/test-tainacan.php @@ -11,6 +11,7 @@ include('classes/Repositories/Collections.php'); include('classes/Repositories/Items.php'); include('classes/Entity.php'); include('classes/Entities/Collection.php'); +include('classes/Entities/Item.php'); /** * diff --git a/tests/test-sample.php b/tests/test-sample.php index 158c7fe66..020369bff 100644 --- a/tests/test-sample.php +++ b/tests/test-sample.php @@ -35,4 +35,37 @@ class TestCollections extends WP_UnitTestCase { } + + function test_item() { + + + $x = new TainacanCollection(); + + $x->set_name('teste'); + $x->set_description('adasdasdsa'); + $x->set_itens_per_page(23); + + global $TainacanCollections; + $cid = $TainacanCollections->insert($x); + + $collection = $TainacanCollections->get_collection_by_id($cid); + + + + $i = new TainacanItem(); + + $i->set_title('item teste'); + $i->set_description('adasdasdsa'); + $i->set_collection($collection); + + global $TainacanItems; + $id = $TainacanItems->insert($i); + + $item = $TainacanItems->get_item_by_id($id); + + $this->assertEquals($item->get_title(), 'item teste'); + $this->assertEquals($item->get_description(), 'adasdasdsa'); + $this->assertEquals($item->get_collection_id(), $cid); + + } }