2014-07-08 07:51:25 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Firefly\Helper\Toolkit;
|
|
|
|
|
2014-07-25 13:02:01 +02:00
|
|
|
use Carbon\Carbon;
|
2014-09-12 17:34:54 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Collection;
|
2014-07-25 13:02:01 +02:00
|
|
|
|
2014-07-23 06:57:51 +02:00
|
|
|
/**
|
|
|
|
* Class Toolkit
|
|
|
|
*
|
|
|
|
* @package Firefly\Helper\Toolkit
|
2014-08-10 15:01:46 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
|
2014-07-23 06:57:51 +02:00
|
|
|
*/
|
2014-07-08 15:17:18 +02:00
|
|
|
class Toolkit implements ToolkitInterface
|
2014-07-08 07:51:25 +02:00
|
|
|
{
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
|
2014-07-08 07:51:25 +02:00
|
|
|
/**
|
2014-09-10 22:22:44 +02:00
|
|
|
* Lots of code in Firefly III still depends on session['start'], session['end'] and
|
|
|
|
* session['range'] to be available, even though this feature has been removed from Firefly
|
|
|
|
* in favor of a new reporting feature. This reporting feature can show the user past and future
|
|
|
|
* date ranges instead of the dashboard (the dashboard always shows "right now").
|
2014-07-08 07:51:25 +02:00
|
|
|
*
|
2014-09-10 22:22:44 +02:00
|
|
|
* The only actual choice the user is left with is the range, which can be changed using the Preferences pane.
|
|
|
|
*
|
|
|
|
* The start/end dates are set here, regardless of what the user might want to see.
|
|
|
|
*
|
|
|
|
* @return null
|
2014-07-08 07:51:25 +02:00
|
|
|
*/
|
2014-09-10 22:22:44 +02:00
|
|
|
public function getDateRange()
|
2014-07-08 07:51:25 +02:00
|
|
|
{
|
2014-08-10 15:01:46 +02:00
|
|
|
$range = $this->_getRange();
|
2014-09-10 22:22:44 +02:00
|
|
|
// start and end are always "now", and get edited later.
|
|
|
|
$start = new Carbon;
|
|
|
|
$end = new Carbon;
|
2014-08-10 15:01:46 +02:00
|
|
|
|
|
|
|
// update start only:
|
|
|
|
$start = $this->_updateStartDate($range, $start);
|
2014-07-26 18:53:41 +02:00
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
// update end only:
|
|
|
|
$end = $this->_updateEndDate($range, $start, $end);
|
2014-07-26 18:53:41 +02:00
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
// save in session:
|
|
|
|
\Session::put('start', $start);
|
|
|
|
\Session::put('end', $end);
|
|
|
|
\Session::put('range', $range);
|
|
|
|
return null;
|
|
|
|
|
2014-08-28 07:53:54 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-09-10 22:22:44 +02:00
|
|
|
protected function _getRange()
|
2014-08-10 15:01:46 +02:00
|
|
|
{
|
2014-09-10 22:22:44 +02:00
|
|
|
if (!is_null(\Session::get('range'))) {
|
|
|
|
$range = \Session::get('range');
|
2014-08-10 15:01:46 +02:00
|
|
|
} else {
|
2014-09-10 22:22:44 +02:00
|
|
|
/** @noinspection PhpUndefinedClassInspection */
|
|
|
|
$preferences = \App::make('Firefly\Helper\Preferences\PreferencesHelperInterface');
|
|
|
|
$viewRange = $preferences->get('viewRange', '1M');
|
2014-08-10 15:01:46 +02:00
|
|
|
|
2014-09-10 22:22:44 +02:00
|
|
|
// default range:
|
|
|
|
$range = $viewRange->data;
|
2014-07-15 06:58:08 +02:00
|
|
|
}
|
2014-08-10 15:01:46 +02:00
|
|
|
return $range;
|
2014-07-24 22:16:42 +02:00
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $range
|
|
|
|
* @param Carbon $start
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
protected function _updateStartDate($range, Carbon $start)
|
|
|
|
{
|
|
|
|
$today = new Carbon;
|
2014-07-15 06:58:08 +02:00
|
|
|
switch ($range) {
|
2014-08-10 15:01:46 +02:00
|
|
|
case '1D':
|
|
|
|
$start->startOfDay();
|
|
|
|
break;
|
|
|
|
case '1W':
|
|
|
|
$start->startOfWeek();
|
|
|
|
break;
|
|
|
|
case '1M':
|
|
|
|
$start->startOfMonth();
|
|
|
|
break;
|
|
|
|
case '3M':
|
|
|
|
$start->firstOfQuarter();
|
|
|
|
break;
|
|
|
|
case '6M':
|
|
|
|
if (intval($today->format('m')) >= 7) {
|
|
|
|
$start->startOfYear()->addMonths(6);
|
2014-07-15 06:58:08 +02:00
|
|
|
} else {
|
2014-08-10 15:01:46 +02:00
|
|
|
$start->startOfYear();
|
2014-07-15 06:58:08 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-08-10 15:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $start;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $range
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
protected function _updateEndDate($range, Carbon $start, Carbon $end)
|
|
|
|
{
|
|
|
|
$today = new Carbon;
|
|
|
|
switch ($range) {
|
2014-07-15 06:58:08 +02:00
|
|
|
case '1D':
|
2014-07-24 22:16:42 +02:00
|
|
|
$end = clone $start;
|
2014-07-15 06:58:08 +02:00
|
|
|
$end->endOfDay();
|
|
|
|
break;
|
|
|
|
case '1W':
|
2014-07-24 22:16:42 +02:00
|
|
|
$end = clone $start;
|
2014-07-08 07:51:25 +02:00
|
|
|
$end->endOfWeek();
|
|
|
|
break;
|
2014-07-15 06:58:08 +02:00
|
|
|
case '1M':
|
2014-07-24 22:16:42 +02:00
|
|
|
$end = clone $start;
|
2014-07-08 07:51:25 +02:00
|
|
|
$end->endOfMonth();
|
|
|
|
break;
|
2014-07-15 06:58:08 +02:00
|
|
|
case '3M':
|
2014-07-24 22:16:42 +02:00
|
|
|
$end = clone $start;
|
2014-07-15 06:58:08 +02:00
|
|
|
$end->lastOfQuarter();
|
|
|
|
break;
|
|
|
|
case '6M':
|
2014-08-10 15:01:46 +02:00
|
|
|
$end = clone $start;
|
2014-07-15 06:58:08 +02:00
|
|
|
if (intval($today->format('m')) >= 7) {
|
|
|
|
$end->endOfYear();
|
|
|
|
} else {
|
|
|
|
$end->startOfYear()->addMonths(6);
|
|
|
|
}
|
|
|
|
break;
|
2014-07-08 07:51:25 +02:00
|
|
|
}
|
2014-07-24 22:16:42 +02:00
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
return $end;
|
|
|
|
}
|
2014-09-12 17:34:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes any collection and tries to make a sensible select list compatible array of it.
|
|
|
|
*
|
|
|
|
* @param Collection $set
|
|
|
|
* @param null $titleField
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function makeSelectList(Collection $set, $titleField = null)
|
|
|
|
{
|
|
|
|
$selectList = [];
|
|
|
|
/** @var Model $entry */
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
$id = intval($entry->id);
|
|
|
|
$title = null;
|
|
|
|
if (is_null($titleField)) {
|
|
|
|
// try 'title' field.
|
|
|
|
if (isset($entry->title)) {
|
|
|
|
$title = $entry->title;
|
|
|
|
}
|
|
|
|
// try 'name' field
|
|
|
|
if (is_null($title)) {
|
|
|
|
$title = $entry->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try 'description' field
|
|
|
|
if (is_null($title)) {
|
|
|
|
$title = $entry->description;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$title = $entry->$titleField;
|
|
|
|
}
|
|
|
|
$selectList[$id] = $title;
|
|
|
|
}
|
|
|
|
return $selectList;
|
|
|
|
}
|
2014-09-10 22:22:44 +02:00
|
|
|
}
|