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": { "dependencies": {
"axios": "^0.18.0", "axios": "^0.18.0",
"buefy": "^0.6.5", "buefy": "^0.6.5",
"bulma": "^0.6.2", "bulma": "^0.7.1",
"html-to-json": "^0.6.0", "html-to-json": "^0.6.0",
"mdi": "^2.2.43", "mdi": "^2.2.43",
"qs": "^6.5.1", "qs": "^6.5.1",
"moment": "^2.22.0", "moment": "^2.22.1",
"node-sass": "^4.8.3", "node-sass": "^4.8.3",
"sass-loader": "^6.0.7", "sass-loader": "^7.0.1",
"vue": "^2.5.16", "vue": "^2.5.16",
"vue-router": "^3.0.1", "vue-router": "^3.0.1",
"vuedraggable": "^2.16.0", "vuedraggable": "^2.16.0",
"vuex": "^3.0.1" "vuex": "^3.0.1"
}, },
"devDependencies": { "devDependencies": {
"autoprefixer": "^8.2.0", "autoprefixer": "^8.3.0",
"babel-core": "^6.26.0", "babel-core": "^6.26.0",
"babel-loader": "^7.1.4", "babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1", "babel-preset-env": "^1.6.1",
@ -31,20 +31,20 @@
"cross-env": "^5.1.4", "cross-env": "^5.1.4",
"css-loader": "^0.28.11", "css-loader": "^0.28.11",
"cypress": "^2.1.0", "cypress": "^2.1.0",
"element-theme-chalk": "^2.3.2", "element-theme-chalk": "^2.3.6",
"eslint": "^4.19.1", "eslint": "^4.19.1",
"eslint-loader": "^2.0.0", "eslint-loader": "^2.0.0",
"eslint-plugin-vue": "^4.4.0", "eslint-plugin-vue": "^4.5.0",
"file-loader": "^1.1.11", "file-loader": "^1.1.11",
"postcss-loader": "^2.1.3", "postcss-loader": "^2.1.4",
"sass-resources-loader": "^1.3.3", "sass-resources-loader": "^1.3.3",
"style-loader": "^0.20.3", "style-loader": "^0.21.0",
"uglifyjs-webpack-plugin": "^1.2.4", "uglifyjs-webpack-plugin": "^1.2.5",
"vue-custom-element": "^2.1.0", "vue-custom-element": "^3.0.4",
"vue-loader": "^14.2.2", "vue-loader": "^14.2.2",
"vue-template-compiler": "^2.5.16", "vue-template-compiler": "^2.5.16",
"webpack": "^4.4.1", "webpack": "^4.6.0",
"webpack-cli": "^2.0.13", "webpack-cli": "^2.0.15",
"webpack-dev-server": "^3.1.1" "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="is-fullheight">
<div class="page-container primary-page"> <div class="page-container primary-page">
<div class="title">{{ event.description }}</div> <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-left"/>
<div class="level-right"> <div class="level-right">
<div class="level-item"> <div class="level-item">
@ -20,10 +22,17 @@
<hr class="divider"> <hr class="divider">
<div v-if="event.title !== undefined && event.title.includes('updated')">
<component <component
:is="comp" :is="comp"
:event="event"/> :event="event"/>
</div> </div>
<div v-else-if="event.title !== undefined">
<no-diff :event="event" />
</div>
</div>
</div> </div>
</div> </div>
</template> </template>
@ -31,8 +40,9 @@
<script> <script>
import {mapActions, mapGetters} from 'vuex'; import {mapActions, mapGetters} from 'vuex';
import Split from '../../components/other/event-diff/event-split.vue'; import Split from '../../components/other/event/diff-exhibition/event-split.vue';
import Unified from '../../components/other/event-diff/event-unified.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 { export default {
name: 'EventPage', name: 'EventPage',
@ -57,7 +67,8 @@
}, },
components: { components: {
Split, Split,
Unified Unified,
NoDiff
}, },
created() { created() {
this.eventId = parseInt(this.$route.params.eventId); 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'])) { if(!isset($request['fetch_only'])) {
$item_array = $item->__toArray(); $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['value']);
unset($item_array['old_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] ) ); $deleted = new Entities\Collection( wp_delete_post( $args[0], $args[1] ) );
if($deleted) { if($deleted) {
do_action( 'tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true ); do_action( 'tainacan-deleted', $deleted, [], false, true );
} }
return $deleted; return $deleted;
@ -276,7 +276,7 @@ class Collections extends Repository {
$trashed = new Entities\Collection( wp_trash_post( $args[0] ) ); $trashed = new Entities\Collection( wp_trash_post( $args[0] ) );
if($trashed) { if($trashed) {
do_action( 'tainacan-trashed', $trashed, $is_update = false, $is_delete_permanently = false ); do_action( 'tainacan-trashed', $trashed, [], false, false, true );
} }
return $trashed; return $trashed;

View File

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

View File

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

View File

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

View File

@ -28,12 +28,11 @@ class Logs extends Repository {
protected function __construct() { protected function __construct() {
parent::__construct(); parent::__construct();
add_action( 'tainacan-insert', 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, 4 ); add_action( 'tainacan-deleted', array( $this, 'insert_log'), 10, 5 );
add_action( 'tainacan-trashed', array( $this, 'insert_log'), 10, 4 ); add_action( 'tainacan-trashed', array( $this, 'insert_log'), 10, 5 );
add_action( 'add_attachment', array( $this, 'prepare_attachment_log_before_insert' ), 10 ); 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() { public function get_map() {
@ -223,10 +222,7 @@ 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 ) {
if ( ! $post_after && ! $post_before ) {
// is add attachment
$attachment = get_post( $post_ID ); $attachment = get_post( $post_ID );
$post = $attachment->post_parent; $post = $attachment->post_parent;
@ -241,8 +237,8 @@ class Logs extends Repository {
// get all attachments except the new // get all attachments except the new
$old_attachments = $tainacan_post->get_attachments( $post_ID ); $old_attachments = $tainacan_post->get_attachments( $post_ID );
foreach ( $old_attachments as $attachment ) { foreach ( $old_attachments as $index => $a ) {
unset( $attachment['id'] ); unset( $old_attachments[$index]['id'] );
} }
$new_attachments[] = [ $new_attachments[] = [
@ -265,9 +261,7 @@ class Logs extends Repository {
} }
} }
} else {
// TODO: Save a log when a attachment is updated
}
} }
/** /**
@ -275,15 +269,17 @@ class Logs extends Repository {
* *
* @param Entity $new_value * @param Entity $new_value
* @param array $diffs * @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 * @return Entities\Log new created log
*/ */
public function insert_log( $new_value, $diffs = [], $is_update = null, $is_delete_permanently = null ) { public function insert_log( $new_value, $diffs = [], $is_update = false, $is_delete = false, $is_trash = false ) {
$msn = ""; $msn = null;
$description = ""; $description = null;
// TODO: Continue with is_delete_permanently
if ( is_object( $new_value ) ) { if ( is_object( $new_value ) ) {
// do not log a log // 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' ) { if ( ( method_exists( $new_value, 'get_post_type' ) && $new_value->get_post_type() === 'tainacan-log' ) || $new_value->get_status() === 'auto-draft' ) {
@ -308,26 +304,62 @@ class Logs extends Repository {
$name = $new_value->get_status(); $name = $new_value->get_status();
} }
if ( $is_update ) {
$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');
} 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 );
}
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'; $articleA = 'A';
$articleAn = 'An'; $articleAn = 'An';
$vowels = 'aeiou'; $vowels = 'aeiou';
if ( $is_update ) {
if ( substr_count( $vowels, strtolower( substr( $class_name, 0, 1 ) ) ) > 0 ) { if ( substr_count( $vowels, strtolower( substr( $class_name, 0, 1 ) ) ) > 0 ) {
$msn = sprintf( __( '%s %s has been updated.', 'tainacan' ), $articleAn, $class_name ); $msn = sprintf( __( '%s %s has been %s.', 'tainacan' ), $articleAn, $class_name, $action_message );
} else { } else {
$msn = sprintf( __( '%s %s has been updated.', 'tainacan' ), $articleA, $class_name ); $msn = sprintf( __( '%s %s has been %s.', 'tainacan' ), $articleA, $class_name, $action_message );
}
$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 );
} }
if ( $new_value instanceof Entities\Field ) { return $msn;
$collection = $new_value->get_collection(); }
/**
* 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; $parent = $collection;
if ( $collection ) { if ( $collection ) {
@ -338,19 +370,12 @@ class Logs extends Repository {
} }
} }
$description = sprintf( __( "The \"%s\" %s has been created on %s.", 'tainacan' ), $name, strtolower( $class_name ), $parent ); $description = sprintf( __( "The \"%s\" %s has been %s (parent %s).", 'tainacan' ), $name, strtolower( $class_name ), $action_message, $parent );
} else { } else {
$description = sprintf( __( "The \"%s\" %s has been created.", 'tainacan' ), $name, strtolower( $class_name ) ); $description = sprintf( __( "The \"%s\" %s has been %s.", 'tainacan' ), $name, strtolower( $class_name ), $action_message );
}
}
} }
$msn = apply_filters( 'tainacan-insert-log-message-title', $msn, $type, $new_value ); return $description;
$description = apply_filters( 'tainacan-insert-log-description', $description, $type, $new_value );
if ( ! empty( $diffs ) ) {
return Entities\Log::create( $msn, $description, $new_value, $diffs );
}
} }
/** /**
@ -366,12 +391,14 @@ class Logs extends Repository {
/** @var Entity $value * */ /** @var Entity $value * */
$value = $log->get_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 ); $repository = self::get_repository( $value );
if($value->validate()) {
return $repository->insert( $value ); return $repository->insert( $value );
} }
}
return false; return false;
} }

View File

@ -680,9 +680,14 @@ abstract class Repository {
$entity_type = get_class( $new ); $entity_type = get_class( $new );
$old_entity = new $entity_type; // there is no saved entity, let compare with a new empty one $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 { } else {
$old_entity = $old; $old_entity = $old;
} }
}
$new_entity = $new; $new_entity = $new;
@ -691,6 +696,7 @@ abstract class Repository {
$diff = []; $diff = [];
foreach ( $map as $prop => $mapped ) { 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 ( $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 ) ) ) ) { 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 ); $diff = apply_filters( 'tainacan-entity-diff', $diff, $new, $old );
return $diff; return $diff;

View File

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

View File

@ -230,7 +230,7 @@ class Terms extends Repository {
if($deleted) { if($deleted) {
$deleted_term_tainacan = new Entities\Term($args[0], $args[1]); $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; return $deleted;