Log for trash, delete and attachments.

This commit is contained in:
weryques 2018-04-24 16:29:28 -03:00
parent 863f4ad742
commit 999e7ea00f
15 changed files with 1112 additions and 1552 deletions

2293
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,20 +9,20 @@
"dependencies": {
"axios": "^0.18.0",
"buefy": "^0.6.5",
"bulma": "^0.6.2",
"bulma": "^0.7.1",
"html-to-json": "^0.6.0",
"mdi": "^2.2.43",
"qs": "^6.5.1",
"moment": "^2.22.0",
"moment": "^2.22.1",
"node-sass": "^4.8.3",
"sass-loader": "^6.0.7",
"sass-loader": "^7.0.1",
"vue": "^2.5.16",
"vue-router": "^3.0.1",
"vuedraggable": "^2.16.0",
"vuex": "^3.0.1"
},
"devDependencies": {
"autoprefixer": "^8.2.0",
"autoprefixer": "^8.3.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
@ -31,20 +31,20 @@
"cross-env": "^5.1.4",
"css-loader": "^0.28.11",
"cypress": "^2.1.0",
"element-theme-chalk": "^2.3.2",
"element-theme-chalk": "^2.3.6",
"eslint": "^4.19.1",
"eslint-loader": "^2.0.0",
"eslint-plugin-vue": "^4.4.0",
"eslint-plugin-vue": "^4.5.0",
"file-loader": "^1.1.11",
"postcss-loader": "^2.1.3",
"postcss-loader": "^2.1.4",
"sass-resources-loader": "^1.3.3",
"style-loader": "^0.20.3",
"uglifyjs-webpack-plugin": "^1.2.4",
"vue-custom-element": "^2.1.0",
"style-loader": "^0.21.0",
"uglifyjs-webpack-plugin": "^1.2.5",
"vue-custom-element": "^3.0.4",
"vue-loader": "^14.2.2",
"vue-template-compiler": "^2.5.16",
"webpack": "^4.4.1",
"webpack-cli": "^2.0.13",
"webpack-dev-server": "^3.1.1"
"webpack": "^4.6.0",
"webpack-cli": "^2.0.15",
"webpack-dev-server": "^3.1.3"
}
}

View File

@ -0,0 +1,92 @@
<template>
<div>
<div
class="tile is-ancestor"
v-if="event.log_diffs.constructor === Object &&
Object.keys(event.log_diffs).length > 0 ||
event.log_diffs.length > 0">
<div class="tile is-parent">
<article class="tile box is-child">
<div class="content">
<div
v-for="(diff, key) in event.log_diffs"
:key="key">
<p/>
<div class="has-text-weight-bold is-capitalized">
{{ `${key.replace('_', ' ')}:` }}
</div>
<div v-if="key === 'featured_image'">
<div class="image is-128x128">
<img :src="diff.new">
</div>
</div>
<div
v-else
v-for="(d, i) in diff.new"
:key="i"
class="content is-inline" >
<div v-if="d.hasOwnProperty('mime_type') && d.mime_type.includes('image') && key === 'attachments'">
<article class="media">
<div class="media-left">
<p class="image is-64x64"><img :src="d.url"></p>
</div>
<div class="media-content">
<div class="content">
<p>
<strong class="is-capitalized">{{ d.title }}</strong> <small class="tag is-light">{{ d.mime_type }}</small>
<br>
{{ d.description }}
</p>
</div>
</div>
</article>
</div>
<div
v-else-if="key === 'fields_order' || key === 'filters_order'"
class="is-capitalized">
{{ `ID: ${d.id} Enabled: ${d.enabled}` }}
</div>
<div
class="is-inline"
v-else-if="!Array.isArray(d) && d.constructor.name !== 'Object' ">{{ d }}
</div>
<div
v-else
v-for="(e, i2) in d"
:key="i2"
class="is-inline">
<div class="is-capitalized">
{{ `${i2.replace('_', ' ')}: ${e} ` }}
</div>
</div>
</div>
</div>
</div>
</article>
</div>
</div>
</div>
</template>
<script>
export default {
name: "EventNoDiff",
props: {
event: Object
}
}
</script>
<style scoped>
</style>

View File

@ -3,7 +3,9 @@
<div class="is-fullheight">
<div class="page-container primary-page">
<div class="title">{{ event.description }}</div>
<div class="level">
<div
class="level"
v-if="event.title !== undefined && event.title.includes('updated')">
<div class="level-left"/>
<div class="level-right">
<div class="level-item">
@ -20,9 +22,16 @@
<hr class="divider">
<component
:is="comp"
:event="event"/>
<div v-if="event.title !== undefined && event.title.includes('updated')">
<component
:is="comp"
:event="event"/>
</div>
<div v-else-if="event.title !== undefined">
<no-diff :event="event" />
</div>
</div>
</div>
</div>
@ -31,8 +40,9 @@
<script>
import {mapActions, mapGetters} from 'vuex';
import Split from '../../components/other/event-diff/event-split.vue';
import Unified from '../../components/other/event-diff/event-unified.vue';
import Split from '../../components/other/event/diff-exhibition/event-split.vue';
import Unified from '../../components/other/event/diff-exhibition/event-unified.vue';
import NoDiff from '../../components/other/event/unique-exhibition/event-nodiff.vue';
export default {
name: 'EventPage',
@ -57,7 +67,8 @@
},
components: {
Split,
Unified
Unified,
NoDiff
},
created() {
this.eventId = parseInt(this.$route.params.eventId);

View File

@ -71,12 +71,6 @@ class REST_Logs_Controller extends REST_Controller {
if(!isset($request['fetch_only'])) {
$item_array = $item->__toArray();
// if ( $request['context'] === 'edit' ) {
// $log_diff = $item->diff();
//
// $item_array['log_diff'] = $log_diff;
// }
unset($item_array['value']);
unset($item_array['old_value']);

View File

@ -267,7 +267,7 @@ class Collections extends Repository {
$deleted = new Entities\Collection( wp_delete_post( $args[0], $args[1] ) );
if($deleted) {
do_action( 'tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true );
do_action( 'tainacan-deleted', $deleted, [], false, true );
}
return $deleted;
@ -276,7 +276,7 @@ class Collections extends Repository {
$trashed = new Entities\Collection( wp_trash_post( $args[0] ) );
if($trashed) {
do_action( 'tainacan-trashed', $trashed, $is_update = false, $is_delete_permanently = false );
do_action( 'tainacan-trashed', $trashed, [], false, false, true );
}
return $trashed;

View File

@ -454,7 +454,7 @@ class Fields extends Repository {
$deleted = new Entities\Field( wp_trash_post( $field_id ) );
if($deleted) {
do_action( 'tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true );
do_action( 'tainacan-deleted', $deleted, [], false, true );
}
return $deleted;

View File

@ -190,7 +190,7 @@ class Filters extends Repository {
$deleted = new Entities\Filter(wp_delete_post($args[0], $args[1]));
if($deleted){
do_action('tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true);
do_action('tainacan-deleted', $deleted, [], false, true);
}
return $deleted;
@ -200,7 +200,7 @@ class Filters extends Repository {
$trashed = new Entities\Filter(wp_trash_post($args[0]));
if($trashed){
do_action('tainacan-trashed', $trashed, $is_update = false, $is_delete_permanently = false);
do_action('tainacan-trashed', $trashed, [], false, false, true);
}
return $trashed;

View File

@ -295,7 +295,7 @@ class Items extends Repository {
$deleted = new Entities\Item( wp_delete_post( $args[0], $args[1] ) );
if($deleted) {
do_action( 'tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true );
do_action( 'tainacan-deleted', $deleted, false, true );
}
return $deleted;
@ -304,7 +304,7 @@ class Items extends Repository {
$trashed = new Entities\Item( wp_trash_post( $args[0] ) );
if($trashed) {
do_action( 'tainacan-trashed', $trashed, $is_update = false, $is_delete_permanently = false );
do_action( 'tainacan-trashed', $trashed, [], false, false, true );
}
return $trashed;

View File

@ -28,12 +28,11 @@ class Logs extends Repository {
protected function __construct() {
parent::__construct();
add_action( 'tainacan-insert', array( $this, 'insert_log' ), 10, 4 );
add_action( 'tainacan-deleted', array( $this, 'insert_log'), 10, 4 );
add_action( 'tainacan-trashed', array( $this, 'insert_log'), 10, 4 );
add_action( 'tainacan-insert', array( $this, 'insert_log' ), 10, 5 );
add_action( 'tainacan-deleted', array( $this, 'insert_log'), 10, 5 );
add_action( 'tainacan-trashed', array( $this, 'insert_log'), 10, 5 );
add_action( 'add_attachment', array( $this, 'prepare_attachment_log_before_insert' ), 10 );
add_action( 'attachment_updated', array( $this, 'prepare_attachment_log_before_insert' ), 10, 3);
}
public function get_map() {
@ -223,51 +222,46 @@ class Logs extends Repository {
}
public function prepare_attachment_log_before_insert( $post_ID, $post_after = null, $post_before = null ) {
public function prepare_attachment_log_before_insert( $post_ID ) {
$attachment = get_post( $post_ID );
$post = $attachment->post_parent;
if ( ! $post_after && ! $post_before ) {
// is add attachment
$attachment = get_post( $post_ID );
$post = $attachment->post_parent;
if ( $post ) {
// was added attachment on a tainacan object
if ( $post ) {
// was added attachment on a tainacan object
$tainacan_post = Repository::get_entity_by_post( $post );
$tainacan_post = Repository::get_entity_by_post( $post );
if($tainacan_post) {
// was added a normal attachment
if($tainacan_post) {
// was added a normal attachment
// get all attachments except the new
$old_attachments = $tainacan_post->get_attachments( $post_ID );
foreach ( $old_attachments as $attachment ) {
unset( $attachment['id'] );
}
$new_attachments[] = [
'title' => $attachment->post_title,
'description' => $attachment->post_content,
'mime_type' => $attachment->post_mime_type,
'url' => $attachment->guid,
];
$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
];
$this->insert_log( $tainacan_post, $diff, true );
// get all attachments except the new
$old_attachments = $tainacan_post->get_attachments( $post_ID );
foreach ( $old_attachments as $index => $a ) {
unset( $old_attachments[$index]['id'] );
}
$new_attachments[] = [
'title' => $attachment->post_title,
'description' => $attachment->post_content,
'mime_type' => $attachment->post_mime_type,
'url' => $attachment->guid,
];
$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
];
$this->insert_log( $tainacan_post, $diff, true );
}
} else {
// TODO: Save a log when a attachment is updated
}
}
/**
@ -275,15 +269,17 @@ class Logs extends Repository {
*
* @param Entity $new_value
* @param array $diffs
* @param null $is_update
* @param bool $is_update
*
* @param bool $is_delete
* @param bool $is_trash
*
* @return Entities\Log new created log
*/
public function insert_log( $new_value, $diffs = [], $is_update = null, $is_delete_permanently = null ) {
$msn = "";
$description = "";
public function insert_log( $new_value, $diffs = [], $is_update = false, $is_delete = false, $is_trash = false ) {
$msn = null;
$description = null;
// TODO: Continue with is_delete_permanently
if ( is_object( $new_value ) ) {
// do not log a log
if ( ( method_exists( $new_value, 'get_post_type' ) && $new_value->get_post_type() === 'tainacan-log' ) || $new_value->get_status() === 'auto-draft' ) {
@ -308,51 +304,80 @@ class Logs extends Repository {
$name = $new_value->get_status();
}
$articleA = 'A';
$articleAn = 'An';
$vowels = 'aeiou';
if ( $is_update ) {
if ( substr_count( $vowels, strtolower( substr( $class_name, 0, 1 ) ) ) > 0 ) {
$msn = sprintf( __( '%s %s has been updated.', 'tainacan' ), $articleAn, $class_name );
} else {
$msn = sprintf( __( '%s %s has been updated.', 'tainacan' ), $articleA, $class_name );
}
$description = sprintf( __( "The \"%s\" %s has been updated.", 'tainacan' ), $name, strtolower( $class_name ) );
} else {
if ( substr_count( $vowels, strtolower( substr( $class_name, 0, 1 ) ) ) > 0 ) {
$msn = sprintf( __( '%s %s has been created.', 'tainacan' ), $articleAn, $class_name );
} else {
$msn = sprintf( __( '%s %s has been created.', 'tainacan' ), $articleA, $class_name );
}
$msn = $this->prepare_event_message($class_name, 'updated');
$description = $this->prepare_description_message($new_value, $name, $class_name, 'updated');
} elseif( $is_delete ){
// was deleted
$msn = $this->prepare_event_message($class_name, 'deleted');
$description = $this->prepare_description_message($new_value, $name, $class_name, 'deleted');
} elseif( !empty($diffs) ) {
// was created
$msn = $this->prepare_event_message($class_name, 'created');
$description = $this->prepare_description_message($new_value, $name, $class_name, 'created');
if ( $new_value instanceof Entities\Field ) {
$collection = $new_value->get_collection();
$parent = $collection;
if ( $collection ) {
$parent = $collection->get_name();
if ( ! $parent ) {
$parent = $collection->get_status();
}
}
$description = sprintf( __( "The \"%s\" %s has been created on %s.", 'tainacan' ), $name, strtolower( $class_name ), $parent );
} else {
$description = sprintf( __( "The \"%s\" %s has been created.", 'tainacan' ), $name, strtolower( $class_name ) );
}
} elseif( $is_trash ) {
// was trashed
$msn = $this->prepare_event_message($class_name, 'trashed');
$description = $this->prepare_description_message($new_value, $name, $class_name, 'trashed');
}
$msn = apply_filters( 'tainacan-insert-log-message-title', $msn, $type, $new_value );
$description = apply_filters( 'tainacan-insert-log-description', $description, $type, $new_value );
}
$msn = apply_filters( 'tainacan-insert-log-message-title', $msn, $type, $new_value );
$description = apply_filters( 'tainacan-insert-log-description', $description, $type, $new_value );
if ( ! empty( $diffs ) ) {
if ( ! empty( $diffs ) || $is_delete || $is_trash ) {
return Entities\Log::create( $msn, $description, $new_value, $diffs );
}
}
private function prepare_event_message($class_name, $action_message){
$articleA = 'A';
$articleAn = 'An';
$vowels = 'aeiou';
if ( substr_count( $vowels, strtolower( substr( $class_name, 0, 1 ) ) ) > 0 ) {
$msn = sprintf( __( '%s %s has been %s.', 'tainacan' ), $articleAn, $class_name, $action_message );
} else {
$msn = sprintf( __( '%s %s has been %s.', 'tainacan' ), $articleA, $class_name, $action_message );
}
return $msn;
}
/**
* This will prepare the event description for objects
*
* @param $object
* @param $name
* @param $class_name
*
* @param $action_message
*
* @return string
*/
private function prepare_description_message($object, $name, $class_name, $action_message){
if ( $object instanceof Entities\Field || $object instanceof Entities\Item || $object instanceof Entities\Filter) {
$collection = $object->get_collection();
$parent = $collection;
if ( $collection ) {
$parent = $collection->get_name();
if ( ! $parent ) {
$parent = $collection->get_status();
}
}
$description = sprintf( __( "The \"%s\" %s has been %s (parent %s).", 'tainacan' ), $name, strtolower( $class_name ), $action_message, $parent );
} else {
$description = sprintf( __( "The \"%s\" %s has been %s.", 'tainacan' ), $name, strtolower( $class_name ), $action_message );
}
return $description;
}
/**
*
* @param Entities\Log $log
@ -366,11 +391,13 @@ class Logs extends Repository {
/** @var Entity $value * */
$value = $log->get_value();
//$value->set_status('publish'); // TODO check if publish the entity on approve
$value->set_status('publish'); // TODO check if publish the entity on approve
$repository = self::get_repository( $value );
return $repository->insert( $value );
if($value->validate()) {
return $repository->insert( $value );
}
}
return false;

View File

@ -681,7 +681,12 @@ abstract class Repository {
$old_entity = new $entity_type; // there is no saved entity, let compare with a new empty one
}
} else {
$old_entity = $old;
if($old->get_status() === 'auto-draft'){
$entity_type = get_class( $new );
$old_entity = new $entity_type;
} else {
$old_entity = $old;
}
}
$new_entity = $new;
@ -691,6 +696,7 @@ abstract class Repository {
$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 ) ) ) ) {
@ -729,6 +735,7 @@ abstract class Repository {
}
}
unset($diff['id'], $diff['collection_id'], $diff['author_id'], $diff['creation_date']);
$diff = apply_filters( 'tainacan-entity-diff', $diff, $new, $old );
return $diff;

View File

@ -199,7 +199,7 @@ class Taxonomies extends Repository {
return $deleted;
}
do_action('tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true);
do_action('tainacan-deleted', $deleted, [], false, true);
return $deleted;
}
@ -210,7 +210,7 @@ class Taxonomies extends Repository {
return $trashed;
}
do_action('tainacan-trashed', $trashed, $diffs = [], $is_update = false, $is_delete_permanently = false );
do_action('tainacan-trashed', $trashed, [], false, false, true );
return $trashed;
}

View File

@ -230,7 +230,7 @@ class Terms extends Repository {
if($deleted) {
$deleted_term_tainacan = new Entities\Term($args[0], $args[1]);
do_action( 'tainacan-deleted', $deleted_term_tainacan, $is_update = false, $is_delete_permanently = true );
do_action( 'tainacan-deleted', $deleted_term_tainacan, [], false, true );
}
return $deleted;