diff --git a/src/classes/entities/class-tainacan-log.php b/src/classes/entities/class-tainacan-log.php index 7a32667cc..ef41a6d0a 100644 --- a/src/classes/entities/class-tainacan-log.php +++ b/src/classes/entities/class-tainacan-log.php @@ -94,6 +94,26 @@ class Log extends Entity { function get_user_id() { 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); + + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 6101fc111..1c5bda1b4 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -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; + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index f1d755d54..14dd21e36 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -1,11 +1,25 @@ 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); } diff --git a/tests/test-logs.php b/tests/test-logs.php index 960d1e82d..b617d9d2e 100644 --- a/tests/test-logs.php +++ b/tests/test-logs.php @@ -1,6 +1,8 @@ set_title('blame someone'); @@ -38,6 +40,38 @@ class Logs extends \WP_UnitTestCase { $this->assertEquals( 'blame someone', $test->get_title() ); $this->assertEquals( 'someone did that', $test->get_description() ); $this->assertEquals( $user_id, $test->get_user_id() ); - $this->assertEquals( $blog_id, $test->get_blog_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); } } \ No newline at end of file