1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00

Delete from history when deleted

This commit is contained in:
Hillel Coren 2018-03-14 10:48:16 +02:00
parent 2028932a94
commit bb8c2f80f6
6 changed files with 183 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace App\Events;
use App\Models\Project;
use Illuminate\Queue\SerializesModels;
/**
* Class ProjectWasDeleted.
*/
class ProjectWasDeleted extends Event
{
use SerializesModels;
/**
* @var Prooject
*/
public $project;
/**
* Create a new event instance.
*
* @param Invoice $invoice
*/
public function __construct(Project $project)
{
$this->project = $project;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Events;
use App\Models\Proposal;
use Illuminate\Queue\SerializesModels;
/**
* Class ProposalWasDeleted.
*/
class ProposalWasDeleted extends Event
{
use SerializesModels;
/**
* @var Proposal
*/
public $proposal;
/**
* Create a new event instance.
*
* @param Invoice $invoice
*/
public function __construct(Proposal $proposal)
{
$this->proposal = $proposal;
}
}

View File

@ -46,6 +46,10 @@ class HistoryUtils
->get();
foreach ($activities->reverse() as $activity) {
if ($activity->client && $activity->client->is_deleted) {
continue;
}
if ($activity->activity_type_id == ACTIVITY_TYPE_CREATE_CLIENT) {
$entity = $activity->client;
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_CREATE_TASK || $activity->activity_type_id == ACTIVITY_TYPE_UPDATE_TASK) {
@ -78,6 +82,28 @@ class HistoryUtils
}
}
public static function deleteHistory(EntityModel $entity)
{
$history = Session::get(RECENTLY_VIEWED) ?: [];
$accountHistory = isset($history[$entity->account_id]) ? $history[$entity->account_id] : [];
$remove = [];
for ($i=0; $i<count($accountHistory); $i++) {
$item = $accountHistory[$i];
if ($entity->equalTo($item)) {
$remove[] = $i;
} elseif ($entity->getEntityType() == ENTITY_CLIENT && $entity->public_id == $item->client_id) {
$remove[] = $i;
}
}
for ($i=count($remove) - 1; $i>=0; $i--) {
array_splice($history[$entity->account_id], $remove[$i], 1);
}
Session::put(RECENTLY_VIEWED, $history);
}
public static function trackViewed(EntityModel $entity)
{
$entityType = $entity->getEntityType();
@ -136,6 +162,7 @@ class HistoryUtils
private static function convertToObject($entity)
{
$object = new stdClass();
$object->id = $entity->id;
$object->accountId = $entity->account_id;
$object->url = $entity->present()->url;
$object->entityType = $entity->subEntityType();

View File

@ -0,0 +1,74 @@
<?php
namespace App\Listeners;
use App\Events\InvoiceWasDeleted;
use App\Events\ClientWasDeleted;
use App\Events\QuoteWasDeleted;
use App\Events\TaskWasDeleted;
use App\Events\ExpenseWasDeleted;
use App\Events\ProjectWasDeleted;
use App\Events\ProposalWasDeleted;
use App\Libraries\HistoryUtils;
/**
* Class InvoiceListener.
*/
class HistoryListener
{
/**
* @param ClientWasDeleted $event
*/
public function deletedClient(ClientWasDeleted $event)
{
HistoryUtils::deleteHistory($event->client);
}
/**
* @param InvoiceWasDeleted $event
*/
public function deletedInvoice(InvoiceWasDeleted $event)
{
HistoryUtils::deleteHistory($event->invoice);
}
/**
* @param QuoteWasDeleted $event
*/
public function deletedQuote(QuoteWasDeleted $event)
{
HistoryUtils::deleteHistory($event->quote);
}
/**
* @param TaskWasDeleted $event
*/
public function deletedTask(TaskWasDeleted $event)
{
HistoryUtils::deleteHistory($event->task);
}
/**
* @param ExpenseWasDeleted $event
*/
public function deletedExpense(ExpenseWasDeleted $event)
{
HistoryUtils::deleteHistory($event->expense);
}
/**
* @param ProjectWasDeleted $event
*/
public function deletedProject(ProjectWasDeleted $event)
{
HistoryUtils::deleteHistory($event->project);
}
/**
* @param ProposalWasDeleted $event
*/
public function deletedProposal(ProposalWasDeleted $event)
{
HistoryUtils::deleteHistory($event->proposal);
}
}

View File

@ -427,4 +427,13 @@ class EntityModel extends Eloquent
throw $exception;
}
}
public function equalTo($obj)
{
if (empty($obj->id)) {
return false;
}
return $this->id == $obj->id && $this->getEntityType() == $obj->entityType;
}
}

View File

@ -27,6 +27,7 @@ class EventServiceProvider extends ServiceProvider
'App\Events\ClientWasDeleted' => [
'App\Listeners\ActivityListener@deletedClient',
'App\Listeners\SubscriptionListener@deletedClient',
'App\Listeners\HistoryListener@deletedClient',
],
'App\Events\ClientWasRestored' => [
'App\Listeners\ActivityListener@restoredClient',
@ -54,6 +55,7 @@ class EventServiceProvider extends ServiceProvider
'App\Listeners\ActivityListener@deletedInvoice',
'App\Listeners\TaskListener@deletedInvoice',
'App\Listeners\ExpenseListener@deletedInvoice',
'App\Listeners\HistoryListener@deletedInvoice',
],
'App\Events\InvoiceWasRestored' => [
'App\Listeners\ActivityListener@restoredInvoice',
@ -89,6 +91,7 @@ class EventServiceProvider extends ServiceProvider
],
'App\Events\QuoteWasDeleted' => [
'App\Listeners\ActivityListener@deletedQuote',
'App\Listeners\HistoryListener@deletedQuote',
],
'App\Events\QuoteWasRestored' => [
'App\Listeners\ActivityListener@restoredQuote',
@ -188,6 +191,7 @@ class EventServiceProvider extends ServiceProvider
'App\Events\TaskWasDeleted' => [
'App\Listeners\ActivityListener@deletedTask',
'App\Listeners\SubscriptionListener@deletedTask',
'App\Listeners\HistoryListener@deletedTask',
],
// Vendor events
@ -219,6 +223,17 @@ class EventServiceProvider extends ServiceProvider
'App\Events\ExpenseWasDeleted' => [
'App\Listeners\ActivityListener@deletedExpense',
'App\Listeners\SubscriptionListener@deletedExpense',
'App\Listeners\HistoryListener@deletedExpense',
],
// Project events
'App\Events\ProjectWasDeleted' => [
'App\Listeners\HistoryListener@deletedProject',
],
// Proposal events
'App\Events\ProposalWasDeleted' => [
'App\Listeners\HistoryListener@deletedProposal',
],
'Illuminate\Queue\Events\JobExceptionOccurred' => [