logging repository inserts using super class
This commit is contained in:
parent
1b0fe58702
commit
b94024b194
|
@ -95,6 +95,26 @@ class Log extends Entity {
|
|||
return $this->get_mapped_property('user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* get value of log entry
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function get_value($value = null) {
|
||||
return maybe_unserialize( base64_decode($this->get_mapped_property('value')));
|
||||
}
|
||||
|
||||
/**
|
||||
* get old value of log entry object
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function get_old_value($value = null) {
|
||||
return maybe_unserialize( base64_decode($this->get_mapped_property('old_value')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set log tittle
|
||||
*
|
||||
|
@ -156,4 +176,63 @@ class Log extends Entity {
|
|||
if(0 == $value) $value = get_current_blog_id();
|
||||
$this->set_mapped_property('blog_id', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* value of log entry
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
protected function set_value($value = null) {
|
||||
$this->set_mapped_property('value', base64_encode( maybe_serialize($value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* set old value of log entry
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
protected function set_old_value($value = null) {
|
||||
$this->set_mapped_property('old_value', base64_encode( maybe_serialize($value)));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param boolean|string $msn
|
||||
* @param string $desc
|
||||
* @param mixed $new_value
|
||||
* @param mixed $old_value
|
||||
* @throws \Exception
|
||||
* @return \Tainacan\Entities\Log
|
||||
*/
|
||||
public static function create($msn = false, $desc = '', $new_value = null, $old_value = null)
|
||||
{
|
||||
$log = new Log();
|
||||
$log->set_title($msn);
|
||||
$log->set_description($desc);
|
||||
|
||||
if(!is_null($new_value))
|
||||
{
|
||||
$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);
|
||||
}
|
||||
elseif($msn === false)
|
||||
{
|
||||
throw new \Exception('msn or new_value is need to log');
|
||||
}
|
||||
global $Tainacan_Logs;
|
||||
return $Tainacan_Logs->insert($log);
|
||||
|
||||
}
|
||||
}
|
|
@ -51,6 +51,14 @@ class Logs extends Repository {
|
|||
'map' => 'meta',
|
||||
'validation' => ''
|
||||
],
|
||||
'value' => [
|
||||
'map' => 'meta',
|
||||
'validation' => ''
|
||||
],
|
||||
'old_value' => [
|
||||
'map' => 'meta',
|
||||
'validation' => ''
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -90,48 +98,6 @@ class Logs extends Repository {
|
|||
register_post_type(Entities\Log::get_post_type(), $args);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* {@inheritDoc}
|
||||
* @see \Tainacan\Repositories\Repository::insert()
|
||||
*
|
||||
* @param \Tainacan\Entities\Log $log
|
||||
*
|
||||
*
|
||||
public function insert($log) {
|
||||
// First iterate through the native post properties
|
||||
$map = $this->get_map();
|
||||
foreach ($map as $prop => $mapped) {
|
||||
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
|
||||
$log->WP_Post->{$mapped['map']} = $log->get_mapped_property($prop);
|
||||
}
|
||||
}
|
||||
|
||||
// save post and geet its ID
|
||||
$log->WP_Post->post_type = Entities\Log::POST_TYPE;
|
||||
$log->WP_Post->post_status = 'publish';
|
||||
|
||||
// TODO verificar se salvou mesmo
|
||||
$id = wp_insert_post($log->WP_Post);
|
||||
$log->WP_Post = get_post($id);
|
||||
|
||||
/* Now run through properties stored as postmeta TODO maybe a parent class function leave for future use /
|
||||
foreach ($map as $prop => $mapped) {
|
||||
if ($mapped['map'] == 'meta') {
|
||||
update_post_meta($id, $prop, $log->get_mapped_property($prop));
|
||||
} elseif ($mapped['map'] == 'meta_multi') {
|
||||
$values = $log->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 Entities\Log($log->WP_Post);
|
||||
}*/
|
||||
|
||||
public function fetch($object = []){
|
||||
if(is_numeric($object)){
|
||||
return new Entities\Log($object);
|
||||
|
@ -161,4 +127,20 @@ class Logs extends Repository {
|
|||
public function update($object){
|
||||
|
||||
}
|
||||
|
||||
public function fetch_last() {
|
||||
$args = [
|
||||
'post_type' => Entities\Log::get_post_type(),
|
||||
'posts_per_page' => 1,
|
||||
'post_status' => 'publish',
|
||||
];
|
||||
|
||||
$posts = get_posts($args);
|
||||
|
||||
foreach ($posts as $post) {
|
||||
$log = new Entities\Log($post);
|
||||
}
|
||||
// TODO: Pegar coleções registradas via código
|
||||
return $log;
|
||||
}
|
||||
}
|
|
@ -1,11 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Repositories;
|
||||
use Tainacan\Entities;
|
||||
use Tainacan\Entities\Entity;
|
||||
use Tainacan;
|
||||
|
||||
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'));
|
||||
|
@ -74,6 +88,9 @@ abstract class Repository {
|
|||
}
|
||||
}
|
||||
|
||||
//not log the log
|
||||
if($this->entities_type != '\Tainacan\Entities\Log') Entities\Log::create($this->log_message, $this->log_description, $obj);
|
||||
|
||||
// return a brand new object
|
||||
return new $this->entities_type($obj->WP_Post);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Tests;
|
||||
use Tainacan\Entities\Log;
|
||||
use Tainacan\Entities\Collection;
|
||||
|
||||
/**
|
||||
* Class TestCollections
|
||||
|
@ -20,7 +22,7 @@ class Logs extends \WP_UnitTestCase {
|
|||
function test_add() {
|
||||
global $Tainacan_Logs;
|
||||
|
||||
$log = new \Tainacan\Entities\Log();
|
||||
$log = new Log();
|
||||
|
||||
//setando os valores na classe do tainacan
|
||||
$log->set_title('blame someone');
|
||||
|
@ -39,5 +41,37 @@ class Logs extends \WP_UnitTestCase {
|
|||
$this->assertEquals( 'someone did that', $test->get_description() );
|
||||
$this->assertEquals( $user_id, $test->get_user_id() );
|
||||
$this->assertEquals( $blog_id, $test->get_blog_id() );
|
||||
|
||||
$value = new Collection();
|
||||
$value->set_name('testeLogs');
|
||||
$value->set_description('adasdasdsa123');
|
||||
$value->set_itens_per_page(23);
|
||||
|
||||
global $Tainacan_Collections;
|
||||
$value = $Tainacan_Collections->insert($value);
|
||||
|
||||
$value->set_name('new_testeLogs');
|
||||
$new_value = $Tainacan_Collections->insert($value);
|
||||
|
||||
$create_log = Log::create('teste create', 'testing a log creation function', $new_value, $value);
|
||||
|
||||
$this->assertEquals( 'teste create', $create_log->get_title() );
|
||||
$this->assertEquals( 'testing a log creation function', $create_log->get_description() );
|
||||
$this->assertEquals( $new_value, $create_log->get_value() );
|
||||
$this->assertEquals( $value, $create_log->get_old_value() );
|
||||
|
||||
$testDB = $Tainacan_Logs->fetch($create_log->get_id());
|
||||
|
||||
$this->assertEquals( 'teste create', $testDB->get_title() );
|
||||
$this->assertEquals( 'testing a log creation function', $testDB->get_description() );
|
||||
$this->assertEquals( $new_value, $testDB->get_value() );
|
||||
$this->assertEquals( $value, $testDB->get_old_value() );
|
||||
|
||||
$last_log = $Tainacan_Logs->fetch_last();
|
||||
$collection = $last_log->get_value();
|
||||
|
||||
$this->assertEquals($collection->get_name(), 'new_testeLogs');
|
||||
$this->assertEquals($collection->get_description(), 'adasdasdsa123');
|
||||
$this->assertEquals($collection->get_itens_per_page(), 23);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue