Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -35,9 +35,6 @@ use Twig\TwigFunction;
*/
class AmountFormat extends AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
@@ -46,9 +43,15 @@ class AmountFormat extends AbstractExtension
];
}
/**
* @return TwigFilter
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
$this->formatAmountBySymbol(),
$this->formatAmountByCurrency(),
];
}
protected function formatAmount(): TwigFilter
{
return new TwigFilter(
@@ -62,9 +65,6 @@ class AmountFormat extends AbstractExtension
);
}
/**
* @return TwigFilter
*/
protected function formatAmountPlain(): TwigFilter
{
return new TwigFilter(
@@ -78,23 +78,11 @@ class AmountFormat extends AbstractExtension
);
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
$this->formatAmountBySymbol(),
$this->formatAmountByCurrency(),
];
}
/**
* Will format the amount by the currency related to the given account.
*
* @return TwigFunction
* TODO remove me when layout v1 is deprecated.
* @return twigFunction
* TODO remove me when layout v1 is deprecated
*/
protected function formatAmountByAccount(): TwigFunction
{
@@ -102,6 +90,7 @@ class AmountFormat extends AbstractExtension
'formatAmountByAccount',
static function (AccountModel $account, string $amount, bool $coloured = null): string {
$coloured ??= true;
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
$currency = $accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
@@ -114,8 +103,6 @@ class AmountFormat extends AbstractExtension
/**
* Will format the amount by the currency related to the given account.
*
* @return TwigFunction
*/
protected function formatAmountBySymbol(): TwigFunction
{
@@ -136,8 +123,6 @@ class AmountFormat extends AbstractExtension
/**
* Will format the amount by the currency related to the given account.
*
* @return TwigFunction
*/
protected function formatAmountByCurrency(): TwigFunction
{

View File

@@ -39,9 +39,6 @@ use Twig\TwigFunction;
*/
class General extends AbstractExtension
{
/**
* @inheritDoc
*/
public function getFilters(): array
{
return [
@@ -53,10 +50,24 @@ class General extends AbstractExtension
];
}
public function getFunctions(): array
{
return [
$this->phpdate(),
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialObjectType(),
$this->menuOpenRoutePartial(),
$this->formatDate(),
$this->getMetaField(),
$this->hasRole(),
$this->getRootSearchOperator(),
$this->carbonize(),
];
}
/**
* Show account balance. Only used on the front page of Firefly III.
*
* @return TwigFilter
*/
protected function balance(): TwigFilter
{
@@ -66,6 +77,7 @@ class General extends AbstractExtension
if (null === $account) {
return '0';
}
/** @var Carbon $date */
$date = session('end', today(config('app.timezone'))->endOfMonth());
@@ -76,8 +88,6 @@ class General extends AbstractExtension
/**
* Used to convert 1024 to 1kb etc.
*
* @return TwigFilter
*/
protected function formatFilesize(): TwigFilter
{
@@ -86,23 +96,21 @@ class General extends AbstractExtension
static function (int $size): string {
// less than one GB, more than one MB
if ($size < (1024 * 1024 * 2014) && $size >= (1024 * 1024)) {
return round($size / (1024 * 1024), 2) . ' MB';
return round($size / (1024 * 1024), 2).' MB';
}
// less than one MB
if ($size < (1024 * 1024)) {
return round($size / 1024, 2) . ' KB';
return round($size / 1024, 2).' KB';
}
return $size . ' bytes';
return $size.' bytes';
}
);
}
/**
* Show icon with attachment.
*
* @return TwigFilter
*/
protected function mimeIcon(): TwigFilter
{
@@ -112,9 +120,11 @@ class General extends AbstractExtension
switch ($string) {
default:
return 'fa-file-o';
case 'application/pdf':
return 'fa-file-pdf-o';
/* image */
// image
case 'image/png':
case 'image/jpeg':
case 'image/svg+xml':
@@ -122,7 +132,8 @@ class General extends AbstractExtension
case 'image/heic-sequence':
case 'application/vnd.oasis.opendocument.image':
return 'fa-file-image-o';
/* MS word */
// MS word
case 'application/msword':
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.template':
@@ -137,7 +148,8 @@ class General extends AbstractExtension
case 'application/vnd.oasis.opendocument.text-web':
case 'application/vnd.oasis.opendocument.text-master':
return 'fa-file-word-o';
/* MS excel */
// MS excel
case 'application/vnd.ms-excel':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.template':
@@ -147,7 +159,8 @@ class General extends AbstractExtension
case 'application/vnd.oasis.opendocument.spreadsheet':
case 'application/vnd.oasis.opendocument.spreadsheet-template':
return 'fa-file-excel-o';
/* MS powerpoint */
// MS powerpoint
case 'application/vnd.ms-powerpoint':
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
case 'application/vnd.openxmlformats-officedocument.presentationml.template':
@@ -158,12 +171,14 @@ class General extends AbstractExtension
case 'application/vnd.oasis.opendocument.presentation':
case 'application/vnd.oasis.opendocument.presentation-template':
return 'fa-file-powerpoint-o';
/* calc */
// calc
case 'application/vnd.sun.xml.draw':
case 'application/vnd.sun.xml.draw.template':
case 'application/vnd.stardivision.draw':
case 'application/vnd.oasis.opendocument.chart':
return 'fa-paint-brush';
case 'application/vnd.oasis.opendocument.graphics':
case 'application/vnd.oasis.opendocument.graphics-template':
case 'application/vnd.sun.xml.math':
@@ -177,9 +192,6 @@ class General extends AbstractExtension
);
}
/**
* @return TwigFilter
*/
protected function markdown(): TwigFilter
{
return new TwigFilter(
@@ -201,8 +213,6 @@ class General extends AbstractExtension
/**
* Show URL host name
*
* @return TwigFilter
*/
protected function phpHostName(): TwigFilter
{
@@ -217,29 +227,8 @@ class General extends AbstractExtension
);
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->phpdate(),
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialObjectType(),
$this->menuOpenRoutePartial(),
$this->formatDate(),
$this->getMetaField(),
$this->hasRole(),
$this->getRootSearchOperator(),
$this->carbonize(),
];
}
/**
* Basic example thing for some views.
*
* @return TwigFunction
*/
protected function phpdate(): TwigFunction
{
@@ -254,8 +243,6 @@ class General extends AbstractExtension
/**
* Will return "active" when the current route matches the given argument
* exactly.
*
* @return TwigFunction
*/
protected function activeRouteStrict(): TwigFunction
{
@@ -265,7 +252,7 @@ class General extends AbstractExtension
$args = func_get_args();
$route = $args[0]; // name of the route.
if (Route::getCurrentRoute()->getName() === $route) {
if (\Route::getCurrentRoute()->getName() === $route) {
return 'active';
}
@@ -277,8 +264,6 @@ class General extends AbstractExtension
/**
* Will return "active" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return TwigFunction
*/
protected function activeRoutePartial(): TwigFunction
{
@@ -287,7 +272,7 @@ class General extends AbstractExtension
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
$name = \Route::getCurrentRoute()->getName() ?? '';
if (str_contains($name, $route)) {
return 'active';
}
@@ -300,8 +285,6 @@ class General extends AbstractExtension
/**
* This function will return "active" when the current route matches the first argument (even partly)
* but, the variable $objectType has been set and matches the second argument.
*
* @return TwigFunction
*/
protected function activeRoutePartialObjectType(): TwigFunction
{
@@ -313,7 +296,7 @@ class General extends AbstractExtension
if ($objectType === $activeObjectType
&& false !== stripos(
Route::getCurrentRoute()->getName(),
\Route::getCurrentRoute()->getName(),
$route
)) {
return 'active';
@@ -328,8 +311,6 @@ class General extends AbstractExtension
/**
* Will return "menu-open" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return TwigFunction
*/
protected function menuOpenRoutePartial(): TwigFunction
{
@@ -338,7 +319,7 @@ class General extends AbstractExtension
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
$name = \Route::getCurrentRoute()->getName() ?? '';
if (str_contains($name, $route)) {
return 'menu-open';
}
@@ -350,8 +331,6 @@ class General extends AbstractExtension
/**
* Formats a string as a thing by converting it to a Carbon first.
*
* @return TwigFunction
*/
protected function formatDate(): TwigFunction
{
@@ -366,8 +345,8 @@ class General extends AbstractExtension
}
/**
* @return TwigFunction
* TODO remove me when layout v1 is deprecated.
* @return twigFunction
* TODO remove me when layout v1 is deprecated
*/
protected function getMetaField(): TwigFunction
{
@@ -388,8 +367,6 @@ class General extends AbstractExtension
/**
* Will return true if the user is of role X.
*
* @return TwigFunction
*/
protected function hasRole(): TwigFunction
{
@@ -406,9 +383,6 @@ class General extends AbstractExtension
);
}
/**
* @return TwigFunction
*/
protected function getRootSearchOperator(): TwigFunction
{
return new TwigFunction(
@@ -421,9 +395,6 @@ class General extends AbstractExtension
);
}
/**
* @return TwigFunction
*/
protected function carbonize(): TwigFunction
{
return new TwigFunction(

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use Config;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
@@ -32,9 +31,6 @@ use Twig\TwigFunction;
*/
class Rule extends AbstractExtension
{
/**
* @inheritDoc
*/
public function getFunctions(): array
{
return [
@@ -44,9 +40,6 @@ class Rule extends AbstractExtension
];
}
/**
* @return TwigFunction
*/
public function allJournalTriggers(): TwigFunction
{
return new TwigFunction(
@@ -60,9 +53,6 @@ class Rule extends AbstractExtension
);
}
/**
* @return TwigFunction
*/
public function allRuleTriggers(): TwigFunction
{
return new TwigFunction(
@@ -72,7 +62,7 @@ class Rule extends AbstractExtension
$possibleTriggers = [];
foreach ($ruleTriggers as $key) {
if ('user_action' !== $key) {
$possibleTriggers[$key] = (string)trans('firefly.rule_trigger_' . $key . '_choice');
$possibleTriggers[$key] = (string)trans('firefly.rule_trigger_'.$key.'_choice');
}
}
unset($ruleTriggers);
@@ -83,19 +73,16 @@ class Rule extends AbstractExtension
);
}
/**
* @return TwigFunction
*/
public function allActionTriggers(): TwigFunction
{
return new TwigFunction(
'allRuleActions',
static function () {
// array of valid values for actions
$ruleActions = array_keys(Config::get('firefly.rule-actions'));
$ruleActions = array_keys(\Config::get('firefly.rule-actions'));
$possibleActions = [];
foreach ($ruleActions as $key) {
$possibleActions[$key] = (string)trans('firefly.rule_action_' . $key . '_choice');
$possibleActions[$key] = (string)trans('firefly.rule_action_'.$key.'_choice');
}
unset($ruleActions);
asort($possibleActions);

View File

@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use Carbon\Carbon;
use DB;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
@@ -38,9 +37,6 @@ use Twig\TwigFunction;
*/
class TransactionGroupTwig extends AbstractExtension
{
/**
* @inheritDoc
*/
public function getFunctions(): array
{
return [
@@ -54,8 +50,6 @@ class TransactionGroupTwig extends AbstractExtension
/**
* Shows the amount for a single journal array.
*
* @return TwigFunction
*/
public function journalArrayAmount(): TwigFunction
{
@@ -76,91 +70,8 @@ class TransactionGroupTwig extends AbstractExtension
);
}
/**
* Generate normal amount for transaction from a transaction group.
*
* @param array $array
*
* @return string
*/
private function normalJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['currency_symbol'], (int)$array['currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* @param string $amount
* @param string $transactionType
* @param string $sourceType
*
* @return string
*/
private function signAmount(string $amount, string $transactionType, string $sourceType): string
{
// withdrawals stay negative
if ($transactionType !== TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
// opening balance and it comes from initial balance? its expense.
if ($transactionType === TransactionType::OPENING_BALANCE && AccountType::INITIAL_BALANCE !== $sourceType) {
$amount = bcmul($amount, '-1');
}
// reconciliation and it comes from reconciliation?
if ($transactionType === TransactionType::RECONCILIATION && AccountType::RECONCILIATION !== $sourceType) {
$amount = bcmul($amount, '-1');
}
return $amount;
}
/**
* Generate foreign amount for transaction from a transaction group.
*
* @param array $array
*
* @return string
*/
private function foreignJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['foreign_amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* Shows the amount for a single journal object.
*
* @return TwigFunction
*/
public function journalObjectAmount(): TwigFunction
{
@@ -180,12 +91,133 @@ class TransactionGroupTwig extends AbstractExtension
);
}
public function journalHasMeta(): TwigFunction
{
return new TwigFunction(
'journalHasMeta',
static function (int $journalId, string $metaField) {
$count = \DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->count()
;
return 1 === $count;
}
);
}
public function journalGetMetaDate(): TwigFunction
{
return new TwigFunction(
'journalGetMetaDate',
static function (int $journalId, string $metaField) {
/** @var null|TransactionJournalMeta $entry */
$entry = \DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first()
;
if (null === $entry) {
return today(config('app.timezone'));
}
return new Carbon(json_decode($entry->data, false));
}
);
}
public function journalGetMetaField(): TwigFunction
{
return new TwigFunction(
'journalGetMetaField',
static function (int $journalId, string $metaField) {
/** @var null|TransactionJournalMeta $entry */
$entry = \DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first()
;
if (null === $entry) {
return '';
}
return json_decode($entry->data, true);
}
);
}
/**
* Generate normal amount for transaction from a transaction group.
*/
private function normalJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
if (TransactionType::TRANSFER === $type) {
$colored = false;
}
$result = app('amount')->formatFlat($array['currency_symbol'], (int)$array['currency_decimal_places'], $amount, $colored);
if (TransactionType::TRANSFER === $type) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
private function signAmount(string $amount, string $transactionType, string $sourceType): string
{
// withdrawals stay negative
if (TransactionType::WITHDRAWAL !== $transactionType) {
$amount = bcmul($amount, '-1');
}
// opening balance and it comes from initial balance? its expense.
if (TransactionType::OPENING_BALANCE === $transactionType && AccountType::INITIAL_BALANCE !== $sourceType) {
$amount = bcmul($amount, '-1');
}
// reconciliation and it comes from reconciliation?
if (TransactionType::RECONCILIATION === $transactionType && AccountType::RECONCILIATION !== $sourceType) {
$amount = bcmul($amount, '-1');
}
return $amount;
}
/**
* Generate foreign amount for transaction from a transaction group.
*/
private function foreignJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['foreign_amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
if (TransactionType::TRANSFER === $type) {
$colored = false;
}
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored);
if (TransactionType::TRANSFER === $type) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* Generate normal amount for transaction from a transaction group.
*
* @param TransactionJournal $journal
*
* @return string
*/
private function normalJournalObjectAmount(TransactionJournal $journal): string
{
@@ -198,22 +230,17 @@ class TransactionGroupTwig extends AbstractExtension
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
if (TransactionType::TRANSFER === $type) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
if (TransactionType::TRANSFER === $type) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
private function journalObjectHasForeign(TransactionJournal $journal): bool
{
/** @var Transaction $first */
@@ -224,14 +251,11 @@ class TransactionGroupTwig extends AbstractExtension
/**
* Generate foreign amount for journal from a transaction group.
*
* @param TransactionJournal $journal
*
* @return string
*/
private function foreignJournalObjectAmount(TransactionJournal $journal): string
{
$type = $journal->transactionType->type;
/** @var Transaction $first */
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $first->foreignCurrency;
@@ -241,79 +265,14 @@ class TransactionGroupTwig extends AbstractExtension
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
if (TransactionType::TRANSFER === $type) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
if (TransactionType::TRANSFER === $type) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* @return TwigFunction
*/
public function journalHasMeta(): TwigFunction
{
return new TwigFunction(
'journalHasMeta',
static function (int $journalId, string $metaField) {
$count = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->count();
return 1 === $count;
}
);
}
/**
* @return TwigFunction
*/
public function journalGetMetaDate(): TwigFunction
{
return new TwigFunction(
'journalGetMetaDate',
static function (int $journalId, string $metaField) {
/** @var TransactionJournalMeta|null $entry */
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first();
if (null === $entry) {
return today(config('app.timezone'));
}
return new Carbon(json_decode($entry->data, false));
}
);
}
/**
* @return TwigFunction
*/
public function journalGetMetaField(): TwigFunction
{
return new TwigFunction(
'journalGetMetaField',
static function (int $journalId, string $metaField) {
/** @var TransactionJournalMeta|null $entry */
$entry = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->first();
if (null === $entry) {
return '';
}
return json_decode($entry->data, true);
}
);
}
}

View File

@@ -32,9 +32,6 @@ use Twig\TwigFunction;
*/
class Translation extends AbstractExtension
{
/**
* @inheritDoc
*/
public function getFilters(): array
{
return [
@@ -48,9 +45,6 @@ class Translation extends AbstractExtension
];
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
@@ -58,9 +52,6 @@ class Translation extends AbstractExtension
];
}
/**
* @return TwigFunction
*/
public function journalLinkTranslation(): TwigFunction
{
return new TwigFunction(