Files
firefly-iii/app/Support/Twig/Journal.php

254 lines
8.0 KiB
PHP
Raw Normal View History

2015-05-01 22:44:35 +02:00
<?php
/**
* Journal.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2015-05-01 22:44:35 +02:00
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Account;
2016-12-22 18:19:50 +01:00
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
2015-05-01 22:44:35 +02:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
2015-05-01 22:44:35 +02:00
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
2015-05-02 22:12:26 +02:00
2015-05-01 22:44:35 +02:00
/**
2015-05-02 11:32:45 +02:00
* Class Journal
2015-05-01 22:44:35 +02:00
*
* @package FireflyIII\Support\Twig
*/
2015-05-02 11:32:45 +02:00
class Journal extends Twig_Extension
2015-05-01 22:44:35 +02:00
{
2016-05-15 09:00:49 +02:00
/**
* @return Twig_SimpleFunction
*/
public function getDestinationAccount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'destinationAccount', function (TransactionJournal $journal) {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-account-string');
if ($cache->has()) {
return $cache->get();
}
$list = $journal->destinationAccountList();
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
2016-12-22 18:19:50 +01:00
if ($entry->accountType->type == AccountType::CASH) {
$array[] = '<span class="text-success">(cash)</span>';
continue;
}
$array[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($entry->name), route('accounts.show', $entry->id));
}
2016-05-11 17:17:43 +02:00
$array = array_unique($array);
$result = join(', ', $array);
$cache->store($result);
return $result;
}
);
}
2015-05-01 22:44:35 +02:00
2015-05-02 22:05:18 +02:00
/**
* @return array
*/
2016-02-06 15:01:26 +01:00
public function getFilters(): array
2015-05-01 22:44:35 +02:00
{
$filters = [
$this->typeIcon(),
];
2015-05-01 22:44:35 +02:00
2015-06-06 17:40:41 +02:00
return $filters;
}
/**
* @return array
*/
2016-02-06 15:01:26 +01:00
public function getFunctions(): array
2015-06-06 17:40:41 +02:00
{
$functions = [
$this->getSourceAccount(),
$this->getDestinationAccount(),
2016-05-15 09:00:49 +02:00
$this->journalBudgets(),
$this->journalCategories(),
2015-06-06 17:40:41 +02:00
];
return $functions;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
2016-02-06 15:01:26 +01:00
public function getName(): string
2015-06-06 17:40:41 +02:00
{
return 'FireflyIII\Support\Twig\Journals';
}
2016-02-06 15:01:26 +01:00
/**
* @return Twig_SimpleFunction
*/
public function getSourceAccount(): Twig_SimpleFunction
2016-02-06 15:01:26 +01:00
{
return new Twig_SimpleFunction(
'sourceAccount', function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('source-account-string');
if ($cache->has()) {
return $cache->get();
2016-02-06 15:01:26 +01:00
}
$list = TransactionJournal::sourceAccountList($journal);
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
if ($entry->accountType->type == 'Cash account') {
$array[] = '<span class="text-success">(cash)</span>';
continue;
}
$array[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($entry->name), route('accounts.show', $entry->id));
}
2016-05-11 17:17:43 +02:00
$array = array_unique($array);
$result = join(', ', $array);
$cache->store($result);
return $result;
2016-02-06 15:01:26 +01:00
}
);
}
2016-05-15 09:00:49 +02:00
/**
* @return Twig_SimpleFunction
*/
public function journalBudgets(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalBudgets', function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('budget-string');
if ($cache->has()) {
2016-05-15 12:39:39 +02:00
return $cache->get();
2016-05-15 09:00:49 +02:00
}
$budgets = [];
// get all budgets:
foreach ($journal->budgets as $budget) {
2016-12-28 05:16:55 +01:00
$budgets[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($budget->name), route('budgets.show', $budget->id));
2016-05-15 09:00:49 +02:00
}
// and more!
foreach ($journal->transactions as $transaction) {
foreach ($transaction->budgets as $budget) {
2016-12-28 05:16:55 +01:00
$budgets[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($budget->name), route('budgets.show', $budget->id));
2016-05-15 09:00:49 +02:00
}
}
$string = join(', ', array_unique($budgets));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalCategories(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'journalCategories', function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('category-string');
if ($cache->has()) {
return $cache->get();
}
$categories = [];
// get all categories for the journal itself (easy):
2016-05-15 09:00:49 +02:00
foreach ($journal->categories as $category) {
2016-12-28 05:16:55 +01:00
$categories[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($category->name), route('categories.show', $category->id));
2016-05-15 09:00:49 +02:00
}
if (count($categories) === 0) {
$set = Category::distinct()->leftJoin('category_transaction', 'categories.id', '=', 'category_transaction.category_id')
->leftJoin('transactions', 'category_transaction.transaction_id', '=', 'transactions.id')
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
2016-05-22 15:48:34 +02:00
->where('categories.user_id', $journal->user_id)
->where('transaction_journals.id', $journal->id)
->get(['categories.*']);
/** @var Category $category */
foreach ($set as $category) {
2016-12-28 05:16:55 +01:00
$categories[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($category->name), route('categories.show', $category->id));
2016-05-15 09:00:49 +02:00
}
}
2016-05-15 09:00:49 +02:00
$string = join(', ', array_unique($categories));
$cache->store($string);
return $string;
}
);
}
2015-06-06 17:40:41 +02:00
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's 5.
*
2015-06-06 17:40:41 +02:00
* @return Twig_SimpleFilter
*/
2016-02-06 15:01:26 +01:00
protected function typeIcon(): Twig_SimpleFilter
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 15:01:26 +01:00
'typeIcon', function (TransactionJournal $journal): string {
2015-06-03 18:22:47 +02:00
switch (true) {
case $journal->isWithdrawal():
$txt = sprintf('<i class="fa fa-long-arrow-left fa-fw" title="%s"></i>', trans('firefly.withdrawal'));
2015-06-03 18:22:47 +02:00
break;
case $journal->isDeposit():
$txt = sprintf('<i class="fa fa-long-arrow-right fa-fw" title="%s"></i>', trans('firefly.deposit'));
2015-06-03 18:22:47 +02:00
break;
case $journal->isTransfer():
$txt = sprintf('<i class="fa fa-fw fa-exchange" title="%s"></i>', trans('firefly.transfer'));
2015-06-03 18:22:47 +02:00
break;
case $journal->isOpeningBalance():
$txt = sprintf('<i class="fa-fw fa fa-ban" title="%s"></i>', trans('firefly.openingBalance'));
2015-06-03 18:22:47 +02:00
break;
2015-05-17 09:35:49 +02:00
default:
2015-06-03 18:22:47 +02:00
$txt = '';
break;
2015-05-01 22:44:35 +02:00
}
2015-06-03 18:22:47 +02:00
return $txt;
2015-05-01 22:44:35 +02:00
}, ['is_safe' => ['html']]
);
}
}