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

224 lines
5.2 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(),
$this->activeRoute()
];
}
/**
* {@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
/**
* @return Twig_SimpleFunction
*/
protected function activeRoute()
{
return 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
}
}