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

261 lines
6.1 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-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;
2015-05-01 13:17:06 -05:00
use Route;
2015-05-17 03:01:47 -05:00
use Session;
2015-05-01 13:17:06 -05:00
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
/**
2015-05-17 02:35:49 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2015-05-01 15:44:35 -05:00
* @return array
*/
2015-05-01 13:17:06 -05:00
public function getFilters()
{
2015-06-06 10:40:41 -05:00
return [
$this->formatAmount(),
$this->formatTransaction(),
$this->formatAmountPlain(),
$this->formatJournal(),
$this->balance(),
$this->getAccountRole()
];
2015-05-01 13:17:06 -05:00
2015-06-06 10:40:41 -05:00
}
/**
* {@inheritDoc}
*/
public function getFunctions()
{
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}
*/
public function getName()
{
return 'FireflyIII\Support\Twig\General';
}
/**
* @return Twig_SimpleFilter
*/
protected function formatAmount()
{
return new Twig_SimpleFilter(
'formatAmount', function ($string) {
2015-05-01 13:17:06 -05:00
return App::make('amount')->format($string);
}, ['is_safe' => ['html']]
);
2015-06-06 10:40:41 -05:00
}
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFilter
*/
protected function formatTransaction()
{
return new Twig_SimpleFilter(
'formatTransaction', function (Transaction $transaction) {
return App::make('amount')->formatTransaction($transaction);
}, ['is_safe' => ['html']]
);
2015-06-06 10:40:41 -05:00
}
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFilter
*/
protected function formatAmountPlain()
{
return new Twig_SimpleFilter(
'formatAmountPlain', function ($string) {
return App::make('amount')->format($string, false);
}, ['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
*/
protected function formatJournal()
{
return new Twig_SimpleFilter(
'formatJournal', function ($journal) {
2015-05-01 13:17:06 -05:00
return App::make('amount')->formatJournal($journal);
}, ['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
*/
protected function balance()
{
return new Twig_SimpleFilter(
'balance', function (Account $account = null) {
2015-05-01 13:17:06 -05:00
if (is_null($account)) {
return 'NULL';
}
2015-05-17 03:01:47 -05:00
$date = Session::get('end', Carbon::now()->endOfMonth());
2015-05-01 13:17:06 -05:00
2015-05-17 03:01:47 -05:00
return App::make('steam')->balance($account, $date);
2015-05-01 15:44:35 -05:00
}
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
*/
protected function getAccountRole()
{
return new Twig_SimpleFilter(
'getAccountRole', function ($name) {
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
*/
2015-06-06 10:40:41 -05:00
protected function getCurrencyCode()
2015-05-01 13:17:06 -05:00
{
2015-06-06 10:40:41 -05:00
return new Twig_SimpleFunction(
'getCurrencyCode', function () {
2015-05-01 13:17:06 -05:00
return App::make('amount')->getCurrencyCode();
}
);
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
*/
protected function getCurrencySymbol()
{
return new Twig_SimpleFunction(
'getCurrencySymbol', function () {
2015-05-02 01:28:24 -05:00
return App::make('amount')->getCurrencySymbol();
}
);
2015-06-06 10:40:41 -05:00
}
2015-05-02 01:28:24 -05:00
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFunction
*/
protected function phpdate()
{
return new Twig_SimpleFunction(
'phpdate', function ($str) {
2015-05-01 16:17:17 -05:00
return date($str);
}
);
2015-06-06 10:40:41 -05:00
}
2015-05-01 16:17:17 -05:00
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFunction
*/
protected function env()
{
return new Twig_SimpleFunction(
'env', function ($name, $default) {
2015-05-01 13:17:06 -05:00
return env($name, $default);
}
);
2015-06-06 10:40:41 -05:00
}
2015-05-01 13:17:06 -05:00
2015-06-06 10:40:41 -05:00
/**
2015-06-06 16:09:12 -05:00
* Will return "active" when the current route matches the given argument
* exactly.
*
* @return Twig_SimpleFunction
*/
protected function activeRouteStrict()
{
return new Twig_SimpleFunction(
'activeRouteStrict', function () {
$args = func_get_args();
$route = $args[0]; // name of the route.
if (Route::getCurrentRoute()->getName() == $route) {
return 'active';
2015-06-06 16:09:12 -05:00
}
return '';
2015-06-06 16:09:12 -05:00
}
);
}
/**
* Will return "active" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return Twig_SimpleFunction
*/
protected function activeRoutePartial()
{
return new Twig_SimpleFunction(
'activeRoutePartial', function () {
$args = func_get_args();
$route = $args[0]; // name of the route.
if (!(strpos(Route::getCurrentRoute()->getName(), $route) === false)) {
return 'active';
2015-06-06 16:09:12 -05:00
}
return '';
2015-06-06 16:09:12 -05:00
}
);
}
/**
* 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.
*
2015-06-06 10:40:41 -05:00
* @return Twig_SimpleFunction
*/
2015-06-06 16:09:12 -05:00
protected function activeRoutePartialWhat()
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFunction(
2015-06-06 16:09:12 -05:00
'activeRoutePartialWhat', function ($context) {
2015-05-02 02:16:17 -05:00
$args = func_get_args();
2015-06-06 16:09:12 -05:00
$route = $args[1]; // name of the route.
$what = $args[2]; // name of the route.
2015-05-01 15:44:35 -05:00
$activeWhat = isset($context['what']) ? $context['what'] : false;
2015-05-02 02:16:17 -05:00
2015-06-06 16:09:12 -05:00
if ($what == $activeWhat && !(strpos(Route::getCurrentRoute()->getName(), $route) === false)) {
return 'active';
2015-05-01 15:44:35 -05:00
}
return '';
2015-05-01 15:44:35 -05:00
}, ['needs_context' => true]
);
2015-05-01 13:17:06 -05:00
}
}