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

330 lines
8.2 KiB
PHP
Raw Normal View History

2015-05-01 20:17:06 +02:00
<?php
/**
* General.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2015-05-01 20:17:06 +02:00
2015-05-01 22:44:35 +02:00
namespace FireflyIII\Support\Twig;
2015-05-01 20:17:06 +02:00
2015-05-17 10:01:47 +02:00
use Carbon\Carbon;
2015-05-01 22:44:35 +02:00
use Config;
2015-05-01 20:17:06 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2015-05-01 20:17:06 +02:00
use Route;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
2015-06-29 09:23:39 +02:00
*
2015-05-01 20:17:06 +02:00
* Class TwigSupport
*
* @package FireflyIII\Support
*/
2015-05-01 22:44:35 +02:00
class General extends Twig_Extension
2015-05-01 20:17:06 +02:00
{
2015-05-01 22:44:35 +02:00
/**
* @return array
*/
2016-02-06 10:15:07 +01:00
public function getFilters(): array
2015-05-01 20:17:06 +02:00
{
2015-06-06 17:40:41 +02:00
return [
$this->formatAmount(),
$this->formatTransaction(),
$this->formatAmountPlain(),
$this->formatJournal(),
$this->balance(),
$this->getAccountRole(),
2015-07-19 11:47:56 +02:00
$this->formatFilesize(),
$this->mimeIcon(),
2015-06-06 17:40:41 +02:00
];
2015-05-01 20:17:06 +02:00
2015-06-06 17:40:41 +02:00
}
/**
* {@inheritDoc}
*/
2016-02-06 10:15:07 +01:00
public function getFunctions(): array
2015-06-06 17:40:41 +02:00
{
return [
$this->getCurrencyCode(),
$this->getCurrencySymbol(),
$this->phpdate(),
$this->env(),
2016-04-03 14:38:12 +02:00
$this->getAmountFromJournal(),
2015-06-06 23:09:12 +02:00
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialWhat(),
2015-06-06 17:40:41 +02:00
];
}
/**
* {@inheritDoc}
*/
2016-02-06 10:15:07 +01:00
public function getName(): string
2015-06-06 17:40:41 +02:00
{
return 'FireflyIII\Support\Twig\General';
}
2015-07-26 19:07:02 +02:00
/**
2016-02-05 09:25:15 +01:00
* Will return "active" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return Twig_SimpleFunction
2015-07-26 19:07:02 +02:00
*/
2016-02-06 10:15:07 +01:00
protected function activeRoutePartial(): Twig_SimpleFunction
{
2016-02-05 09:25:15 +01:00
return new Twig_SimpleFunction(
2016-02-06 10:15:07 +01:00
'activeRoutePartial', function () : string {
2016-02-05 09:25:15 +01:00
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
if (!(strpos($name, $route) === false)) {
2016-02-05 09:25:15 +01:00
return 'active';
}
2016-02-05 09:25:15 +01:00
return '';
}
);
}
/**
* This function will return "active" when the current route matches the first argument (even partly)
* but, the variable $what has been set and matches the second argument.
*
* @return Twig_SimpleFunction
*/
2016-02-06 10:15:07 +01:00
protected function activeRoutePartialWhat(): Twig_SimpleFunction
2016-02-05 09:25:15 +01:00
{
return new Twig_SimpleFunction(
2016-02-10 16:23:37 +01:00
'activeRoutePartialWhat', function ($context) : string {
2016-02-05 09:25:15 +01:00
$args = func_get_args();
$route = $args[1]; // name of the route.
$what = $args[2]; // name of the route.
$activeWhat = $context['what'] ?? false;
if ($what == $activeWhat && !(strpos(Route::getCurrentRoute()->getName(), $route) === false)) {
return 'active';
}
2016-02-05 09:25:15 +01:00
return '';
}, ['needs_context' => true]
);
}
/**
* Will return "active" when the current route matches the given argument
* exactly.
*
* @return Twig_SimpleFunction
*/
2016-02-06 10:15:07 +01:00
protected function activeRouteStrict(): Twig_SimpleFunction
2016-02-05 09:25:15 +01:00
{
return new Twig_SimpleFunction(
2016-02-06 10:17:41 +01:00
'activeRouteStrict', function () : string {
2016-02-05 09:25:15 +01:00
$args = func_get_args();
$route = $args[0]; // name of the route.
if (Route::getCurrentRoute()->getName() == $route) {
return 'active';
}
2016-02-05 09:25:15 +01:00
return '';
}
);
}
2015-07-19 11:47:56 +02:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 10:15:07 +01:00
protected function balance(): Twig_SimpleFilter
2015-07-19 11:47:56 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 10:17:41 +01:00
'balance', function (Account $account = null) : string {
2016-02-05 09:25:15 +01:00
if (is_null($account)) {
return 'NULL';
2015-07-19 11:47:56 +02:00
}
2016-02-05 09:25:15 +01:00
$date = session('end', Carbon::now()->endOfMonth());
return app('steam')->balance($account, $date);
}
);
}
/**
* @return Twig_SimpleFunction
*/
2016-02-06 10:15:07 +01:00
protected function env(): Twig_SimpleFunction
2016-02-05 09:25:15 +01:00
{
return new Twig_SimpleFunction(
2016-02-06 10:17:41 +01:00
'env', function (string $name, string $default) : string {
2016-02-05 09:25:15 +01:00
return env($name, $default);
}
2015-07-19 11:47:56 +02:00
);
}
2015-06-06 17:40:41 +02:00
/**
*
2015-06-06 17:40:41 +02:00
* @return Twig_SimpleFilter
*/
2016-02-06 10:15:07 +01:00
protected function formatAmount(): Twig_SimpleFilter
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 10:17:41 +01:00
'formatAmount', function (string $string) : string {
2016-02-05 09:25:15 +01:00
return app('amount')->format($string);
2015-05-01 20:17:06 +02:00
}, ['is_safe' => ['html']]
);
2015-06-06 17:40:41 +02:00
}
2015-06-06 17:40:41 +02:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 10:15:07 +01:00
protected function formatAmountPlain(): Twig_SimpleFilter
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 10:17:41 +01:00
'formatAmountPlain', function (string $string) : string {
return app('amount')->format($string, false);
}, ['is_safe' => ['html']]
);
2015-06-06 17:40:41 +02:00
}
2015-06-06 17:40:41 +02:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 10:15:07 +01:00
protected function formatFilesize(): Twig_SimpleFilter
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 10:17:41 +01:00
'filesize', function (int $size) : string {
2016-02-05 09:25:15 +01:00
// less than one GB, more than one MB
if ($size < (1024 * 1024 * 2014) && $size >= (1024 * 1024)) {
return round($size / (1024 * 1024), 2) . ' MB';
}
// less than one MB
if ($size < (1024 * 1024)) {
return round($size / 1024, 2) . ' KB';
}
return $size . ' bytes';
}
);
2015-06-06 17:40:41 +02:00
}
2015-05-01 20:17:06 +02:00
2015-06-06 17:40:41 +02:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 10:15:07 +01:00
protected function formatJournal(): Twig_SimpleFilter
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 10:17:41 +01:00
'formatJournal', function (TransactionJournal $journal) : string {
2015-07-07 19:09:45 +02:00
return app('amount')->formatJournal($journal);
2015-05-01 20:17:06 +02:00
}, ['is_safe' => ['html']]
);
2015-06-06 17:40:41 +02:00
}
2015-05-01 20:17:06 +02:00
2015-06-06 17:40:41 +02:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 10:15:07 +01:00
protected function formatTransaction(): Twig_SimpleFilter
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 10:17:41 +01:00
'formatTransaction', function (Transaction $transaction) : string {
2016-02-05 09:25:15 +01:00
return app('amount')->formatTransaction($transaction);
}, ['is_safe' => ['html']]
2015-05-01 20:17:06 +02:00
);
2015-06-06 17:40:41 +02:00
}
2015-05-01 20:17:06 +02:00
2015-06-06 17:40:41 +02:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 10:15:07 +01:00
protected function getAccountRole(): Twig_SimpleFilter
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 10:17:41 +01:00
'getAccountRole', function (string $name) : string {
2015-05-01 22:44:35 +02:00
return Config::get('firefly.accountRoles.' . $name);
2015-05-01 20:17:06 +02:00
}
);
}
/**
2015-06-06 17:40:41 +02:00
* @return Twig_SimpleFunction
2015-05-01 20:17:06 +02:00
*/
2016-02-06 10:15:07 +01:00
protected function getCurrencyCode(): Twig_SimpleFunction
2015-05-01 20:17:06 +02:00
{
2015-06-06 17:40:41 +02:00
return new Twig_SimpleFunction(
2016-02-06 10:17:41 +01:00
'getCurrencyCode', function () : string {
2015-07-07 19:09:45 +02:00
return app('amount')->getCurrencyCode();
2015-05-01 20:17:06 +02:00
}
);
2015-06-06 17:40:41 +02:00
}
2015-05-01 20:17:06 +02:00
2015-06-06 17:40:41 +02:00
/**
* @return Twig_SimpleFunction
*/
2016-02-06 10:15:07 +01:00
protected function getCurrencySymbol(): Twig_SimpleFunction
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFunction(
2016-02-06 10:17:41 +01:00
'getCurrencySymbol', function () : string {
2015-07-07 19:09:45 +02:00
return app('amount')->getCurrencySymbol();
2015-05-02 08:28:24 +02:00
}
);
2015-06-06 17:40:41 +02:00
}
2015-05-02 08:28:24 +02:00
2015-06-06 17:40:41 +02:00
/**
2016-02-05 09:25:15 +01:00
* @return Twig_SimpleFilter
2015-06-06 23:09:12 +02:00
*/
2016-02-06 10:15:07 +01:00
protected function mimeIcon(): Twig_SimpleFilter
2015-06-06 23:09:12 +02:00
{
2016-02-05 09:25:15 +01:00
return new Twig_SimpleFilter(
2016-02-06 10:17:41 +01:00
'mimeIcon', function (string $string) : string {
2016-02-05 09:25:15 +01:00
switch ($string) {
default:
return 'fa-file-o';
case 'application/pdf':
return 'fa-file-pdf-o';
case 'image/png':
case 'image/jpeg':
return 'fa-file-image-o';
2015-06-06 23:09:12 +02:00
}
2016-02-05 09:25:15 +01:00
}, ['is_safe' => ['html']]
2015-06-06 23:09:12 +02:00
);
}
/**
* @return Twig_SimpleFunction
*/
2016-02-05 09:25:15 +01:00
protected function phpdate()
2015-06-06 23:09:12 +02:00
{
return new Twig_SimpleFunction(
2016-02-06 10:17:41 +01:00
'phpdate', function (string $str) : string {
2016-02-05 09:25:15 +01:00
return date($str);
2015-06-06 23:09:12 +02:00
}
);
}
2016-04-03 14:38:12 +02:00
/**
* @return Twig_SimpleFunction
*/
private function getAmountFromJournal()
{
return new Twig_SimpleFunction(
'getAmount', function (TransactionJournal $journal) : string {
return TransactionJournal::amount($journal);
}
);
}
}