Merge branch 'master' of github.com:medialab-ufg/tainacan-test-repo

This commit is contained in:
Leo Germani 2017-11-28 18:49:29 -02:00
commit 9b02ecc101
8 changed files with 97 additions and 51 deletions

View File

@ -53,8 +53,8 @@ class Collection extends Entity {
* This method register the post type for a collection, so that items can be created.
*
* @return void
*
function tainacan_register_post_type() {
*/
function register_collection_item_post_type() {
$cpt_labels = array(
'name' => 'Item',
'singular_name' => 'Item',
@ -97,7 +97,7 @@ class Collection extends Entity {
unregister_post_type($this->get_db_identifier());
register_post_type($cpt_slug, $args);
}*/
}
/**
* Get the collection ID

View File

@ -216,14 +216,6 @@ class Log extends Entity {
{
$type = gettype($new_value);
if($msn === false)
{
if(is_object($new_value))
{
$type = get_class($new_value);
}
$msn = sprintf( esc_html__( 'a %s has been created/modified.', 'tainacan' ), $type );
}
$log->set_value($new_value);
if(!is_null($old_value)) $log->set_old_value($old_value);
}

View File

@ -6,6 +6,7 @@ use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
use Tainacan\Entities\Collection;
class Collections extends Repository {
protected $entities_type = '\Tainacan\Entities\Collection';
@ -135,6 +136,17 @@ class Collections extends Repository {
register_post_type(Entities\Collection::get_post_type(), $args);
}
/**
* @param Tainacan\Entities\Collection $collection
* {@inheritDoc}
* @see \Tainacan\Repositories\Repository::insert()
*/
public function insert($collection){
$new_collection = parent::insert($collection);
$collection->register_collection_item_post_type();
return $new_collection;
}
public function update($object){
}

View File

@ -23,7 +23,8 @@ class Item_Metadata extends Repository {
}
}
Entities\Log::create($this->log_message, $this->log_description, $item_metadata);
do_action('tainacan-insert', $item_metadata);
do_action('tainacan-insert-Item_Metadata_Entity', $item_metadata);
// return a brand new object
return new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_metadata());

View File

@ -53,17 +53,61 @@ class Items extends Repository {
}
public function insert($item) {
$new_item = parent::insert($item);
// save metadata
$metadata = $item->get_metadata();
global $Tainacan_Item_Metadata;
foreach ($metadata as $meta) {
$Tainacan_Item_Metadata->insert($meta);
}
return $new_item;
$map = $this->get_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['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property($prop);
}
}
// save post and geet its ID
$item->WP_Post->post_type = $cpt;
$item->WP_Post->post_status = 'publish';
$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['map'] == 'meta') {
update_post_meta($id, $prop, $item->get_mapped_property($prop));
} elseif ($mapped['map'] == '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);
}
}
}
}
// save metadata
$metadata = $item->get_metadata();
global $Tainacan_Item_Metadata;
foreach ($metadata as $meta) {
$Tainacan_Item_Metadata->insert($meta);
}
do_action('tainacan-insert', $item);
do_action('tainacan-insert-Item', $item);
// return a brand new object
return new Entities\Item($item->WP_Post);
}
public function fetch($args = [], $object = []){

View File

@ -13,6 +13,11 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class Logs extends Repository {
protected $entities_type = '\Tainacan\Entities\Log';
public function __construct() {
parent::__construct();
add_action('tainacan-insert', array($this, 'log_inserts'));
}
public function get_map() {
return [
'id' => [
@ -143,4 +148,19 @@ class Logs extends Repository {
// TODO: Pegar coleções registradas via código
return $log;
}
public function log_inserts($new_value, $value = null)
{
$msn = "";
if(is_object($new_value))
{
// do not log a log
if(method_exists($new_value, 'get_post_type') && $new_value->get_post_type() == 'tainacan-logs') return;
$type = get_class($new_value);
$msn = sprintf( esc_html__( 'a %s has been created/modified.', 'tainacan' ), $type );
}
$msn = apply_filters('tainacan-insert-log-message-title', $msn, $type, $new_value);
Entities\Log::create($msn, '', $new_value, $value);
}
}

View File

@ -9,17 +9,6 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
abstract class Repository {
protected $entities_type = '\Tainacan\Entities\Entity';
/**
*
* @var string Text to put on log Title, false to use default
*/
protected $log_message = false;
/**
*
* @var string Text to put on log Description, false for no description
*/
protected $log_description = false;
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
@ -49,20 +38,7 @@ abstract class Repository {
$obj->WP_Post->{$mapped['map']} = $obj->get_mapped_property($prop);
}
}
// not have a post_type get its collection relation, else get post type from entity
if ( $obj->get_post_type() === false ) {
$collection = $obj->get_collection();
if (!$collection){
return false;
}
$cpt = $collection->get_db_identifier();
$obj->WP_Post->post_type = $cpt;
}
else {
$obj->WP_Post->post_type = $obj::get_post_type();
}
$obj->WP_Post->post_type = $obj::get_post_type();
$obj->WP_Post->post_status = 'publish';
// TODO verificar se salvou mesmo
@ -88,8 +64,8 @@ abstract class Repository {
}
}
//not log the log
if($this->entities_type != '\Tainacan\Entities\Log') Entities\Log::create($this->log_message, $this->log_description, $obj);
do_action('tainacan-insert', $obj);
do_action('tainacan-insert-'.$obj->get_post_type(), $obj);
// return a brand new object
return new $this->entities_type($obj->WP_Post);

View File

@ -62,7 +62,8 @@ class Terms extends Repository {
}
}
Entities\Log::create($this->log_message, $this->log_description, $term);
do_action('tainacan-insert', $term);
do_action('tainacan-insert-Term', $term);
return $term_inserted['term_id'];
}