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

310 lines
7.7 KiB
PHP
Raw Normal View History

2015-05-01 13:17:06 -05:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-05-01 13:17:06 -05:00
2015-05-01 15:44:35 -05:00
namespace FireflyIII\Support\Twig;
2015-05-01 13:17:06 -05:00
2015-05-17 03:01:47 -05:00
use Carbon\Carbon;
2015-05-01 15:44:35 -05:00
use Config;
2015-05-01 13:17:06 -05:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2015-05-01 13:17:06 -05:00
use Route;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
2015-06-29 02:23:39 -05:00
* @codeCoverageIgnore
*
2015-05-01 13:17:06 -05:00
* Class TwigSupport
*
* @package FireflyIII\Support
*/
2015-05-01 15:44:35 -05:00
class General extends Twig_Extension
2015-05-01 13:17:06 -05:00
{
2015-05-01 15:44:35 -05:00
/**
* @return array
*/
2016-02-06 03:15:07 -06:00
public function getFilters(): array
2015-05-01 13:17:06 -05:00
{
2015-06-06 10:40:41 -05:00
return [
$this->formatAmount(),
$this->formatTransaction(),
$this->formatAmountPlain(),
$this->formatJournal(),
$this->balance(),
$this->getAccountRole(),
2015-07-19 04:47:56 -05:00
$this->formatFilesize(),
$this->mimeIcon(),
2015-06-06 10:40:41 -05:00
];
2015-05-01 13:17:06 -05:00
2015-06-06 10:40:41 -05:00
}
/**
* {@inheritDoc}
*/
2016-02-06 03:15:07 -06:00
public function getFunctions(): array
2015-06-06 10:40:41 -05:00
{
return [
$this->getCurrencyCode(),
$this->getCurrencySymbol(),
$this->phpdate(),
$this->env(),
2015-06-06 16:09:12 -05:00
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialWhat(),
2015-06-06 10:40:41 -05:00
];
}
/**
* {@inheritDoc}
*/
2016-02-06 03:15:07 -06:00
public function getName(): string
2015-06-06 10:40:41 -05:00
{
return 'FireflyIII\Support\Twig\General';
}
2015-07-26 12:07:02 -05:00
/**
2016-02-05 02:25:15 -06: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 12:07:02 -05:00
*/
2016-02-06 03:15:07 -06:00
protected function activeRoutePartial(): Twig_SimpleFunction
{
2016-02-05 02:25:15 -06:00
return new Twig_SimpleFunction(
2016-02-06 03:15:07 -06:00
'activeRoutePartial', function () : string {
2016-02-05 02:25:15 -06:00
$args = func_get_args();
$route = $args[0]; // name of the route.
if (!(strpos(Route::getCurrentRoute()->getName(), $route) === false)) {
return 'active';
}
2016-02-05 02:25:15 -06: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 03:15:07 -06:00
protected function activeRoutePartialWhat(): Twig_SimpleFunction
2016-02-05 02:25:15 -06:00
{
return new Twig_SimpleFunction(
2016-02-10 09:23:37 -06:00
'activeRoutePartialWhat', function ($context) : string {
2016-02-05 02:25:15 -06: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 02:25:15 -06:00
return '';
}, ['needs_context' => true]
);
}
/**
* Will return "active" when the current route matches the given argument
* exactly.
*
* @return Twig_SimpleFunction
*/
2016-02-06 03:15:07 -06:00
protected function activeRouteStrict(): Twig_SimpleFunction
2016-02-05 02:25:15 -06:00
{
return new Twig_SimpleFunction(
2016-02-06 03:17:41 -06:00
'activeRouteStrict', function () : string {
2016-02-05 02:25:15 -06:00
$args = func_get_args();
$route = $args[0]; // name of the route.
if (Route::getCurrentRoute()->getName() == $route) {
return 'active';
}
2016-02-05 02:25:15 -06:00
return '';
}
);
}
2015-07-19 04:47:56 -05:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 03:15:07 -06:00
protected function balance(): Twig_SimpleFilter
2015-07-19 04:47:56 -05:00
{
return new Twig_SimpleFilter(
2016-02-06 03:17:41 -06:00
'balance', function (Account $account = null) : string {
2016-02-05 02:25:15 -06:00
if (is_null($account)) {
return 'NULL';
2015-07-19 04:47:56 -05:00
}
2016-02-05 02:25:15 -06:00
$date = session('end', Carbon::now()->endOfMonth());
return app('steam')->balance($account, $date);
}
);
}
/**
* @return Twig_SimpleFunction
*/
2016-02-06 03:15:07 -06:00
protected function env(): Twig_SimpleFunction
2016-02-05 02:25:15 -06:00
{
return new Twig_SimpleFunction(
2016-02-06 03:17:41 -06:00
'env', function (string $name, string $default) : string {
2016-02-05 02:25:15 -06:00
return env($name, $default);
}
2015-07-19 04:47:56 -05:00
);
}
2015-06-06 10:40:41 -05:00
/**
*
2015-06-06 10:40:41 -05:00
* @return Twig_SimpleFilter
*/
2016-02-06 03:15:07 -06:00
protected function formatAmount(): Twig_SimpleFilter
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFilter(
2016-02-06 03:17:41 -06:00
'formatAmount', function (string $string) : string {
2016-02-05 02:25:15 -06:00
return app('amount')->format($string);
2015-05-01 13:17:06 -05:00
}, ['is_safe' => ['html']]
);
2015-06-06 10:40:41 -05:00
}
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 03:15:07 -06:00
protected function formatAmountPlain(): Twig_SimpleFilter
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFilter(
2016-02-06 03:17:41 -06:00
'formatAmountPlain', function (string $string) : string {
return app('amount')->format($string, false);
}, ['is_safe' => ['html']]
);
2015-06-06 10:40:41 -05:00
}
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 03:15:07 -06:00
protected function formatFilesize(): Twig_SimpleFilter
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFilter(
2016-02-06 03:17:41 -06:00
'filesize', function (int $size) : string {
2016-02-05 02:25:15 -06: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 10:40:41 -05:00
}
2015-05-01 13:17:06 -05:00
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 03:15:07 -06:00
protected function formatJournal(): Twig_SimpleFilter
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFilter(
2016-02-06 03:17:41 -06:00
'formatJournal', function (TransactionJournal $journal) : string {
2015-07-07 12:09:45 -05:00
return app('amount')->formatJournal($journal);
2015-05-01 13:17:06 -05:00
}, ['is_safe' => ['html']]
);
2015-06-06 10:40:41 -05:00
}
2015-05-01 13:17:06 -05:00
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 03:15:07 -06:00
protected function formatTransaction(): Twig_SimpleFilter
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFilter(
2016-02-06 03:17:41 -06:00
'formatTransaction', function (Transaction $transaction) : string {
2016-02-05 02:25:15 -06:00
return app('amount')->formatTransaction($transaction);
}, ['is_safe' => ['html']]
2015-05-01 13:17:06 -05:00
);
2015-06-06 10:40:41 -05:00
}
2015-05-01 13:17:06 -05:00
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 03:15:07 -06:00
protected function getAccountRole(): Twig_SimpleFilter
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFilter(
2016-02-06 03:17:41 -06:00
'getAccountRole', function (string $name) : string {
2015-05-01 15:44:35 -05:00
return Config::get('firefly.accountRoles.' . $name);
2015-05-01 13:17:06 -05:00
}
);
}
/**
2015-06-06 10:40:41 -05:00
* @return Twig_SimpleFunction
2015-05-01 13:17:06 -05:00
*/
2016-02-06 03:15:07 -06:00
protected function getCurrencyCode(): Twig_SimpleFunction
2015-05-01 13:17:06 -05:00
{
2015-06-06 10:40:41 -05:00
return new Twig_SimpleFunction(
2016-02-06 03:17:41 -06:00
'getCurrencyCode', function () : string {
2015-07-07 12:09:45 -05:00
return app('amount')->getCurrencyCode();
2015-05-01 13:17:06 -05:00
}
);
2015-06-06 10:40:41 -05:00
}
2015-05-01 13:17:06 -05:00
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFunction
*/
2016-02-06 03:15:07 -06:00
protected function getCurrencySymbol(): Twig_SimpleFunction
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFunction(
2016-02-06 03:17:41 -06:00
'getCurrencySymbol', function () : string {
2015-07-07 12:09:45 -05:00
return app('amount')->getCurrencySymbol();
2015-05-02 01:28:24 -05:00
}
);
2015-06-06 10:40:41 -05:00
}
2015-05-02 01:28:24 -05:00
2015-06-06 10:40:41 -05:00
/**
2016-02-05 02:25:15 -06:00
* @return Twig_SimpleFilter
2015-06-06 16:09:12 -05:00
*/
2016-02-06 03:15:07 -06:00
protected function mimeIcon(): Twig_SimpleFilter
2015-06-06 16:09:12 -05:00
{
2016-02-05 02:25:15 -06:00
return new Twig_SimpleFilter(
2016-02-06 03:17:41 -06:00
'mimeIcon', function (string $string) : string {
2016-02-05 02:25:15 -06: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 16:09:12 -05:00
}
2016-02-05 02:25:15 -06:00
}, ['is_safe' => ['html']]
2015-06-06 16:09:12 -05:00
);
}
/**
* @return Twig_SimpleFunction
*/
2016-02-05 02:25:15 -06:00
protected function phpdate()
2015-06-06 16:09:12 -05:00
{
return new Twig_SimpleFunction(
2016-02-06 03:17:41 -06:00
'phpdate', function (string $str) : string {
2016-02-05 02:25:15 -06:00
return date($str);
2015-06-06 16:09:12 -05:00
}
);
}
}