adicionando tipos de campos - Tainacan

This commit is contained in:
Eduardo humberto 2017-11-14 08:17:28 -02:00
parent ddc207ab8d
commit 2a25057ecd
14 changed files with 331 additions and 18 deletions

View File

@ -10,6 +10,7 @@ Version: 10.9.8.7.6.5.4
const ENTITIES_DIR = __DIR__ . '/classes/entities/';
const FIELD_TYPES_DIR = __DIR__ . '/classes/field-types/';
const FILTER_TYPES_DIR = __DIR__ . '/classes/filter-types/';
const REPOSITORIES_DIR = __DIR__ . '/classes/repositories/';
const TRAITS_DIR = __DIR__ . '/classes/traits/';
const CLASSES_DIR = __DIR__ . '/classes/';
@ -18,6 +19,7 @@ const DIRS = [
CLASSES_DIR,
ENTITIES_DIR,
FIELD_TYPES_DIR,
FILTER_TYPES_DIR,
REPOSITORIES_DIR,
TRAITS_DIR,
];
@ -44,6 +46,9 @@ $Tainacan_Item_Metadata = new Tainacan_Item_Metadata();
global $Tainacan_Metadatas;
$Tainacan_Metadatas = new Tainacan_Metadatas();
global $Tainacan_Filters;
$Tainacan_Filters = new Tainacan_Filters();
global $Tainacan_Taxonomies;
$Tainacan_Taxonomies = new Tainacan_Taxonomies();

View File

@ -38,6 +38,11 @@ class Tainacan_Filter extends Entity {
return $this->get_mapped_property('order');
}
function get_color() {
return $this->get_mapped_property('color');
}
function get_widget( $output = 'object' ){
if( $output === 'object'){
return unserialize( $this->get_mapped_property('option') );
@ -47,6 +52,22 @@ class Tainacan_Filter extends Entity {
}
// Setters
function set_name($value) {
return $this->set_mapped_property('name', $value);
}
function set_order($value) {
return $this->set_mapped_property('order', $value);
}
function set_description($value) {
return $this->set_mapped_property('description', $value);
}
function set_color($value) {
return $this->set_mapped_property('parent', $value);
}
function set_widget($value){
if( is_object( $value ) && is_subclass_of( $value, 'Tainacan_Filter_Type' ) ){
$this->set_option( $value );

View File

@ -0,0 +1,23 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
class Tainacan_Checkbox_Field_Type extends Tainacan_Field_Type {
function __construct(){
$this->primitive_type = 'date';
}
/**
* @param $metadata
* @return string
*/
function render( $metadata ){
return '<tainacan-checkbox name="'.$metadata->get_name().'"></tainacan-checkbox>';
}
}

View File

@ -0,0 +1,23 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
class Tainacan_Textarea_Field_Type extends Tainacan_Field_Type {
function __construct(){
$this->primitive_type = 'date';
}
/**
* @param $metadata
* @return string
*/
function render( $metadata ){
return '<tainacan-date name="'.$metadata->get_name().'"></tainacan-date>';
}
}

View File

@ -8,6 +8,8 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
abstract class Tainacan_Field_Type {
var $primitive_type;
abstract function render( $metadata );
function validate($value) {

View File

@ -0,0 +1,23 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
class Tainacan_Numeric_Field_Type extends Tainacan_Field_Type {
function __construct(){
$this->primitive_type = 'float';
}
/**
* @param $metadata
* @return string
*/
function render( $metadata ){
return '<tainacan-numeric name="'.$metadata->get_name().'"></tainacan-numeric>';
}
}

View File

@ -0,0 +1,23 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
class Tainacan_Radio_Field_Type extends Tainacan_Field_Type {
function __construct(){
$this->primitive_type = '';
}
/**
* @param $metadata
* @return string
*/
function render( $metadata ){
return '<tainacan-radio name="'.$metadata->get_name().'"></tainacan-radio>';
}
}

View File

@ -0,0 +1,23 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
class Tainacan_Relationship_Field_Type extends Tainacan_Field_Type {
function __construct(){
$this->primitive_type = '';
}
/**
* @param $metadata
* @return string
*/
function render( $metadata ){
return '<tainacan-relationship name="'.$metadata->get_name().'"></tainacan-relationship>';
}
}

View File

@ -0,0 +1,23 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
class Tainacan_Selectbox_Field_Type extends Tainacan_Field_Type {
function __construct(){
$this->primitive_type = '';
}
/**
* @param $metadata
* @return string
*/
function render( $metadata ){
return '<tainacan-selectbox name="'.$metadata->get_name().'"></tainacan-selectbox>';
}
}

View File

@ -8,6 +8,10 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class Tainacan_Text_Field_Type extends Tainacan_Field_Type {
function __construct(){
$this->primitive_type = 'string';
}
/**
* @param $metadata
* @return string

View File

@ -0,0 +1,23 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
class Tainacan_Textarea_Field_Type extends Tainacan_Field_Type {
function __construct(){
$this->primitive_type = 'string';
}
/**
* @param $metadata
* @return string
*/
function render( $metadata ){
return '<tainacan-textarea name="'.$metadata->get_name().'"></tainacan-textarea>';
}
}

View File

@ -5,13 +5,7 @@ if ( ! defined( 'ABSPATH' ) ) {
abstract class Tainacan_Filter_Type extends Entity {
var $supported_types = [];
abstract function render( $metadata );
function validate( $value ) {
return true;
}
function get_validation_errors() {
return [];
}
}

View File

@ -28,10 +28,6 @@ class Tainacan_Filters {
'map' => 'meta',
'validation' => ''
],
'mask' => [
'map' => 'meta',
'validation' => ''
],
'option' => [
'map' => 'meta',
'validation' => ''
@ -40,14 +36,17 @@ class Tainacan_Filters {
'map' => 'meta',
'validation' => ''
],
'color' => [
'map' => 'meta',
'validation' => ''
],
];
function __construct(){
add_action('init', array(&$this, 'register_post_type'));
}
function register_post_type()
{
function register_post_type(){
$labels = array(
'name' => 'Filter',
'singular_name' => 'Filter',
@ -82,4 +81,74 @@ class Tainacan_Filters {
);
register_post_type(self::POST_TYPE, $args);
}
/**
* @param Tainacan_Metadata $metadata
* @return int
*/
function insert( Tainacan_Filter $metadata ) {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop);
}
}
// save post and get its ID
$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['map'] == 'meta') {
update_post_meta($id, $prop, $metadata->get_mapped_property($prop));
} elseif ($mapped['map'] == '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);
}
}
// return a brand new object
return new Tainacan_Filter($metadata->WP_Post);
}
/**
* @param ( Tainacan_Collection ) $collection_id
* @param array $args
* @return WP_Query
*/
function get_filter_by_collection( $collection, $args = array()) {
// TODO: get filters from parent collections
$collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection;
$args = array_merge([
'post_type' => self::POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'collection_id',
'meta_value' => $collection_id
], $args);
$wp_query = new WP_Query($args);
return $wp_query;
}
/**
* @param int $id
* @return Tainacan_Filter
*/
function get_filter_by_id($id) {
return new Tainacan_Filter($id);
}
}

57
tests/test-filters.php Normal file
View File

@ -0,0 +1,57 @@
<?php
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class Test_Filters extends WP_UnitTestCase {
function teste_add(){
global $Tainacan_Collections, $Tainacan_Filters;
$collection = new Tainacan_Collection();
$filter = new Tainacan_Filter();
$collection->set_name('teste');
$collection = $Tainacan_Collections->insert($collection);
//setando os valores na classe do metadado
$filter->set_name('metadado');
$filter->set_description('descricao');
$filter->set_collection( $collection );
//inserindo o metadado
$metadata = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id());
$i = new Tainacan_Item();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
global $Tainacan_Items;
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->get_item_by_id($item->get_id());
$value = 'teste_val';
$item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata);
$item_metadata->set_value($value);
$this->assertTrue($item_metadata->validate());
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
$n_item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata);
$n_item_metadata->set_value($value);
$this->assertFalse($n_item_metadata->validate());
}
}