Time-based navigation and some feedback for the user as to his import.

This commit is contained in:
James Cole 2014-09-14 21:09:52 +02:00
parent 49b8742082
commit 4f3493f9ff
8 changed files with 277 additions and 12 deletions

View File

@ -30,6 +30,33 @@ class HomeController extends BaseController
$this->_reminders = $reminders;
}
public function jobDev() {
$fullName = storage_path().DIRECTORY_SEPARATOR.'firefly-export-2014-07-23.json';
\Log::notice('Pushed start job.');
Queue::push('Firefly\Queue\Import@start', ['file' => $fullName, 'user' => 1]);
}
/*
*
*/
public function sessionPrev() {
/** @var \Firefly\Helper\Toolkit\ToolkitInterface $toolkit */
$toolkit = App::make('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->prev();
return Redirect::route('index');
}
/*
*
*/
public function sessionNext() {
/** @var \Firefly\Helper\Toolkit\ToolkitInterface $toolkit */
$toolkit = App::make('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->next();
return Redirect::route('index');
}
/**
* @return \Illuminate\Http\RedirectResponse
*/

View File

@ -6,8 +6,10 @@ App::before(
function ($request) {
if (Auth::check()) {
/** @var \Firefly\Helper\Toolkit\ToolkitInterface $toolkit */
$toolkit = App::make('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->getDateRange();
$toolkit->checkImportJobs();
}
}

View File

@ -3,6 +3,7 @@
namespace Firefly\Helper\Toolkit;
use Carbon\Carbon;
use Firefly\Exception\FireflyException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
@ -30,25 +31,81 @@ class Toolkit implements ToolkitInterface
*/
public function getDateRange()
{
/*
* Get all data from the session:
*/
$range = $this->_getRange();
// start and end are always "now", and get edited later.
$start = new Carbon;
$end = new Carbon;
#\Log::debug('Range is: ' . $range);
$start = \Session::has('start') ? \Session::get('start') : new Carbon;
// update start only:
#\Log::debug('Session start is: ' . $start->format('Y-m-d'));
$end = \Session::has('end') ? \Session::get('end') : new Carbon;
#\Log::debug('Session end is : ' . $end->format('Y-m-d'));
/*
* Force start date to at the start of the $range.
* Ie. the start of the week, month, year.
*/
$start = $this->_updateStartDate($range, $start);
#\Log::debug('After update, session start is: ' . $start->format('Y-m-d'));
// update end only:
$end = $this->_updateEndDate($range, $start, $end);
/*
* Force end date to at the END of the $range. Always based on $start.
* Ie. the END of the week, month, year.
*/
$end = $this->_updateEndDate($range, $start);
#\Log::debug('After update, session end is : ' . $end->format('Y-m-d'));
// save in session:
/*
* get the name of the month, depending on the range. Purely for astetics
*/
$period = $this->_periodName($range, $start);
/*
* Get the date for the previous and next period.
* Ie. next week, next month, etc.
*/
$prev = $this->_previous($range, clone $start);
$next = $this->_next($range, clone $start);
/*
* Save everything in the session:
*/
\Session::put('start', $start);
\Session::put('end', $end);
\Session::put('range', $range);
\Session::put('period', $period);
\Session::put('prev', $this->_periodName($range, $prev));
\Session::put('next', $this->_periodName($range, $next));
return null;
}
/**
*
*/
public function checkImportJobs() {
/*
* Get all jobs.
*/
/** @var \Importmap $importJob */
$importJob = \Importmap::where('user_id',\Auth::user()->id)
->where('totaljobs','>',\DB::Raw('`jobsdone`'))
->orderBy('created_at','DESC')
->first();
if(!is_null($importJob)) {
$diff = intval($importJob->totaljobs) - intval($importJob->jobsdone);
$date = new Carbon;
$today = new Carbon;
$date->addSeconds($diff);
\Session::put('job_pct',$importJob->pct());
\Session::put('job_text',$date->diffForHumans());
} else {
\Session::forget('job_pct');
\Session::forget('job_text');
}
}
/**
* @return mixed
*/
@ -63,6 +120,7 @@ class Toolkit implements ToolkitInterface
// default range:
$range = $viewRange->data;
\Session::put('range', $range);
}
return $range;
@ -110,9 +168,8 @@ class Toolkit implements ToolkitInterface
*
* @return Carbon
*/
protected function _updateEndDate($range, Carbon $start, Carbon $end)
protected function _updateEndDate($range, Carbon $start)
{
$today = new Carbon;
switch ($range) {
case '1D':
$end = clone $start;
@ -132,17 +189,132 @@ class Toolkit implements ToolkitInterface
break;
case '6M':
$end = clone $start;
if (intval($today->format('m')) >= 7) {
if (intval($start->format('m')) >= 7) {
$end->endOfYear();
} else {
$end->startOfYear()->addMonths(6);
}
break;
default:
throw new FireflyException('Nothing happened with $end!');
break;
}
return $end;
}
protected function _periodName($range, Carbon $date)
{
switch ($range) {
default:
throw new FireflyException('No _periodName() for range "' . $range . '"');
break;
case '1M':
return $date->format('F Y');
break;
}
}
protected function _previous($range, Carbon $date)
{
switch ($range) {
case '1D':
$date->startOfDay()->subDay();
break;
case '1W':
$date->startOfWeek()->subWeek();
break;
case '1M':
$date->startOfMonth()->subMonth();
break;
case '3M':
$date->firstOfQuarter()->subMonths(3)->firstOfQuarter();
break;
case '6M':
if (intval($date->format('m')) >= 7) {
$date->startOfYear();
} else {
$date->startOfYear()->subMonths(6);
}
break;
}
return $date;
}
protected function _next($range, Carbon $date)
{
switch ($range) {
case '1D':
$date->endOfDay()->addDay();
break;
case '1W':
$date->endOfWeek()->addDay()->startOfWeek();
break;
case '1M':
$date->endOfMonth()->addDay()->startOfMonth();
break;
case '3M':
$date->lastOfQuarter();
break;
case '6M':
if (intval($date->format('m')) >= 7) {
$date->startOfYear()->addYear();
} else {
$date->startOfYear()->addMonths(6);
}
break;
}
return $date;
}
public function next()
{
/*
* Get the start date and the range from the session
*/
$range = $this->_getRange();
$start = \Session::get('start');
/*
* Add some period to $start.
*/
$next = $this->_next($range, clone $start);
/*
* Save in session:
*/
\Session::put('start', $next);
return true;
}
public function prev()
{
/*
* Get the start date and the range from the session
*/
$range = $this->_getRange();
$start = \Session::get('start');
/*
* Substract some period to $start.
*/
$prev = $this->_previous($range, clone $start);
/*
* Save in session:
*/
\Session::put('start', $prev);
return true;
}
/**
* This method checks and
*/
public function bootstrapDaterange()
{
}
/**
* Takes any collection and tries to make a sensible select list compatible array of it.
*

View File

@ -28,4 +28,11 @@ interface ToolkitInterface
*/
public function makeSelectList(Collection $set, $titleField = null);
public function bootstrapDaterange();
public function next();
public function prev();
public function checkImportJobs();
}

View File

@ -120,9 +120,20 @@ Route::bind('piggybank', function($value, $route)
});
// a development route:
Route::get('/dev', ['uses' => 'HomeController@jobDev']);
// protected routes:
Route::group(['before' => 'auth'], function () {
// some date routes:
Route::get('/prev',['uses' => 'HomeController@sessionPrev', 'as' => 'sessionPrev']);
Route::get('/next',['uses' => 'HomeController@sessionNext', 'as' => 'sessionNext']);
// account controller:
Route::get('/accounts', ['uses' => 'AccountController@index', 'as' => 'accounts.index']);

View File

@ -61,6 +61,20 @@
</div>
<div class="col-lg-4 col-md-6 col-sm-12">
<!-- time based navigation -->
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-clock-o fa-fw"></i>
{{{\Session::get('period')}}}
</div>
<div class="panel-body">
<div class="btn-group btn-group-sm btn-group-justified">
<a class="btn btn-default" href="{{route('sessionPrev')}}"><i class="fa fa-arrow-left"></i> {{{\Session::get('prev')}}}</a>
<a class="btn btn-default" href="{{route('sessionNext')}}">{{{\Session::get('next')}}} <i class="fa fa-arrow-right"></i></a>
</div>
</div>
</div>
<!-- TRANSACTIONS -->
@foreach($transactions as $data)
<div class="panel panel-default">

View File

@ -12,7 +12,40 @@
<!-- /.navbar-header -->
<ul class="nav navbar-top-links navbar-right">
<li class="dropdown">
<!-- /.dropdown -->
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-tasks fa-fw"></i> <i class="fa fa-caret-down"></i>
</a>
<!-- display for import tasks, possibly others -->
@if(Session::has('job_pct'))
<ul class="dropdown-menu dropdown-tasks">
<li>
<a href="#">
<div>
<p>
<strong>Import from Firefly II</strong>
<span class="pull-right text-muted">{{Session::get('job_pct')}}% Complete</span>
</p>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="{{Session::get('job_pct')}}" aria-valuemin="0" aria-valuemax="100" style="width: {{Session::get('job_pct')}}%">
<span class="sr-only">{{Session::get('job_pct')}}% Complete (success)</span>
</div>
</div>
<p>
<small>Finished ~ {{Session::get('job_text')}}</small>
</p>
</div>
</a>
</li>
</ul>
<!-- /.dropdown-tasks -->
</li>
@endif
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
</a>

View File

@ -1,6 +1,5 @@
@extends('layouts.guest')
@section('content')
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">