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

351 lines
12 KiB
PHP
Raw Normal View History

2015-05-01 20:17:06 +02:00
<?php
/**
* General.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-05-01 20:17:06 +02:00
2015-05-01 22:44:35 +02:00
namespace FireflyIII\Support\Twig;
2015-05-01 20:17:06 +02:00
2015-05-17 10:01:47 +02:00
use Carbon\Carbon;
2015-05-01 20:17:06 +02:00
use FireflyIII\Models\Account;
2019-04-16 16:20:46 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-07-31 19:28:56 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-11-18 20:26:57 +01:00
use League\CommonMark\CommonMarkConverter;
2015-05-01 20:17:06 +02:00
use Route;
2019-12-28 09:44:56 +01:00
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
2015-05-01 20:17:06 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class TwigSupport.
2015-05-01 20:17:06 +02:00
*/
2019-12-28 09:44:56 +01:00
class General extends AbstractExtension
2015-05-01 20:17:06 +02:00
{
2015-05-01 22:44:35 +02:00
/**
* @return array
*/
2016-02-06 10:15:07 +01:00
public function getFilters(): array
2015-05-01 20:17:06 +02:00
{
2015-06-06 17:40:41 +02:00
return [
$this->balance(),
2015-07-19 11:47:56 +02:00
$this->formatFilesize(),
$this->mimeIcon(),
2018-08-06 19:14:30 +02:00
$this->markdown(),
2015-06-06 17:40:41 +02:00
];
}
/**
2017-11-15 12:25:49 +01:00
* {@inheritdoc}
2015-06-06 17:40:41 +02:00
*/
2016-02-06 10:15:07 +01:00
public function getFunctions(): array
2015-06-06 17:40:41 +02:00
{
return [
$this->phpdate(),
2015-06-06 23:09:12 +02:00
$this->activeRouteStrict(),
$this->activeRoutePartial(),
2019-05-04 20:58:43 +02:00
$this->activeRoutePartialObjectType(),
$this->formatDate(),
2019-04-16 16:20:46 +02:00
$this->getMetaField(),
2018-07-31 19:28:56 +02:00
$this->hasRole(),
2015-06-06 17:40:41 +02:00
];
}
2015-07-26 19:07:02 +02:00
/**
2016-02-05 09:25:15 +01:00
* Will return "active" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
2019-12-28 09:44:56 +01:00
* @return TwigFunction
2015-07-26 19:07:02 +02:00
*/
2019-12-28 09:44:56 +01:00
protected function activeRoutePartial(): TwigFunction
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2017-11-15 10:52:29 +01:00
'activeRoutePartial',
2019-12-28 09:44:56 +01:00
static function (): string {
2019-05-04 20:58:43 +02:00
$args = func_get_args();
2017-11-15 10:52:29 +01:00
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
2017-11-15 12:25:49 +01:00
if (!(false === strpos($name, $route))) {
2017-11-15 10:52:29 +01:00
return 'active';
}
return '';
2016-02-05 09:25:15 +01: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.
*
2019-12-28 09:44:56 +01:00
* @return TwigFunction
2016-02-05 09:25:15 +01:00
*/
2019-12-28 09:44:56 +01:00
protected function activeRoutePartialObjectType(): TwigFunction
2016-02-05 09:25:15 +01:00
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2019-05-04 20:58:43 +02:00
'activeRoutePartialObjectType',
static function ($context): string {
[, $route, $objectType] = func_get_args();
$activeObjectType = $context['objectType'] ?? false;
2016-02-05 09:25:15 +01:00
2019-05-04 20:58:43 +02:00
if ($objectType === $activeObjectType && !(false === stripos(Route::getCurrentRoute()->getName(), $route))) {
2017-11-15 10:52:29 +01:00
return 'active';
}
2017-11-15 10:52:29 +01:00
return '';
},
['needs_context' => true]
2016-02-05 09:25:15 +01:00
);
}
/**
* Will return "active" when the current route matches the given argument
* exactly.
*
2019-12-28 09:44:56 +01:00
* @return TwigFunction
2016-02-05 09:25:15 +01:00
*/
2019-12-28 09:44:56 +01:00
protected function activeRouteStrict(): TwigFunction
2016-02-05 09:25:15 +01:00
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2017-11-15 10:52:29 +01:00
'activeRouteStrict',
2019-12-28 09:44:56 +01:00
static function (): string {
2019-06-21 19:10:02 +02:00
$args = func_get_args();
2017-11-15 10:52:29 +01:00
$route = $args[0]; // name of the route.
2016-02-05 09:25:15 +01:00
2017-11-15 10:52:29 +01:00
if (Route::getCurrentRoute()->getName() === $route) {
return 'active';
}
2017-11-15 10:52:29 +01:00
return '';
}
);
}
2015-07-19 11:47:56 +02:00
/**
2019-04-16 16:20:46 +02:00
* Show account balance. Only used on the front page of Firefly III.
*
2019-12-28 09:44:56 +01:00
* @return TwigFilter
2015-07-19 11:47:56 +02:00
*/
2019-12-28 09:44:56 +01:00
protected function balance(): TwigFilter
2015-07-19 11:47:56 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFilter(
2017-11-15 10:52:29 +01:00
'balance',
static function (?Account $account): string {
2017-11-15 12:25:49 +01:00
if (null === $account) {
2017-11-15 10:52:29 +01:00
return 'NULL';
}
/** @var Carbon $date */
2017-11-15 10:52:29 +01:00
$date = session('end', Carbon::now()->endOfMonth());
return app('steam')->balance($account, $date);
2015-07-19 11:47:56 +02:00
}
2016-02-05 09:25:15 +01:00
);
}
/**
2019-04-16 16:20:46 +02:00
* Formats a string as a thing by converting it to a Carbon first.
*
2019-12-28 09:44:56 +01:00
* @return TwigFunction
*/
2019-12-28 09:44:56 +01:00
protected function formatDate(): TwigFunction
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
'formatDate',
function (string $date, string $format): string {
$carbon = new Carbon($date);
return $carbon->formatLocalized($format);
}
);
}
2015-06-06 17:40:41 +02:00
/**
2019-04-16 16:20:46 +02:00
* Used to convert 1024 to 1kb etc.
*
2019-12-28 09:44:56 +01:00
* @return TwigFilter
2015-06-06 17:40:41 +02:00
*/
2019-12-28 09:44:56 +01:00
protected function formatFilesize(): TwigFilter
2015-06-06 17:40:41 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFilter(
2017-11-15 10:52:29 +01:00
'filesize',
2019-12-28 09:44:56 +01:00
static function (int $size): string {
2017-11-15 11:33:07 +01:00
// less than one GB, more than one MB
2017-11-15 10:52:29 +01:00
if ($size < (1024 * 1024 * 2014) && $size >= (1024 * 1024)) {
return round($size / (1024 * 1024), 2) . ' MB';
}
2016-02-05 09:25:15 +01:00
2017-11-15 10:52:29 +01:00
// less than one MB
if ($size < (1024 * 1024)) {
return round($size / 1024, 2) . ' KB';
}
2016-02-05 09:25:15 +01:00
2017-11-15 10:52:29 +01:00
return $size . ' bytes';
}
);
2015-06-06 17:40:41 +02:00
}
2015-05-01 20:17:06 +02:00
/**
2019-12-28 09:44:56 +01:00
* @return TwigFunction
2015-05-01 20:17:06 +02:00
*/
2019-12-28 09:44:56 +01:00
protected function getMetaField(): TwigFunction
2015-05-01 20:17:06 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2019-04-16 16:20:46 +02:00
'accountGetMetaField',
static function (Account $account, string $field): string {
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$result = $repository->getMetaValue($account, $field);
if (null === $result) {
return '';
}
2015-05-01 20:17:06 +02:00
2019-04-16 16:20:46 +02:00
return $result;
2017-11-15 10:52:29 +01:00
}
2015-05-02 08:28:24 +02:00
);
2015-06-06 17:40:41 +02:00
}
2015-05-02 08:28:24 +02:00
2018-07-31 19:28:56 +02:00
/**
* Will return true if the user is of role X.
*
2019-12-28 09:44:56 +01:00
* @return TwigFunction
2018-07-31 19:28:56 +02:00
*/
2019-12-28 09:44:56 +01:00
protected function hasRole(): TwigFunction
2018-07-31 19:28:56 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2018-07-31 19:28:56 +02:00
'hasRole',
static function (string $role): bool {
2018-07-31 19:28:56 +02:00
$repository = app(UserRepositoryInterface::class);
if ($repository->hasRole(auth()->user(), $role)) {
return true;
}
2018-08-06 19:14:30 +02:00
2018-07-31 19:28:56 +02:00
return false;
}
);
}
2017-11-22 21:12:27 +01:00
/**
2019-12-28 09:44:56 +01:00
* @return TwigFilter
2017-11-22 21:12:27 +01:00
*/
2019-12-28 09:44:56 +01:00
protected function markdown(): TwigFilter
2017-11-22 21:12:27 +01:00
{
2019-12-28 09:44:56 +01:00
return new TwigFilter(
2017-11-22 21:12:27 +01:00
'markdown',
2019-06-23 05:53:01 +02:00
static function (string $text): string {
2017-11-22 21:12:27 +01:00
$converter = new CommonMarkConverter;
return $converter->convertToHtml($text);
}, ['is_safe' => ['html']]
);
}
2015-06-06 17:40:41 +02:00
/**
2019-04-16 16:20:46 +02:00
* Show icon with attachment.
*
2019-12-28 09:44:56 +01:00
* @return TwigFilter
2015-06-06 23:09:12 +02:00
*/
2019-12-28 09:44:56 +01:00
protected function mimeIcon(): TwigFilter
2015-06-06 23:09:12 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFilter(
2017-11-15 10:52:29 +01:00
'mimeIcon',
2019-06-23 05:53:01 +02:00
static function (string $string): string {
2017-11-15 10:52:29 +01:00
switch ($string) {
2017-11-15 11:33:07 +01:00
default:
return 'fa-file-o';
case 'application/pdf':
return 'fa-file-pdf-o';
2018-01-25 19:21:40 +01:00
/* image */
2017-11-15 11:33:07 +01:00
case 'image/png':
case 'image/jpeg':
2018-01-25 19:21:40 +01:00
case 'image/svg+xml':
case 'image/heic':
case 'image/heic-sequence':
case 'application/vnd.oasis.opendocument.image':
2017-11-15 11:33:07 +01:00
return 'fa-file-image-o';
2018-01-25 19:21:40 +01:00
/* MS word */
case 'application/msword':
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.template':
case 'application/x-iwork-pages-sffpages':
case 'application/vnd.sun.xml.writer':
case 'application/vnd.sun.xml.writer.template':
case 'application/vnd.sun.xml.writer.global':
case 'application/vnd.stardivision.writer':
case 'application/vnd.stardivision.writer-global':
case 'application/vnd.oasis.opendocument.text':
case 'application/vnd.oasis.opendocument.text-template':
case 'application/vnd.oasis.opendocument.text-web':
case 'application/vnd.oasis.opendocument.text-master':
return 'fa-file-word-o';
/* MS excel */
case 'application/vnd.ms-excel':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.template':
case 'application/vnd.sun.xml.calc':
case 'application/vnd.sun.xml.calc.template':
case 'application/vnd.stardivision.calc':
case 'application/vnd.oasis.opendocument.spreadsheet':
case 'application/vnd.oasis.opendocument.spreadsheet-template':
return 'fa-file-excel-o';
/* MS powerpoint */
case 'application/vnd.ms-powerpoint':
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
case 'application/vnd.openxmlformats-officedocument.presentationml.template':
case 'application/vnd.openxmlformats-officedocument.presentationml.slideshow':
case 'application/vnd.sun.xml.impress':
case 'application/vnd.sun.xml.impress.template':
case 'application/vnd.stardivision.impress':
case 'application/vnd.oasis.opendocument.presentation':
case 'application/vnd.oasis.opendocument.presentation-template':
return 'fa-file-powerpoint-o';
/* calc */
case 'application/vnd.sun.xml.draw':
case 'application/vnd.sun.xml.draw.template':
case 'application/vnd.stardivision.draw':
case 'application/vnd.oasis.opendocument.chart':
return 'fa-paint-brush';
case 'application/vnd.oasis.opendocument.graphics':
case 'application/vnd.oasis.opendocument.graphics-template':
case 'application/vnd.sun.xml.math':
case 'application/vnd.stardivision.math':
case 'application/vnd.oasis.opendocument.formula':
case 'application/vnd.oasis.opendocument.database':
return 'fa-calculator';
2017-11-15 11:33:07 +01:00
}
2017-11-15 10:52:29 +01:00
},
['is_safe' => ['html']]
2015-06-06 23:09:12 +02:00
);
}
/**
2019-04-16 16:20:46 +02:00
* Basic example thing for some views.
*
2019-12-28 09:44:56 +01:00
* @return TwigFunction
2015-06-06 23:09:12 +02:00
*/
2019-12-28 09:44:56 +01:00
protected function phpdate(): TwigFunction
2015-06-06 23:09:12 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2017-11-15 10:52:29 +01:00
'phpdate',
2019-06-23 05:53:01 +02:00
static function (string $str): string {
2017-11-15 10:52:29 +01:00
return date($str);
}
2015-06-06 23:09:12 +02:00
);
}
}