Skip to content

Commit

Permalink
Working on history sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
hillelcoren committed Aug 31, 2016
1 parent a2f7d32 commit db0c305
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 27 deletions.
2 changes: 0 additions & 2 deletions app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ public function store(CreateClientRequest $request)
public function show(ClientRequest $request)
{
$client = $request->entity();

$user = Auth::user();
Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT);

$actionLinks = [];
if($user->can('create', ENTITY_TASK)){
Expand Down
7 changes: 6 additions & 1 deletion app/Http/Requests/EntityRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Input;
use Utils;
use App\Libraries\HistoryUtils;

class EntityRequest extends Request {

Expand Down Expand Up @@ -52,7 +53,10 @@ public function setEntity($entity)
public function authorize()
{
if ($this->entity()) {
return $this->user()->can('view', $this->entity());
if ($this->user()->can('view', $this->entity())) {
HistoryUtils::trackViewed($this->entity());
return true;
}
} else {
return $this->user()->can('create', $this->entityType);
}
Expand All @@ -62,4 +66,5 @@ public function rules()
{
return [];
}

}
2 changes: 1 addition & 1 deletion app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@
define('ENV_DEVELOPMENT', 'local');
define('ENV_STAGING', 'staging');

define('RECENTLY_VIEWED', 'RECENTLY_VIEWED');
define('RECENTLY_VIEWED', 'recent_history');

define('ENTITY_CLIENT', 'client');
define('ENTITY_CONTACT', 'contact');
Expand Down
117 changes: 117 additions & 0 deletions app/Libraries/HistoryUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php namespace App\Libraries;

use Request;
use stdClass;
use Session;
use App\Models\EntityModel;

class HistoryUtils
{
public static function trackViewed(EntityModel $entity)
{
if ($entity->isEntityType(ENTITY_CREDIT) || $entity->isEntityType(ENTITY_VENDOR)) {
return;
}

$object = static::convertToObject($entity);
$history = Session::get(RECENTLY_VIEWED);
$data = [];

// Add to the list and make sure to only show each item once
for ($i = 0; $i<count($history); $i++) {
$item = $history[$i];

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);
}

//$data = [];
Session::put(RECENTLY_VIEWED, $data);
}

private static function convertToObject($entity)
{
$object = new stdClass();
$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();
} else {
$object->client_id = 0;
$object->client_name = 0;
}

return $object;
}

public static function renderHtml()
{
$lastClientId = false;
$clientMap = [];
$str = '';

foreach (Session::get(RECENTLY_VIEWED) as $item)
{
if ($item->entityType == ENTITY_CLIENT && isset($clientMap[$item->client_id])) {
continue;
}

$clientMap[$item->client_id] = true;

if ($lastClientId === false || $item->client_id != $lastClientId)
{
$icon = '<i class="fa fa-users" style="width:30px"></i>';
if ($item->client_id) {
$link = url('/clients/' . $item->client_id);
$name = $item->client_name ;

$buttonLink = url('/invoices/create/' . $item->client_id);
$button = '<a type="button" class="btn btn-primary btn-sm pull-right" style="margin-top:5px; margin-right:10px; text-indent:0px"
href="' . $buttonLink . '">
<i class="fa fa-plus-circle" style="width:20px" title="' . trans('texts.create_new') . '"></i>
</a>';
} else {
$link = '#';
$name = trans('texts.unassigned');
$button = '';
}

$str .= sprintf('<li>%s<a href="%s"><div>%s %s</div></a></li>', $button, $link, $icon, $name);
$lastClientId = $item->client_id;
}

if ($item->entityType == ENTITY_CLIENT) {
continue;
}

$icon = '<i class="fa fa-' . EntityModel::getIcon($item->entityType . 's') . '"></i>';
$str .= sprintf('<li style="text-align:right; padding-right:20px;"><a href="%s">%s %s</a></li>', $item->url, $item->name, $icon);
}

//dd($str);
return $str;
}
}
2 changes: 2 additions & 0 deletions app/Libraries/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ public static function today($formatResult = true)

public static function trackViewed($name, $type, $url = false)
{
/*
if (!$url) {
$url = Request::url();
}
Expand Down Expand Up @@ -643,6 +644,7 @@ public static function trackViewed($name, $type, $url = false)
}
Session::put(RECENTLY_VIEWED, $data);
*/
}

public static function processVariables($str)
Expand Down
29 changes: 29 additions & 0 deletions app/Models/EntityModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public function entityKey()
return $this->public_id . ':' . $this->getEntityType();
}

public function subEntityType()
{
return $this->getEntityType();
}

public function isEntityType($type)
{
return $this->getEntityType() === $type;
}

/*
public function getEntityType()
{
Expand Down Expand Up @@ -229,4 +239,23 @@ public static function validate($data, $entityType, $entity = false)
}
}

public static function getIcon($entityType)
{
$icons = [
'dashboard' => 'tachometer',
'clients' => 'users',
'invoices' => 'file-pdf-o',
'payments' => 'credit-card',
'recurring_invoices' => 'files-o',
'credits' => 'credit-card',
'quotes' => 'file-text-o',
'tasks' => 'clock-o',
'expenses' => 'file-image-o',
'vendors' => 'building',
'settings' => 'cog',
];

return array_get($icons, $entityType);
}

}
5 changes: 1 addition & 4 deletions app/Models/Expense.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ public function documents()
*/
public function getName()
{
if($this->expense_number)
return $this->expense_number;

return $this->public_id;
return $this->transaction_id ?: '#' . $this->public_id;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,15 @@ public function getEntityType()
return $this->isType(INVOICE_TYPE_QUOTE) ? ENTITY_QUOTE : ENTITY_INVOICE;
}

public function subEntityType()
{
if ($this->is_recurring) {
return ENTITY_RECURRING_INVOICE;
} else {
return $this->getEntityType();
}
}

/**
* @return bool
*/
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,15 @@ public function getRoute()
{
return "/tasks/{$this->public_id}/edit";
}

public function getName()
{
return '#' . $this->public_id;
}
}



Task::created(function ($task) {
event(new TaskWasCreated($task));
});
Expand Down
8 changes: 8 additions & 0 deletions app/Ninja/Presenters/EntityPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public function link()

return link_to($link, $name)->toHtml();
}

public function titledName()
{
$entity = $this->entity;
$entityType = $entity->getEntityType();

return sprintf('%s: %s', trans('texts.' . $entityType), $entity->getDisplayName());
}
}
1 change: 1 addition & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class EventServiceProvider extends ServiceProvider {
* @var array
*/
protected $listen = [

// Clients
'App\Events\ClientWasCreated' => [
'App\Listeners\ActivityListener@createdClient',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/texts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,8 @@

'toggle_navigation' => 'Toggle Navigation',
'toggle_history' => 'Toggle History',
'unassigned' => 'Unassigned',
'task' => 'Task',

);

Expand Down

0 comments on commit db0c305

Please sign in to comment.