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

146 lines
3.7 KiB
PHP
Raw Normal View History

2015-05-01 13:17:06 -05:00
<?php
2015-05-01 15:44:35 -05:00
namespace FireflyIII\Support\Twig;
2015-05-01 13:17:06 -05:00
use App;
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;
2015-05-01 13:17:06 -05:00
use Route;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* 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
*/
2015-05-01 13:17:06 -05:00
public function getFilters()
{
$filters = [];
$filters[] = new Twig_SimpleFilter(
'formatAmount', function ($string) {
return App::make('amount')->format($string);
}, ['is_safe' => ['html']]
);
$filters[] = new Twig_SimpleFilter(
'formatTransaction', function (Transaction $transaction) {
return App::make('amount')->formatTransaction($transaction);
}, ['is_safe' => ['html']]
);
$filters[] = new Twig_SimpleFilter(
'formatAmountPlain', function ($string) {
return App::make('amount')->format($string, false);
}, ['is_safe' => ['html']]
);
2015-05-01 13:17:06 -05:00
$filters[] = new Twig_SimpleFilter(
'formatJournal', function ($journal) {
return App::make('amount')->formatJournal($journal);
}, ['is_safe' => ['html']]
);
$filters[] = new Twig_SimpleFilter(
'balance', function (Account $account = null) {
if (is_null($account)) {
return 'NULL';
}
2015-05-01 15:44:35 -05:00
return App::make('steam')->balance($account);
}
2015-05-01 13:17:06 -05:00
);
2015-05-01 15:44:35 -05:00
// should be a function but OK
$filters[] = new Twig_SimpleFilter(
'getAccountRole', function ($name) {
return Config::get('firefly.accountRoles.' . $name);
2015-05-01 13:17:06 -05:00
}
);
2015-05-01 15:44:35 -05:00
2015-05-01 13:17:06 -05:00
return $filters;
}
/**
* {@inheritDoc}
*/
public function getFunctions()
{
$functions = [];
$functions[] = new Twig_SimpleFunction(
'getCurrencyCode', function () {
return App::make('amount')->getCurrencyCode();
}
);
2015-05-02 01:28:24 -05:00
$functions[] = new Twig_SimpleFunction(
'getCurrencySymbol', function () {
return App::make('amount')->getCurrencySymbol();
}
);
2015-05-01 16:17:17 -05:00
$functions[] = new Twig_SimpleFunction(
'phpdate', function ($str) {
return date($str);
}
);
2015-05-01 15:44:35 -05:00
2015-05-01 13:17:06 -05:00
$functions[] = new Twig_SimpleFunction(
'env', function ($name, $default) {
return env($name, $default);
}
);
2015-05-01 15:44:35 -05:00
$functions[] = new Twig_SimpleFunction(
'activeRoute', function ($context) {
2015-05-02 02:16:17 -05:00
$args = func_get_args();
$route = $args[1];
$what = isset($args[2]) ? $args[2] : false;
$strict = isset($args[3]) ? $args[3] : false;
2015-05-01 15:44:35 -05:00
$activeWhat = isset($context['what']) ? $context['what'] : false;
2015-05-02 02:16:17 -05:00
2015-05-01 15:44:35 -05:00
// activeRoute
if (!($what === false)) {
if ($what == $activeWhat && Route::getCurrentRoute()->getName() == $route) {
return 'active because-active-what';
}
} else {
2015-05-02 02:16:17 -05:00
if (!$strict && !(strpos(Route::getCurrentRoute()->getName(), $route) === false)) {
return 'active because-route-matches-non-strict';
} else {
if ($strict && Route::getCurrentRoute()->getName() == $route) {
return 'active because-route-matches-strict';
}
2015-05-01 15:44:35 -05:00
}
}
return 'not-xxx-at-all';
}, ['needs_context' => true]
);
2015-05-01 13:17:06 -05:00
return $functions;
}
/**
* {@inheritDoc}
*/
public function getName()
{
2015-05-01 15:44:35 -05:00
return 'FireflyIII\Support\Twig\General';
2015-05-01 13:17:06 -05:00
}
}