1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Libraries/HistoryUtils.php

237 lines
8.0 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Libraries;
2016-08-31 21:10:41 +02:00
2016-09-01 17:52:26 +02:00
use App\Models\Activity;
2017-01-30 20:40:43 +01:00
use App\Models\EntityModel;
use Session;
use stdClass;
2016-08-31 21:10:41 +02:00
class HistoryUtils
{
2016-09-01 17:52:26 +02:00
public static function loadHistory($users)
{
$userIds = [];
2018-02-15 09:49:46 +01:00
session([RECENTLY_VIEWED => false]);
2018-02-20 09:55:38 +01:00
2016-09-01 17:52:26 +02:00
if (is_array($users)) {
foreach ($users as $user) {
$userIds[] = $user->user_id;
}
} else {
$userIds[] = $users;
}
$activityTypes = [
ACTIVITY_TYPE_CREATE_CLIENT,
2016-09-06 13:55:15 +02:00
ACTIVITY_TYPE_CREATE_TASK,
ACTIVITY_TYPE_UPDATE_TASK,
ACTIVITY_TYPE_CREATE_EXPENSE,
ACTIVITY_TYPE_UPDATE_EXPENSE,
2016-09-01 17:52:26 +02:00
ACTIVITY_TYPE_CREATE_INVOICE,
ACTIVITY_TYPE_UPDATE_INVOICE,
ACTIVITY_TYPE_EMAIL_INVOICE,
ACTIVITY_TYPE_CREATE_QUOTE,
ACTIVITY_TYPE_UPDATE_QUOTE,
ACTIVITY_TYPE_EMAIL_QUOTE,
ACTIVITY_TYPE_VIEW_INVOICE,
ACTIVITY_TYPE_VIEW_QUOTE,
];
2018-01-25 10:26:53 +01:00
$activities = Activity::with(['client.contacts', 'invoice', 'task.project', 'expense'])
2016-09-01 17:52:26 +02:00
->whereIn('user_id', $userIds)
->whereIn('activity_type_id', $activityTypes)
2016-12-29 11:49:08 +01:00
->orderBy('id', 'desc')
2016-09-01 17:52:26 +02:00
->limit(100)
->get();
2017-01-30 17:05:31 +01:00
foreach ($activities->reverse() as $activity) {
2018-03-14 09:48:16 +01:00
if ($activity->client && $activity->client->is_deleted) {
continue;
}
2016-09-01 17:52:26 +02:00
if ($activity->activity_type_id == ACTIVITY_TYPE_CREATE_CLIENT) {
$entity = $activity->client;
2017-01-30 17:05:31 +01:00
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_CREATE_TASK || $activity->activity_type_id == ACTIVITY_TYPE_UPDATE_TASK) {
2016-09-06 13:55:15 +02:00
$entity = $activity->task;
2017-01-30 17:05:31 +01:00
if (! $entity) {
2016-09-19 09:04:30 +02:00
continue;
}
2016-09-06 13:55:15 +02:00
$entity->setRelation('client', $activity->client);
2018-01-25 10:26:53 +01:00
if ($entity->project) {
$project = $entity->project;
$project->setRelation('client', $activity->client);
static::trackViewed($project);
}
2017-01-30 17:05:31 +01:00
} elseif ($activity->activity_type_id == ACTIVITY_TYPE_CREATE_EXPENSE || $activity->activity_type_id == ACTIVITY_TYPE_UPDATE_EXPENSE) {
$entity = $activity->expense;
2017-01-30 17:05:31 +01:00
if (! $entity) {
continue;
}
$entity->setRelation('client', $activity->client);
2016-09-01 17:52:26 +02:00
} else {
$entity = $activity->invoice;
2017-01-30 17:05:31 +01:00
if (! $entity) {
2016-09-19 09:04:30 +02:00
continue;
}
2016-09-01 19:02:37 +02:00
$entity->setRelation('client', $activity->client);
2016-09-01 17:52:26 +02:00
}
static::trackViewed($entity);
}
}
2018-03-14 09:48:16 +01:00
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);
}
2016-08-31 21:10:41 +02:00
public static function trackViewed(EntityModel $entity)
{
2016-09-14 09:08:46 +02:00
$entityType = $entity->getEntityType();
$trackedTypes = [
ENTITY_CLIENT,
ENTITY_INVOICE,
ENTITY_QUOTE,
ENTITY_TASK,
2017-01-30 20:40:43 +01:00
ENTITY_EXPENSE,
2018-01-25 10:26:53 +01:00
ENTITY_PROJECT,
2018-02-08 12:41:22 +01:00
ENTITY_PROPOSAL,
2017-06-26 15:25:11 +02:00
//ENTITY_RECURRING_EXPENSE,
2016-09-14 09:08:46 +02:00
];
2017-01-30 17:05:31 +01:00
if (! in_array($entityType, $trackedTypes)) {
2016-08-31 21:10:41 +02:00
return;
}
2018-02-20 09:55:38 +01:00
if ($entity->is_deleted) {
return;
}
2017-01-30 20:40:43 +01:00
$object = static::convertToObject($entity);
2016-09-01 17:52:26 +02:00
$history = Session::get(RECENTLY_VIEWED) ?: [];
$accountHistory = isset($history[$entity->account_id]) ? $history[$entity->account_id] : [];
2016-08-31 21:10:41 +02:00
$data = [];
// Add to the list and make sure to only show each item once
2017-01-30 20:40:43 +01:00
for ($i = 0; $i < count($accountHistory); $i++) {
2016-09-01 17:52:26 +02:00
$item = $accountHistory[$i];
2016-08-31 21:10:41 +02:00
if ($object->url == $item->url) {
continue;
}
array_push($data, $item);
if (isset($counts[$item->accountId])) {
$counts[$item->accountId]++;
} else {
$counts[$item->accountId] = 1;
}
}
array_unshift($data, $object);
if (isset($counts[$entity->account_id]) && $counts[$entity->account_id] > RECENTLY_VIEWED_LIMIT) {
array_pop($data);
}
2016-09-01 17:52:26 +02:00
$history[$entity->account_id] = $data;
Session::put(RECENTLY_VIEWED, $history);
2016-08-31 21:10:41 +02:00
}
private static function convertToObject($entity)
{
$object = new stdClass();
2018-03-14 09:48:16 +01:00
$object->id = $entity->id;
2016-08-31 21:10:41 +02:00
$object->accountId = $entity->account_id;
$object->url = $entity->present()->url;
$object->entityType = $entity->subEntityType();
$object->name = $entity->present()->titledName;
$object->timestamp = time();
if ($entity->isEntityType(ENTITY_CLIENT)) {
$object->client_id = $entity->public_id;
$object->client_name = $entity->getDisplayName();
} elseif (method_exists($entity, 'client') && $entity->client) {
$object->client_id = $entity->client->public_id;
$object->client_name = $entity->client->getDisplayName();
2018-02-08 12:41:22 +01:00
} elseif (method_exists($entity, 'invoice') && $entity->invoice) {
$object->client_id = $entity->invoice->client->public_id;
$object->client_name = $entity->invoice->client->getDisplayName();
2016-08-31 21:10:41 +02:00
} else {
$object->client_id = 0;
$object->client_name = 0;
}
return $object;
}
2016-09-01 17:52:26 +02:00
public static function renderHtml($accountId)
2016-08-31 21:10:41 +02:00
{
$lastClientId = false;
$clientMap = [];
$str = '';
2016-09-01 17:52:26 +02:00
2016-08-31 21:11:43 +02:00
$history = Session::get(RECENTLY_VIEWED, []);
2016-09-01 17:52:26 +02:00
$history = isset($history[$accountId]) ? $history[$accountId] : [];
2016-08-31 21:10:41 +02:00
2017-01-30 17:05:31 +01:00
foreach ($history as $item) {
2016-08-31 21:10:41 +02:00
if ($item->entityType == ENTITY_CLIENT && isset($clientMap[$item->client_id])) {
continue;
}
$clientMap[$item->client_id] = true;
2017-01-30 17:05:31 +01:00
if ($lastClientId === false || $item->client_id != $lastClientId) {
2016-09-01 17:52:26 +02:00
$icon = '<i class="fa fa-users" style="width:32px"></i>';
2016-08-31 21:10:41 +02:00
if ($item->client_id) {
$link = url('/clients/' . $item->client_id);
2017-04-19 16:59:58 +02:00
$name = e($item->client_name);
2016-08-31 21:10:41 +02:00
$buttonLink = url('/invoices/create/' . $item->client_id);
2016-09-01 17:52:26 +02:00
$button = '<a type="button" class="btn btn-primary btn-sm pull-right" href="' . $buttonLink . '">
<i class="fa fa-plus-circle" style="width:20px" title="' . trans('texts.create_invoice') . '"></i>
2016-08-31 21:10:41 +02:00
</a>';
} else {
$link = '#';
$name = trans('texts.unassigned');
$button = '';
}
2018-02-08 11:39:58 +01:00
$padding = $str ? 16 : 0;
$str .= sprintf('<li style="margin-top: %spx">%s<a href="%s"><div>%s %s</div></a></li>', $padding, $button, $link, $icon, $name);
2016-08-31 21:10:41 +02:00
$lastClientId = $item->client_id;
}
if ($item->entityType == ENTITY_CLIENT) {
continue;
}
2016-09-01 17:52:26 +02:00
$icon = '<i class="fa fa-' . EntityModel::getIcon($item->entityType . 's') . '" style="width:24px"></i>';
2017-05-16 10:20:07 +02:00
$str .= sprintf('<li style="text-align:right; padding-right:18px;"><a href="%s">%s %s</a></li>', $item->url, e($item->name), $icon);
2016-08-31 21:10:41 +02:00
}
return $str;
}
}