organizando metodos de relações entre classes

This commit is contained in:
Leo Germani 2017-11-11 20:25:54 -02:00
parent d69aad435b
commit 15f6821293
14 changed files with 279 additions and 216 deletions

View File

@ -7,7 +7,7 @@ if ( ! defined( 'ABSPATH' ) ) {
class TainacanCollection extends Entity {
class TainacanCollection extends Entity {
function __construct($which = 0) {
@ -28,6 +28,51 @@ class TainacanCollection extends Entity {
}
function register_post_type() {
$cpt_labels = array(
'name' => 'Item',
'singular_name' => 'Item',
'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Item',
'edit_item' => 'Editar',
'new_item' => 'Novo Item',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum Item encontrado',
'not_found_in_trash' => 'Nenhum Item encontrado na lixeira',
'parent_item_colon' => 'Item acima:',
'menu_name' => $this->get_name()
);
$cpt_slug = $this->get_db_identifier();
$args = array(
'labels' => $cpt_labels,
'hierarchical' => true,
//'supports' => array('title'),
//'taxonomies' => array(self::TAXONOMY),
'public' => true,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_in_menu' => tnc_enable_dev_wp_interface(),
//'menu_position' => 5,
//'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => [
'slug' => $this->get_slug()
],
'capability_type' => 'post',
);
if (post_type_exists($this->get_db_identifier()))
unregister_post_type($this->get_db_identifier());
register_post_type($cpt_slug, $args);
}
// Getters
//
function get_id() {
@ -36,6 +81,9 @@ class TainacanCollection extends Entity {
function get_name() {
return $this->get_mapped_property('name');
}
function get_slug() {
return $this->get_mapped_property('slug');
}
function get_order() {
return $this->get_mapped_property('order');
}
@ -53,8 +101,7 @@ class TainacanCollection extends Entity {
//
function get_db_identifier() {
global $TainacanCollections;
return $TainacanCollections->get_collection_db_identifier($this->get_id());
return $this->get_id() ? 'tnc_col_' . $this->get_id() : false;
}
@ -64,6 +111,9 @@ class TainacanCollection extends Entity {
function set_name($value) {
return $this->set_mapped_property('name', $value);
}
function set_slug($value) {
return $this->set_mapped_property('slug', $value);
}
function set_order($value) {
return $this->set_mapped_property('order', $value);
}
@ -78,4 +128,19 @@ class TainacanCollection extends Entity {
}
// Relation methods
function get_metadata( $collection ){
global $Tainacan_Metadatas;
return $Tainacan_Metadatas->get_collection_metadata( $collection );
}
}

View File

@ -9,6 +9,7 @@ if ( ! defined( 'ABSPATH' ) ) {
class TainacanItem extends Entity {
use EntityCollectionRelation;
function __construct($which = 0) {
@ -45,26 +46,7 @@ class TainacanItem extends Entity {
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;
}
function get_metadata(){
global $TainacanItems;
@ -91,16 +73,11 @@ class TainacanItem extends Entity {
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());
}
function set_metadata( $metadata ){

View File

@ -5,6 +5,8 @@ if ( ! defined( 'ABSPATH' ) ) {
class Tainacan_Metadata extends Entity {
use EntityCollectionRelation;
function __construct( $which = 0 ) {
$this->repository = 'Tainacan_Metadatas';
@ -76,12 +78,6 @@ class Tainacan_Metadata extends Entity {
}
function get_collection() {
return new TainacanCollection( $this->get_mapped_property('collection') );
}
// Setters
@ -132,12 +128,5 @@ class Tainacan_Metadata extends Entity {
return $this->set_mapped_property('option', serialize($value) ) ;
}
/**
* @param TainacanCollection
*/
function set_collection($value) {
$ID = ($value instanceof TainacanCollection ) ? $value->get_id() : $value;
return $this->set_mapped_property('collection', $ID);
}
}

View File

@ -5,6 +5,8 @@ if ( ! defined( 'ABSPATH' ) ) {
class Tainacan_Taxonomy extends Entity {
use EntityCollectionsRelation;
function __construct( $which = 0 ) {
$this->repository = 'Tainacan_Taxonomies';
@ -23,6 +25,49 @@ class Tainacan_Taxonomy extends Entity {
}
function register_taxonomy() {
$labels = array(
'name' => $this->get_name(),
'singular_name' => __( 'Taxonomy','textdomain' ),
'search_items' => __( 'Search taxonomies', 'textdomain' ),
'all_items' => __( 'All taxonomies', 'textdomain' ),
'parent_item' => __( 'Parent taxonomy', 'textdomain' ),
'parent_item_colon' => __( 'Parent taxonomy:', 'textdomain' ),
'edit_item' => __( 'Edit taxonomy', 'textdomain' ),
'update_item' => __( 'Update taxonomy', 'textdomain' ),
'add_new_item' => __( 'Add New taxonomy', 'textdomain' ),
'new_item_name' => __( 'New Genre taxonomy', 'textdomain' ),
'menu_name' => __( 'Genre', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_admin_column' => tnc_enable_dev_wp_interface(),
'rewrite' => [
'slug' => $this->get_slug()
],
);
$tax_cpts = [];
if (is_array($this->get_collections()))
foreach ($this->get_collections() as $tax_col)
$tax_cpts[] = $tax_col->get_db_identifier();
if (taxonomy_exists($this->get_db_identifier()))
unregister_taxonomy($this->get_db_identifier());
register_taxonomy(
$this->get_db_identifier(),
$tax_cpts,
$args
);
return true;
}
// Getters
function get_id() {
@ -47,17 +92,18 @@ class Tainacan_Taxonomy extends Entity {
return ( boolean ) $this->get_mapped_property('allow_insert');
}
function get_taxonomy_name() {
return $this->get_mapped_property('taxonomy_name');
function get_slug() {
return $this->get_mapped_property('slug');
}
// special Getters
//
// TODO: Uma taxonomia pode estar vinculada a mais de uma coleção
function get_collection() {
return new TainacanCollection( $this->get_mapped_property('collection') );
function get_db_identifier() {
return $this->get_id() ? 'tnc_tax_' . $this->get_id() : false;
}
// Setters
@ -69,6 +115,10 @@ class Tainacan_Taxonomy extends Entity {
return $this->set_mapped_property('parent', $value);
}
function set_slug($value) {
return $this->set_mapped_property('slug', $value);
}
function set_description($value) {
return $this->set_mapped_property('description', $value);
@ -78,13 +128,4 @@ class Tainacan_Taxonomy extends Entity {
return $this->set_mapped_property('allow_insert', $value);
}
/**
* @param TainacanCollection
*
* TODO: Uma taxonomia pode estar vinculada a mais de uma coleção
*/
function set_collection($value) {
$ID = ($value instanceof TainacanCollection ) ? $value->get_id() : $value;
return $this->set_mapped_property('collection', $ID);
}
}

View File

@ -22,6 +22,8 @@ class Entity {
if ( $mapped == 'meta') {
return get_post_meta($this->WP_Post->ID, $prop, true);
}elseif ( $mapped == 'meta_multi') {
return get_post_meta($this->WP_Post->ID, $prop, false);
}elseif ( $mapped == 'termmeta' ){
return get_term_meta($this->WP_Term->term_id, $prop, true);
}elseif ( isset( $this->WP_Post )) {
@ -40,5 +42,16 @@ class Entity {
}
function save() {
// validate
global ${$this->repository};
return ${$this->repository}->insert($this);
}
}

View File

@ -8,7 +8,6 @@ if ( ! defined( 'ABSPATH' ) ) {
class TainacanCollections {
const POST_TYPE = 'tainacan-collections';
const DB_IDENTIFIER_META = '_db_identifier';
var $map = [
'ID' => 'ID',
@ -16,6 +15,7 @@ class TainacanCollections {
'order' => 'menu_order',
'parent' => 'parent',
'description' => 'post_content',
'slug' => 'post_name',
'itens_per_page' => 'meta'
];
@ -71,24 +71,27 @@ class TainacanCollections {
// save post and geet its ID
$collection->WP_Post->post_type = self::POST_TYPE;
$collection->WP_Post->post_status = 'publish';
// TODO verificar se salvou mesmo
$id = wp_insert_post($collection->WP_Post);
// reset object
$collection->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
update_post_meta($id, $prop, $collection->get_mapped_property($prop));
} elseif ($mapped == 'meta_multi') {
$values = $collection->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values))
foreach ($values as $value)
add_post_meta($id, $prop, $value);
}
}
// DB Identifier
// the first time a collection is saved, save the slug as a db Identifier,
// This will be the slug of the post type that will be created for this collection
// Later, if the slug is changed (and thus the URL of this collection) the db Identifier
// does not change and we dont lose all the items
if (!get_post_meta($id, self::DB_IDENTIFIER_META, true)) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
}
$collection->register_post_type();
return $id;
}
@ -114,26 +117,10 @@ class TainacanCollections {
return $return;
}
function get_collection_db_identifier($id) {
$meta = get_post_meta($id, self::DB_IDENTIFIER_META, true);
if (!$meta) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
return $p->post_name;
}
return $meta;
}
function get_collection_by_id($id) {
return new TainacanCollection($id);
}
function get_metadata( $collection ){
global $Tainacan_Metadatas;
return $Tainacan_Metadatas->get_collection_metadata( $collection );
}
}

View File

@ -26,21 +26,6 @@ class TainacanItems {
$collections = $TainacanCollections->get_collections();
$taxonomies = $Tainacan_Taxonomies->get_taxonomies();
$cpt_labels = array(
'name' => 'Item',
'singular_name' => 'Item',
'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Item',
'edit_item' => 'Editar',
'new_item' => 'Novo Item',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum Item encontrado',
'not_found_in_trash' => 'Nenhum Item encontrado na lixeira',
'parent_item_colon' => 'Item acima:',
'menu_name' => 'Item'
);
if (!is_array($collections))
return;
@ -51,59 +36,14 @@ class TainacanItems {
// register collections post type and associate taxonomies
foreach ($collections as $collection) {
$cpt_labels['menu_name'] = $collection->get_name();
$cpt_slug = $TainacanCollections->get_collection_db_identifier($collection->get_id());
$collection->register_post_type();
$args = array(
'labels' => $labels,
'hierarchical' => true,
//'supports' => array('title'),
//'taxonomies' => array(self::TAXONOMY),
'public' => true,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_in_menu' => tnc_enable_dev_wp_interface(),
//'menu_position' => 5,
//'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
);
register_post_type($cpt_slug, $args);
}
// register taxonomies
foreach ($taxonomies as $taxonomy) {
$labels = array(
'name' => __( 'Taxonomies', 'textdomain' ),
'singular_name' => __( 'Taxonomy','textdomain' ),
'search_items' => __( 'Search taxonomies', 'textdomain' ),
'all_items' => __( 'All taxonomies', 'textdomain' ),
'parent_item' => __( 'Parent taxonomy', 'textdomain' ),
'parent_item_colon' => __( 'Parent taxonomy:', 'textdomain' ),
'edit_item' => __( 'Edit taxonomy', 'textdomain' ),
'update_item' => __( 'Update taxonomy', 'textdomain' ),
'add_new_item' => __( 'Add New taxonomy', 'textdomain' ),
'new_item_name' => __( 'New Genre taxonomy', 'textdomain' ),
'menu_name' => __( 'Genre', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_admin_column' => tnc_enable_dev_wp_interface(),
);
// TODO Uma taxonomia pode estar vinculada a mais de uma coleção
register_taxonomy(
$Tainacan_Taxonomies->get_taxonomy_db_identifier($taxonomy),
array( $TainacanCollections->get_collection_db_identifier($taxonomy->get_collection()->get_id()) ), $args
);
$taxonomy->register_taxonomy();
}
@ -134,11 +74,18 @@ class TainacanItems {
// save post and geet its ID
$item->WP_Post->post_type = $cpt;
$id = wp_insert_post($item->WP_Post);
$item->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
update_post_meta($id, $prop, $item->get_mapped_property($prop));
} elseif ($mapped == 'meta_multi') {
$values = $item->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values))
foreach ($values as $value)
add_post_meta($id, $prop, $value);
}
}

View File

@ -9,7 +9,6 @@ if ( ! defined( 'ABSPATH' ) ) {
class Tainacan_Metadatas {
const POST_TYPE = 'tainacan-metadata';
const DB_IDENTIFIER_META = '_db_identifier';
var $map = [
'ID' => 'ID',
@ -24,7 +23,7 @@ class Tainacan_Metadatas {
'mask' => 'meta',
'default_value' => 'meta',
'option' => 'meta',
'collection' => 'meta',
'collection_id' => 'meta',
];
function __construct() {
@ -85,24 +84,21 @@ class Tainacan_Metadatas {
$metadata->WP_Post->post_type = self::POST_TYPE;
$metadata->WP_Post->post_status = 'publish';
$id = wp_insert_post($metadata->WP_Post);
$metadata->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
update_post_meta($id, $prop, $metadata->get_mapped_property($prop));
} elseif ($mapped == 'meta_multi') {
$values = $metadata->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values))
foreach ($values as $value)
add_post_meta($id, $prop, $value);
}
}
// DB Identifier
// the first time a collection is saved, save the slug as a db Identifier,
// This will be the slug of the post type that will be created for this collection
// Later, if the slug is changed (and thus the URL of this collection) the db Identifier
// does not change and we dont lose all the items
if (!get_post_meta($id, self::DB_IDENTIFIER_META, true)) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
}
return $id;
}
@ -111,7 +107,7 @@ class Tainacan_Metadatas {
* @param array $args
* @return array
*/
function get_collection_metadata( $collection, $args = array()) {
function get_metadata_by_collection( $collection, $args = array()) {
$collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection;
$args = array_merge([
@ -133,17 +129,7 @@ class Tainacan_Metadatas {
return $return;
}
function get_metadata_db_identifier($id) {
$meta = get_post_meta($id, self::DB_IDENTIFIER_META, true);
if (!$meta) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
return $p->post_name;
}
return $meta;
}
/**
* @param int $id
* @return Tainacan_Metadata

View File

@ -9,16 +9,15 @@ if ( ! defined( 'ABSPATH' ) ) {
class Tainacan_Taxonomies {
const POST_TYPE = 'tainacan-taxonomies';
const DB_IDENTIFIER_META = '_db_identifier';
var $map = [
'ID' => 'ID',
'name' => 'post_title',
'parent' => 'parent',
'description' => 'post_content',
'taxonomy_name' => 'post_name',
'slug' => 'post_name',
'allow_insert' => 'meta',
'collection' => 'meta',
'collections_ids' => 'meta_multi',
];
@ -52,9 +51,9 @@ class Tainacan_Taxonomies {
'show_in_menu' => tnc_enable_dev_wp_interface(),
//'menu_position' => 5,
//'show_in_nav_menus' => false,
'publicly_queryable' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'has_archive' => true,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
@ -83,30 +82,6 @@ class Tainacan_Taxonomies {
return $return;
}
function get_taxonomy_db_identifier($id) {
$meta = get_post_meta($id, self::DB_IDENTIFIER_META, true);
if (!$meta) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
return $p->post_name;
}
return $meta;
}
function get_metadata_db_identifier($id) {
$meta = get_post_meta($id, self::DB_IDENTIFIER_META, true);
if (!$meta) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
return $p->post_name;
}
return $meta;
}
/**
* @param Tainacan_Taxonomy $metadata
@ -125,24 +100,22 @@ class Tainacan_Taxonomies {
$taxonomy->WP_Post->post_type = self::POST_TYPE;
$taxonomy->WP_Post->post_status = 'publish';
$id = wp_insert_post($taxonomy->WP_Post);
$taxonomy->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
update_post_meta($id, $prop, $taxonomy->get_mapped_property($prop));
} elseif ($mapped == 'meta_multi') {
$values = $taxonomy->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values))
foreach ($values as $value)
add_post_meta($id, $prop, $value);
}
}
// DB Identifier
// the first time a collection is saved, save the slug as a db Identifier,
// This will be the slug of the post type that will be created for this collection
// Later, if the slug is changed (and thus the URL of this collection) the db Identifier
// does not change and we dont lose all the items
if (!get_post_meta($id, self::DB_IDENTIFIER_META, true)) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
$this->registerTainacanTaxonomy( $p->post_name );
}
$taxonomy->register_taxonomy();
return $id;
}

View File

@ -0,0 +1,36 @@
<?php
// used by Item, Event, Field
trait EntityCollectionRelation {
function get_collection_id() {
return $this->get_mapped_property('collection_id');
}
function get_collection() {
if (isset($this->collection) && $this->collection instanceof TainacanCollection)
return $this->collection;
if (is_numeric($this->get_collection_id())) {
global $TainacanCollections;
$this->collection = $TainacanCollections->get_collection_by_id($this->get_collection_id());
return $this->collection;
}
return null;
}
function set_collection_id($value) {
return $this->set_mapped_property('collection_id', $value);
$this->collection = null;
}
function set_collection(TainacanCollection $collection) {
$this->collection = $collection;
$this->set_collection_id($collection->get_id());
}
}

View File

@ -0,0 +1,46 @@
<?php
// used by Taxonomy
trait EntityCollectionsRelation {
function get_collections_ids() {
return $this->get_mapped_property('collections_ids');
}
function get_collections() {
if (isset($this->collection) && !empty($this->collection) && is_array($this->collection))
return $this->collection;
if (is_array($this->get_collections_ids()) && !empty(is_array($this->get_collections_ids()))) {
global $TainacanCollections;
$collections = [];
foreach ($this->get_collections_ids() as $col_id) {
$collections[] = $TainacanCollections->get_collection_by_id($col_id);
}
return $collections;
}
return null;
}
function set_collections_ids(Array $value) {
return $this->set_mapped_property('collection_id', $value);
$this->collections = null;
}
function set_collections(Array $collections) {
$collections_ids = [];
$this->collections = $collections;
foreach ($collections as $collection)
$collections_ids[] = $collection->get_id();
$this->set_collections_ids($collections_ids);
}
}

View File

@ -7,6 +7,8 @@ Author: MediaLab UFG
Version: 10.9.8.7.6.5.4
*/
include('classes/Traits/EntityCollectionRelation.php');
include('classes/Traits/EntityCollectionsRelation.php');
include('classes/Repositories/Collections.php');
include('classes/Repositories/Items.php');
include('classes/Repositories/Metatadas.php');
@ -21,6 +23,7 @@ include('classes/Entities/Term.php');
include('classes/FieldTypes/FieldType.php');
include('classes/FieldTypes/TextFieldType.php');
/**
*
*

View File

@ -25,7 +25,7 @@ class TestMetadata extends WP_UnitTestCase {
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( new TainacanCollection( $id ) );
$metadata->set_collection_id( $id );
//inserindo o metadado
$metadata_id = $Tainacan_Metadatas->insert($metadata);
@ -53,7 +53,7 @@ class TestMetadata extends WP_UnitTestCase {
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_collection( new TainacanCollection( $id ) );
$metadata->set_collection_id( $id );
$metadata->set_type( $type );
//inserindo o metadado

View File

@ -33,7 +33,7 @@ class TestTaxonomies extends WP_UnitTestCase {
$this->assertEquals( $test->get_name(), 'genero' );
$this->assertEquals( $test->get_description(), 'tipos de musica' );
$this->assertEquals( $test->get_allow_insert(), true );
$this->assertEquals( taxonomy_exists( $test->get_taxonomy_name() ) , true );
$this->assertEquals( taxonomy_exists( $test->get_db_identifier() ) , true );
}
function test_add_term_taxonomy(){
@ -51,13 +51,13 @@ class TestTaxonomies extends WP_UnitTestCase {
$taxonomy_test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy_id);
//insere um termo na taxonomia
$term->set_taxonomy( $taxonomy_test->get_taxonomy_name() );
$term->set_taxonomy( $taxonomy_test->get_db_identifier() );
$term->set_name('Rock');
$term->set_user(56);
$term_id = $Tainacan_Terms->insert( $term ) ;
//retorna um objeto da classe Tainacan_Term
$test = $Tainacan_Terms->get_term_by('id', $term_id, $taxonomy_test->get_taxonomy_name());
$test = $Tainacan_Terms->get_term_by('id', $term_id, $taxonomy_test->get_db_identifier());
$this->assertEquals( $test->get_name(), 'Rock' );
$this->assertEquals( $test->get_user(), 56 );