mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Code cleanup
This commit is contained in:
@@ -72,109 +72,6 @@ class General extends AbstractExtension
|
||||
];
|
||||
}
|
||||
|
||||
protected function getRootSearchOperator(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'getRootSearchOperator',
|
||||
static function (string $operator): string {
|
||||
return OperatorQuerySearch::getRootOperator($operator);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "active" when a part of the route matches the argument.
|
||||
* ie. "accounts" will match "accounts.index".
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function activeRoutePartial(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'activeRoutePartial',
|
||||
static function (): string {
|
||||
$args = func_get_args();
|
||||
$route = $args[0]; // name of the route.
|
||||
$name = Route::getCurrentRoute()->getName() ?? '';
|
||||
if (false !== strpos($name, $route)) {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "menu-open" when a part of the route matches the argument.
|
||||
* ie. "accounts" will match "accounts.index".
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function menuOpenRoutePartial(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'menuOpenRoutePartial',
|
||||
static function (): string {
|
||||
$args = func_get_args();
|
||||
$route = $args[0]; // name of the route.
|
||||
$name = Route::getCurrentRoute()->getName() ?? '';
|
||||
if (false !== strpos($name, $route)) {
|
||||
return 'menu-open';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will return "active" when the current route matches the first argument (even partly)
|
||||
* but, the variable $objectType has been set and matches the second argument.
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function activeRoutePartialObjectType(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'activeRoutePartialObjectType',
|
||||
static function ($context): string {
|
||||
[, $route, $objectType] = func_get_args();
|
||||
$activeObjectType = $context['objectType'] ?? false;
|
||||
|
||||
if ($objectType === $activeObjectType && false !== stripos(Route::getCurrentRoute()->getName(), $route)) {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return '';
|
||||
},
|
||||
['needs_context' => true]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "active" when the current route matches the given argument
|
||||
* exactly.
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function activeRouteStrict(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'activeRouteStrict',
|
||||
static function (): string {
|
||||
$args = func_get_args();
|
||||
$route = $args[0]; // name of the route.
|
||||
|
||||
if (Route::getCurrentRoute()->getName() === $route) {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show account balance. Only used on the front page of Firefly III.
|
||||
*
|
||||
@@ -196,23 +93,6 @@ class General extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a string as a thing by converting it to a Carbon first.
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function formatDate(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'formatDate',
|
||||
function (string $date, string $format): string {
|
||||
$carbon = new Carbon($date);
|
||||
|
||||
return $carbon->formatLocalized($format);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to convert 1024 to 1kb etc.
|
||||
*
|
||||
@@ -238,79 +118,6 @@ class General extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFunction
|
||||
* @deprecated because it uses a query in a view
|
||||
* TODO remove me.
|
||||
*/
|
||||
protected function getMetaField(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'accountGetMetaField',
|
||||
static function (Account $account, string $field): string {
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$result = $repository->getMetaValue($account, $field);
|
||||
if (null === $result) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return true if the user is of role X.
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function hasRole(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'hasRole',
|
||||
static function (string $role): bool {
|
||||
$repository = app(UserRepositoryInterface::class);
|
||||
if ($repository->hasRole(auth()->user(), $role)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFilter
|
||||
*/
|
||||
protected function markdown(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'markdown',
|
||||
static function (string $text): string {
|
||||
$environment = Environment::createCommonMarkEnvironment();
|
||||
$environment->addExtension(new GithubFlavoredMarkdownExtension());
|
||||
|
||||
$converter = new CommonMarkConverter(['allow_unsafe_links' => false, 'max_nesting_level' => 3, 'html_input' => 'escape'], $environment);
|
||||
|
||||
return $converter->convertToHtml($text);
|
||||
}, ['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFilter
|
||||
*/
|
||||
protected function floatval(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'floatval',
|
||||
static function ($value): float {
|
||||
return (float)$value;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show icon with attachment.
|
||||
*
|
||||
@@ -390,6 +197,37 @@ class General extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFilter
|
||||
*/
|
||||
protected function markdown(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'markdown',
|
||||
static function (string $text): string {
|
||||
$environment = Environment::createCommonMarkEnvironment();
|
||||
$environment->addExtension(new GithubFlavoredMarkdownExtension());
|
||||
|
||||
$converter = new CommonMarkConverter(['allow_unsafe_links' => false, 'max_nesting_level' => 3, 'html_input' => 'escape'], $environment);
|
||||
|
||||
return $converter->convertToHtml($text);
|
||||
}, ['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFilter
|
||||
*/
|
||||
protected function floatval(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'floatval',
|
||||
static function ($value): float {
|
||||
return (float)$value;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic example thing for some views.
|
||||
*
|
||||
@@ -404,4 +242,166 @@ class General extends AbstractExtension
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "active" when the current route matches the given argument
|
||||
* exactly.
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function activeRouteStrict(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'activeRouteStrict',
|
||||
static function (): string {
|
||||
$args = func_get_args();
|
||||
$route = $args[0]; // name of the route.
|
||||
|
||||
if (Route::getCurrentRoute()->getName() === $route) {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "active" when a part of the route matches the argument.
|
||||
* ie. "accounts" will match "accounts.index".
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function activeRoutePartial(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'activeRoutePartial',
|
||||
static function (): string {
|
||||
$args = func_get_args();
|
||||
$route = $args[0]; // name of the route.
|
||||
$name = Route::getCurrentRoute()->getName() ?? '';
|
||||
if (false !== strpos($name, $route)) {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will return "active" when the current route matches the first argument (even partly)
|
||||
* but, the variable $objectType has been set and matches the second argument.
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function activeRoutePartialObjectType(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'activeRoutePartialObjectType',
|
||||
static function ($context): string {
|
||||
[, $route, $objectType] = func_get_args();
|
||||
$activeObjectType = $context['objectType'] ?? false;
|
||||
|
||||
if ($objectType === $activeObjectType && false !== stripos(Route::getCurrentRoute()->getName(), $route)) {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return '';
|
||||
},
|
||||
['needs_context' => true]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "menu-open" when a part of the route matches the argument.
|
||||
* ie. "accounts" will match "accounts.index".
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function menuOpenRoutePartial(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'menuOpenRoutePartial',
|
||||
static function (): string {
|
||||
$args = func_get_args();
|
||||
$route = $args[0]; // name of the route.
|
||||
$name = Route::getCurrentRoute()->getName() ?? '';
|
||||
if (false !== strpos($name, $route)) {
|
||||
return 'menu-open';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a string as a thing by converting it to a Carbon first.
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function formatDate(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'formatDate',
|
||||
function (string $date, string $format): string {
|
||||
$carbon = new Carbon($date);
|
||||
|
||||
return $carbon->formatLocalized($format);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFunction
|
||||
* @deprecated because it uses a query in a view
|
||||
* TODO remove me.
|
||||
*/
|
||||
protected function getMetaField(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'accountGetMetaField',
|
||||
static function (Account $account, string $field): string {
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$result = $repository->getMetaValue($account, $field);
|
||||
if (null === $result) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return true if the user is of role X.
|
||||
*
|
||||
* @return TwigFunction
|
||||
*/
|
||||
protected function hasRole(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'hasRole',
|
||||
static function (string $role): bool {
|
||||
$repository = app(UserRepositoryInterface::class);
|
||||
if ($repository->hasRole(auth()->user(), $role)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function getRootSearchOperator(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'getRootSearchOperator',
|
||||
static function (string $operator): string {
|
||||
return OperatorQuerySearch::getRootOperator($operator);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user