From 9054b207002f05f888b255877793b17ae843c86a Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 29 Jul 2014 12:55:41 +0200 Subject: [PATCH] Some work on the chart controller. [skip ci] --- app/assets/javascripts/firefly/accounts.js | 6 +- app/assets/javascripts/firefly/index.js | 6 +- app/controllers/ChartController.php | 78 +++---------------- app/lib/Firefly/Helper/Controllers/Chart.php | 71 +++++++++++++++++ .../Helper/Controllers/ChartInterface.php | 17 ++++ .../Firefly/Helper/HelperServiceProvider.php | 4 + app/routes.php | 2 +- 7 files changed, 111 insertions(+), 73 deletions(-) create mode 100644 app/lib/Firefly/Helper/Controllers/Chart.php create mode 100644 app/lib/Firefly/Helper/Controllers/ChartInterface.php diff --git a/app/assets/javascripts/firefly/accounts.js b/app/assets/javascripts/firefly/accounts.js index 67040dd4d5..63efd1db42 100644 --- a/app/assets/javascripts/firefly/accounts.js +++ b/app/assets/javascripts/firefly/accounts.js @@ -10,9 +10,9 @@ if($('#chart').length == 1) { type: 'line' }, - series: data, + series: data.series, title: { - text: 'BETTER TITLE HERE' + text: data.chart_title }, yAxis: { formatter: function () { @@ -20,7 +20,7 @@ if($('#chart').length == 1) { } }, subtitle: { - text: 'View more', + text: data.subtitle, useHTML: true }, diff --git a/app/assets/javascripts/firefly/index.js b/app/assets/javascripts/firefly/index.js index e077bc457c..b46c80c64a 100644 --- a/app/assets/javascripts/firefly/index.js +++ b/app/assets/javascripts/firefly/index.js @@ -11,9 +11,9 @@ $(function () { type: 'line' }, - series: data, + series: data.series, title: { - text: 'All accounts' + text: data.chart_title }, yAxis: { formatter: function () { @@ -21,7 +21,7 @@ $(function () { } }, subtitle: { - text: 'View more', + text: data.subtitle, useHTML: true }, diff --git a/app/controllers/ChartController.php b/app/controllers/ChartController.php index 04eaae3091..d6870547f9 100644 --- a/app/controllers/ChartController.php +++ b/app/controllers/ChartController.php @@ -1,11 +1,6 @@ _accounts = $accounts; - $this->_journals = $journals; - $this->_preferences = $preferences; - $this->_tk = $toolkit; - $this->_budgets = $budgets; + $this->_chart = $chart; } /** - * @param null $accountId - * - * @return \Illuminate\Http\JsonResponse + * @param Account $account + * @return mixed */ - public function homeAccount($accountId = null) + public function homeAccount(Account $account = null) { - list($start, $end) = $this->_tk->getDateRangeDates(); - $current = clone $start; - $return = []; - $account = !is_null($accountId) ? $this->_accounts->find($accountId) : null; - $today = new Carbon\Carbon; - if (is_null($account)) { - - $pref = $this->_preferences->get('frontpageAccounts', []); - if ($pref->data == []) { - $accounts = $this->_accounts->getActiveDefault(); - } else { - $accounts = $this->_accounts->getByIds($pref->data); - } - foreach ($accounts as $account) { - $return[] = ['name' => $account->name, 'id' => 'acc-' . $account->id, 'data' => []]; - - } - while ($current <= $end) { - // loop accounts: - foreach ($accounts as $index => $account) { - if ($current > $today) { - $return[$index]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)]; - } else { - $return[$index]['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)]; - } - } - $current->addDay(); - } + if (!is_null($account)) { + $data = $this->_chart->account($account); } else { - $return[0] = ['name' => $account->name, 'id' => $account->id, 'data' => []]; - while ($current <= $end) { - if ($current > $today) { - $return[0]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)]; - } else { - $return[0]['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)]; - } - - $current->addDay(); - } + $data = $this->_chart->accounts(); } - - return Response::json($return); + return Response::json($data); } /** diff --git a/app/lib/Firefly/Helper/Controllers/Chart.php b/app/lib/Firefly/Helper/Controllers/Chart.php new file mode 100644 index 0000000000..afbaf7d271 --- /dev/null +++ b/app/lib/Firefly/Helper/Controllers/Chart.php @@ -0,0 +1,71 @@ + $account->name, + 'subtitle' => 'View more', + 'series' => [$this->_account($account)] + ]; + return $data; + } + + public function accounts() + { + $data = [ + 'chart_title' => 'All accounts', + 'subtitle' => 'View more', + 'series' => [] + ]; + /** @var \Firefly\Helper\Preferences\PreferencesHelperInterface $prefs */ + $prefs = \App::make('Firefly\Helper\Preferences\PreferencesHelperInterface'); + $pref = $prefs->get('frontpageAccounts', []); + + /** @var \Firefly\Storage\Account\AccountRepositoryInterface $acct */ + $acct = \App::make('Firefly\Storage\Account\AccountRepositoryInterface'); + + if ($pref->data == []) { + $accounts = $acct->getActiveDefault(); + } else { + $accounts = $acct->getByIds($pref->data); + } + foreach($accounts as $account) { + $data['series'][] = $this->_account($account); + } + return $data; + + } + + protected function _account(\Account $account) + { + $start = \Session::get('start'); + $end = \Session::get('end'); + $current = clone $start; + $today = new Carbon; + $return = ['name' => $account->name, 'id' => $account->id, 'data' => []]; + while ($current <= $end) { + if ($current > $today) { + $return['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)]; + } else { + $return['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)]; + } + + $current->addDay(); + } + return $return; + } + +} \ No newline at end of file diff --git a/app/lib/Firefly/Helper/Controllers/ChartInterface.php b/app/lib/Firefly/Helper/Controllers/ChartInterface.php new file mode 100644 index 0000000000..5ad025d894 --- /dev/null +++ b/app/lib/Firefly/Helper/Controllers/ChartInterface.php @@ -0,0 +1,17 @@ +app->bind( + 'Firefly\Helper\Controllers\ChartInterface', + 'Firefly\Helper\Controllers\Chart' + ); $this->app->bind( 'Firefly\Helper\Controllers\BudgetInterface', diff --git a/app/routes.php b/app/routes.php index c65281d2d7..49d5962f03 100644 --- a/app/routes.php +++ b/app/routes.php @@ -30,7 +30,7 @@ Route::group(['before' => 'auth'], function () { Route::get('/flush', ['uses' => 'HomeController@flush', 'as' => 'flush']); // chart controller - Route::get('/chart/home/account/{accountname?}', ['uses' => 'ChartController@homeAccount', 'as' => 'chart.home']); + Route::get('/chart/home/account/{account?}', ['uses' => 'ChartController@homeAccount', 'as' => 'chart.home']); Route::get('/chart/home/categories', ['uses' => 'ChartController@homeCategories', 'as' => 'chart.categories']); Route::get('/chart/home/budgets', ['uses' => 'ChartController@homeBudgets', 'as' => 'chart.budgets']); Route::get('/chart/home/info/{accountname}/{day}/{month}/{year}', ['uses' => 'ChartController@homeAccountInfo', 'as' => 'chart.info']);