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

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