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 Sep 1, 2016
1 parent 69e54e7 commit e535750
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 306 deletions.
2 changes: 0 additions & 2 deletions app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ public function getStarted()
$account = $this->accountRepo->create();
$user = $account->users()->first();

Session::forget(RECENTLY_VIEWED);

if ($prevUserId) {
$users = $this->accountRepo->associateAccounts($user->id, $prevUserId);
Session::put(SESSION_USER_ACCOUNTS, $users);
Expand Down
5 changes: 0 additions & 5 deletions app/Http/Controllers/InvoiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public function edit(InvoiceRequest $request, $publicId, $clone = false)
$method = 'POST';
$url = "{$entityType}s";
} else {
Utils::trackViewed($invoice->getDisplayName().' - '.$invoice->client->getDisplayName(), $invoice->getEntityType());
$method = 'PUT';
$url = "{$entityType}s/{$invoice->public_id}";
$clients->whereId($invoice->client_id);
Expand Down Expand Up @@ -401,14 +400,10 @@ public function store(CreateInvoiceRequest $request)
$entityType = $invoice->getEntityType();
$message = trans("texts.created_{$entityType}");

// check if we created a new client with the invoice
// TODO: replace with HistoryListener
$input = $request->input();
$clientPublicId = isset($input['client']['public_id']) ? $input['client']['public_id'] : false;
if ($clientPublicId == '-1') {
$message = $message.' '.trans('texts.and_created_client');
$trackUrl = URL::to('clients/' . $invoice->client->public_id);
Utils::trackViewed($invoice->client->getDisplayName(), ENTITY_CLIENT, $trackUrl);
}

Session::flash('message', $message);
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/VendorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public function show(VendorRequest $request)
{
$vendor = $request->entity();

Utils::trackViewed($vendor->getDisplayName(), 'vendor');

$actionLinks = [
['label' => trans('texts.new_vendor'), 'url' => URL::to('/vendors/create/' . $vendor->public_id)]
];
Expand Down Expand Up @@ -134,7 +132,7 @@ public function create(VendorRequest $request)
public function edit(VendorRequest $request)
{
$vendor = $request->entity();

$data = [
'vendor' => $vendor,
'method' => 'PUT',
Expand Down
2 changes: 1 addition & 1 deletion app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@
define('ACTIVITY_TYPE_UPDATE_TASK', 43);

define('DEFAULT_INVOICE_NUMBER', '0001');
define('RECENTLY_VIEWED_LIMIT', 8);
define('RECENTLY_VIEWED_LIMIT', 20);
define('LOGGED_ERROR_LIMIT', 100);
define('RANDOM_KEY_LENGTH', 32);
define('MAX_NUM_USERS', 20);
Expand Down
77 changes: 63 additions & 14 deletions app/Libraries/HistoryUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,70 @@
use stdClass;
use Session;
use App\Models\EntityModel;
use App\Models\Activity;

class HistoryUtils
{
public static function loadHistory($users)
{
$userIds = [];

if (is_array($users)) {
foreach ($users as $user) {
$userIds[] = $user->user_id;
}
} else {
$userIds[] = $users;
}

$activityTypes = [
ACTIVITY_TYPE_CREATE_CLIENT,
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,
];

$activities = Activity::scope()
->with('client.contacts', 'invoice')
->whereIn('user_id', $userIds)
->whereIn('activity_type_id', $activityTypes)
->orderBy('id', 'asc')
->limit(100)
->get();

foreach ($activities as $activity)
{
if ($activity->activity_type_id == ACTIVITY_TYPE_CREATE_CLIENT) {
$entity = $activity->client;
} else {
$entity = $activity->invoice;
}

static::trackViewed($entity);
}
}

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

$object = static::convertToObject($entity);
$history = Session::get(RECENTLY_VIEWED);
$history = Session::get(RECENTLY_VIEWED) ?: [];
$accountHistory = isset($history[$entity->account_id]) ? $history[$entity->account_id] : [];
$data = [];

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

if ($object->url == $item->url) {
continue;
Expand All @@ -40,8 +88,9 @@ public static function trackViewed(EntityModel $entity)
array_pop($data);
}

//$data = [];
Session::put(RECENTLY_VIEWED, $data);
$history[$entity->account_id] = $data;

Session::put(RECENTLY_VIEWED, $history);
}

private static function convertToObject($entity)
Expand All @@ -67,12 +116,14 @@ private static function convertToObject($entity)
return $object;
}

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

$history = Session::get(RECENTLY_VIEWED, []);
$history = isset($history[$accountId]) ? $history[$accountId] : [];

foreach ($history as $item)
{
Expand All @@ -84,15 +135,14 @@ public static function renderHtml()

if ($lastClientId === false || $item->client_id != $lastClientId)
{
$icon = '<i class="fa fa-users" style="width:30px"></i>';
$icon = '<i class="fa fa-users" style="width:32px"></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>
$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>
</a>';
} else {
$link = '#';
Expand All @@ -108,11 +158,10 @@ public static function renderHtml()
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);
$icon = '<i class="fa fa-' . EntityModel::getIcon($item->entityType . 's') . '" style="width:24px"></i>';
$str .= sprintf('<li style="text-align:right; padding-right:18px;"><a href="%s">%s %s</a></li>', $item->url, $item->name, $icon);
}

//dd($str);
return $str;
}
}
60 changes: 0 additions & 60 deletions app/Libraries/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,6 @@ public static function getNewsFeedResponse($userType = false)
return $response;
}

public static function getLastURL()
{
if (!count(Session::get(RECENTLY_VIEWED))) {
return '#';
}

$history = Session::get(RECENTLY_VIEWED);
$last = $history[0];
$penultimate = count($history) > 1 ? $history[1] : $last;

return Request::url() == $last->url ? $penultimate->url : $last->url;
}

public static function getProLabel($feature)
{
if (Auth::check()
Expand Down Expand Up @@ -600,53 +587,6 @@ public static function today($formatResult = true)
}
}

public static function trackViewed($name, $type, $url = false)
{
/*
if (!$url) {
$url = Request::url();
}
$viewed = Session::get(RECENTLY_VIEWED);
if (!$viewed) {
$viewed = [];
}
$object = new stdClass();
$object->accountId = Auth::user()->account_id;
$object->url = $url;
$object->name = ucwords($type).': '.$name;
$data = [];
$counts = [];
for ($i = 0; $i<count($viewed); $i++) {
$item = $viewed[$i];
if ($object->url == $item->url || $object->name == $item->name) {
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[Auth::user()->account_id]) && $counts[Auth::user()->account_id] > RECENTLY_VIEWED_LIMIT) {
array_pop($data);
}
Session::put(RECENTLY_VIEWED, $data);
*/
}

public static function processVariables($str)
{
if (!$str) {
Expand Down
6 changes: 4 additions & 2 deletions app/Listeners/HandleUserLoggedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Events\UserLoggedIn;
use App\Events\UserSignedUp;
use App\Ninja\Repositories\AccountRepository;
use App\Libraries\HistoryUtils;

/**
* Class HandleUserLoggedIn
Expand All @@ -19,7 +20,7 @@ class HandleUserLoggedIn {

/**
* Create the event handler.
*
*
* @param AccountRepository $accountRepo
*/
public function __construct(AccountRepository $accountRepo)
Expand Down Expand Up @@ -47,10 +48,11 @@ public function handle(UserLoggedIn $event)

$users = $this->accountRepo->loadAccounts(Auth::user()->id);
Session::put(SESSION_USER_ACCOUNTS, $users);
HistoryUtils::loadHistory($users ?: Auth::user()->id);

$account->loadLocalizationSettings();

// if they're using Stripe make sure they're using Stripe.js
// if they're using Stripe make sure they're using Stripe.js
$accountGateway = $account->getGatewayConfig(GATEWAY_STRIPE);
if ($accountGateway && ! $accountGateway->getPublishableStripeKey()) {
Session::flash('warning', trans('texts.missing_publishable_key'));
Expand Down
1 change: 0 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ public function getRememberTokenName()
public function clearSession()
{
$keys = [
RECENTLY_VIEWED,
SESSION_USER_ACCOUNTS,
SESSION_TIMEZONE,
SESSION_DATE_FORMAT,
Expand Down
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ elixir(function(mix) {
'bootstrap-combobox.css',
'typeahead.js-bootstrap.css',
'style.css',
'sidebar.css',
'fonts.css'
], 'public/css/built.css');

Expand Down
6 changes: 5 additions & 1 deletion public/css/built.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/css/built.css.map

Large diffs are not rendered by default.

0 comments on commit e535750

Please sign in to comment.