From 14dce8a10b99fc439a6699966087d6be809524ae Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 3 Jun 2015 21:58:06 +0200 Subject: [PATCH] Optimized preferences. --- app/Http/Controllers/HomeController.php | 3 +++ app/Models/Preference.php | 24 ----------------- app/Support/Amount.php | 24 ++++++++++++++--- app/Support/Preferences.php | 34 +++++++++---------------- 4 files changed, 36 insertions(+), 49 deletions(-) diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 4477ae09d3..16a9e120b4 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -67,6 +67,7 @@ class HomeController extends Controller $frontPage = Preferences::get('frontPageAccounts', []); $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); + $accounts = $repository->getFrontpageAccounts($frontPage); $savings = $repository->getSavingsAccounts(); @@ -79,6 +80,7 @@ class HomeController extends Controller } $sum = $repository->sumOfEverything(); + if ($sum != 0) { Session::flash( 'error', 'Your transactions are unbalanced. This means a' @@ -89,6 +91,7 @@ class HomeController extends Controller foreach ($accounts as $account) { $set = $repository->getFrontpageTransactions($account, $start, $end); + if (count($set) > 0) { $transactions[] = [$set, $account]; } diff --git a/app/Models/Preference.php b/app/Models/Preference.php index 488a57d0bb..263e7e33bc 100644 --- a/app/Models/Preference.php +++ b/app/Models/Preference.php @@ -55,21 +55,6 @@ class Preference extends Model return ['created_at', 'updated_at']; } - /** - * @param $value - * - * @return float|int - */ - public function getNameAttribute($value) - { - if (is_null($this->name_encrypted)) { - return $value; - } - $value = Crypt::decrypt($this->name_encrypted); - - return $value; - } - /** * @param $value */ @@ -79,15 +64,6 @@ class Preference extends Model $this->attributes['data_encrypted'] = Crypt::encrypt(json_encode($value)); } - /** - * @param $value - */ - public function setNameAttribute($value) - { - $this->attributes['name_encrypted'] = Crypt::encrypt($value); - $this->attributes['name'] = $value; - } - /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 8a1916e9ca..101ff86d41 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -84,6 +84,15 @@ class Amount */ public function formatJournal(TransactionJournal $journal, $coloured = true) { + $cache = new CacheProperties; + $cache->addProperty($journal->id); + $cache->addProperty('formatJournal'); + + if ($cache->has()) { + return $cache->get(); + } + + if (is_null($journal->symbol)) { $symbol = $journal->transactionCurrency->symbol; } else { @@ -94,13 +103,22 @@ class Amount $amount = $amount * -1; } if ($journal->transactionType->type == 'Transfer' && $coloured) { - return '' . $this->formatWithSymbol($symbol, $amount, false) . ''; + $txt = '' . $this->formatWithSymbol($symbol, $amount, false) . ''; + $cache->store($txt); + + return $txt; } if ($journal->transactionType->type == 'Transfer' && !$coloured) { - return $this->formatWithSymbol($symbol, $amount, false); + $txt = $this->formatWithSymbol($symbol, $amount, false); + $cache->store($txt); + + return $txt; } - return $this->formatWithSymbol($symbol, $amount, $coloured); + $txt = $this->formatWithSymbol($symbol, $amount, $coloured); + $cache->store($txt); + + return $txt; } /** diff --git a/app/Support/Preferences.php b/app/Support/Preferences.php index 5d24ca6818..8ed4b17924 100644 --- a/app/Support/Preferences.php +++ b/app/Support/Preferences.php @@ -30,13 +30,10 @@ class Preferences */ public function get($name, $default = null) { - $preferences = Preference::where('user_id', Auth::user()->id)->get(); + $preference = Preference::where('user_id', Auth::user()->id)->where('name', $name)->first(['id','name','data_encrypted']); - /** @var Preference $preference */ - foreach ($preferences as $preference) { - if ($preference->name == $name) { - return $preference; - } + if ($preference) { + return $preference; } // no preference found and default is null: if (is_null($default)) { @@ -56,24 +53,17 @@ class Preferences */ public function set($name, $value) { - $preferences = Preference::where('user_id', Auth::user()->id)->get(); - /** @var Preference $preference */ - foreach ($preferences as $preference) { - if ($preference->name == $name) { - $preference->data = $value; - $preference->save(); - - return $preference; - } - } - $pref = new Preference; - $pref->name = $name; - $pref->data = $value; - - if (!is_null(Auth::user()->id)) { + $pref = Preference::where('user_id', Auth::user()->id)->where('name', $name)->first(['id','name','data_encrypted']); + if ($pref) { + $pref->data = $value; + } else { + $pref = new Preference; + $pref->name = $name; + $pref->data = $value; $pref->user()->associate(Auth::user()); - $pref->save(); + } + $pref->save(); return $pref;