Optimized preferences.

This commit is contained in:
James Cole 2015-06-03 21:58:06 +02:00
parent 1240c8f685
commit 14dce8a10b
4 changed files with 36 additions and 49 deletions

View File

@ -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];
}

View File

@ -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
*/

View File

@ -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 '<span class="text-info">' . $this->formatWithSymbol($symbol, $amount, false) . '</span>';
$txt = '<span class="text-info">' . $this->formatWithSymbol($symbol, $amount, false) . '</span>';
$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;
}
/**

View File

@ -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;