From c246f7355686134193403f104a6db08c2eb0261e Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Fri, 6 Sep 2019 19:44:41 -0300 Subject: [PATCH 001/198] Beginning refactor of logs #261 --- src/classes/class-tainacan-private-files.php | 92 +++++----- src/classes/entities/class-tainacan-log.php | 163 ++++++----------- .../repositories/class-tainacan-logs.php | 166 ++++++++++++++---- .../class-tainacan-repository.php | 105 +---------- 4 files changed, 228 insertions(+), 298 deletions(-) diff --git a/src/classes/class-tainacan-private-files.php b/src/classes/class-tainacan-private-files.php index 6afd86f05..56da2f3cf 100644 --- a/src/classes/class-tainacan-private-files.php +++ b/src/classes/class-tainacan-private-files.php @@ -31,7 +31,7 @@ class Private_Files { add_filter('image_get_intermediate_size', [$this, 'image_get_intermediate_size'], 10, 3); add_filter('wp_get_attachment_url', [$this, 'wp_get_attachment_url'], 10, 2); - add_action('tainacan-insert', [$this, 'update_item_and_collection'], 10, 3); + add_action('tainacan-insert', [$this, 'update_item_and_collection']); add_action('tainacan-bulk-edit-set-status', [$this, 'bulk_edit'], 10, 4); @@ -276,61 +276,57 @@ class Private_Files { * if the items upload directory mus be renamed to add or remove the * private folder prefix */ - function update_item_and_collection($obj, $diffs, $is_update) { + function update_item_and_collection($obj) { - // updating collection or item - if ( $is_update ) { + $folder = DIRECTORY_SEPARATOR; + $check_folder = DIRECTORY_SEPARATOR; + $check = false; + + if ( $obj instanceof \Tainacan\Entities\Collection ) { - $folder = DIRECTORY_SEPARATOR; - $check_folder = DIRECTORY_SEPARATOR; - $check = false; + $status_obj = get_post_status_object($obj->get_status()); - if ( $obj instanceof \Tainacan\Entities\Collection ) { - - $status_obj = get_post_status_object($obj->get_status()); - - $folder .= $status_obj->public ? $obj->get_id() : $this->get_private_folder_prefix() . $obj->get_id(); - $check_folder .= ! $status_obj->public ? $obj->get_id() : $this->get_private_folder_prefix() . $obj->get_id(); - - $check = true; - - } + $folder .= $status_obj->public ? $obj->get_id() : $this->get_private_folder_prefix() . $obj->get_id(); + $check_folder .= ! $status_obj->public ? $obj->get_id() : $this->get_private_folder_prefix() . $obj->get_id(); - if ( $obj instanceof \Tainacan\Entities\Item ) { - - $collection = $obj->get_collection(); - $col_status_object = get_post_status_object($collection->get_status()); - - $folder .= $col_status_object->public ? $collection->get_id() : $this->get_private_folder_prefix() . $collection->get_id() . DIRECTORY_SEPARATOR; - $check_folder .= $col_status_object->public ? $collection->get_id() : $this->get_private_folder_prefix() . $collection->get_id() . DIRECTORY_SEPARATOR; - - $folder .= DIRECTORY_SEPARATOR; - $check_folder .= DIRECTORY_SEPARATOR; - - $status_obj = get_post_status_object($obj->get_status()); - - $folder .= $status_obj->public ? $obj->get_id() : $this->get_private_folder_prefix() . $obj->get_id(); - $check_folder .= ! $status_obj->public ? $obj->get_id() : $this->get_private_folder_prefix() . $obj->get_id(); - - $check = true; - - } + $check = true; - if ($check) { - - $upload_dir = wp_get_upload_dir(); - $base_dir = $upload_dir['basedir']; - $full_path = $base_dir . DIRECTORY_SEPARATOR . $this->get_items_uploads_folder() . $folder; - $full_path_check = $base_dir . DIRECTORY_SEPARATOR . $this->get_items_uploads_folder() . $check_folder; - - if (\file_exists($full_path_check)) { - rename($full_path_check, $full_path); - do_action('tainacan-upload-folder-renamed', $full_path_check, $full_path); - } - + } + + if ( $obj instanceof \Tainacan\Entities\Item ) { + + $collection = $obj->get_collection(); + $col_status_object = get_post_status_object($collection->get_status()); + + $folder .= $col_status_object->public ? $collection->get_id() : $this->get_private_folder_prefix() . $collection->get_id() . DIRECTORY_SEPARATOR; + $check_folder .= $col_status_object->public ? $collection->get_id() : $this->get_private_folder_prefix() . $collection->get_id() . DIRECTORY_SEPARATOR; + + $folder .= DIRECTORY_SEPARATOR; + $check_folder .= DIRECTORY_SEPARATOR; + + $status_obj = get_post_status_object($obj->get_status()); + + $folder .= $status_obj->public ? $obj->get_id() : $this->get_private_folder_prefix() . $obj->get_id(); + $check_folder .= ! $status_obj->public ? $obj->get_id() : $this->get_private_folder_prefix() . $obj->get_id(); + + $check = true; + + } + + if ($check) { + + $upload_dir = wp_get_upload_dir(); + $base_dir = $upload_dir['basedir']; + $full_path = $base_dir . DIRECTORY_SEPARATOR . $this->get_items_uploads_folder() . $folder; + $full_path_check = $base_dir . DIRECTORY_SEPARATOR . $this->get_items_uploads_folder() . $check_folder; + + if (\file_exists($full_path_check)) { + rename($full_path_check, $full_path); + do_action('tainacan-upload-folder-renamed', $full_path_check, $full_path); } } + } diff --git a/src/classes/entities/class-tainacan-log.php b/src/classes/entities/class-tainacan-log.php index 850365f2a..b980eaf6b 100644 --- a/src/classes/entities/class-tainacan-log.php +++ b/src/classes/entities/class-tainacan-log.php @@ -12,14 +12,17 @@ class Log extends Entity { protected $title, $order, - $parent, $description, $blog_id, $user_id, - $log_date, + $date, $user_name, $collection_id, - $item_id; + $item_id, + $object_type, + $object_id, + $old_value, + $new_value; static $post_type = 'tainacan-log'; /** @@ -34,7 +37,6 @@ class Log extends Entity { if ( is_int( $which ) && $which == 0 ) { $this->set_user_id(); - $this->set_blog_id(); } } @@ -89,27 +91,10 @@ class Log extends Entity { * * @return mixed|null */ - function get_log_date() { - return $this->get_mapped_property( 'log_date' ); + function get_date() { + return $this->get_mapped_property( 'date' ); } - /** - * Return the log order type - * - * @return string - */ - function get_order() { - return $this->get_mapped_property( 'order' ); - } - - /** - * Retun the parent ID - * - * @return integer - */ - function get_parent() { - return $this->get_mapped_property( 'parent' ); - } /** * Return the Log description @@ -120,15 +105,6 @@ class Log extends Entity { return $this->get_mapped_property( 'description' ); } - /** - * Return the ID of blog - * - * @return integer - */ - function get_blog_id() { - return $this->get_mapped_property( 'blog_id' ); - } - /** * Return User Id of who make the action * @@ -138,27 +114,28 @@ class Log extends Entity { return $this->get_mapped_property( 'user_id' ); } + /** - * Get value of log entry + * Get old value of log entry object * * @param mixed $value * * @return void */ - public function get_value() { - return maybe_unserialize( base64_decode( $this->get_mapped_property( 'value' ) ) ); + public function get_old_value() { + return $this->get_mapped_property( 'old_value' ); + } + + /** + * Get new value of log entry object + * + * @param mixed $value + * + * @return void + */ + public function get_new_value() { + return $this->get_mapped_property( 'new_value' ); } - -// /** -// * Get old value of log entry object -// * -// * @param mixed $value -// * -// * @return void -// */ -// public function get_old_value() { -// return maybe_unserialize( base64_decode( $this->get_mapped_property( 'old_value' ) ) ); -// } /** * Set log tittle @@ -167,32 +144,10 @@ class Log extends Entity { * * @return void */ - function set_title( $value ) { + public function set_title( $value ) { $this->set_mapped_property( 'title', $value ); } - /** - * Define the order type - * - * @param [string] $value - * - * @return void - */ - function set_order( $value ) { - $this->set_mapped_property( 'order', $value ); - } - - /** - * Define the parent ID - * - * @param [integer] $value - * - * @return void - */ - function set_parent( $value ) { - $this->set_mapped_property( 'parent', $value ); - } - /** * Define the Log description * @@ -200,7 +155,7 @@ class Log extends Entity { * * @return void */ - function set_description( $value ) { + public function set_description( $value ) { $this->set_mapped_property( 'description', $value ); } @@ -211,7 +166,7 @@ class Log extends Entity { * * @return void */ - protected function set_user_id( $value = 0 ) { + public function set_user_id( $value = 0 ) { if ( 0 == $value ) { $value = get_current_user_id(); } @@ -219,46 +174,25 @@ class Log extends Entity { } /** - * Define the blog ID of log entry - * - * @param [integer] $value - * - * @return void - */ - protected function set_blog_id( $value = 0 ) { - if ( 0 == $value ) { - $value = get_current_blog_id(); - } - $this->set_mapped_property( 'blog_id', $value ); - } - - /** - * Define the value of log entry + * Set old 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 ) ) ); + public function set_old_value( $value ) { + $this->set_mapped_property( 'old_value', $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 $diffs + * Set new value of log entry + * + * @param [mixed] $value + * + * @return void */ - public function set_log_diffs($diffs){ - $this->set_mapped_property( 'log_diffs', $diffs ); + public function set_new_value( $value ) { + $this->set_mapped_property( 'new_value', $value ); } /** @@ -275,12 +209,11 @@ class Log extends Entity { * @param null $value * @param array $diffs * @param string $status 'publish', 'private', 'pending', 'processing' or 'error' - * @param int $parent * * @return \Tainacan\Entities\Log | bool * @throws \Exception */ - public static function create( $message = false, $desc = '', $value = null, $diffs = [], $status = 'publish', $parent = 0 ) { + public static function create( $message = false, $desc = '', $value = null, $diffs = [], $status = 'publish' ) { $log = new Log(); @@ -289,10 +222,6 @@ class Log extends Entity { $log->set_status( $status ); $log->set_log_diffs( $diffs ); - if($parent > 0) { - $log->set_parent($parent); - } - if(is_object($value) || is_string($value)) { if(array_search( 'Tainacan\Traits\Entity_Collection_Relation', class_uses($value))) { $log->set_collection_id( $value->get_collection_id() ); @@ -318,6 +247,22 @@ class Log extends Entity { throw new \Exception( 'Invalid log' ); } } + + public function get_object_type() { + $this->get_mapped_property('object_type'); + } + + public function set_object_type($value) { + $this->set_mapped_property('object_type', $value); + } + + public function get_object_id() { + $this->get_mapped_property('object_id'); + } + + public function set_object_id($value) { + $this->set_mapped_property('object_id', $value); + } /** * @param $item_id diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 8b3578c44..f55afc8c9 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -16,6 +16,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); class Logs extends Repository { public $entities_type = '\Tainacan\Entities\Log'; private static $instance = null; + private $current_diff = null; public static function get_instance() { if ( ! isset( self::$instance ) ) { @@ -30,6 +31,9 @@ class Logs extends Repository { parent::__construct(); add_action( 'add_attachment', array( $this, 'prepare_attachment_log_before_insert' ), 10 ); + + add_action( 'tainacan-pre-insert', array( $this, 'prepare_diff' ) ); + add_action( 'tainacan-insert', array( $this, 'insert_entity' ) ); } protected function _get_map() { @@ -42,26 +46,19 @@ class Logs extends Repository { 'on_error' => __( 'The title should be a text value and not empty', 'tainacan' ), 'validation' => '' ], - 'log_date' => [ + 'date' => [ 'map' => 'post_date', 'title' => __( 'Log date', 'tainacan' ), 'type' => 'string', 'description' => __( 'The log date', 'tainacan' ), ], - 'order' => [ - 'map' => 'menu_order', - 'title' => __( 'Menu order', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'Log order' ), - 'validation' => '' - ], - 'parent' => [ - 'map' => 'parent', - 'title' => __( 'Parent', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'Log order' ), - 'validation' => '' - ], + // 'parent' => [ + // 'map' => 'parent', + // 'title' => __( 'Parent', 'tainacan' ), + // 'type' => 'string', + // 'description' => __( 'Log order' ), + // 'validation' => '' + // ], 'description' => [ 'map' => 'post_content', 'title' => __( 'Description', 'tainacan' ), @@ -77,13 +74,6 @@ class Logs extends Repository { 'description' => __( 'The log slug' ), 'validation' => '' ], - 'items_per_page' => [ - 'map' => 'meta', - 'title' => __( 'Items per page', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'The quantity of items that should be loaded' ), - 'validation' => '' - ], 'user_id' => [ 'map' => 'post_author', 'title' => __( 'User ID', 'tainacan' ), @@ -91,21 +81,14 @@ class Logs extends Repository { 'description' => __( 'Unique identifier' ), 'validation' => '' ], - 'blog_id' => [ - 'map' => 'meta', - 'title' => __( 'Blog ID', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'Unique identifier' ), - 'validation' => '' - ], - 'value' => [ - 'map' => 'meta', - 'title' => __( 'Actual value', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The actual log value' ), - 'validation' => '' - ], - 'log_diffs' => [ + // 'value' => [ + // 'map' => 'meta', + // 'title' => __( 'Actual value', 'tainacan' ), + // 'type' => 'string', + // 'description' => __( 'The actual log value' ), + // 'validation' => '' + // ], + 'log_diffs' => [ // deprecated 'map' => 'meta', 'title' => __( 'Log differences', 'tainacan' ), 'description' => __( 'Differences between old and new versions of object', 'tainacan' ) @@ -115,10 +98,23 @@ class Logs extends Repository { 'title' => __( 'Log collection relationship', 'tainacan' ), 'description' => __( 'The ID of the collection that this log is related to', 'tainacan' ) ], - 'item_id' => [ + 'object_id' => [ 'map' => 'meta', 'title' => __( 'Log item relationship', 'tainacan' ), - 'description' => __( 'The id of the item that this log is related to', 'tainacan' ), + 'description' => __( 'The id of the object that this log is related to', 'tainacan' ), + ], + 'object_type' => [ + 'map' => 'meta', + 'title' => __( 'Log item relationship', 'tainacan' ), + 'description' => __( 'The type of the object that this log is related to', 'tainacan' ), + ], + 'old_value' => [ + 'map' => 'meta', + 'title' => __( 'Old Value', 'tainacan' ), + ], + 'new_value' => [ + 'map' => 'meta', + 'title' => __( 'New value', 'tainacan' ), ] ] ); } @@ -358,6 +354,98 @@ class Logs extends Repository { return Entities\Log::create( $title, $description, $value, $diffs ); } } + + /** + * Compare two repository entities and sets the current_diff property to be used in the insert hook + * + * @param Entity $unsaved The new entity that is going to be saved + * + * @return void + */ + public function prepare_diff( Entities\Entity $unsaved ) { + + if ( ! $unsaved->get_repository()->use_logs ) { + return; + } + + // do not log a log + if ( ( method_exists( $unsaved, 'get_post_type' ) && $unsaved->get_post_type() === 'tainacan-log' ) || $unsaved->get_status() === 'auto-draft' ) { + return; + } + + $creating = true; + $old = $unsaved->get_repository()->fetch( $unsaved->get_id() ); + + if ( $old instanceof Entities\Entity ) { + + if ( $old->get_status() !== 'auto-draft' ) { + $creating = false; + } + + } + + $diff = [ + 'old' => [], + 'new' => [] + ]; + + if ( $creating ) { + $diff['new'] = $unsaved->_toArray(); + } else { + $map = $unsaved->get_repository()->get_map(); + + foreach ( $map as $prop => $mapped ) { + // I can't verify differences on item, because it attributes are added when item is a auto-draft + if ( $old->get( $prop ) != $unsaved->get( $prop ) ) { + + $diff['old'][$prop] = $old->get( $prop ); + $diff['new'][$prop] = $unsaved->get( $prop ); + + } + } + } + + $diff = apply_filters( 'tainacan-entity-diff', $diff, $unsaved, $old ); + + $this->current_diff = $diff; + + } + + public function insert_entity( Entities\Entity $entity ) { + + if ( ! $entity->get_repository()->use_logs ) { + return; + } + + // do not log a log + if ( ( method_exists( $entity, 'get_post_type' ) && $entity->get_post_type() === 'tainacan-log' ) || $entity->get_status() === 'auto-draft' ) { + return false; + } + + $log = new Entities\Log(); + + $collection_id = method_exists($entity, 'get_collection_id') ? $entity->get_collection_id() : 'default'; + + if ( $entity instanceof Entities\Collection ) { + $collection_id = $entity->get_id(); + } + + $object_type = get_class($entity); + $object_id = $entity->get_id(); + + $diff = $this->current_diff; + + $log->set_collection_id($collection_id); + $log->set_object_type($object_type); + $log->set_object_id($object_id); + $log->set_old_value($diff['old']); + $log->set_new_value($diff['new']); + + if ( $log->validate() ) { + $this->insert($log); + } + + } /** * This will prepare the event title for objects diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index a0f182b9d..3e05a946f 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -128,21 +128,9 @@ abstract class Repository { $old = ''; $diffs = []; - - if ( $this->use_logs ) { - if ( $obj->get_id() ) { - - $old = $obj->get_repository()->fetch( $obj->get_id() ); - - if ( method_exists( $old, 'get_status' ) && $old->get_status() === 'auto-draft' ) { - $is_update = false; - } else { - $is_update = true; - } - - $diffs = $this->diff( $old, $obj ); - } - } + + do_action( 'tainacan-pre-insert', $obj ); + do_action( 'tainacan-pre-insert-' . $obj->get_post_type(), $obj ); $map = $this->get_map(); @@ -186,11 +174,6 @@ abstract class Repository { } } - // TODO: Logs for header image insert and update - if ( $this->use_logs ) { - $this->logs_repository->insert_log( $obj, $diffs, $is_update ); - } - do_action( 'tainacan-insert', $obj, $diffs, $is_update ); do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj ); @@ -804,88 +787,6 @@ abstract class Repository { return $temp_array; } - /** - * Compare two repository entities - * - * @param Entity|integer|\WP_Post $old default ($which = 0) to self compare with stored entity - * @param Entity|integer|\WP_Post $new - * - * @return array List of diff values - * @throws \Exception - */ - public function diff( $old = 0, $new ) { - $old_entity = null; - - if ( $old === 0 || is_array( $old ) && count( $old ) == 0 ) { // self diff or other entity? - $id = $new->get_id(); - - if ( ! empty( $id ) ) { // there is a repository entity? - $old_entity = $this->get_entity_by_post( $new->WP_Post->ID ); - } else { - $entity_type = get_class( $new ); - $old_entity = new $entity_type; // there is no saved entity, let compare with a new empty one - } - } else { - if ( $old->get_status() === 'auto-draft' ) { - $entity_type = get_class( $new ); - $old_entity = new $entity_type; - } else { - $old_entity = $old; - } - } - - $new_entity = $new; - - $map = $this->get_map(); - - $diff = []; - - foreach ( $map as $prop => $mapped ) { - // I can't verify differences on item, because it attributes are added when item is a auto-draft - if ( $old_entity->get_mapped_property( $prop ) != $new_entity->get_mapped_property( $prop ) ) { - - if ( $mapped['map'] === 'meta_multi' || ( $mapped['map'] === 'meta' && is_array( $new_entity->get_mapped_property( $prop ) ) ) ) { - - // Array of diffs with index of diff in new array - $new_v = $new_entity->get_mapped_property( $prop ); - $old_v = $old_entity->get_mapped_property( $prop ); - - $old_v = ! is_array( $old_v ) && empty( $old_v ) && ! is_string( $old_v ) ? array() : ( ! is_string( $old_v ) ? $old_v : [ $old_v ] ); - - $array_diff_with_index = array_map( 'unserialize', - array_diff_assoc( array_map( 'serialize', $new_v ), array_map( 'serialize', $old_v ) ) ); - - if ( ! empty( $array_diff_with_index ) ) { - - $diff[ $prop ] = [ - 'new' => $new_entity->get_mapped_property( $prop ), - 'old' => $old_entity->get_mapped_property( $prop ), - 'diff_with_index' => $array_diff_with_index, - ]; - } - } elseif ( $mapped['map'] !== 'post_modified' ) { - $new_as_array = explode( ' ', $new_entity->get_mapped_property( $prop ) ); - $old_as_array = explode( ' ', $old_entity->get_mapped_property( $prop ) ); - - // Array of diffs with index of diff in new array - $array_diff_with_index = array_diff_assoc( $new_as_array, $old_as_array ); - - $diff[ $prop ] = [ - 'new' => $new_as_array, - 'old' => $old_entity->get_mapped_property( $prop ), - 'diff_with_index' => $array_diff_with_index, - ]; - } - - } - } - - unset( $diff['id'], $diff['collection_id'], $diff['author_id'], $diff['creation_date'], $diff['_thumbnail_id'] ); - $diff = apply_filters( 'tainacan-entity-diff', $diff, $new, $old ); - - return $diff; - } - /** * Inserts or update thumbnail for items and collections and return an array * with old thumbnail and new thumbnail From e958497d597c4acd98ea9b8c3d3a6ecfa4745bd3 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 9 Sep 2019 19:03:14 -0300 Subject: [PATCH 002/198] Continue refactor. Insert item metadata #261 --- .../taxonomy/class-tainacan-taxonomy.php | 4 + .../class-tainacan-item-metadata.php | 54 ++------------ .../repositories/class-tainacan-logs.php | 74 ++++++++++++++++++- 3 files changed, 80 insertions(+), 52 deletions(-) diff --git a/src/classes/metadata-types/taxonomy/class-tainacan-taxonomy.php b/src/classes/metadata-types/taxonomy/class-tainacan-taxonomy.php index 8689f3343..910d612da 100644 --- a/src/classes/metadata-types/taxonomy/class-tainacan-taxonomy.php +++ b/src/classes/metadata-types/taxonomy/class-tainacan-taxonomy.php @@ -217,6 +217,10 @@ class Taxonomy extends Metadata_Type { $count ++; + if ( is_integer($term) ) { + $term = \Tainacan\Repositories\Terms::get_instance()->fetch($term, $this->get_option('taxonomy_id')); + } + if ( $term instanceof \Tainacan\Entities\Term ) { $return .= $prefix; diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index bb647f215..cab3bc6dc 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -36,26 +36,12 @@ class Item_Metadata extends Repository { throw new \Exception( 'Entities must be validated before you can save them' ); // TODO: Throw Warning saying you must validate object before insert() } - - $is_update = true; + + do_action( 'tainacan-pre-insert', $item_metadata ); + do_action( 'tainacan-pre-insert-Item_Metadata_Entity', $item_metadata ); $new = $item_metadata->get_value(); - $diffs = []; - - if ($this->use_logs) { - $old = get_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), true ); - if($old != $new) { - $diffs['value'] = [ - 'new' => $new, - 'old' => $old, - 'diff_with_index' => [], - ]; - } else { - $diffs['value'] = []; - } - } - $unique = ! $item_metadata->is_multiple(); $metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object(); @@ -106,13 +92,10 @@ class Item_Metadata extends Repository { } } - if ($this->use_logs) { - $this->logs_repository->insert_log( $item_metadata, $diffs, $is_update ); - } - - do_action( 'tainacan-insert', $item_metadata, $diffs, $is_update ); - do_action( 'tainacan-insert-Item_Metadata_Entity', $item_metadata ); } + + do_action( 'tainacan-insert', $item_metadata ); + do_action( 'tainacan-insert-Item_Metadata_Entity', $item_metadata ); $new_entity = new Entities\Item_Metadata_Entity( $item_metadata->get_item(), $item_metadata->get_metadatum() ); @@ -164,11 +147,6 @@ class Item_Metadata extends Repository { $taxonomy = new Entities\Taxonomy( $metadata_type->get_option( 'taxonomy_id' ) ); if ( $taxonomy ) { - if ( $this->use_logs ) { - $old = wp_get_object_terms( $item_metadata->get_item()->get_id(), $taxonomy->get_db_identifier(), [ - 'fields' => 'names' - ] ); - } // We can not simply use wp_set_object_terms() because it uses term_exists() which is not reliable // see https://core.trac.wordpress.org/ticket/45333 and https://core.trac.wordpress.org/ticket/47099 @@ -200,26 +178,6 @@ class Item_Metadata extends Repository { $success = wp_set_object_terms( $item_metadata->get_item()->get_id(), $insert, $taxonomy->get_db_identifier() ); - if ( $this->use_logs && ! $success instanceof \WP_Error ) { - - $new = get_terms(array( - 'taxonomy' => $taxonomy->get_db_identifier(), - 'hide_empty' => false, - 'object_ids' => $item_metadata->get_item()->get_id(), - 'fields' => 'names', - )); - - $diffs[ 'value' ] = [ - 'new' => $new, - 'old' => $old, - 'diff_with_index' => [] - ]; - - if($this->use_logs){ - $this->logs_repository->insert_log( $item_metadata, $diffs, true ); - //do_action( 'tainacan-insert', $item_metadata, $diffs, true ); - } - } } } } diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index f55afc8c9..82e1a40b0 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -32,7 +32,8 @@ class Logs extends Repository { add_action( 'add_attachment', array( $this, 'prepare_attachment_log_before_insert' ), 10 ); - add_action( 'tainacan-pre-insert', array( $this, 'prepare_diff' ) ); + add_action( 'tainacan-pre-insert', array( $this, 'prepare_entity_diff' ) ); + add_action( 'tainacan-insert', array( $this, 'insert_entity' ) ); } @@ -81,6 +82,11 @@ class Logs extends Repository { 'description' => __( 'Unique identifier' ), 'validation' => '' ], + 'item_id' => [ + 'map' => 'meta', + 'title' => __( 'Item ID', 'tainacan' ), + 'type' => 'integer', + ], // 'value' => [ // 'map' => 'meta', // 'title' => __( 'Actual value', 'tainacan' ), @@ -362,12 +368,18 @@ class Logs extends Repository { * * @return void */ - public function prepare_diff( Entities\Entity $unsaved ) { - + public function prepare_entity_diff( Entities\Entity $unsaved ) { + if ( ! $unsaved->get_repository()->use_logs ) { return; } + if ( $unsaved instanceof Entities\Item_Metadata_Entity ) { + return $this->prepare_item_metadata_diff($unsaved); + } elseif ( $unsaved instanceof Entities\Term ) { + return $this->prepare_term_diff($unsaved); + } + // do not log a log if ( ( method_exists( $unsaved, 'get_post_type' ) && $unsaved->get_post_type() === 'tainacan-log' ) || $unsaved->get_status() === 'auto-draft' ) { return; @@ -411,12 +423,39 @@ class Logs extends Repository { } - public function insert_entity( Entities\Entity $entity ) { + private function prepare_item_metadata_diff( Entities\Entity $unsaved ) { + $diff = [ + 'old' => [], + 'new' => [] + ]; + + $old = new Entities\Item_Metadata_Entity($unsaved->get_item(), $unsaved->get_metadatum()); + + if ( $old instanceof Entities\Item_Metadata_Entity ) { + $diff['old'] = $old->get_value_as_string(); + } + + $diff['new'] = $unsaved->get_value_as_string(); + + $diff = apply_filters( 'tainacan-entity-diff', $diff, $unsaved, $old ); + + $this->current_diff = $diff; + + } + + public function insert_entity( Entities\Entity $entity ) { + if ( ! $entity->get_repository()->use_logs ) { return; } + if ( $entity instanceof Entities\Item_Metadata_Entity ) { + return $this->insert_item_metadata($entity); + } elseif ( $entity instanceof Entities\Term ) { + return $this->insert_term($entity); + } + // do not log a log if ( ( method_exists( $entity, 'get_post_type' ) && $entity->get_post_type() === 'tainacan-log' ) || $entity->get_status() === 'auto-draft' ) { return false; @@ -429,6 +468,9 @@ class Logs extends Repository { if ( $entity instanceof Entities\Collection ) { $collection_id = $entity->get_id(); } + if ( $entity instanceof Entities\Item ) { + $log->set_item_id($entity->get_id()); + } $object_type = get_class($entity); $object_id = $entity->get_id(); @@ -446,6 +488,30 @@ class Logs extends Repository { } } + + private function insert_item_metadata( Entities\Item_Metadata_Entity $entity ) { + + $log = new Entities\Log(); + + $item_id = $entity->get_item()->get_id(); + $collection_id = $entity->get_item()->get_collection_id(); + $object_type = get_class($entity); + $object_id = $entity->get_metadatum()->get_id(); + + $diff = $this->current_diff; + + $log->set_collection_id($collection_id); + $log->set_object_type($object_type); + $log->set_object_id($object_id); + $log->set_item_id($item_id); + $log->set_old_value($diff['old']); + $log->set_new_value($diff['new']); + + if ( $log->validate() ) { + $this->insert($log); + } + + } /** * This will prepare the event title for objects From 92a32fdf6cb228c3f407f5c847b096ebe857d3ee Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Tue, 10 Sep 2019 16:46:40 -0300 Subject: [PATCH 003/198] refactoring logs: delete entities and log terms #261 --- src/classes/entities/class-tainacan-log.php | 8 ++ .../repositories/class-tainacan-logs.php | 128 +++++++++++++++++- .../class-tainacan-repository.php | 7 +- .../repositories/class-tainacan-terms.php | 27 ++-- 4 files changed, 148 insertions(+), 22 deletions(-) diff --git a/src/classes/entities/class-tainacan-log.php b/src/classes/entities/class-tainacan-log.php index b980eaf6b..5e16a014b 100644 --- a/src/classes/entities/class-tainacan-log.php +++ b/src/classes/entities/class-tainacan-log.php @@ -277,6 +277,14 @@ class Log extends Entity { public function get_item_id(){ return $this->get_mapped_property('item_id'); } + + public function get_action() { + $this->get_mapped_property('action'); + } + + public function set_action($value) { + $this->set_mapped_property('action', $value); + } /** * {@inheritDoc} diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 82e1a40b0..c55bcc091 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -17,6 +17,8 @@ class Logs extends Repository { public $entities_type = '\Tainacan\Entities\Log'; private static $instance = null; private $current_diff = null; + private $current_deleting_entity; + private $current_action; public static function get_instance() { if ( ! isset( self::$instance ) ) { @@ -35,6 +37,8 @@ class Logs extends Repository { add_action( 'tainacan-pre-insert', array( $this, 'prepare_entity_diff' ) ); add_action( 'tainacan-insert', array( $this, 'insert_entity' ) ); + add_action( 'tainacan-deleted', array( $this, 'delete_entity' ), 10, 2 ); + add_action( 'tainacan-pre-delete', array( $this, 'pre_delete_entity' ), 10, 2 ); } protected function _get_map() { @@ -121,6 +125,10 @@ class Logs extends Repository { 'new_value' => [ 'map' => 'meta', 'title' => __( 'New value', 'tainacan' ), + ], + 'action' => [ + 'map' => 'meta', + 'title' => __( 'Action', 'tainacan' ), ] ] ); } @@ -420,6 +428,48 @@ class Logs extends Repository { $diff = apply_filters( 'tainacan-entity-diff', $diff, $unsaved, $old ); $this->current_diff = $diff; + $this->current_action = $creating ? 'create' : 'update'; + + } + + private function prepare_term_diff( Entities\Term $unsaved ) { + + $creating = true; + $old = $unsaved->get_repository()->fetch( $unsaved->get_id(), $unsaved->get_taxonomy() ); + + if ( $old instanceof Entities\Entity ) { + + if ( $old->get_status() !== 'auto-draft' ) { + $creating = false; + } + + } + + $diff = [ + 'old' => [], + 'new' => [] + ]; + + if ( $creating ) { + $diff['new'] = $unsaved->_toArray(); + } else { + $map = $unsaved->get_repository()->get_map(); + + foreach ( $map as $prop => $mapped ) { + // I can't verify differences on item, because it attributes are added when item is a auto-draft + if ( $old->get( $prop ) != $unsaved->get( $prop ) ) { + + $diff['old'][$prop] = $old->get( $prop ); + $diff['new'][$prop] = $unsaved->get( $prop ); + + } + } + } + + $diff = apply_filters( 'tainacan-entity-diff', $diff, $unsaved, $old ); + + $this->current_diff = $diff; + $this->current_action = $creating ? 'create' : 'update'; } @@ -441,6 +491,7 @@ class Logs extends Repository { $diff = apply_filters( 'tainacan-entity-diff', $diff, $unsaved, $old ); $this->current_diff = $diff; + $this->current_action = 'update'; } @@ -453,7 +504,7 @@ class Logs extends Repository { if ( $entity instanceof Entities\Item_Metadata_Entity ) { return $this->insert_item_metadata($entity); } elseif ( $entity instanceof Entities\Term ) { - return $this->insert_term($entity); + //return $this->insert_term($entity); } // do not log a log @@ -489,6 +540,81 @@ class Logs extends Repository { } + public function pre_delete_entity( Entities\Entity $entity, bool $permanent) { + + if ( ! $entity->get_repository()->use_logs ) { + return; + } + + if ( $entity instanceof Entities\Item_Metadata_Entity ) { + //return $this->insert_item_metadata($entity); + } elseif ( $entity instanceof Entities\Term ) { + //return $this->insert_term($entity); + } + + // do not log a log + if ( ( method_exists( $entity, 'get_post_type' ) && $entity->get_post_type() === 'tainacan-log' ) || $entity->get_status() === 'auto-draft' ) { + return false; + } + + $log = new Entities\Log(); + + $this->current_deleting_entity = $entity->_toArray(); + $this->current_action = $permanent ? 'delete' : 'trash'; + + } + + public function delete_entity( Entities\Entity $entity, bool $permanent) { + + if ( ! $entity->get_repository()->use_logs ) { + return; + } + + if ( $entity instanceof Entities\Item_Metadata_Entity ) { + //return $this->insert_item_metadata($entity); + } elseif ( $entity instanceof Entities\Term ) { + //return $this->insert_term($entity); + } + + // do not log a log + if ( ( method_exists( $entity, 'get_post_type' ) && $entity->get_post_type() === 'tainacan-log' ) || $entity->get_status() === 'auto-draft' ) { + return false; + } + + $log = new Entities\Log(); + + $collection_id = method_exists($entity, 'get_collection_id') ? $entity->get_collection_id() : 'default'; + + if ( $entity instanceof Entities\Collection ) { + $collection_id = $entity->get_id(); + } + if ( $entity instanceof Entities\Item ) { + $log->set_item_id($entity->get_id()); + } + + $object_type = get_class($entity); + $object_id = $entity->get_id(); + + $diff = $this->current_diff; + + $log->set_collection_id($collection_id); + $log->set_object_type($object_type); + $log->set_object_id($object_id); + + if ( $permanent ) { + $log->set_old_value( $this->current_deleting_entity ); + } else { + $log->set_old_value( ['status' => $entity->get_status()] ); + $log->set_new_value( ['status' => 'trash'] ); + } + + + if ( $log->validate() ) { + $this->insert($log); + } + + } + private function insert_item_metadata( Entities\Item_Metadata_Entity $entity ) { $log = new Entities\Log(); diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 3e05a946f..0d829a8e8 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -607,6 +607,10 @@ abstract class Repository { * @return mixed|Entity @see https://developer.wordpress.org/reference/functions/wp_delete_post/ */ public function delete( Entities\Entity $entity, $permanent = true ) { + + do_action( 'tainacan-pre-delete', $entity, $permanent ); + do_action( 'tainacan-pre-delete-' . $entity->get_post_type(), $entity, $permanent ); + if ($permanent === true) { $return = wp_delete_post( $entity->get_id(), $permanent ); } elseif ($permanent === false) { @@ -614,8 +618,7 @@ abstract class Repository { } - if ( $return instanceof \WP_Post && $this->use_logs) { - $this->logs_repository->insert_log( $entity, [], false, false, true ); + if ( $return instanceof \WP_Post && $this->use_logs ) { do_action( 'tainacan-deleted', $entity, $permanent ); do_action( 'tainacan-deleted-' . $entity->get_post_type(), $entity, $permanent ); diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index bfce0d9e7..3568d04be 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -117,16 +117,9 @@ class Terms extends Repository { throw new \Exception( 'Entities must be validated before you can save them' ); } - $is_update = false; - $diffs = []; - if ( $term->get_id() && $this->use_logs) { - $is_update = true; - - $old = $this->fetch( $term->get_id(), $term->get_taxonomy() ); - - $diffs = $this->diff( $old, $term ); - } - + do_action( 'tainacan-pre-insert', $term ); + do_action( 'tainacan-pre-insert-term', $term ); + // First iterate through the native post properties $map = $this->get_map(); foreach ( $map as $prop => $mapped ) { @@ -169,17 +162,13 @@ class Terms extends Repository { update_term_meta( $term_saved['term_id'], $prop, wp_slash( $term->get_mapped_property( $prop ) ) ); } } + + $new_entity = new Entities\Term( $term_saved['term_id'], $term->get_taxonomy() ); - if($this->use_logs){ - // TODO: Log header image updates - $this->logs_repository->insert_log( $term, $diffs, $is_update ); - } + do_action( 'tainacan-insert', $new_entity ); + do_action( 'tainacan-insert-term', $new_entity ); - - do_action( 'tainacan-insert', $term, $diffs, $is_update ); - do_action( 'tainacan-insert-term', $term ); - - return new Entities\Term( $term_saved['term_id'], $term->get_taxonomy() ); + return $new_entity; } // TODO: Is this workaround ok to avoid getting htmlentities ? From 693c2ffa1785c0316d75cb1eb64b46c9702e7d49 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Wed, 11 Sep 2019 21:37:10 -0300 Subject: [PATCH 004/198] Log attachments insertion and deletion #261 --- .../repositories/class-tainacan-logs.php | 126 ++++++++++++++---- 1 file changed, 102 insertions(+), 24 deletions(-) diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index c55bcc091..02e08b2d9 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -32,13 +32,15 @@ class Logs extends Repository { protected function __construct() { parent::__construct(); - add_action( 'add_attachment', array( $this, 'prepare_attachment_log_before_insert' ), 10 ); - add_action( 'tainacan-pre-insert', array( $this, 'prepare_entity_diff' ) ); add_action( 'tainacan-insert', array( $this, 'insert_entity' ) ); add_action( 'tainacan-deleted', array( $this, 'delete_entity' ), 10, 2 ); add_action( 'tainacan-pre-delete', array( $this, 'pre_delete_entity' ), 10, 2 ); + + add_action( 'add_attachment', array( $this, 'insert_attachment' ) ); + add_action( 'delete_attachment', array( $this, 'pre_delete_attachment' ) ); + add_action( 'delete_post', array( $this, 'delete_attachment' ) ); } protected function _get_map() { @@ -243,43 +245,116 @@ class Logs extends Repository { } - public function prepare_attachment_log_before_insert( $post_ID ) { + public function insert_attachment( $post_ID ) { $attachment = get_post( $post_ID ); $post = $attachment->post_parent; if ( $post ) { // was added attachment on a tainacan object - $tainacan_post = Repository::get_entity_by_post( $post ); + $entity = Repository::get_entity_by_post( $post ); - if ( $tainacan_post ) { + if ( $entity ) { // was added a normal attachment - - // get all attachments except the new - $old_attachments_objects = $tainacan_post->get_attachments( $post_ID ); - - // get all attachments - $new_attachments_objects = $tainacan_post->get_attachments(); - $old_attachments = $this->prepare_attachments($old_attachments_objects); - $new_attachments = $this->prepare_attachments($new_attachments_objects); - - $array_diff_with_index = array_map( 'unserialize', - array_diff_assoc( array_map( 'serialize', $new_attachments ), array_map( 'serialize', $old_attachments ) ) ); - - $diff['attachments'] = [ - 'new' => $new_attachments, - 'old' => $old_attachments, - 'diff_with_index' => $array_diff_with_index + $log = new Entities\Log(); + + $collection_id = method_exists($entity, 'get_collection_id') ? $entity->get_collection_id() : 'default'; + + if ( $entity instanceof Entities\Collection ) { + $collection_id = $entity->get_id(); + } + if ( $entity instanceof Entities\Item ) { + $log->set_item_id($entity->get_id()); + } + + $object_type = get_class($entity); + $object_id = $entity->get_id(); + + $diff = []; + + $log->set_collection_id($collection_id); + $log->set_object_type($object_type); + $log->set_object_id($object_id); + $log->set_action('new-attachment'); + + $prepared = [ + 'id' => $attachment->ID, + 'title' => $attachment->post_title, + 'description' => $attachment->post_content, + 'mime_type' => $attachment->post_mime_type, + 'url' => wp_get_attachment_url($attachment->ID), + 'thumb' => wp_get_attachment_image_src($attachment->ID, 'thumbnail'), ]; - - $this->insert_log( $tainacan_post, $diff, true ); + + $log->set_new_value($prepared); + + if ( $log->validate() ) { + $this->insert($log); + } } } } + public function pre_delete_attachment($attachment_id) { + + $attachment_post = get_post($attachment_id); + + $entity_post = get_post($attachment_post->post_parent); + + if ( $entity_post ) { + // was added attachment on a tainacan object + + $entity = Repository::get_entity_by_post( $entity_post ); + + if ( $entity ) { + + + $collection_id = method_exists($entity, 'get_collection_id') ? $entity->get_collection_id() : 'default'; + + $log = new Entities\Log(); + + if ( $entity instanceof Entities\Collection ) { + $collection_id = $entity->get_id(); + } + if ( $entity instanceof Entities\Item ) { + $log->set_item_id($entity->get_id()); + } + + $object_type = get_class($entity); + $object_id = $entity->get_id(); + + $preapred = [ + 'id' => $attachment_id, + 'title' => $attachment_post->post_title, + 'description' => $attachment_post->post_content, + ]; + + $log->set_collection_id($collection_id); + $log->set_object_type($object_type); + $log->set_object_id($object_id); + $log->set_old_value($preapred); + $log->set_action('delete-attachment'); + + $this->current_attachment_delete_log = $log; + + } + + } + } + + public function delete_attachment($attachment_id) { + if ( isset($this->current_attachment_delete_log) && $this->current_attachment_delete_log instanceof Entities\Log ) { + $log = $this->current_attachment_delete_log; + $att = $log->get_old_value(); + if ( is_array($att) && isset($att['id']) && $att['id'] == $attachment_id && $log->validate() ) { + $this->insert($log); + } + } + } + private function prepare_attachments($attachments) { $attachments_prepared = []; if ( is_array($attachments) ) { @@ -289,7 +364,7 @@ class Logs extends Repository { 'title' => $attachment->post_title, 'description' => $attachment->post_content, 'mime_type' => $attachment->post_mime_type, - 'url' => $attachment->guid, + 'url' => wp_get_attachment_url($attachment->ID), ]; array_push( $attachments_prepared, $prepared ); @@ -533,6 +608,7 @@ class Logs extends Repository { $log->set_object_id($object_id); $log->set_old_value($diff['old']); $log->set_new_value($diff['new']); + $log->set_action($this->current_action); if ( $log->validate() ) { $this->insert($log); @@ -600,6 +676,7 @@ class Logs extends Repository { $log->set_collection_id($collection_id); $log->set_object_type($object_type); $log->set_object_id($object_id); + $log->set_action($this->current_action); if ( $permanent ) { $log->set_old_value( $this->current_deleting_entity ); @@ -632,6 +709,7 @@ class Logs extends Repository { $log->set_item_id($item_id); $log->set_old_value($diff['old']); $log->set_new_value($diff['new']); + $log->set_action($this->current_action); if ( $log->validate() ) { $this->insert($log); From 9b84e67c99c5e39425029b771edf609bcbe11d0a Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Thu, 12 Sep 2019 18:08:28 -0300 Subject: [PATCH 005/198] Creating logs titles. entity and attachment insertion #261 --- .../repositories/class-tainacan-logs.php | 88 +++++++++++++++++-- .../class-tainacan-taxonomies.php | 8 ++ 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 02e08b2d9..8951afe6f 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -263,9 +263,11 @@ class Logs extends Repository { if ( $entity instanceof Entities\Collection ) { $collection_id = $entity->get_id(); + $log->set_title( sprintf( __( 'New file was attached to Collection "%s"', 'tainacan'), $entity->get_name() ) ); } if ( $entity instanceof Entities\Item ) { $log->set_item_id($entity->get_id()); + $log->set_title( sprintf( __( 'New file was attached to Item "%s"', 'tainacan'), $entity->get_title() ) ); } $object_type = get_class($entity); @@ -278,6 +280,8 @@ class Logs extends Repository { $log->set_object_id($object_id); $log->set_action('new-attachment'); + $title = __( sprintf('') , 'tainacan'); + $prepared = [ 'id' => $attachment->ID, 'title' => $attachment->post_title, @@ -318,9 +322,11 @@ class Logs extends Repository { if ( $entity instanceof Entities\Collection ) { $collection_id = $entity->get_id(); + $log->set_title( sprintf(__( 'File attached to Collection "%s" was removed', 'tainacan'), $entity->get_name() ) ); } if ( $entity instanceof Entities\Item ) { $log->set_item_id($entity->get_id()); + $log->set_title( sprintf( __( 'File attached to Item "%s" was removed' , 'tainacan'), $entity->get_title() ) ); } $object_type = get_class($entity); @@ -591,18 +597,90 @@ class Logs extends Repository { $collection_id = method_exists($entity, 'get_collection_id') ? $entity->get_collection_id() : 'default'; + $diff = $this->current_diff; + if ( $entity instanceof Entities\Collection ) { + $collection_id = $entity->get_id(); - } - if ( $entity instanceof Entities\Item ) { + + if ($this->current_action == 'update') { + $log->set_title( sprintf( __( 'Collection "%s" was updated', 'tainacan'), $entity->get_name() ) ); + } elseif ($this->current_action == 'create') { + $log->set_title( sprintf( __( 'Collection "%s" was created', 'tainacan'), $entity->get_name() ) ); + } + + } elseif ( $entity instanceof Entities\Item ) { + $log->set_item_id($entity->get_id()); + + if ($this->current_action == 'update') { + if (isset($diff['new']['document'])) { + $log->set_title( sprintf( __( 'Item "%s" document was updated', 'tainacan'), $entity->get_title() ) ); + } else { + $log->set_title( sprintf( __( 'Item "%s" was updated', 'tainacan'), $entity->get_title() ) ); + } + } elseif ($this->current_action == 'create') { + $log->set_title( sprintf( __( 'Item "%1$s" was created with the ID %2$s', 'tainacan'), $entity->get_title(), $entity->get_id() ) ); + } + } elseif ( $entity instanceof Entities\Filter ) { + + if ( 'default' == $collection_id ) { + if ($this->current_action == 'update') { + $log->set_title( sprintf( __( 'Filter "%s" was updated in repository level', 'tainacan'), $entity->get_name() ) ); + } elseif ($this->current_action == 'create') { + $log->set_title( sprintf( __( 'Filter "%1$s" was added to the repository', 'tainacan'), $entity->get_name() ) ); + } + } elseif ( is_numeric($collection_id) ) { + if ($this->current_action == 'update') { + $log->set_title( sprintf( __( 'Filter "%s" was updated in Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + } elseif ($this->current_action == 'create') { + $log->set_title( sprintf( __( 'Filter "%1$s" was added to Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + } + } + + } elseif ( $entity instanceof Entities\Metadatum ) { + + if ( 'default' == $collection_id ) { + if ($this->current_action == 'update') { + $log->set_title( sprintf( __( 'Metadatum "%s" was updated in repository level', 'tainacan'), $entity->get_name() ) ); + } elseif ($this->current_action == 'create') { + $log->set_title( sprintf( __( 'Metadatum "%1$s" was added to the repository', 'tainacan'), $entity->get_name() ) ); + } + } elseif ( is_numeric($collection_id) ) { + if ($this->current_action == 'update') { + $log->set_title( sprintf( __( 'Metadatum "%s" was updated in Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + } elseif ($this->current_action == 'create') { + $log->set_title( sprintf( __( 'Metadatum "%1$s" was added to Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + } + } + + } elseif ( $entity instanceof Entities\Taxonomy ) { + + if ($this->current_action == 'update') { + $log->set_title( sprintf( __( 'Taxonomy "%s" was updated', 'tainacan'), $entity->get_name() ) ); + } elseif ($this->current_action == 'create') { + $log->set_title( sprintf( __( 'Taxonomy "%1$s" was created', 'tainacan'), $entity->get_name() ) ); + } + + } elseif ( $entity instanceof Entities\Term ) { + + $taxonomy = Taxonomies::get_instance()->fetch_by_db_identifier($entity->get_taxonomy()); + $tax_name = ''; + if ($taxonomy instanceof Entities\Taxonomy) { + $tax_name = $taxonomy->get_name(); + } + + if ($this->current_action == 'update') { + $log->set_title( sprintf( __( 'Term "%1$s" was updated in "%2$s" taxonomy', 'tainacan'), $entity->get_name(), $tax_name ) ); + } elseif ($this->current_action == 'create') { + $log->set_title( sprintf( __( 'Term "%1$s" was added to "%2$s" taxonomy', 'tainacan'), $entity->get_name(), $tax_name ) ); + } + } $object_type = get_class($entity); $object_id = $entity->get_id(); - $diff = $this->current_diff; - $log->set_collection_id($collection_id); $log->set_object_type($object_type); $log->set_object_id($object_id); @@ -633,8 +711,6 @@ class Logs extends Repository { return false; } - $log = new Entities\Log(); - $this->current_deleting_entity = $entity->_toArray(); $this->current_action = $permanent ? 'delete' : 'trash'; diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index 0a1337f4a..72743b66f 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -240,6 +240,14 @@ class Taxonomies extends Repository { return $this->fetch($args, $output); } + + public function fetch_by_db_identifier($db_identifier) { + $id = $this->get_id_by_db_identifier($db_identifier); + if ($id) { + return $this->fetch( (int) $id ); + } + return []; + } public function update( $object, $new_values = null ) { return $this->insert( $object ); From 3535217df7793326e45eeff693f3091b3cd9ffe5 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Fri, 13 Sep 2019 16:26:22 -0300 Subject: [PATCH 006/198] setting title for all logs #261 --- .../repositories/class-tainacan-logs.php | 84 ++++++++++++++++++- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 8951afe6f..56d5a4391 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -626,13 +626,13 @@ class Logs extends Repository { if ( 'default' == $collection_id ) { if ($this->current_action == 'update') { - $log->set_title( sprintf( __( 'Filter "%s" was updated in repository level', 'tainacan'), $entity->get_name() ) ); + $log->set_title( sprintf( __( 'Filter "%1$s" was updated in repository level', 'tainacan'), $entity->get_name() ) ); } elseif ($this->current_action == 'create') { $log->set_title( sprintf( __( 'Filter "%1$s" was added to the repository', 'tainacan'), $entity->get_name() ) ); } } elseif ( is_numeric($collection_id) ) { if ($this->current_action == 'update') { - $log->set_title( sprintf( __( 'Filter "%s" was updated in Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + $log->set_title( sprintf( __( 'Filter "%1$s" was updated in Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); } elseif ($this->current_action == 'create') { $log->set_title( sprintf( __( 'Filter "%1$s" was added to Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); } @@ -738,12 +738,81 @@ class Logs extends Repository { $collection_id = method_exists($entity, 'get_collection_id') ? $entity->get_collection_id() : 'default'; if ( $entity instanceof Entities\Collection ) { + $collection_id = $entity->get_id(); - } - if ( $entity instanceof Entities\Item ) { + + if ($this->current_action == 'delete') { + $log->set_title( sprintf( __( 'Collection "%s" was permanently deleted', 'tainacan'), $entity->get_name() ) ); + } elseif ($this->current_action == 'trash') { + $log->set_title( sprintf( __( 'Collection "%s" was moved to trash', 'tainacan'), $entity->get_name() ) ); + } + + } elseif ( $entity instanceof Entities\Item ) { + $log->set_item_id($entity->get_id()); + + if ($this->current_action == 'delete') { + $log->set_title( sprintf( __( 'Item "%1$s" (ID %2$s) document was updated', 'tainacan'), $entity->get_title(), $entity->get_id() ) ); + } elseif ($this->current_action == 'trash') { + $log->set_title( sprintf( __( 'Item "%1$s" (ID %2$s) was moved to trash', 'tainacan'), $entity->get_title(), $entity->get_id() ) ); + } + } elseif ( $entity instanceof Entities\Filter ) { + + if ( 'default' == $collection_id ) { + if ($this->current_action == 'delete') { + $log->set_title( sprintf( __( 'Filter "%s" was permanently deleted from the repository', 'tainacan'), $entity->get_name() ) ); + } elseif ($this->current_action == 'trash') { + $log->set_title( sprintf( __( 'Repository Filter "%1$s" was moved to trash', 'tainacan'), $entity->get_name() ) ); + } + } elseif ( is_numeric($collection_id) ) { + if ($this->current_action == 'delete') { + $log->set_title( sprintf( __( 'Filter "%1$s" was permanently deleted from Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + } elseif ($this->current_action == 'trash') { + $log->set_title( sprintf( __( 'Filter "%1$s" was moved to trash in Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + } + } + + } elseif ( $entity instanceof Entities\Metadatum ) { + + if ( 'default' == $collection_id ) { + if ($this->current_action == 'delete') { + $log->set_title( sprintf( __( 'Metadatum "%s" was permanently deleted from the repository', 'tainacan'), $entity->get_name() ) ); + } elseif ($this->current_action == 'trash') { + $log->set_title( sprintf( __( 'Repository Metadatum "%1$s" was moved to trash', 'tainacan'), $entity->get_name() ) ); + } + } elseif ( is_numeric($collection_id) ) { + if ($this->current_action == 'delete') { + $log->set_title( sprintf( __( 'Metadatum "%1$s" was permanently deleted from Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + } elseif ($this->current_action == 'trash') { + $log->set_title( sprintf( __( 'Metadatum "%1$s" was moved to trash in Collection "%2$s"', 'tainacan'), $entity->get_name(), $entity->get_collection()->get_name() ) ); + } + } + + } elseif ( $entity instanceof Entities\Taxonomy ) { + + if ($this->current_action == 'delete') { + $log->set_title( sprintf( __( 'Taxonomy "%s" was permanently deleted', 'tainacan'), $entity->get_name() ) ); + } elseif ($this->current_action == 'trash') { + $log->set_title( sprintf( __( 'Taxonomy "%1$s" was moved to trash', 'tainacan'), $entity->get_name() ) ); + } + + } elseif ( $entity instanceof Entities\Term ) { + + $taxonomy = Taxonomies::get_instance()->fetch_by_db_identifier($entity->get_taxonomy()); + $tax_name = ''; + if ($taxonomy instanceof Entities\Taxonomy) { + $tax_name = $taxonomy->get_name(); + } + + if ($this->current_action == 'delete') { + $log->set_title( sprintf( __( 'Term "%1$s" was permanently deleted from "%2$s" taxonomy', 'tainacan'), $entity->get_name(), $tax_name ) ); + } elseif ($this->current_action == 'trash') { + $log->set_title( sprintf( __( 'Term "%1$s" was moved to trash in "%2$s" taxonomy', 'tainacan'), $entity->get_name(), $tax_name ) ); + } + } + $object_type = get_class($entity); $object_id = $entity->get_id(); @@ -787,6 +856,13 @@ class Logs extends Repository { $log->set_new_value($diff['new']); $log->set_action($this->current_action); + $meta_name = $entity->get_metadatum()->get_name(); + $item_title = $entity->get_item()->get_title(); + + $title = sprintf( __( 'Value for %1$s metadatum was updated in item "%2$s"', 'tainacan' ), $meta_name, $item_title ); + + $log->set_title($title); + if ( $log->validate() ) { $this->insert($log); } From c3f97cf61b9410b54f87189b6e4c1a8d0ab3787e Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Fri, 13 Sep 2019 16:49:59 -0300 Subject: [PATCH 007/198] Cleaning up old methods #261 --- .../repositories/class-tainacan-logs.php | 234 +++--------------- .../class-tainacan-taxonomies.php | 10 +- .../repositories/class-tainacan-terms.php | 15 +- 3 files changed, 47 insertions(+), 212 deletions(-) diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 56d5a4391..2729a0057 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -32,7 +32,7 @@ class Logs extends Repository { protected function __construct() { parent::__construct(); - add_action( 'tainacan-pre-insert', array( $this, 'prepare_entity_diff' ) ); + add_action( 'tainacan-pre-insert', array( $this, 'pre_insert_entity' ) ); add_action( 'tainacan-insert', array( $this, 'insert_entity' ) ); add_action( 'tainacan-deleted', array( $this, 'delete_entity' ), 10, 2 ); @@ -59,13 +59,6 @@ class Logs extends Repository { 'type' => 'string', 'description' => __( 'The log date', 'tainacan' ), ], - // 'parent' => [ - // 'map' => 'parent', - // 'title' => __( 'Parent', 'tainacan' ), - // 'type' => 'string', - // 'description' => __( 'Log order' ), - // 'validation' => '' - // ], 'description' => [ 'map' => 'post_content', 'title' => __( 'Description', 'tainacan' ), @@ -230,7 +223,11 @@ class Logs extends Repository { public function update( $object, $new_values = null ) { return $this->insert( $object ); } - + + /** + * Feth most recent log + * @return Entities\Log The most recent Log entity + */ public function fetch_last() { $args = [ 'post_type' => Entities\Log::get_post_type(), @@ -244,18 +241,18 @@ class Logs extends Repository { return array_pop( $logs ); } - + /** + * Callback to generate log when attachments are added to any Tainacan entity + */ public function insert_attachment( $post_ID ) { $attachment = get_post( $post_ID ); $post = $attachment->post_parent; - if ( $post ) { - // was added attachment on a tainacan object + if ( $post ) { // attached to a post $entity = Repository::get_entity_by_post( $post ); - if ( $entity ) { - // was added a normal attachment + if ( $entity ) { // attached to a tainacan entity $log = new Entities\Log(); @@ -302,6 +299,9 @@ class Logs extends Repository { } + /** + * Callback to generate log when attachments attached to any Tainacan entity are deleted + */ public function pre_delete_attachment($attachment_id) { $attachment_post = get_post($attachment_id); @@ -309,13 +309,11 @@ class Logs extends Repository { $entity_post = get_post($attachment_post->post_parent); if ( $entity_post ) { - // was added attachment on a tainacan object $entity = Repository::get_entity_by_post( $entity_post ); if ( $entity ) { - $collection_id = method_exists($entity, 'get_collection_id') ? $entity->get_collection_id() : 'default'; $log = new Entities\Log(); @@ -351,6 +349,9 @@ class Logs extends Repository { } } + /** + * Callback to generate log when attachments attached to any Tainacan entity are deleted + */ public function delete_attachment($attachment_id) { if ( isset($this->current_attachment_delete_log) && $this->current_attachment_delete_log instanceof Entities\Log ) { $log = $this->current_attachment_delete_log; @@ -361,95 +362,6 @@ class Logs extends Repository { } } - private function prepare_attachments($attachments) { - $attachments_prepared = []; - if ( is_array($attachments) ) { - foreach ( $attachments as $attachment ) { - $prepared = [ - 'id' => $attachment->ID, - 'title' => $attachment->post_title, - 'description' => $attachment->post_content, - 'mime_type' => $attachment->post_mime_type, - 'url' => wp_get_attachment_url($attachment->ID), - ]; - - array_push( $attachments_prepared, $prepared ); - } - } - return $attachments_prepared; - } - - /** - * Insert a log when a new entity is inserted - * - * @param Entity $value - * @param array $diffs - * @param bool $is_update - * - * @param bool $is_delete - * @param bool $is_trash - * - * @return Entities\Log|bool new created log - */ - public function insert_log( $value, $diffs = [], $is_update = false, $is_delete = false, $is_trash = false ) { - $title = null; - $description = null; - - if ( is_object( $value ) ) { - // do not log a log - if ( ( method_exists( $value, 'get_post_type' ) && $value->get_post_type() === 'tainacan-log' ) || $value->get_status() === 'auto-draft' ) { - return false; - } - - if ( $value instanceof Entities\Metadatum ) { - $type = $value->get_metadata_type(); - - if ( $type === 'Tainacan\Metadata_Types\Core_Title' || $type === 'Tainacan\Metadata_Types\Core_Description' ) { - return false; - } - } - - $type = get_class( $value ); - $class_name = explode( '\\', $type )[2]; - - $name = method_exists( $value, 'get_name' ) ? $value->get_name() : - ( method_exists( $value, 'get_title' ) ? $value->get_title() : $value->get_metadatum()->get_name() ); - - if ( ! $name ) { - $name = $value->get_status(); - } - - if ( $is_update ) { - // entity was delete - $title = $this->prepare_event_title( $value, $name, $class_name, 'updated' ); - - $description = $title; - } elseif ( $is_delete ) { - // entity was deleted - $title = $this->prepare_event_title( $value, $name, $class_name, 'deleted' ); - - $description = $title; - } elseif ( ! empty( $diffs ) ) { - // entity was created - $title = $this->prepare_event_title( $value, $name, $class_name, 'created' ); - - $description = $title; - } elseif ( $is_trash ) { - // entity was trashed - $title = $this->prepare_event_title( $value, $name, $class_name, 'trashed' ); - - $description = $title; - } - - $title = apply_filters( 'tainacan-insert-log-message-title', $title, $type, $value ); - $description = apply_filters( 'tainacan-insert-log-description', $description, $type, $value ); - } - - if ( !empty( $diffs ) || $is_delete || $is_trash) { - return Entities\Log::create( $title, $description, $value, $diffs ); - } - } - /** * Compare two repository entities and sets the current_diff property to be used in the insert hook * @@ -457,7 +369,7 @@ class Logs extends Repository { * * @return void */ - public function prepare_entity_diff( Entities\Entity $unsaved ) { + public function pre_insert_entity( Entities\Entity $unsaved ) { if ( ! $unsaved->get_repository()->use_logs ) { return; @@ -465,8 +377,6 @@ class Logs extends Repository { if ( $unsaved instanceof Entities\Item_Metadata_Entity ) { return $this->prepare_item_metadata_diff($unsaved); - } elseif ( $unsaved instanceof Entities\Term ) { - return $this->prepare_term_diff($unsaved); } // do not log a log @@ -475,7 +385,12 @@ class Logs extends Repository { } $creating = true; - $old = $unsaved->get_repository()->fetch( $unsaved->get_id() ); + + if ( $unsaved instanceof Entities\Term ) { + $old = $unsaved->get_repository()->fetch( $unsaved->get_id(), $unsaved->get_taxonomy() ); + } else { + $old = $unsaved->get_repository()->fetch( $unsaved->get_id() ); + } if ( $old instanceof Entities\Entity ) { @@ -496,7 +411,6 @@ class Logs extends Repository { $map = $unsaved->get_repository()->get_map(); foreach ( $map as $prop => $mapped ) { - // I can't verify differences on item, because it attributes are added when item is a auto-draft if ( $old->get( $prop ) != $unsaved->get( $prop ) ) { $diff['old'][$prop] = $old->get( $prop ); @@ -513,46 +427,6 @@ class Logs extends Repository { } - private function prepare_term_diff( Entities\Term $unsaved ) { - - $creating = true; - $old = $unsaved->get_repository()->fetch( $unsaved->get_id(), $unsaved->get_taxonomy() ); - - if ( $old instanceof Entities\Entity ) { - - if ( $old->get_status() !== 'auto-draft' ) { - $creating = false; - } - - } - - $diff = [ - 'old' => [], - 'new' => [] - ]; - - if ( $creating ) { - $diff['new'] = $unsaved->_toArray(); - } else { - $map = $unsaved->get_repository()->get_map(); - - foreach ( $map as $prop => $mapped ) { - // I can't verify differences on item, because it attributes are added when item is a auto-draft - if ( $old->get( $prop ) != $unsaved->get( $prop ) ) { - - $diff['old'][$prop] = $old->get( $prop ); - $diff['new'][$prop] = $unsaved->get( $prop ); - - } - } - } - - $diff = apply_filters( 'tainacan-entity-diff', $diff, $unsaved, $old ); - - $this->current_diff = $diff; - $this->current_action = $creating ? 'create' : 'update'; - - } private function prepare_item_metadata_diff( Entities\Entity $unsaved ) { @@ -576,6 +450,9 @@ class Logs extends Repository { } + /** + * Callback to generate log when Tainacan entities are edited + */ public function insert_entity( Entities\Entity $entity ) { if ( ! $entity->get_repository()->use_logs ) { @@ -584,9 +461,7 @@ class Logs extends Repository { if ( $entity instanceof Entities\Item_Metadata_Entity ) { return $this->insert_item_metadata($entity); - } elseif ( $entity instanceof Entities\Term ) { - //return $this->insert_term($entity); - } + } // do not log a log if ( ( method_exists( $entity, 'get_post_type' ) && $entity->get_post_type() === 'tainacan-log' ) || $entity->get_status() === 'auto-draft' ) { @@ -700,12 +575,6 @@ class Logs extends Repository { return; } - if ( $entity instanceof Entities\Item_Metadata_Entity ) { - //return $this->insert_item_metadata($entity); - } elseif ( $entity instanceof Entities\Term ) { - //return $this->insert_term($entity); - } - // do not log a log if ( ( method_exists( $entity, 'get_post_type' ) && $entity->get_post_type() === 'tainacan-log' ) || $entity->get_status() === 'auto-draft' ) { return false; @@ -722,12 +591,6 @@ class Logs extends Repository { return; } - if ( $entity instanceof Entities\Item_Metadata_Entity ) { - //return $this->insert_item_metadata($entity); - } elseif ( $entity instanceof Entities\Term ) { - //return $this->insert_term($entity); - } - // do not log a log if ( ( method_exists( $entity, 'get_post_type' ) && $entity->get_post_type() === 'tainacan-log' ) || $entity->get_status() === 'auto-draft' ) { return false; @@ -869,47 +732,6 @@ class Logs extends Repository { } - /** - * This will prepare the event title for objects - * - * @param $object - * @param $name - * @param $class_name - * - * @param $action_message - * - * @return string - */ - private function prepare_event_title( $object, $name, $class_name, $action_message ) { - - // translators: 1=Object name, 2=Object type, 3=Action. e.g. The "Subject" taxonomy has been created - $title_format = __( '"%1$s" %2$s has been %3$s', 'tainacan' ); - - if ( $object instanceof Entities\Metadatum || $object instanceof Entities\Item || $object instanceof Entities\Filter ) { - $collection = $object->get_collection(); - - if ( $collection ) { - $parent = sprintf( __('(collection: %s)', 'tainacan'), $collection->get_name() ); - } else { - $parent = __('(on repository level)', 'tainacan'); - } - - $title = sprintf( $title_format, $name, strtolower( $class_name ), $action_message ); - $title .= ' ' . $parent . '.'; - } elseif($object instanceof Entities\Item_Metadata_Entity) { - $title = sprintf( - $title_format, - $name, - __('item metadatum', 'tainacan'), - $action_message - ) . ' ' . sprintf( __('(item: %s)', 'tainacan'), $object->get_item()->get_title() ) . '.'; - } else { - $title = sprintf( $title_format, $name, strtolower( $class_name ), $action_message ) . '.'; - } - - return $title; - } - /** * * @param Entities\Log $log diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index 72743b66f..722e86f88 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -208,7 +208,7 @@ class Taxonomies extends Repository { * @param array $args WP_Query args plus disabled_metadata * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) * - * @return array Entities\Metadatum + * @return array Entities\Taxonomy * @throws \Exception */ public function fetch_by_collection( Entities\Collection $collection, $args = [], $output = null ) { @@ -241,6 +241,14 @@ class Taxonomies extends Repository { } + /** + * fetch taxonomies by DB Identifier + * + * @param string $db_identifier The db Identifier of the taxonomy. This is the internal WordPress taxonomy slug, something like tnc_123_tax + * + * @return Entities\Taxonomy|Array The entity when found. An empty array when nothing was found + * @throws \Exception + */ public function fetch_by_db_identifier($db_identifier) { $id = $this->get_id_by_db_identifier($db_identifier); if ($id) { diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 3568d04be..698a5aa37 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -250,18 +250,23 @@ class Terms extends Repository { /** * @param Entities\Term $term - * @param bool $permanent this parameter is not used by Terms repository + * @param bool $permanent this parameter is not used by Terms repository. Delete is always permanent * * @return bool|int|mixed|\WP_Error */ public function delete( Entities\Entity $term, $permanent = true ) { $deleted = $term; + + $permanent = true; // there is no such option for terms + + do_action( 'tainacan-pre-delete', $deleted, $permanent ); + do_action( 'tainacan-pre-delete-term', $deleted, $permanent ); + $return = wp_delete_term( $term->get_id(), $term->get_taxonomy() ); - if ( $deleted && $this->use_logs ) { - $this->logs_repository->insert_log( $deleted, [], false, true ); - do_action( 'tainacan-deleted', $deleted ); - do_action( 'tainacan-deleted-term', $deleted ); + if ( $deleted ) { + do_action( 'tainacan-deleted', $deleted, $permanent ); + do_action( 'tainacan-deleted-term', $deleted, $permanent ); } return $return; From 5dc058a787b3e83282b6110e5aa0370d9d257489 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Fri, 13 Sep 2019 19:25:42 -0300 Subject: [PATCH 008/198] Clear tests #261 --- .../text/class-tainacan-text.php | 2 +- .../repositories/class-tainacan-logs.php | 37 +--- tests/_test-api-item-metadata.php | 59 ++++++ tests/_test-logs.php | 21 ++ tests/test-api-item-metadata.php | 188 ------------------ tests/test-api-items.php | 9 +- tests/test-collections.php | 28 --- tests/test-logs.php | 72 ------- 8 files changed, 96 insertions(+), 320 deletions(-) create mode 100644 tests/_test-api-item-metadata.php create mode 100644 tests/_test-logs.php delete mode 100644 tests/test-api-item-metadata.php delete mode 100644 tests/test-logs.php diff --git a/src/classes/metadata-types/text/class-tainacan-text.php b/src/classes/metadata-types/text/class-tainacan-text.php index 31e3d2390..552da64a7 100644 --- a/src/classes/metadata-types/text/class-tainacan-text.php +++ b/src/classes/metadata-types/text/class-tainacan-text.php @@ -45,7 +45,7 @@ class Text extends Metadata_Type { public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { $value = $item_metadata->get_value(); $return = ''; - if ( $item_metadata->is_multiple() ) { + if ( is_array($value) && $item_metadata->is_multiple() ) { $total = sizeof($value); $count = 0; $prefix = $item_metadata->get_multivalue_prefix(); diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 2729a0057..08dd193af 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -386,12 +386,17 @@ class Logs extends Repository { $creating = true; - if ( $unsaved instanceof Entities\Term ) { - $old = $unsaved->get_repository()->fetch( $unsaved->get_id(), $unsaved->get_taxonomy() ); - } else { - $old = $unsaved->get_repository()->fetch( $unsaved->get_id() ); + $old = null; + + if ( is_numeric( $unsaved->get_id() ) ) { + if ( $unsaved instanceof Entities\Term ) { + $old = $unsaved->get_repository()->fetch( $unsaved->get_id(), $unsaved->get_taxonomy() ); + } else { + $old = $unsaved->get_repository()->fetch( $unsaved->get_id() ); + } } + if ( $old instanceof Entities\Entity ) { if ( $old->get_status() !== 'auto-draft' ) { @@ -732,28 +737,4 @@ class Logs extends Repository { } - /** - * - * @param Entities\Log $log - * - * @return Entities\Entity|boolean return insert/update valeu or false - * @throws \Exception - */ - public function approve( $log ) { - $log = self::get_entity_by_post( $log ); - if ( $log->get_status() == 'pending' ) { - /** @var Entity $value * */ - $value = $log->get_value(); - - $value->set_status( 'publish' ); // TODO check if publish the entity on approve - - $repository = self::get_repository( $value ); - - if ( $value->validate() ) { - return $repository->insert( $value ); - } - } - - return false; - } } \ No newline at end of file diff --git a/tests/_test-api-item-metadata.php b/tests/_test-api-item-metadata.php new file mode 100644 index 000000000..0cf82f4ff --- /dev/null +++ b/tests/_test-api-item-metadata.php @@ -0,0 +1,59 @@ +tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'testeItemMetadata', + 'description' => 'No description', + ), + true, + true + ); + + $type = $this->tainacan_metadatum_factory->create_metadatum('text'); + + $metadatum = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'teste_metadado', + 'description' => 'descricao', + 'collection' => $collection, + 'metadata_type' => $type, + 'accept_suggestion' => true + ), + true, + true + ); + + $item = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'item_teste_metadado', + 'description' => 'adasdasdsa', + 'collection' => $collection + ), + true, + true + ); + $this->collection = $collection; + $this->item = $item; + $this->metadatum = $metadatum; + return ['collection' => $collection, 'item' => $item, 'metadatum' => $metadatum]; + } + + + +} + +?> \ No newline at end of file diff --git a/tests/_test-logs.php b/tests/_test-logs.php new file mode 100644 index 000000000..aab578492 --- /dev/null +++ b/tests/_test-logs.php @@ -0,0 +1,21 @@ +tainacan_entity_factory->create_entity( - 'collection', - array( - 'name' => 'testeItemMetadata', - 'description' => 'No description', - ), - true, - true - ); - - $type = $this->tainacan_metadatum_factory->create_metadatum('text'); - - $metadatum = $this->tainacan_entity_factory->create_entity( - 'metadatum', - array( - 'name' => 'teste_metadado', - 'description' => 'descricao', - 'collection' => $collection, - 'metadata_type' => $type, - 'accept_suggestion' => true - ), - true, - true - ); - - $item = $this->tainacan_entity_factory->create_entity( - 'item', - array( - 'title' => 'item_teste_metadado', - 'description' => 'adasdasdsa', - 'collection' => $collection - ), - true, - true - ); - $this->collection = $collection; - $this->item = $item; - $this->metadatum = $metadatum; - return ['collection' => $collection, 'item' => $item, 'metadatum' => $metadatum]; - } - - public function test_create_suggestion_item_metadata_in_a_collection(){ - - extract($this->create_meta_requirements()); - - $item__metadata_json = json_encode([ - 'values' => 'TestValues_metadado', - ]); - - $request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() ); - $request->set_body($item__metadata_json); - - $response = $this->server->dispatch($request); - - $this->assertEquals(200, $response->get_status()); - - $data = $response->get_data(); - - $this->assertEquals($item->get_id() , $data['item']['id']); - $this->assertEquals('TestValues_metadado', $data['value']); - - $request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $metadatum->get_id() ); - $response = $this->server->dispatch($request); - $this->assertEquals(200, $response->get_status()); - $data = $response->get_data(); - $this->assertEquals( 'TestValues_metadado', $data['value'] ); - - // Test Suggestion - $new_user = $this->factory()->user->create(array( 'role' => 'subscriber' )); - wp_set_current_user($new_user); - - $item__metadata_json = json_encode([ - 'values' => 'TestValuesSuggestion_metadado', - ]); - - $request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() ); - $request->set_body($item__metadata_json); - $response = $this->server->dispatch($request); - - $this->assertEquals(200, $response->get_status()); - $data = $response->get_data(); - $this->assertEquals( 'pending', $data['status'] ); - $Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance(); - $query = $Tainacan_Logs->fetch(['post_status' => 'pending']); - - $log = false; - while ($query->have_posts()) { - $query->the_post(); - $post = get_post(); - $log = $Tainacan_Logs->get_entity_by_post($post); - } - - $pending = $log->get_value(); - - $this->assertEquals('TestValuesSuggestion_metadado', $pending->get_value()); - - wp_set_current_user($this->user_id); - - $request = new \WP_REST_Request('POST', $this->namespace . '/logs/' . $log->get_id() . '/approve' ); - $response = $this->server->dispatch($request); - - $this->assertEquals(200, $response->get_status()); - - $request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $metadatum->get_id() ); - $response = $this->server->dispatch($request); - $this->assertEquals(200, $response->get_status()); - $data = $response->get_data(); - $this->assertEquals( 'TestValuesSuggestion_metadado', $data['value'] ); - - } - - function test_create_anonymous_suggestion_item_metadata_in_a_collection() { - extract($this->create_meta_requirements()); - - $item__metadata_json = json_encode([ - 'values' => 'TestValues_metadado', - ]); - - $request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() ); - $request->set_body($item__metadata_json); - - $response = $this->server->dispatch($request); - - $this->assertEquals(200, $response->get_status()); - - // Test Anonymous Suggestion - wp_logout(); - wp_set_current_user(0); - - $this->assertEquals(0, get_current_user_id()); - - $item__metadata_json = json_encode([ - 'values' => 'TestValuesAnonymousSuggestion_metadado', - ]); - - $request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $metadatum->get_id() ); - $request->set_body($item__metadata_json); - $response = $this->server->dispatch($request); - - $this->assertEquals(200, $response->get_status()); - $data = $response->get_data(); - $this->assertEquals( 'pending', $data['status'] ); - $Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance(); - $query = $Tainacan_Logs->fetch(['post_status' => 'pending']); - - $log = false; - while ($query->have_posts()) { - $query->the_post(); - $post = get_post(); - $log = $Tainacan_Logs->get_entity_by_post($post); - } - - $pending = $log->get_value(); - - $this->assertEquals('TestValuesAnonymousSuggestion_metadado', $pending->get_value()); - - wp_set_current_user($this->user_id); - - $request = new \WP_REST_Request('POST', $this->namespace . '/logs/' . $log->get_id() . '/approve' ); - $response = $this->server->dispatch($request); - - $this->assertEquals(200, $response->get_status()); - - $request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $metadatum->get_id() ); - $response = $this->server->dispatch($request); - $this->assertEquals(200, $response->get_status()); - $data = $response->get_data(); - $this->assertEquals( 'TestValuesAnonymousSuggestion_metadado', $data['value'] ); - - } - - -} - -?> \ No newline at end of file diff --git a/tests/test-api-items.php b/tests/test-api-items.php index f9a1f93c2..a4ec553ea 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -72,7 +72,6 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(200, $response->get_status()); $data = $response->get_data()['items']; - $items_titles = [$data[0]['title'], $data[1]['title']]; $this->assertContains($item1->get_title(), $items_titles); @@ -155,7 +154,11 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { } public function test_update_item(){ - $collection = $this->tainacan_entity_factory->create_entity('collection', '', true); + $collection = $this->tainacan_entity_factory->create_entity('collection', array( + 'name' => 'Agile', + 'description' => 'Agile methods', + 'status' => 'publish' + ), true); $item = $this->tainacan_entity_factory->create_entity( 'item', @@ -163,6 +166,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { 'title' => 'SCRUM e PMBOK', 'description' => 'Unidos no Gerenciamento de Projetos', 'collection' => $collection, + 'status' => 'publish' ), true ); @@ -181,7 +185,6 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $data = $response->get_data(); - $this->assertNotEquals($item->get_title(), $data['title']); $this->assertEquals('SCRUM e XP', $data['title']); } diff --git a/tests/test-collections.php b/tests/test-collections.php index 1cc82dc6b..7e4b3e99f 100644 --- a/tests/test-collections.php +++ b/tests/test-collections.php @@ -318,34 +318,6 @@ class Collections extends TAINACAN_UnitTestCase { $this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!'); } - /** - * @group diff - */ - function test_diff() { - $x = $this->tainacan_entity_factory->create_entity( - 'collection', - array( - 'name' => 'testeDiff', - 'description' => 'adasdasdsa', - 'default_order' => 'DESC', - 'moderators_ids' => [1,2,3] - ), - true - ); - - $x->set_name('OtherValue'); - $x->set_description('testeDiff2'); - $x->set_moderators_ids([3,4,5]); - - $diff = $x->diff(); - $this->assertEquals(3, count($diff)); - $this->assertEquals($diff['name']['new'][0], 'OtherValue'); - $this->assertEquals($diff['name']['old'], 'testeDiff'); - $this->assertEquals($diff['description']['new'][0], 'testeDiff2'); - $this->assertEquals($diff['description']['old'], 'adasdasdsa'); - $this->assertEquals([1 => 4, 2 => 5, 0 => 3], $diff['moderators_ids']['diff_with_index']); - - } function test_create_child_collection() { diff --git a/tests/test-logs.php b/tests/test-logs.php deleted file mode 100644 index 695a3143e..000000000 --- a/tests/test-logs.php +++ /dev/null @@ -1,72 +0,0 @@ -tainacan_entity_factory->create_entity( - 'log', - array( - 'title' => 'blame someone', - 'description' => 'someone did that' - ), - true - ); - - $user_id = get_current_user_id(); - $blog_id = get_current_blog_id(); - - //retorna a taxonomia - $test = $Tainacan_Logs->fetch( $log->get_id() ); - - $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() ); - } - - public function test_log_diff() { - $Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance(); - $Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance(); - - $filter = $this->tainacan_entity_factory->create_entity( - 'filter', - array( - 'name' => 'No name', - ), - true - ); - - // Modify filter name - $filter->set_name( 'With name' ); - - $Tainacan_Filters->update( $filter ); - - $log = $Tainacan_Logs->fetch_last(); - - $diff = $log->get_log_diffs(); - - $this->assertEquals( 'With name', "{$diff['name']['new'][0]} {$diff['name']['new'][1]}" ); - $this->assertEquals( 'No name', $diff['name']['old'] ); - $this->assertEquals( 'With', $diff['name']['diff_with_index'][0] ); - } -} \ No newline at end of file From c6d6346e8d8bdd98cb1bce60f0eaa263ed41ba3b Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Fri, 13 Sep 2019 19:25:50 -0300 Subject: [PATCH 009/198] Refactor Logs API #261 --- .../class-tainacan-rest-logs-controller.php | 235 +++++------------- 1 file changed, 64 insertions(+), 171 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-logs-controller.php b/src/api/endpoints/class-tainacan-rest-logs-controller.php index 26379c0f6..4f425eb6c 100644 --- a/src/api/endpoints/class-tainacan-rest-logs-controller.php +++ b/src/api/endpoints/class-tainacan-rest-logs-controller.php @@ -17,12 +17,6 @@ class REST_Logs_Controller extends REST_Controller { public function __construct() { $this->rest_base = 'logs'; parent::__construct(); - add_action('init', array($this, 'init_objects')); - } - - public function init_objects(){ - $this->logs_repository = Repositories\Logs::get_instance(); - $this->log = new Entities\Log(); } public function register_routes() { @@ -32,7 +26,7 @@ class REST_Logs_Controller extends REST_Controller { 'methods' => \WP_REST_Server::READABLE, 'callback' => array($this, 'get_items'), 'permission_callback' => array($this, 'get_items_permissions_check'), - 'args' => $this->get_collection_params() + 'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::READABLE) ) ) ); @@ -46,15 +40,6 @@ class REST_Logs_Controller extends REST_Controller { ) ) ); - register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P[\d]+)/approve', - array( - array( - 'methods' => \WP_REST_Server::EDITABLE, - 'callback' => array($this, 'approve_item'), - 'permission_callback' => array($this, 'approve_item_permissions_check'), - ) - ) - ); register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base, array( array( @@ -75,6 +60,46 @@ class REST_Logs_Controller extends REST_Controller { ) ) ); + register_rest_route($this->namespace, '/filter/(?P[\d]+)/' . $this->rest_base, + array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check'), + 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::READABLE) + ) + ) + ); + register_rest_route($this->namespace, '/metadatum/(?P[\d]+)/' . $this->rest_base, + array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check'), + 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::READABLE) + ) + ) + ); + register_rest_route($this->namespace, '/taxonomy/(?P[\d]+)/' . $this->rest_base, + array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check'), + 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::READABLE) + ) + ) + ); + register_rest_route($this->namespace, '/term/(?P[\d]+)/' . $this->rest_base, + array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check'), + 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::READABLE) + ) + ) + ); } /** @@ -88,10 +113,6 @@ class REST_Logs_Controller extends REST_Controller { if(!isset($request['fetch_only'])) { $item_array = $item->_toArray(); - - unset($item_array['value']); - unset($item_array['old_value']); - return $item_array; } @@ -114,91 +135,28 @@ class REST_Logs_Controller extends REST_Controller { $args = $this->prepare_filters( $request ); - if ($request['item_id']){ - $item_id = $request['item_id']; - - $item_repository = Repositories\Items::get_instance(); - - $item = $item_repository->fetch($item_id); - - if(!$item){ - return new \WP_REST_Response([ - 'error_message' => __('An item with this ID does not exist', 'tainacan'), - 'item_id' => $item - ], 400); - } - - if($args && - array_key_exists('meta_query', $args) && - array_key_exists('relation', $args['meta_query'])){ - - $metaq = $args['meta_query']; - - unset($args['meta_query']); - - $args['meta_query'][] = $metaq; - $args['meta_query']['relation'] = 'AND'; - - } elseif($args && - array_key_exists('meta_query', $args)){ - $args['meta_query']['relation'] = 'AND'; - } - - $args = array_merge_recursive(array( - 'meta_query' => array( - 'item_clause' => array( - 'key' => 'item_id', - 'value' => $item_id, - 'compare' => '=' - ) - ) - ), $args); + if ($request['item_id']) { + $args['item_id'] = $request['item_id']; + } elseif ($request['collection_id']) { + $args['collection_id'] = $request['collection_id']; + } elseif ($request['filter_id']) { + $args['object_type'] = 'Tainacan\Entities\Filter'; + $args['object_id'] = $request['filter_id']; + } elseif ($request['metadatum_id']) { + $args['object_type'] = 'Tainacan\Entities\Metadatum'; + $args['object_id'] = $request['metadatum_id']; + } elseif ($request['taxonomy_id']) { + $args['object_type'] = 'Tainacan\Entities\Taxonomy'; + $args['object_id'] = $request['taxonomy_id']; + } elseif ($request['term_id']) { + $args['object_type'] = 'Tainacan\Entities\Term'; + $args['object_id'] = $request['term_id']; } - if($request['collection_id']){ - $collection_id = $request['collection_id']; - - $collection_repository = Repositories\Collections::get_instance(); - - $collection = $collection_repository->fetch($collection_id); - - if(!$collection){ - return new \WP_REST_Response([ - 'error_message' => __('A collection with this ID does not exist', 'tainacan'), - 'collection_id' => $collection_id - ], 400); - } - - if($args && - array_key_exists('meta_query', $args) && - array_key_exists('relation', $args['meta_query'])){ - - $metaq = $args['meta_query']; - - unset($args['meta_query']); - - $args['meta_query'][] = $metaq; - $args['meta_query']['relation'] = 'AND'; - - } elseif($args && - array_key_exists('meta_query', $args)){ - $args['meta_query']['relation'] = 'AND'; - } - - $args = array_merge_recursive(array( - 'meta_query' => array( - 'collection_clause' => array( - 'key' => 'collection_id', - 'value' => $collection_id, - 'compare' => '=' - ) - ) - ), $args); - } - - $logs = $this->logs_repository->fetch($args); - + $logs = Repositories\Logs::get_instance()->fetch($args); + $response = []; + if($logs->have_posts()){ while ($logs->have_posts()){ $logs->the_post(); @@ -228,6 +186,7 @@ class REST_Logs_Controller extends REST_Controller { * @return bool|\WP_Error */ public function get_items_permissions_check( $request ) { + return true; return current_user_can('read'); } @@ -239,7 +198,7 @@ class REST_Logs_Controller extends REST_Controller { public function get_item( $request ) { $log_id = $request['log_id']; - $log = $this->logs_repository->fetch($log_id); + $log = Repositories\Logs::get_instance()->fetch($log_id); $prepared_log = $this->prepare_item_for_response( $log, $request ); @@ -252,7 +211,8 @@ class REST_Logs_Controller extends REST_Controller { * @return bool|\WP_Error */ public function get_item_permissions_check( $request ) { - $log = $this->logs_repository->fetch($request['log_id']); + return true; + $log = Repositories\Logs::get_instance()->fetch($request['log_id']); if(($log instanceof Entities\Log)) { if('edit' === $request['context'] && !$log->can_read()) { @@ -265,51 +225,7 @@ class REST_Logs_Controller extends REST_Controller { return false; } - /** - * @param \WP_REST_Request $request - * - * @return bool|\WP_Error - */ - public function approve_item_permissions_check( $request ) { - $log = $this->logs_repository->fetch($request['log_id']); - - if($log instanceof Entities\Log){ - if($log->can_read()) { - $entity = $log->get_value(); - - if($entity instanceof Entities\Entity) { - if($entity instanceof Entities\Item_Metadata_Entity) { - $item = $entity->get_item(); - return $item->can_edit(); - } // TODO for other entities types - else { - return $entity->can_edit(); - } - } - - return new \WP_Error(); - } - } - - return false; - } - /** - * approve a logged modification - * @param \WP_REST_Request $request - * - * @return \WP_Error|\WP_REST_Response - */ - public function approve_item($request) { - $log = $this->logs_repository->fetch($request['log_id']); - - if($log instanceof Entities\Log){ - $entity = $log->approve(); - $prepared_entity = $this->prepare_item_for_response( $entity, $request ); - - return new \WP_REST_Response($prepared_entity, 200); - } - } /** * @param string $method @@ -328,29 +244,6 @@ class REST_Logs_Controller extends REST_Controller { return $endpoint_args; } - - /** - * - * Return the queries supported when getting a collection of objects - * - * @param null $object_name - * - * @return array - */ - public function get_collection_params($object_name = null) { - $query_params['context']['default'] = 'view'; - - $query_params = array_merge($query_params, parent::get_collection_params('log')); - - $query_params['title'] = array( - 'description' => __('Limits the result set to a log with a specific title'), - 'type' => 'string', - ); - - $query_params = array_merge($query_params, parent::get_meta_queries_params()); - - return $query_params; - } } ?> \ No newline at end of file From 5bd3c89a972028a9646df97374bbe028c7ec1646 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Fri, 13 Sep 2019 20:00:11 -0300 Subject: [PATCH 010/198] Enable logs for importers #261 --- .../repositories/class-tainacan-logs.php | 9 ++++++++- src/importer/class-tainacan-csv.php | 10 +++++++--- src/importer/class-tainacan-importer.php | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 08dd193af..90940d623 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -410,6 +410,8 @@ class Logs extends Repository { 'new' => [] ]; + $has_diff = false; + if ( $creating ) { $diff['new'] = $unsaved->_toArray(); } else { @@ -420,6 +422,7 @@ class Logs extends Repository { $diff['old'][$prop] = $old->get( $prop ); $diff['new'][$prop] = $unsaved->get( $prop ); + $has_diff = true; } } @@ -427,7 +430,7 @@ class Logs extends Repository { $diff = apply_filters( 'tainacan-entity-diff', $diff, $unsaved, $old ); - $this->current_diff = $diff; + $this->current_diff = $has_diff ? $diff : false; $this->current_action = $creating ? 'create' : 'update'; } @@ -479,6 +482,10 @@ class Logs extends Repository { $diff = $this->current_diff; + if (false === $diff) { + return; + } + if ( $entity instanceof Entities\Collection ) { $collection_id = $entity->get_id(); diff --git a/src/importer/class-tainacan-csv.php b/src/importer/class-tainacan-csv.php index a5c6d6b98..8221b6fc1 100644 --- a/src/importer/class-tainacan-csv.php +++ b/src/importer/class-tainacan-csv.php @@ -581,9 +581,13 @@ class CSV extends Importer { $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - $Tainacan_Items->disable_logs(); - $Tainacan_Metadata->disable_logs(); - $Tainacan_Item_Metadata->disable_logs(); + //$Tainacan_Items->disable_logs(); + //$Tainacan_Metadata->disable_logs(); + // When creating a new item, disable log for each metadata to speed things up + if ( ! is_numeric($this->get_transient('item_id')) ) { + $Tainacan_Item_Metadata->disable_logs(); + } + $item = new Entities\Item( ( is_numeric($this->get_transient('item_id')) ) ? $this->get_transient('item_id') : 0 ); $itemMetadataArray = []; diff --git a/src/importer/class-tainacan-importer.php b/src/importer/class-tainacan-importer.php index 9841b34dc..130b57c7f 100644 --- a/src/importer/class-tainacan-importer.php +++ b/src/importer/class-tainacan-importer.php @@ -159,8 +159,25 @@ abstract class Importer { } } + // add_filter('tainacan-entity-set-property', [$this, 'filter_log_title']); + } + // public function filter_log_title($title, $prop, $entity) { + // if ($entity instanceof Entities\Log && $prop == 'title') { + // global $Tainacan_Importer_Handler; + // $importer = $Tainacan_Importer_Handler->get_importer_by_object($this); + // if ($importer && isset($importer['name'])) { + // // translators: %s is the name of the importer. Ex: CSV Importer + // $_title = sprintf( __('%s Importer'), $importer['name'] ); + // } else { + // $_title = __('Importer'); + // } + // $title .= " ($_title)"; + // } + // return $title; + // } + public function _to_Array($short = false) { $return = ['id' => $this->get_id()]; foreach ($this->array_attributes as $attr) { From 4d2acfcf4e670290fb4f7ab7dde57c3fc8005a65 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 16 Sep 2019 19:48:27 -0300 Subject: [PATCH 011/198] allow terms fetch o get just an ID as paramater --- src/classes/repositories/class-tainacan-terms.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 698a5aa37..18888140b 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -235,8 +235,8 @@ class Terms extends Repository { } return $return; - } elseif ( is_numeric( $args ) && ! empty( $cpt ) && ! is_array( $cpt ) ) { // if an id is passed taxonomy cannot be an array - $wp_term = get_term_by( 'id', $args, $cpt ); + } elseif ( is_numeric( $args ) ) { + $wp_term = get_term( (int) $args, $cpt ); $tainacan_term = new Entities\Term( $wp_term ); return $tainacan_term; } else { From 92619becf508f6875f761616e6449efd3431d262 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 16 Sep 2019 19:48:39 -0300 Subject: [PATCH 012/198] cleaning legacy code #261 --- src/classes/entities/class-tainacan-log.php | 46 --------------------- 1 file changed, 46 deletions(-) diff --git a/src/classes/entities/class-tainacan-log.php b/src/classes/entities/class-tainacan-log.php index 5e16a014b..db9061b28 100644 --- a/src/classes/entities/class-tainacan-log.php +++ b/src/classes/entities/class-tainacan-log.php @@ -201,52 +201,6 @@ class Log extends Entity { public function get_log_diffs(){ return $this->get_mapped_property('log_diffs'); } - - /** - * - * @param bool $message - * @param string $desc - * @param null $value - * @param array $diffs - * @param string $status 'publish', 'private', 'pending', 'processing' or 'error' - * - * @return \Tainacan\Entities\Log | bool - * @throws \Exception - */ - public static function create( $message = false, $desc = '', $value = null, $diffs = [], $status = 'publish' ) { - - $log = new Log(); - - $log->set_title( $message ); - $log->set_description( $desc ); - $log->set_status( $status ); - $log->set_log_diffs( $diffs ); - - if(is_object($value) || is_string($value)) { - if(array_search( 'Tainacan\Traits\Entity_Collection_Relation', class_uses($value))) { - $log->set_collection_id( $value->get_collection_id() ); - } elseif($value instanceof Collection){ - $log->set_collection_id( $value->get_id()); - } elseif($value instanceof Item_Metadata_Entity){ - $log->set_item_id($value->get_item()->get_id()); - $log->set_collection_id($value->get_item()->get_collection_id()); - } - } - - if ( ! is_null( $value ) ) { - $log->set_value( $value ); - } elseif ( $message === false ) { - throw new \Exception( 'Message or value is needed to log' ); - } - - $Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance(); - - if ( $log->validate() ) { - return $Tainacan_Logs->insert( $log ); - } else { - throw new \Exception( 'Invalid log' ); - } - } public function get_object_type() { $this->get_mapped_property('object_type'); From e0212355193af0c409fc56182083cf0993b0ce51 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 16 Sep 2019 19:49:36 -0300 Subject: [PATCH 013/198] logs API response for single logs #261 --- .../class-tainacan-rest-logs-controller.php | 83 +++++++++++++++++-- .../repositories/class-tainacan-logs.php | 8 +- 2 files changed, 81 insertions(+), 10 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-logs-controller.php b/src/api/endpoints/class-tainacan-rest-logs-controller.php index 4f425eb6c..bc8ed19ee 100644 --- a/src/api/endpoints/class-tainacan-rest-logs-controller.php +++ b/src/api/endpoints/class-tainacan-rest-logs-controller.php @@ -111,18 +111,89 @@ class REST_Logs_Controller extends REST_Controller { public function prepare_item_for_response( $item, $request ) { if(!empty($item)){ - if(!isset($request['fetch_only'])) { - $item_array = $item->_toArray(); - return $item_array; - } + + if ($request['log_id']) { + + if ($item->get_log_diffs() && $item->get_value()) { + $item_array = $this->prepare_legacy_item_for_response($item, $request); + } else { + + $item_array = $item->_toArray(); + + $related_object = true; + + if ($item_array['item_id']) { + $item = Repositories\Items::get_instance()->fetch( (int) $item_array['item_id'] ); + if ($item instanceof Entities\Item ) { + $item_array['item'] = $item->_toArray(); + } + } + if ($item_array['collection_id']) { + $collection = Repositories\Collections::get_instance()->fetch( (int) $item_array['collection_id'] ); + if ($collection instanceof Entities\Item ) { + $item_array['collection'] = $collection->_toArray(); + } + } + + if ( $item_array['object_id'] && + ( isset($item_array['collection_id']) && $item_array['object_id'] != $item_array['collection_id'] ) && + ( isset($item_array['item_id']) && $item_array['object_id'] != $item_array['item_id'] ) ) { + + if ( $item_array['object_type'] == 'Tainacan\Entities\Term' ) { + $related_entity = Repositories\Terms::get_instance()->fetch( (int) $item_array['object_id'] ); + } else { + $related_post = get_post($item_array['object_id']); + $related_entity = Repository::get_entity_by_post( $related_post ); + } + + if ($related_entity instanceof Entities\Entity ) { + $item_array[ strtolower($related_entity->get_repository()->get_name()) ] = $related_entity->_toArray(); + } + + } + + if ( $item_array['action'] == 'new-attachment' ) { + if ( isset($item_array['new_value']['id']) ) { + $item_array['new_value']['url'] = wp_get_attachment_url($item_array['new_value']['id']); + $item_array['new_value']['thumb'] = wp_get_attachment_image_src($item_array['new_value']['id'], 'thumbnail'); + } + } elseif ( $item_array['action'] == 'update' && isset( $item_array['new_value']['document_type'] ) && $item_array['new_value']['document_type'] == 'attachment' ) { + if ( isset($item_array['new_value']['document']) ) { + $item_array['new_value']['url'] = wp_get_attachment_url($item_array['new_value']['document']); + $item_array['new_value']['thumb'] = wp_get_attachment_image_src($item_array['new_value']['document'], 'thumbnail'); + } + } + + + + + return $item_array; + + } + + + } else { + + if(!isset($request['fetch_only'])) { + $item_array = $item->_toArray(); + return $item_array; + } - $attributes_to_filter = $request['fetch_only']; + $attributes_to_filter = $request['fetch_only']; + + } + + return $this->filter_object_by_attributes($item, $attributes_to_filter); } return $item; } + + private function prepare_legacy_item_for_response($item, $request) { + return $item->_toArray(); + } /** * @param \WP_REST_Request $request @@ -186,7 +257,7 @@ class REST_Logs_Controller extends REST_Controller { * @return bool|\WP_Error */ public function get_items_permissions_check( $request ) { - return true; + //return true; return current_user_can('read'); } diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 90940d623..2c6ca5ed6 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -284,8 +284,6 @@ class Logs extends Repository { 'title' => $attachment->post_title, 'description' => $attachment->post_content, 'mime_type' => $attachment->post_mime_type, - 'url' => wp_get_attachment_url($attachment->ID), - 'thumb' => wp_get_attachment_image_src($attachment->ID, 'thumbnail'), ]; $log->set_new_value($prepared); @@ -454,7 +452,7 @@ class Logs extends Repository { $diff = apply_filters( 'tainacan-entity-diff', $diff, $unsaved, $old ); $this->current_diff = $diff; - $this->current_action = 'update'; + $this->current_action = 'update-metadata-value'; } @@ -477,6 +475,7 @@ class Logs extends Repository { } $log = new Entities\Log(); + $log->set_action($this->current_action); $collection_id = method_exists($entity, 'get_collection_id') ? $entity->get_collection_id() : 'default'; @@ -503,6 +502,7 @@ class Logs extends Repository { if ($this->current_action == 'update') { if (isset($diff['new']['document'])) { $log->set_title( sprintf( __( 'Item "%s" document was updated', 'tainacan'), $entity->get_title() ) ); + $log->set_action('update-document'); } else { $log->set_title( sprintf( __( 'Item "%s" was updated', 'tainacan'), $entity->get_title() ) ); } @@ -573,7 +573,7 @@ class Logs extends Repository { $log->set_object_id($object_id); $log->set_old_value($diff['old']); $log->set_new_value($diff['new']); - $log->set_action($this->current_action); + if ( $log->validate() ) { $this->insert($log); From 78a2f344ffde0cfc1c38c2a2d23310323bc04d25 Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Tue, 17 Sep 2019 09:45:31 -0300 Subject: [PATCH 014/198] Loads single activity on modal instead of passing it to list. #261. --- .../components/lists/activities-list.vue | 4 +- .../other/activity/activity-details-modal.vue | 66 +++++++++++++------ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/admin/components/lists/activities-list.vue b/src/admin/components/lists/activities-list.vue index c0f7c4eb6..a3b6553d8 100644 --- a/src/admin/components/lists/activities-list.vue +++ b/src/admin/components/lists/activities-list.vue @@ -125,7 +125,7 @@ diff --git a/src/js/store/modules/activity/actions.js b/src/js/store/modules/activity/actions.js index 42fe0d9db..756b262df 100644 --- a/src/js/store/modules/activity/actions.js +++ b/src/js/store/modules/activity/actions.js @@ -3,7 +3,7 @@ import axios from '../../../axios/axios' export const fetchActivities = ({ commit }, { page, activitiesPerPage } ) => { return new Promise((resolve, reject) => { - axios.tainacan.get(`/logs?paged=${page}&perpage=${activitiesPerPage}&context=edit`) + axios.tainacan.get(`/logs?paged=${page}&perpage=${activitiesPerPage}&context=edit&orderby=id&order=desc`) .then(res => { let activities = res.data; @@ -20,7 +20,7 @@ export const fetchActivities = ({ commit }, { page, activitiesPerPage } ) => { export const fetchCollectionActivities = ({ commit }, { page, activitiesPerPage, collectionId }) => { return new Promise((resolve, reject) => { - axios.tainacan.get(`/collection/${collectionId}/logs?paged=${page}&perpage=${activitiesPerPage}&context=edit`) + axios.tainacan.get(`/collection/${collectionId}/logs?paged=${page}&perpage=${activitiesPerPage}&context=edit&orderby=id&order=desc`) .then(res => { let activities = res.data; @@ -37,7 +37,7 @@ export const fetchCollectionActivities = ({ commit }, { page, activitiesPerPage, export const fetchItemActivities = ({ commit }, { page, activitiesPerPage, itemId }) => { return new Promise((resolve, reject) => { - axios.tainacan.get(`/item/${itemId}/logs?paged=${page}&perpage=${activitiesPerPage}&context=edit`) + axios.tainacan.get(`/item/${itemId}/logs?paged=${page}&perpage=${activitiesPerPage}&context=edit&orderby=id&order=desc`) .then(res => { let activities = res.data; From 15e143b09ad67d4e59891ff702327b9ceaefe78e Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Mon, 23 Sep 2019 14:08:57 -0300 Subject: [PATCH 030/198] Removes unecessary request to repository filters on filters list. Hides loading container on facets block when number of facets is empty. --- src/admin/components/lists/filters-list.vue | 6 ------ .../tainacan-facets/facets-list/facets-list-theme.vue | 5 +++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/admin/components/lists/filters-list.vue b/src/admin/components/lists/filters-list.vue index da180a040..d0e886be8 100644 --- a/src/admin/components/lists/filters-list.vue +++ b/src/admin/components/lists/filters-list.vue @@ -684,12 +684,6 @@ export default { }) .catch(() => this.isLoadingFilters = false); - // On repository level we also fetch collection filters - if (this.isRepositoryLevel) { - this.fetchRepositoryCollectionFilters() - .catch(() => this.isLoadingFilters = false); - } - // Obtains collection name if (!this.isRepositoryLevel) { diff --git a/src/gutenberg-blocks/tainacan-facets/facets-list/facets-list-theme.vue b/src/gutenberg-blocks/tainacan-facets/facets-list/facets-list-theme.vue index d25d45220..9ea3dc21a 100644 --- a/src/gutenberg-blocks/tainacan-facets/facets-list/facets-list-theme.vue +++ b/src/gutenberg-blocks/tainacan-facets/facets-list/facets-list-theme.vue @@ -123,8 +123,9 @@
- {{ facets.length > 0 ? '' : $root.__('Nothing found.', 'tainacan') }} + class="spinner-container" + :style="{ display: facets.length > 0 ? 'none' : 'flex'}"> + {{ $root.__('Nothing found.', 'tainacan') }}
From 0e2b68791ceac60cbbffab7f3373ac4842932429 Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Tue, 24 Sep 2019 14:58:08 -0300 Subject: [PATCH 031/198] Patches exporter session updates at once. Error check with promisses instead of return function. --- .../bulk-edition/bulk-edition-modal.vue | 4 +- .../edition/exporter-edition-form.vue | 67 +++++++------------ ...ass-tainacan-rest-exporters-controller.php | 4 +- src/js/store/modules/exporter/actions.js | 38 ++++++----- 4 files changed, 49 insertions(+), 64 deletions(-) diff --git a/src/admin/components/bulk-edition/bulk-edition-modal.vue b/src/admin/components/bulk-edition/bulk-edition-modal.vue index fa3cdd9f9..9ff7c27a7 100644 --- a/src/admin/components/bulk-edition/bulk-edition-modal.vue +++ b/src/admin/components/bulk-edition/bulk-edition-modal.vue @@ -370,10 +370,10 @@ add: this.$i18n.get('add_value'), redefine: this.$i18n.get('set_new_value'), remove: this.$i18n.get('remove_value'), - replace: this.$i18n.get('replace_value'), + replace: this.$i18n.get('replace_value') }, editionActionsForNotMultiple: { - redefine: this.$i18n.get('set_new_value'), + redefine: this.$i18n.get('set_new_value') }, bulkEditionProcedures: { 1: { diff --git a/src/admin/components/edition/exporter-edition-form.vue b/src/admin/components/edition/exporter-edition-form.vue index 99d44bf9c..d6f93d046 100644 --- a/src/admin/components/edition/exporter-edition-form.vue +++ b/src/admin/components/edition/exporter-edition-form.vue @@ -142,10 +142,12 @@ ...mapActions('collection', [ 'fetchCollections' ]), - updateExporterOptions(){ + runExporter(){ + this.runButtonLoading = true; + let formElement = document.getElementById('exporterOptionsForm'); let formData = new FormData(formElement); - + let options = {}; for (let [key, value] of formData.entries()) @@ -153,21 +155,9 @@ let exporterSessionUpdated = { body: { - options: options, - }, - id: this.exporterSession.id, - }; - - return this.updateExporterSession(exporterSessionUpdated) - .then(exporterSessionUpdated => this.verifyError(exporterSessionUpdated)); - }, - runExporter(){ - this.runButtonLoading = true; - - let exporterSessionUpdated = { - body: { - mapping_selected: this.selectedMapping ? this.selectedMapping : this.selectedMapping, - send_email: this.sendEmail + mapping_selected: this.selectedMapping, + send_email: this.sendEmail, + options: options }, id: this.exporterSession.id, }; @@ -176,26 +166,28 @@ exporterSessionUpdated['body']['collection'] = { id: this.selectedCollection }; - } + } this.updateExporterSession(exporterSessionUpdated) - .then(exporterSessionUpdated => { - this.verifyError(exporterSessionUpdated); + .then(() => { + + if (!this.formErrorMessage) { + this.runExporterSession(this.exporterSession.id) + .then((bgp) => { + this.runButtonLoading = false; + this.$router.push(this.$routerHelper.getProcessesPage(bgp.bg_process_id)); + }) + .catch((error) => { + this.formErrorMessage = error.error_message; + this.runButtonLoading = false; + }); + } - this.updateExporterOptions().then(() => { - if(!this.formErrorMessage) { - this.runExporterSession(this.exporterSession.id) - .then((bgp) => { - this.runButtonLoading = false; - this.$router.push(this.$routerHelper.getProcessesPage(bgp.bg_process_id)); - }) - .catch(() => { - this.runButtonLoading = false; - }); - } - }); }) - .catch(() => this.runButtonLoading = false); + .catch((error) => { + this.formErrorMessage = error.error_message; + this.runButtonLoading = false; + }); }, formIsValid(){ return ( @@ -204,15 +196,6 @@ this.exporterSession.accept_no_mapping) && !this.formErrorMessage ); - }, - verifyError(response){ - if(response.constructor.name === 'Object' && - (response.data && response.data.status && - response.data.status.toString().split('')[0] != 2) || response.error_message) { - this.formErrorMessage = response.data.error_message; - } else { - this.exporterSession = response.data; - } } }, created(){ diff --git a/src/api/endpoints/class-tainacan-rest-exporters-controller.php b/src/api/endpoints/class-tainacan-rest-exporters-controller.php index 51c4a4c57..f252cdedf 100644 --- a/src/api/endpoints/class-tainacan-rest-exporters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-exporters-controller.php @@ -68,7 +68,7 @@ class REST_Exporters_Controller extends REST_Controller { ], 'options' => [ 'type' => 'array/object', - 'description' => __( 'The importer options', 'tainacan' ), + 'description' => __( 'The exporter options', 'tainacan' ), ] ], ), @@ -172,7 +172,7 @@ class REST_Exporters_Controller extends REST_Controller { } return new \WP_REST_Response([ - 'error_message' => __('Importer Session not found', 'tainacan' ), + 'error_message' => __('Exporter Session not found', 'tainacan' ), 'session_id' => $session_id ], 400); } diff --git a/src/js/store/modules/exporter/actions.js b/src/js/store/modules/exporter/actions.js index b8ae73fd5..e9f7df725 100644 --- a/src/js/store/modules/exporter/actions.js +++ b/src/js/store/modules/exporter/actions.js @@ -26,26 +26,28 @@ export const createExporterSession = ({commit}, slug) => { export const updateExporterSession = ({commit}, exporterSessionUpdated) => { - return tainacan.patch(`/exporters/session/${exporterSessionUpdated.id}`, exporterSessionUpdated.body) - .then(response => { - commit('setExporterSession'); - - return response; - }) - .catch(error => { - console.error(error.response.data); - }); + return new Promise(( resolve, reject ) => { + tainacan.patch(`/exporters/session/${exporterSessionUpdated.id}`, exporterSessionUpdated.body) + .then(response => { + commit('setExporterSession'); + resolve( response.data ); + }) + .catch(error => { + reject( error.response.data ); + }); + }); }; export const runExporterSession = ({commit}, exporterSessionID) => { - return tainacan.post(`/exporters/session/${exporterSessionID}/run`) - .then(response => { - commit('setBackGroundProcessID', response.data); - - return response.data; - }) - .catch(error => { - console.error(error.response.data); - }) + return new Promise(( resolve, reject ) => { + tainacan.post(`/exporters/session/${exporterSessionID}/run`) + .then(response => { + commit('setBackGroundProcessID', response.data); + resolve(response.data); + }) + .catch(error => { + reject( error.response.data ); + }) + }); }; \ No newline at end of file From 7a30b167c90f639e988765532a8c67881b5c2c2b Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Tue, 24 Sep 2019 16:35:11 -0300 Subject: [PATCH 032/198] Adds customized disabled style for selects on bulk edition modal. --- src/admin/components/bulk-edition/bulk-edition-modal.vue | 8 ++++---- src/admin/scss/_selects.scss | 2 +- src/admin/scss/_tainacan-form.scss | 8 +++++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/admin/components/bulk-edition/bulk-edition-modal.vue b/src/admin/components/bulk-edition/bulk-edition-modal.vue index 9ff7c27a7..abb01581f 100644 --- a/src/admin/components/bulk-edition/bulk-edition-modal.vue +++ b/src/admin/components/bulk-edition/bulk-edition-modal.vue @@ -17,7 +17,7 @@ option:checked, &:focus>option:hover { background-color: $turquoise1 !important; } - } + &[disabled=disabled] { + border: 1px solid $gray1 !important; + } + } } + .hidden-select-arrow .select::after { + color: transparent !important; + } .dropdown { width: 100%; .dropdown-trigger { width: 100% } From 3f59c2e141504af07844b6910e40763a1239a647 Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Tue, 24 Sep 2019 16:56:06 -0300 Subject: [PATCH 033/198] Removes unsed functions from tainacan filter item comonente. --- .../filter-types/checkbox/Checkbox.vue | 15 ---------- .../filter-types/tainacan-filter-item.vue | 28 +------------------ src/js/event-bus-search.js | 14 ---------- 3 files changed, 1 insertion(+), 56 deletions(-) diff --git a/src/classes/filter-types/checkbox/Checkbox.vue b/src/classes/filter-types/checkbox/Checkbox.vue index be40b103c..b355537ae 100644 --- a/src/classes/filter-types/checkbox/Checkbox.vue +++ b/src/classes/filter-types/checkbox/Checkbox.vue @@ -3,12 +3,6 @@ :style="{ 'height': isLoadingOptions ? (Number(filter.max_options)*28) + 'px' : 'auto' }" :class="{ 'skeleton': isLoadingOptions }" class="block"> -
 {{ "(" + option.total_items + ")" }} - +

+
+ +
+

+ +

+
+

until

+
+

+ +

+
+ +
+

+ +

+
+ + '); + } + + public function get_form_labels(){ + return [ + 'step' => [ + 'title' => __( 'Step', 'tainacan' ), + 'description' => __( 'The amount to be increased or decreased when clicking on filter control buttons.', 'tainacan' ), + ], + 'input-mode' => [ + 'title' => __( 'Input mode', 'tainacan' ), + 'description' => __( 'Input mode', 'tainacan' ), + ], + 'custom' => ['title' => __('Custom interval','tainacan')], + 'list' => ['title' => __('Predefined intervals','tainacan')], + ]; + } + + /** + * @param $filter + * @return string + * @internal param $metadatum + */ + public function render( $filter ){ + return ''; + } +} \ No newline at end of file diff --git a/src/classes/tainacan-creator.php b/src/classes/tainacan-creator.php index 964f38ee8..32896322d 100644 --- a/src/classes/tainacan-creator.php +++ b/src/classes/tainacan-creator.php @@ -141,6 +141,7 @@ $Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Taginput'); $Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Checkbox'); $Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\TaxonomyTaginput'); $Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\TaxonomyCheckbox'); +$Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Numeric_Interval'); $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); From 65bd5fcf6d1285c4bc0f119c49fba9727029c66b Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Fri, 27 Sep 2019 12:00:26 -0300 Subject: [PATCH 036/198] Small fix on term search input which wasn't searching on click. --- src/admin/components/lists/terms-list.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/admin/components/lists/terms-list.vue b/src/admin/components/lists/terms-list.vue index cf4ad7d40..5d7ebcf17 100644 --- a/src/admin/components/lists/terms-list.vue +++ b/src/admin/components/lists/terms-list.vue @@ -64,8 +64,8 @@ type="search" :aria-label="$i18n.get('instruction_search') + ' ' + $i18n.get('terms')" autocomplete="on" - :value="searchQuery" - @keyup.enter="searchQuery = $event.target.value;searchTerms(0)" + v-model="searchQuery" + @keyup.enter="searchTerms(0)" :disabled="isEditingTerm"> Date: Fri, 27 Sep 2019 16:36:13 -0300 Subject: [PATCH 037/198] Allow legacy logs to be displayed the same as before. #261. --- .../other/activity/activity-details-modal.vue | 246 +++++++++++++++++- 1 file changed, 245 insertions(+), 1 deletion(-) diff --git a/src/admin/components/other/activity/activity-details-modal.vue b/src/admin/components/other/activity/activity-details-modal.vue index 0fc62d39c..df5ed68c3 100644 --- a/src/admin/components/other/activity/activity-details-modal.vue +++ b/src/admin/components/other/activity/activity-details-modal.vue @@ -16,7 +16,235 @@

{{ $i18n.get('label_activity_creation_date') }}: {{ activityCreationDate }}

{{ $i18n.get('label_activity_author') }}: {{ activity.user_name }}

-
+ + +
+ +
+ +
@@ -445,6 +673,22 @@ } } + .tainacan-attachments-in-modal { + display: flex; + flex-wrap: wrap; + flex-direction: row; + align-content: baseline; + resize: vertical; + overflow-y: auto; + overflow-x: hidden; + height: 200px; + border: 1px solid $gray3; + + &>div { + margin: 0.5rem; + } + } + .tainacan-p-overflow { text-overflow: ellipsis; white-space: nowrap; From 46b9c979fdd49418ca7e5ff833f19b4d661bf5a8 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 27 Sep 2019 16:46:14 -0300 Subject: [PATCH 038/198] Add search and filters to activities lists. #307 --- src/admin/components/lists/terms-list.vue | 12 +- src/admin/pages/lists/activities-page.vue | 146 ++++++++++++++++++++-- src/admin/tainacan-admin-i18n.php | 15 +-- src/js/store/modules/activity/actions.js | 71 +++++++++-- 4 files changed, 215 insertions(+), 29 deletions(-) diff --git a/src/admin/components/lists/terms-list.vue b/src/admin/components/lists/terms-list.vue index cf4ad7d40..788f34b18 100644 --- a/src/admin/components/lists/terms-list.vue +++ b/src/admin/components/lists/terms-list.vue @@ -67,12 +67,12 @@ :value="searchQuery" @keyup.enter="searchQuery = $event.target.value;searchTerms(0)" :disabled="isEditingTerm"> - - - + + +
diff --git a/src/admin/pages/lists/activities-page.vue b/src/admin/pages/lists/activities-page.vue index f7faa2cfe..3d94e3a3c 100644 --- a/src/admin/pages/lists/activities-page.vue +++ b/src/admin/pages/lists/activities-page.vue @@ -28,6 +28,73 @@ :active.sync="isLoading" :can-cancel="false"/> +
+ + + + +

+ +

+
+ +
+ + + + +
+
+ import ActivitiesList from "../../components/lists/activities-list.vue"; import ProcessesList from "../../components/lists/processes-list.vue"; + import { dateInter } from "../../../admin/js/mixins"; import { mapActions, mapGetters } from 'vuex'; import moment from 'moment' export default { name: 'ActivitiesPage', + mixins: [ dateInter ], data(){ return { isLoading: false, @@ -165,6 +234,8 @@ isRepositoryLevel: false, tab: '', isItemLevel: false, + searchQuery: '', + searchDates: [] } }, components: { @@ -237,10 +308,15 @@ loadActivities() { this.isLoading = true; + let dataInit = this.searchDates[0] ? moment(this.searchDates[0]).format('YYYY-MM-DD') : null; + let dataEnd = this.searchDates[1] ? moment(this.searchDates[1]).format('YYYY-MM-DD') : null; + if(this.isRepositoryLevel) { this.fetchActivities({ 'page': this.activitiesPage, - 'activitiesPerPage': this.activitiesPerPage + 'activitiesPerPage': this.activitiesPerPage, + 'search': this.searchQuery, + 'searchDates': [dataInit, dataEnd] }) .then((res) => { this.isLoading = false; @@ -253,7 +329,9 @@ this.fetchCollectionActivities({ 'page': this.activitiesPage, 'activitiesPerPage': this.activitiesPerPage, - 'collectionId': this.$route.params.collectionId + 'collectionId': this.$route.params.collectionId, + 'search': this.searchQuery, + 'searchDates': [dataInit, dataEnd] }) .then((res) => { this.isLoading = false; @@ -266,7 +344,9 @@ this.fetchItemActivities({ 'page': this.activitiesPage, 'activitiesPerPage': this.activitiesPerPage, - 'itemId': this.$route.params.itemId + 'itemId': this.$route.params.itemId, + 'search': this.searchQuery, + 'searchDates': [dataInit, dataEnd] }) .then((res) => { this.isLoading = false; @@ -300,6 +380,21 @@ getLastProcessesNumber() { let last = (Number(this.processesPerPage * (this.processesPage - 1)) + Number(this.processesPerPage)); return last > this.total ? this.total : last; + }, + searchActivities() { + this.activitiesPage = 1; + this.loadActivities(); + }, + dateFormatter(dateObject) { + if (dateObject.length == 0 ) + return "" + return moment(dateObject[0], moment.ISO_8601).format(this.dateFormat) + " - " + moment(dateObject[1], moment.ISO_8601).format(this.dateFormat); + }, + dateParser(dateString) { + return [ + moment(dateString[0], this.dateFormat).toDate(), + moment(dateString[1], this.dateFormat).toDate() + ]; } }, computed: { @@ -356,16 +451,49 @@ \ No newline at end of file From fe2e0ce32e365da36724c97401bf206db09fb8ed Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Mon, 30 Sep 2019 15:19:56 -0300 Subject: [PATCH 047/198] add `clearSearchDates()` on filter activities #261 --- src/admin/pages/lists/activities-page.vue | 29 +++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/admin/pages/lists/activities-page.vue b/src/admin/pages/lists/activities-page.vue index 3d94e3a3c..59e597a2b 100644 --- a/src/admin/pages/lists/activities-page.vue +++ b/src/admin/pages/lists/activities-page.vue @@ -5,7 +5,7 @@ 'repository-level-page': isRepositoryLevel, 'page-container': isRepositoryLevel }"> -
@@ -14,16 +14,16 @@ v-if="isRepositoryLevel" class="tabs">
- @@ -37,7 +37,7 @@ ref="datepicker" :placeholder="$i18n.get('instruction_filter_activities_date')" v-model="searchDates" - range + range @input="searchActivities()" :date-formatter="(date) => dateFormatter(date)" :date-parser="(date) => dateParser(date)" @@ -66,13 +66,12 @@ $i18n.get('datepicker_month_november'), $i18n.get('datepicker_month_december') ]"/> - -

+ v-if="searchDates && searchDates.length != 0">

@@ -308,8 +307,8 @@ loadActivities() { this.isLoading = true; - let dataInit = this.searchDates[0] ? moment(this.searchDates[0]).format('YYYY-MM-DD') : null; - let dataEnd = this.searchDates[1] ? moment(this.searchDates[1]).format('YYYY-MM-DD') : null; + let dataInit = this.searchDates && this.searchDates[0] ? moment(this.searchDates[0]).format('YYYY-MM-DD') : null; + let dataEnd = this.searchDates && this.searchDates[1] ? moment(this.searchDates[1]).format('YYYY-MM-DD') : null; if(this.isRepositoryLevel) { this.fetchActivities({ @@ -385,9 +384,13 @@ this.activitiesPage = 1; this.loadActivities(); }, + clearSearchDates() { + this.searchDates = null; + this.searchActivities(); + }, dateFormatter(dateObject) { - if (dateObject.length == 0 ) - return "" + if (dateObject == null || dateObject.length == 0 || dateObject[0] == null || dateObject[1] == null) + return ""; return moment(dateObject[0], moment.ISO_8601).format(this.dateFormat) + " - " + moment(dateObject[1], moment.ISO_8601).format(this.dateFormat); }, dateParser(dateString) { From 8bf143aa7420714dd4d53f5686955d0431826b31 Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Mon, 30 Sep 2019 15:25:29 -0300 Subject: [PATCH 048/198] Color adjustment on dropdown. --- src/admin/scss/_dropdown-and-autocomplete.scss | 1 + 1 file changed, 1 insertion(+) diff --git a/src/admin/scss/_dropdown-and-autocomplete.scss b/src/admin/scss/_dropdown-and-autocomplete.scss index 86be6b010..271f81387 100644 --- a/src/admin/scss/_dropdown-and-autocomplete.scss +++ b/src/admin/scss/_dropdown-and-autocomplete.scss @@ -249,6 +249,7 @@ &.is-selectable.is-first-hovered, &.is-selectable.is-last-hovered { + color: $gray4 !important; background-color: $gray3 !important; } &.is-today, From b3ccca80f6d0c20c21be4514927f1a0aeb757081 Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Mon, 30 Sep 2019 15:54:26 -0300 Subject: [PATCH 049/198] Moves activity modal to root 'other' folder. Adjusts clear button css. --- src/admin/components/lists/activities-list.vue | 2 +- .../components/other/{activity => }/activity-details-modal.vue | 2 +- src/admin/pages/lists/activities-page.vue | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) rename src/admin/components/other/{activity => }/activity-details-modal.vue (99%) diff --git a/src/admin/components/lists/activities-list.vue b/src/admin/components/lists/activities-list.vue index e997905f5..d4cf3a41f 100644 --- a/src/admin/components/lists/activities-list.vue +++ b/src/admin/components/lists/activities-list.vue @@ -127,7 +127,7 @@ + + diff --git a/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-list.scss b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-list.scss new file mode 100644 index 000000000..68dff7d07 --- /dev/null +++ b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-list.scss @@ -0,0 +1,360 @@ +@import '../../gutenberg-blocks-style.scss'; + +.components-panel__body .term-carousel-view-modes { + display: flex; + margin: 12px 0 24px 0; + + button { + background: transparent; + border: 1px solid #cbcbcb; + border-radius: 5px; + padding: 2px; + margin: 0; + width: calc(50% - 6px); + flex-shrink: 0; + cursor: pointer; + transition: border ease 0.5s, opacity ease 0.5; + + &.term-carousel-view-mode-grid { + margin-right: 6px; + div { + display: grid; + grid-template-columns: 33% 33% 33%; + grid-template-rows: 50% 50%; + grid-column-gap: 2px; + grid-row-gap: 2px; + width: 60px; + height: 40px; + margin: 4px auto 8px auto; + + &>div:first-of-type { + grid-column-start: 1; + grid-column-end: span 2; + grid-row-start: 1; + grid-row-end: span 2; + width: 41px; + height: 42px; + } + + &>div { + width: 20px; + height: 20px; + background: #555758; + transition: background-color ease 0.5s; + } + } + } + &.term-carousel-view-mode-thumbnail { + margin-left: 6px; + + div { + display: block; + height: 40px; + width: 40px; + background: #555758; + margin: 4px auto 8px auto; + transition: background-color ease 0.5s; + } + } + + label { + font-size: 0.75rem; + color: #555758; + } + + &.is-active { + border: 2px solid #298596; + + &.term-carousel-view-mode-thumbnail div, + &.term-carousel-view-mode-grid div>div { + background-color: #298596; + } + } + &:hover { + opacity: 0.9; + border-width: 2px; + button { + background: #f2f2f2; + } + } + + } +} + +.wp-block-tainacan-carousel-terms-list { + margin: 2rem 0px; + + // Spinner + .spinner-container { + min-height: 56px; + padding: 1rem; + display: flex; + justify-content: center; + align-items: center; + color: #555758; + } + + // Skeleton loading + @-webkit-keyframes skeleton-animation { + 0%{opacity: 1.0} + 50%{opacity: 0.2} + 100%{opacity: 1.0} + } + @-moz-keyframes skeleton-animation { + 0%{opacity: 1.0} + 50%{opacity: 0.2} + 100%{opacity: 1.0} + } + @-o-keyframes skeleton-animation { + 0%{opacity: 1.0} + 50%{opacity: 0.2} + 100%{opacity: 1.0} + } + @keyframes skeleton-animation { + 0%{opacity: 1.0} + 50%{opacity: 0.2} + 100%{opacity: 1.0} + } + .skeleton { + border-radius: 2px; + background: #f2f2f2; + + -webkit-animation: skeleton-animation 1.8s ease infinite; + -moz-animation: skeleton-animation 1.8s ease infinite; + -o-animation: skeleton-animation 1.8s ease infinite; + animation: skeleton-animation 1.8s ease infinite; + } + + // Tainacan Carousel + .tainacan-carousel { + position: relative; + width: calc(100% + 40px); + left: -20px; + + .swiper-container { + margin: 0 50px; + + a>span, + a:hover>span { + color: black; + font-weight: bold; + text-decoration: none; + padding: 8px 16px; + display: block; + line-height: 1.2rem; + } + a>img { + width: 100%; + height: auto; + } + a:hover { + text-decoration: none; + } + + .swiper-slide.term-list-item-grid { + + a { + width: 100%; + display: block; + } + .term-items-grid { + display: grid; + grid-template-columns: 33% 33% 33%; + grid-template-rows: 50% 50%; + width: 100%; + + img:first-of-type { + grid-column-start: 1; + grid-column-end: span 2; + grid-row-start: 1; + grid-row-end: span 2; + } + + img { + width: 100%; + height: auto; + padding: 2px; + margin-bottom: 0px; + } + } + } + } + } + + .preview-warning { + width: 100%; + font-size: 0.875rem; + font-style: italic; + color: #898d8f; + text-align: center; + margin: 4px auto; + } + + // Next and previous buttons + .swiper-button-prev, .swiper-button-next { + top: calc(50% - 42px); + bottom: initial; + background: none; + border: none; + width: 42px; + height: 42px; + padding: 0; + margin: 0 -4px; + + svg { + fill: #298596; + } + } + + // Carousel placeholder on editor side ---------------------------------------------------- + .terms-list-edit-container, + .tainacan-carousel { + position: relative; + + & .skeleton { + min-height: 150px; + max-height: 150px; + } + + &.has-arrows-none .swiper-button-prev, + &.has-arrows-none .swiper-button-next { + display: none; + } + &.has-arrows-left .swiper-button-next { + left: 10px; + right: auto; + top: calc(50% + 12px) !important; + } + &.has-arrows-right .swiper-button-prev { + right: 10px; + left: auto; + } + &.has-arrows-right .swiper-button-next { + top: calc(50% + 12px) !important; + } + } + ul.terms-list-edit { + display: flex; + align-items: flex-start; + overflow-x: scroll; + list-style: none; + margin: 0 36px; + + li.term-list-item { + position: relative; + display: block; + margin: 16px 32px 16px 0px; + width: calc(16.666% - 32px); + min-width: calc(16.666% - 32px); + + &.term-list-item-grid { + margin: 16px; + width: calc(25% - 16px); + min-width: calc(25% - 16px); + } + + a { + color: #454647; + font-weight: bold; + line-height: normal; + } + + img { + height: auto; + padding: 0px; + margin-bottom: 0.5rem; + } + + &:hover a { + color: #454647; + text-decoration: none; + } + + button { + position: absolute !important; + background-color: rgba(255, 255, 255, 0.75); + color: #454647; + padding: 2px; + margin-left: 5px; + min-width: 14px; + visibility: hidden; + position: relative; + opacity: 0; + right: -14px; + top: 0px; + justify-content: center; + z-index: 999; + } + + &:hover button { + visibility: visible; + background-color: rgba(255, 255, 255, 1) !important; + opacity: 1; + right: -8px; + top: -8px; + border: 1px solid #cbcbcb; + border-radius: 12px; + transition: opacity linear 0.15s, right linear 0.15s; + } + &:hover button:hover { + background-color: rgba(255, 255, 255, 1) !important; + border: 1px solid #cbcbcb !important; + } + + + .term-items-grid { + display: grid; + grid-template-columns: 33% 33% 33%; + grid-template-rows: 50% 50%; + + img:first-of-type { + grid-column-start: 1; + grid-column-end: span 2; + grid-row-start: 1; + grid-row-end: span 2; + } + + img { + padding: 2px; + margin-bottom: 0px; + } + } + } + } + @media only screen and (max-width: 1686px) { + + ul.terms-list-edit li.term-list-item { + width: calc(20% - 32px); + min-width: calc(20% - 32px); + } + } + @media only screen and (max-width: 1452px) { + + ul.terms-list-edit li.term-list-item { + width: calc(25% - 32px); + min-width: calc(25% - 32px); + } + } + @media only screen and (max-width: 1118px) { + + ul.terms-edit li.term-list-item { + width: calc(33.333% - 32px); + min-width: calc(33.333% - 32px); + } + } + @media only screen and (max-width: 854px) { + + ul.terms-list-edit li.term-list-item { + width: calc(50% - 32px); + min-width: calc(50% - 32px); + } + } + @media only screen and (max-width: 584px) { + + ul.terms-list-edit li.term-list-item { + width: calc(100% - 32px); + min-width: calc(100% - 32px); + } + } + +} diff --git a/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-modal.js b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-modal.js new file mode 100644 index 000000000..51ee66038 --- /dev/null +++ b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-modal.js @@ -0,0 +1,309 @@ +import tainacan from '../../api-client/axios.js'; +import axios from 'axios'; + +const { __ } = wp.i18n; + +const { TextControl, Button, Modal, CheckboxControl, Spinner } = wp.components; + +export default class TermsModal extends React.Component { + constructor(props) { + super(props); + + // Initialize state + this.state = { + searchTermName: '', + termsRequestSource: undefined, + terms: [], + temporarySelectedTerms: [], + isLoadingTerms: false, + modalTerms: [], + totalModalTerms: 0, + termsPerPage: 24, + termsPage: 1, + }; + + // Bind events + this.selectTemporaryTerm = this.selectTemporaryTerm.bind(this); + this.removeTemporaryTermOfId = this.removeTemporaryTermOfId.bind(this); + this.applySelectedTerms = this.applySelectedTerms.bind(this); + this.isTemporaryTermSelected = this.isTemporaryTermSelected.bind(this); + this.toggleSelectTemporaryTerm = this.toggleSelectTemporaryTerm.bind(this); + this.cancelSelection = this.cancelSelection.bind(this); + this.selectTerm = this.selectTerm.bind(this); + this.fetchModalTerms = this.fetchModalTerms.bind(this); + this.fetchTerms = this.fetchTerms.bind(this); + } + + componentWillMount() { + + this.fetchModalTerms(); + + this.setState( { + terms: [], + termsPage: 1, + temporarySelectedTerms: JSON.parse(JSON.stringify(this.props.selectedTermsObject)) + } ); + } + + selectTemporaryTerm(term) { + let existingTermIndex = this.state.temporarySelectedTerms.findIndex((existingTerm) => existingTerm.id == term.id); + + if (existingTermIndex < 0) { + let aTemporarySelectedTerms = this.state.temporarySelectedTerms; + aTemporarySelectedTerms.push({ + id: term.id, + name: term.name, + url: term.url, + thumbnail: term.thumbnail + }); + this.setState({ temporarySelectedTerms: aTemporarySelectedTerms }); + } + } + + removeTemporaryTermOfId(termId) { + + let existingTermIndex = this.state.temporarySelectedTerms.findIndex((existingTerm) => existingTerm.id == termId); + + if (existingTermIndex >= 0) { + let aTemporarySelectedTerms = this.state.temporarySelectedTerms; + aTemporarySelectedTerms.splice(existingTermIndex, 1); + this.setState({ temporarySelectedTerms: aTemporarySelectedTerms }); + } + } + + applySelectedTerms() { + let aSelectedTermsObject = JSON.parse(JSON.stringify(this.state.temporarySelectedTerms)); + this.props.onApplySelection(aSelectedTermsObject); + } + + isTemporaryTermSelected(termId) { + return this.state.temporarySelectedTerms.findIndex(term => term.id == termId) >= 0; + } + + toggleSelectTemporaryTerm(term, isChecked) { + if (isChecked) + this.selectTemporaryTerm(term); + else + this.removeTemporaryTermOfId(term.id); + } + + cancelSelection() { + + this.setState({ + termsPage: 1, + modalTerms: [] + }); + + this.props.onCancelSelection(); + } + + selectTerm(selectedTermId) { + + this.setState({ + termId: selectedTermId + }); + this.fetchTerm(); + this.fetchModalTerms(); + } + + fetchModalTerms() { + + let currentModalTerms = this.state.modalTerms; + if (this.state.termsPage <= 1) + currentModalTerms = []; + + let endpoint = '/terms/?orderby=title&order=asc&perpage=' + this.state.termsPerPage + '&paged=' + this.state.termsPage; + + this.setState({ + isLoadingTerms: true, + modalTerms: currentModalTerms, + }); + + tainacan.get(endpoint) + .then(response => { + + for (let term of response.data) { + currentModalTerms.push({ + name: term.name, + id: term.id, + url: term.url, + thumbnail: [{ + src: term.thumbnail['tainacan-medium'] != undefined ? term.thumbnail['tainacan-medium'][0] : term.thumbnail['medium'][0], + alt: term.name + }] + }); + } + + this.setState({ + termsPage: this.state.termsPage + 1, + isLoadingTerms: false, + modalTerms: currentModalTerms, + totalModalTerms: response.headers['x-wp-total'] + }); + + return currentModalTerms; + }) + .catch(error => { + console.log('Error trying to fetch terms: ' + error); + }); + } + + fetchTerms(name) { + if (this.state.termsRequestSource != undefined) + this.state.termsRequestSource.cancel('Previous terms search canceled.'); + + let aTermRequestSource = axios.CancelToken.source(); + this.setState({ + termsRequestSource: aTermRequestSource, + isLoadingTerms: true + }); + + let endpoint = '/terms/?orderby=title&order=asc&perpage=' + this.state.termsPerPage; + + if (name != undefined && name != '') + endpoint += '&search=' + name; + + tainacan.get(endpoint, { cancelToken: aTermRequestSource.token }) + .then(response => { + + let someTerms = this.state.terms; + someTerms = response.data.map((term) => ({ + name: term.name, + id: term.id, + url: term.url, + thumbnail: [{ + src: term.thumbnail['tainacan-medium'] != undefined ? term.thumbnail['tainacan-medium'][0] : term.thumbnail['medium'][0], + alt: term.name + }] + })); + + this.setState({ + isLoadingTerms: false, + terms: someTerms + }); + + return someTerms; + }) + .catch(error => { + console.log('Error trying to fetch terms: ' + error); + }); + } + + render() { + return ( + this.cancelSelection() } + contentLabel={__('Select terms', 'tainacan')}> + +
+
+ { + this.setState({ + searchTermName: value.target.value + }); + }} + onChange={(value) => this.fetchTerms(value)}/> +
+ {( + this.state.searchTermName != '' ? ( + + this.state.terms.length > 0 ? + ( +
+
    + { + this.state.terms.map((term) => +
  • + { term.thumbnail ? + { + : null + } + { this.toggleSelectTemporaryTerm(term, isChecked) } } + /> +
  • + ) + } +
+ { this.state.isLoadingTerms ?
: null } +
+ ) + : this.state.isLoadingTerms ?
: +
+

{ __('Sorry, no terms found.', 'tainacan') }

+
+ ) : + this.state.modalTerms.length > 0 ? + ( +
+
    + { + this.state.modalTerms.map((term) => +
  • + { term.thumbnail ? + { + : null + } + { this.toggleSelectTemporaryTerm(term, isChecked) } } /> +
  • + ) + } + { this.state.isLoadingTerms ?
    : null } +
+
+

{ __('Showing', 'tainacan') + " " + this.state.modalTerms.length + " " + __('of', 'tainacan') + " " + this.state.totalModalTerms + " " + __('terms', 'tainacan') + "."}

+ { + this.state.modalTerms.length < this.state.totalModalTerms ? ( + + ) : null + } +
+
+ ) : this.state.isLoadingTerms ? : +
+

{ __('Sorry, no terms found.', 'tainacan') }

+
+ )} +
+ + +
+
+
+ ); + } +} \ No newline at end of file diff --git a/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/index.js b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/index.js new file mode 100644 index 000000000..a6ea32bec --- /dev/null +++ b/src/gutenberg-blocks/tainacan-terms/carousel-terms-list/index.js @@ -0,0 +1,551 @@ +const { registerBlockType } = wp.blocks; + +const { __ } = wp.i18n; + +const { RangeControl, Spinner, Button, BaseControl, ToggleControl, SelectControl, Placeholder, IconButton, PanelBody } = wp.components; + +const { InspectorControls } = wp.editor; + +import CarouselTermsModal from './carousel-terms-modal.js'; +import tainacan from '../../api-client/axios.js'; +import axios from 'axios'; +import qs from 'qs'; + +registerBlockType('tainacan/carousel-terms-list', { + title: __('Tainacan Terms Carousel', 'tainacan'), + icon: + + + , + category: 'tainacan-blocks', + keywords: [ __( 'terms', 'tainacan' ), __( 'carousel', 'tainacan' ), __( 'slider', 'tainacan' ), __( 'taxonomy', 'tainacan' ) ], + attributes: { + content: { + type: 'array', + source: 'children', + selector: 'div' + }, + terms: { + type: Array, + default: [] + }, + isModalOpen: { + type: Boolean, + default: false + }, + selectedTerms: { + type: Array, + default: [] + }, + itemsRequestSource: { + type: String, + default: undefined + }, + maxTermsNumber: { + type: Number, + value: undefined + }, + isLoading: { + type: Boolean, + value: false + }, + isLoadingTerm: { + type: Boolean, + value: false + }, + arrowsPosition: { + type: String, + value: 'search' + }, + autoPlay: { + type: Boolean, + value: false + }, + autoPlaySpeed: { + type: Number, + value: 3 + }, + loopSlides: { + type: Boolean, + value: false + }, + hideName: { + type: Boolean, + value: true + }, + showTermThumbnail: { + type: Boolean, + value: false + }, + term: { + type: Object, + value: undefined + }, + blockId: { + type: String, + default: undefined + }, + termBackgroundColor: { + type: String, + default: "#454647" + }, + termTextColor: { + type: String, + default: "#ffffff" + } + }, + supports: { + align: ['full', 'wide'], + html: false, + multiple: true + }, + edit({ attributes, setAttributes, className, isSelected, clientId }){ + let { + terms, + content, + isModalOpen, + itemsRequestSource, + selectedTerms, + isLoading, + arrowsPosition, + autoPlay, + autoPlaySpeed, + loopSlides, + hideName, + showTermThumbnail + } = attributes; + + // Obtains block's client id to render it on save function + setAttributes({ blockId: clientId }); + + function prepareItem(term, termItems) { + return ( +
  • + removeItemOfId(term.id) } + icon="no-alt" + label={__('Remove', 'tainacan')}/> + + { !showTermThumbnail ? +
    + { + { + { +
    + : + { + } + { !hideName ? { term.name ? term.name : '' } : null } +
    +
  • + ); + } + + function setContent(){ + isLoading = true; + + setAttributes({ + isLoading: isLoading + }); + + if (itemsRequestSource != undefined && typeof itemsRequestSource == 'function') + itemsRequestSource.cancel('Previous terms search canceled.'); + + itemsRequestSource = axios.CancelToken.source(); + + terms = []; + + let endpoint = '/terms?'+ qs.stringify({ postin: selectedTerms }) + '&fetch_only=name,url,thumbnail'; + tainacan.get(endpoint, { cancelToken: itemsRequestSource.token }) + .then(response => { + + if (showTermThumbnail) { + for (let term of response.data) { + terms.push(prepareItem(term)); + } + setAttributes({ + content:
    , + terms: terms, + isLoading: false, + itemsRequestSource: itemsRequestSource + }); + } else { + let promises = []; + for (let term of response.data) { + promises.push( + tainacan.get('/term/' + term.id + '/items?perpage=3&fetch_only=name,url,thumbnail') + .then(response => { return({ term: term, termItems: response.data.items }) }) + .catch((error) => console.log(error)) + ); + } + axios.all(promises).then((results) => { + for (let result of results) { + terms.push(prepareItem(result.term, result.termItems)); + } + setAttributes({ + content:
    , + terms: terms, + isLoading: false, + itemsRequestSource: itemsRequestSource + }); + }) + } + }); + } + + function openCarouselModal() { + isModalOpen = true; + setAttributes( { + isModalOpen: isModalOpen + } ); + } + + function removeItemOfId(itemId) { + + let existingItemIndex = terms.findIndex((existingItem) => existingItem.key == itemId); + if (existingItemIndex >= 0) + terms.splice(existingItemIndex, 1); + + let existingSelectedItemIndex = selectedTerms.findIndex((existingSelectedItem) => existingSelectedItem == itemId); + if (existingSelectedItemIndex >= 0) + selectedTerms.splice(existingSelectedItemIndex, 1); + + setAttributes({ + selectedTerms: selectedTerms, + terms: terms, + content:
    + }); + } + + // Executed only on the first load of page + if(content && content.length && content[0].type) + setContent(); + + return ( +
    + +
    + + + +
    + {/* { + showTermThumbnail = isChecked; + setAttributes({ showTermThumbnail: showTermThumbnail }); + setContent(); + } + } + /> */} + +
    + + +
    +
    + { + hideName = isChecked; + setAttributes({ hideName: hideName }); + setContent(); + } + } + /> + { + loopSlides = isChecked; + setAttributes({ loopSlides: loopSlides }); + } + } + /> + { + autoPlay = isChecked; + setAttributes({ autoPlay: autoPlay }); + } + } + /> + { + autoPlay ? + { + autoPlaySpeed = aAutoPlaySpeed; + setAttributes( { autoPlaySpeed: aAutoPlaySpeed } ) + }} + min={ 1 } + max={ 5 } + /> + : null + } + { + arrowsPosition = aPosition; + + setAttributes({ arrowsPosition: arrowsPosition }); + }}/> +
    +
    +
    +
    + + { isSelected ? + ( +
    + { isModalOpen ? + { + selectedTerms = selectedTerms.concat(aSelectionOfTerms.map((term) => { return term.id; })); + setAttributes({ + selectedTerms: selectedTerms, + isModalOpen: false + }); + setContent(); + }} + onCancelSelection={ () => setAttributes({ isModalOpen: false }) }/> + : null + } + + { terms.length ? ( +
    +

    + + + + {__('List terms on a Carousel', 'tainacan')} +

    + +
    + ): null + } +
    + ) : null + } + + { !terms.length && !isLoading ? ( + + )}> +

    + + + + {__('List terms on a Carousel, using search or term selection.', 'tainacan')} +

    + +
    + ) : null + } + + { isLoading ? +
    + +
    : +
    + { isSelected && terms.length ? +
    {__('Warning: this is just a demonstration. To see the carousel in action, either preview or publish your post.', 'tainacan')}
    + : null + } + { terms.length ? ( +
    + +
      + { terms } +
    + +
    + ):null + } +
    + } +
    + ); + }, + save({ attributes, className }){ + const { + content, + blockId, + selectedTerms, + arrowsPosition, + maxTermsNumber, + autoPlay, + autoPlaySpeed, + loopSlides, + hideName, + showTermThumbnail + } = attributes; + return
    + { content } +
    + } +}); \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 29b336d26..8e5f21324 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -27,7 +27,11 @@ module.exports = { gutenberg_carousel_collections_list_theme: './src/gutenberg-blocks/tainacan-collections/carousel-collections-list/carousel-collections-list-theme.js', gutenberg_facets_list: './src/gutenberg-blocks/tainacan-facets/facets-list/index.js', - gutenberg_facets_list_theme: './src/gutenberg-blocks/tainacan-facets/facets-list/facets-list-theme.js' + gutenberg_facets_list_theme: './src/gutenberg-blocks/tainacan-facets/facets-list/facets-list-theme.js', + + gutenberg_carousel_terms_list: './src/gutenberg-blocks/tainacan-terms/carousel-terms-list/index.js', + gutenberg_carousel_terms_list_theme: './src/gutenberg-blocks/tainacan-terms/carousel-terms-list/carousel-terms-list-theme.js' + }, output: { path: path.resolve(__dirname, './src/assets/'), From b3376c7a71e62ebd8be50afebfc0c64112b6a3d0 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 30 Sep 2019 17:15:46 -0300 Subject: [PATCH 051/198] fix logs for repository filters not being properly created #261 --- src/classes/repositories/class-tainacan-logs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index d7e5ed081..82076fa63 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -533,7 +533,7 @@ class Logs extends Repository { } } elseif ( $entity instanceof Entities\Filter ) { - if ( 'default' == $collection_id ) { + if ( 'filter_in_repository' == $collection_id ) { if ($this->current_action == 'update') { $log->set_title( sprintf( __( 'Filter "%1$s" was updated in repository level', 'tainacan'), $entity->get_name() ) ); } elseif ($this->current_action == 'create') { From 7f79e6488770ba494592f39158767e99ff3a7e98 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 30 Sep 2019 18:00:44 -0300 Subject: [PATCH 052/198] starting refactor of repository filter meta #261 --- .../class-tainacan-rest-filters-controller.php | 4 ++-- src/classes/repositories/class-tainacan-filters.php | 4 ++-- src/migrations.php | 10 ++++++++++ tests/test-api-filters.php | 2 +- tests/test-filters.php | 2 +- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php index fe0906f6e..9ef54331d 100644 --- a/src/api/endpoints/class-tainacan-rest-filters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -148,7 +148,7 @@ class REST_Filters_Controller extends REST_Controller { $filter_obj->set_metadatum_id($body['metadatum_id']); } else { - $filter_obj->set_collection_id( 'filter_in_repository' ); + $filter_obj->set_collection_id( 'default' ); if(!isset($body['metadatum_id'])){ throw new \InvalidArgumentException('You need provide a metadatum id'); @@ -375,7 +375,7 @@ class REST_Filters_Controller extends REST_Controller { if(!isset($request['collection_id'])) { $args['meta_query'][] = [ 'key' => 'collection_id', - 'value' => 'filter_in_repository', + 'value' => 'default', 'compare' => '=' ]; diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index 9a102ef76..54e815467 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -365,7 +365,7 @@ class Filters extends Repository { $parents[] = $collection_id; //search for default metadatum - $parents[] = 'filter_in_repository'; + $parents[] = 'default'; $meta_query = array( 'key' => 'collection_id', @@ -416,7 +416,7 @@ class Filters extends Repository { $parents[] = $collection_id; //search for default metadatum - $parents[] = 'filter_in_repository'; + $parents[] = 'default'; $meta_query = array( 'key' => 'collection_id', diff --git a/src/migrations.php b/src/migrations.php index 3aab96a54..caecdeb4d 100644 --- a/src/migrations.php +++ b/src/migrations.php @@ -348,6 +348,16 @@ class Migrations { } + static function update_repository_filters_meta() { + global $wpdb; + + $wpdb->query( "UPDATE $wpdb->postmeta SET meta_value = 'default' WHERE + post_id IN ( + SELECT ID FROM $wpdb->posts WHERE post_type = 'tainacan-filter' + ) AND meta_key = 'collection_id' AND meta_value = 'filter_in_repository'" ); + + } + } diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index 8e4258761..717409af9 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -321,7 +321,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { $data = $response_create->get_data(); - $this->assertEquals('filter_in_repository', $data['collection_id']); + $this->assertEquals('default', $data['collection_id']); #### CREATE A FILTER IN COLLECTION WITHOUT METADATUM ASSOCIATION #### diff --git a/tests/test-filters.php b/tests/test-filters.php index a27b62591..92b9a9a33 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -178,7 +178,7 @@ class Filters extends TAINACAN_UnitTestCase { 'filter', array( 'name' => 'filter default', - 'collection_id' => 'filter_in_repository', + 'collection_id' => 'default', 'filter_type' => 'Tainacan\Filter_Types\Selectbox', 'metadatum_id' => $meta_repo->get_id(), 'status' => 'publish' From 4e5371ce6032bc0af0e969a59ce27f2f3aa7a2d0 Mon Sep 17 00:00:00 2001 From: Mateus Machado Luna Date: Tue, 1 Oct 2019 10:18:48 -0300 Subject: [PATCH 053/198] Several replacements of filter_in_repository by default. --- src/admin/components/other/activity-details-modal.vue | 2 +- src/admin/components/other/checkbox-radio-modal.vue | 6 +++--- src/classes/filter-types/autocomplete/Autocomplete.vue | 2 +- src/classes/filter-types/checkbox/Checkbox.vue | 2 +- .../filter-types/custom-interval/CustomInterval.vue | 2 +- src/classes/filter-types/date/Date.vue | 2 +- src/classes/filter-types/filter-types-mixin.js | 4 ++-- src/classes/filter-types/numeric/Numeric.vue | 2 +- src/classes/filter-types/selectbox/Selectbox.vue | 2 +- src/classes/filter-types/taginput/Taginput.vue | 2 +- src/classes/filter-types/taxonomy/Checkbox.vue | 4 ++-- src/classes/filter-types/taxonomy/Taginput.vue | 2 +- src/classes/repositories/class-tainacan-logs.php | 2 +- src/js/store/modules/filter/actions.js | 8 ++++---- 14 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/admin/components/other/activity-details-modal.vue b/src/admin/components/other/activity-details-modal.vue index 596c5a475..9dc34ba18 100644 --- a/src/admin/components/other/activity-details-modal.vue +++ b/src/admin/components/other/activity-details-modal.vue @@ -647,7 +647,7 @@