From c98275e73a126b5c7d795fcfc73803f55025e69b Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 1 May 2015 18:44:49 +0200 Subject: [PATCH] More templates converted to twig. --- app/Http/breadcrumbs.php | 8 + app/Providers/AppServiceProvider.php | 1 + app/Providers/ConfigServiceProvider.php | 207 +++++++++++++++++++++- app/Providers/FireflyServiceProvider.php | 83 ++++++++- app/Validation/FireflyValidator.php | 13 ++ resources/twig/index.twig | 215 +++++++++++++++++++++++ resources/twig/layout/default.twig | 157 +++++++++++++++++ resources/twig/list/journals-tiny.twig | 37 ++++ resources/twig/partials/boxes.twig | 92 ++++++++++ resources/twig/partials/flashes.twig | 27 +++ resources/twig/partials/menu.twig | 182 +++++++++++++++++++ 11 files changed, 1019 insertions(+), 3 deletions(-) create mode 100644 resources/twig/index.twig create mode 100644 resources/twig/layout/default.twig create mode 100644 resources/twig/list/journals-tiny.twig create mode 100644 resources/twig/partials/boxes.twig create mode 100644 resources/twig/partials/flashes.twig create mode 100644 resources/twig/partials/menu.twig diff --git a/app/Http/breadcrumbs.php b/app/Http/breadcrumbs.php index 610ad4418f..0574f00b80 100644 --- a/app/Http/breadcrumbs.php +++ b/app/Http/breadcrumbs.php @@ -22,6 +22,14 @@ Breadcrumbs::register( } ); +Breadcrumbs::register( + 'index', + function (Generator $breadcrumbs) { + + $breadcrumbs->push('Home', route('index')); + } +); + // accounts Breadcrumbs::register( 'accounts.index', function (Generator $breadcrumbs, $what) { diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index bfff79a9f2..af0a5a5b8e 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -35,6 +35,7 @@ class AppServiceProvider extends ServiceProvider 'Illuminate\Contracts\Auth\Registrar', 'FireflyIII\Services\Registrar' ); + } } diff --git a/app/Providers/ConfigServiceProvider.php b/app/Providers/ConfigServiceProvider.php index 94d9ee03fd..c4deebb1d1 100644 --- a/app/Providers/ConfigServiceProvider.php +++ b/app/Providers/ConfigServiceProvider.php @@ -23,7 +23,212 @@ class ConfigServiceProvider extends ServiceProvider { config( [ - // + 'twigbridge' => [ + + 'twig' => [ + /* + |-------------------------------------------------------------------------- + | Extension + |-------------------------------------------------------------------------- + | + | File extension for Twig view files. + | + */ + 'extension' => 'twig', + + /* + |-------------------------------------------------------------------------- + | Accepts all Twig environment configuration options + |-------------------------------------------------------------------------- + | + | http://twig.sensiolabs.org/doc/api.html#environment-options + | + */ + 'environment' => [ + + // When set to true, the generated templates have a __toString() method + // that you can use to display the generated nodes. + // default: false + 'debug' => config('app.debug', false), + + // The charset used by the templates. + // default: utf-8 + 'charset' => 'utf-8', + + // The base template class to use for generated templates. + // default: TwigBridge\Twig\Template + 'base_template_class' => 'TwigBridge\Twig\Template', + + // An absolute path where to store the compiled templates, or false to disable caching. If null + // then the cache file path is used. + // default: cache file storage path + 'cache' => null, + + // When developing with Twig, it's useful to recompile the template + // whenever the source code changes. If you don't provide a value + // for the auto_reload option, it will be determined automatically based on the debug value. + 'auto_reload' => true, + + // If set to false, Twig will silently ignore invalid variables + // (variables and or attributes/methods that do not exist) and + // replace them with a null value. When set to true, Twig throws an exception instead. + // default: false + 'strict_variables' => false, + + // If set to true, auto-escaping will be enabled by default for all templates. + // default: true + 'autoescape' => true, + + // A flag that indicates which optimizations to apply + // (default to -1 -- all optimizations are enabled; set it to 0 to disable) + 'optimizations' => -1, + ], + + /* + |-------------------------------------------------------------------------- + | Global variables + |-------------------------------------------------------------------------- + | + | These will always be passed in and can be accessed as Twig variables. + | NOTE: these will be overwritten if you pass data into the view with the same key. + | + */ + 'globals' => [], + ], + + 'extensions' => [ + + /* + |-------------------------------------------------------------------------- + | Extensions + |-------------------------------------------------------------------------- + | + | Enabled extensions. + | + | `Twig_Extension_Debug` is enabled automatically if twig.debug is TRUE. + | + */ + 'enabled' => [ + 'TwigBridge\Extension\Loader\Facades', + 'TwigBridge\Extension\Loader\Filters', + 'TwigBridge\Extension\Loader\Functions', + + 'TwigBridge\Extension\Laravel\Auth', + 'TwigBridge\Extension\Laravel\Config', + 'TwigBridge\Extension\Laravel\Dump', + 'TwigBridge\Extension\Laravel\Input', + 'TwigBridge\Extension\Laravel\Session', + 'TwigBridge\Extension\Laravel\String', + 'TwigBridge\Extension\Laravel\Translator', + 'TwigBridge\Extension\Laravel\Url', + + // 'TwigBridge\Extension\Laravel\Form', + // 'TwigBridge\Extension\Laravel\Html', + // 'TwigBridge\Extension\Laravel\Legacy\Facades', + ], + + /* + |-------------------------------------------------------------------------- + | Facades + |-------------------------------------------------------------------------- + | + | Available facades. Access like `{{ Config.get('foo.bar') }}`. + | + | Each facade can take an optional array of options. To mark the whole facade + | as safe you can set the option `'is_safe' => true`. Setting the facade as + | safe means that any HTML returned will not be escaped. + | + | It is advisable to not set the whole facade as safe and instead mark the + | each appropriate method as safe for security reasons. You can do that with + | the following syntax: + | + | + | 'Form' => [ + | 'is_safe' => [ + | 'open' + | ] + | ] + | + | + | The values of the `is_safe` array must match the called method on the facade + | in order to be marked as safe. + | + */ + 'facades' => [ + 'Breadcrumbs' => [ + 'is_safe' => [ + 'renderIfExists' + ] + ], + 'Session', + 'Route' + ], + + /* + |-------------------------------------------------------------------------- + | Functions + |-------------------------------------------------------------------------- + | + | Available functions. Access like `{{ secure_url(...) }}`. + | + | Each function can take an optional array of options. These options are + | passed directly to `Twig_SimpleFunction`. + | + | So for example, to mark a function as safe you can do the following: + | + | + | 'link_to' => [ + | 'is_safe' => ['html'] + | ] + | + | + | The options array also takes a `callback` that allows you to name the + | function differently in your Twig templates than what it's actually called. + | + | + | 'link' => [ + | 'callback' => 'link_to' + | ] + | + | + */ + 'functions' => [ + 'elixir', + 'head', + 'last', + ], + + /* + |-------------------------------------------------------------------------- + | Filters + |-------------------------------------------------------------------------- + | + | Available filters. Access like `{{ variable|filter }}`. + | + | Each filter can take an optional array of options. These options are + | passed directly to `Twig_SimpleFilter`. + | + | So for example, to mark a filter as safe you can do the following: + | + | + | 'studly_case' => [ + | 'is_safe' => ['html'] + | ] + | + | + | The options array also takes a `callback` that allows you to name the + | filter differently in your Twig templates than what is actually called. + | + | + | 'snake' => [ + | 'callback' => 'snake_case' + | ] + | + | + */ + 'filters' => [], + ], + ] ] ); } diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 1b57c3508e..e047b1dbb9 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -3,6 +3,7 @@ namespace FireflyIII\Providers; use App; +use FireflyIII\Models\Account; use FireflyIII\Support\Amount; use FireflyIII\Support\ExpandedForm; use FireflyIII\Support\Navigation; @@ -10,7 +11,10 @@ use FireflyIII\Support\Preferences; use FireflyIII\Support\Steam; use FireflyIII\Validation\FireflyValidator; use Illuminate\Support\ServiceProvider; +use Route; use Twig; +use Twig_SimpleFilter; +use Twig_SimpleFunction; use TwigBridge\Extension\Loader\Functions; use Validator; @@ -28,14 +32,89 @@ class FireflyServiceProvider extends ServiceProvider return new FireflyValidator($translator, $data, $rules, $messages); } ); + /* + * Default Twig configuration: + */ + $config = App::make('config'); + Twig::addExtension(new Functions($config)); + + /* + * Amount::format + */ + $filter = new Twig_SimpleFilter( + 'formatAmount', function ($string) { + return App::make('amount')->format($string); + }, ['is_safe' => ['html']] + ); + Twig::addFilter($filter); + + /* + * Amount::formatJournal + */ + $filter = new Twig_SimpleFilter( + 'formatJournal', function ($journal) { + return App::make('amount')->formatJournal($journal); + }, ['is_safe' => ['html']] + ); + Twig::addFilter($filter); + + /* + * Steam::balance() + */ + + $filter = new Twig_SimpleFilter( + 'balance', function (Account $account = null) { + if (is_null($account)) { + return 'NULL'; + } + + return App::make('amount')->format(App::make('steam')->balance($account)); + //return App::make('steam')->balance($account); + },['is_safe' => ['html']] + ); + Twig::addFilter($filter); + + + /* + * Current active route. + */ + $filter = new Twig_SimpleFilter( + 'activeRoute', function ($string) { + if (Route::getCurrentRoute()->getName() == $string) { + return 'active'; + } + + return ''; + } + ); + Twig::addFilter($filter); + + /* + * Amount::getCurrencyCode() + */ + $function = new Twig_SimpleFunction( + 'getCurrencyCode', function () { + return App::make('amount')->getCurrencyCode(); + } + ); + Twig::addFunction($function); + + + /* + * env + */ + $function = new Twig_SimpleFunction( + 'env', function ($a, $b) { + return env($a, $b); + } + ); + Twig::addFunction($function); } public function register() { - $config = App::make('config'); - Twig::addExtension(new Functions($config)); $this->app->bind( 'preferences', function () { diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index b886074aa9..b8cf753024 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -13,6 +13,7 @@ use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Validation\Validator; use Log; use Navigation; +use Symfony\Component\Translation\TranslatorInterface; /** * Class FireflyValidator @@ -22,6 +23,18 @@ use Navigation; class FireflyValidator extends Validator { + /** + * @param TranslatorInterface $translator + * @param array $data + * @param array $rules + * @param array $messages + * @param array $customAttributes + */ + public function __construct(TranslatorInterface $translator, array $data, array $rules, array $messages = [], array $customAttributes = []) + { + parent::__construct($translator, $data, $rules, $messages); + } + /** * @param $attribute * @param $value diff --git a/resources/twig/index.twig b/resources/twig/index.twig new file mode 100644 index 0000000000..c767243e4c --- /dev/null +++ b/resources/twig/index.twig @@ -0,0 +1,215 @@ +{% extends "./layout/default.twig" %} +{% block content %} +{{ Breadcrumbs.renderIfExists }} + +{% if count == 0 %} +
+
+

Welcome to Firefly III.

+ +

+ Create a new asset account to get started. +

+
+
+
+ +{% else %} + + + {% include 'partials/boxes.twig' %} + +
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+ + +
+
+ Savings + {{ savingsTotal|formatAmount }} +
+
+ {% if savings|length == 0 %} +

Mark your asset accounts as "Savings account" to fill this panel.

+ {% else %} + {% for account in savings %} + +
+ +
{{ account.startBalance|formatAmount }}
+ +
+ {% if account.difference < 0 %} + +
+
+ {% if account.percentage <= 50 %} + {{account.difference|formatAmount}} + {% endif %} +
+
+ {% if account.percentage > 50 %} + {{account.difference|formatAmount}} + {% endif %} +
+
+ {% else %} + +
+
+ {% if account.percentage <= 50 %} + {{account.difference|formatAmount}} + {{account.difference|formatAmount}} + {% endif %} +
+
+ {% if account.percentage > 50 %} + {{account.difference|formatAmount}} + {% endif %} +
+
+ {% endif %} + +
+ +
{{ account.endBalance|formatAmount }}
+
+ {% endfor %} + {% endif %} +
+
+ + +
+
+ Piggy banks +
+
+ {% if piggyBankAccounts|length == 0%} +

Create piggy banks to fill this panel.

+ {% else %} + {% for account in piggyBankAccounts %} + +
+ +
{{ account.startBalance|formatAmount }}
+ +
+ +
+
+ {% if account.percentage <= 50 %} + {{account.piggyBalance|formatAmount}} divided + {% endif %} +
+
+ {% if account.percentage > 50 %} + {{account.difference|formatAmount}} left to divide + {% endif %} +
+
+
+ +
{{ account.piggyBalance|formatAmount }}
+
+ {% endfor %} + {% endif %} +
+
+ + +
+
+ + +
+
+ Bills +
+
+
+
+
+ + + {% for data in transactions %} +
+
+ + {{data[1].name}} ({{ data[1]|balance }}) + + + +
+
+ + +
+
+ + + +
+
+ + {% include 'list/journals-tiny.twig' with {'transactions': data[0],'account': data[1]} %} +
+
+ {% endfor %} +
+
+ +{% endif %} + + +{% endblock %} +{% block scripts %} + + + + + + +{% endblock %} \ No newline at end of file diff --git a/resources/twig/layout/default.twig b/resources/twig/layout/default.twig new file mode 100644 index 0000000000..58d5289c8e --- /dev/null +++ b/resources/twig/layout/default.twig @@ -0,0 +1,157 @@ + + + + + + + + Firefly + {% if title != "Firefly" %} + // {{ title }} + {% endif %} + + {% if subTitle %} + // {{subTitle}} + {% endif %} + + + + + + + + + + + + + + {% block styles %}{% endblock %} + + + + + + + + + + + + + + + + + + + + + + + +
+ + {% include('partials/menu.twig') %} + +
+ +
+
+

+ {% if mainTitleIcon %} + + {% endif %} + + {{ title }} + + {% if subTitle %} + + {% if subTitleIcon %} + + {% endif %} + {{ subTitle }} + + {% endif %} + +

+ +
+ +
+ + {% include('partials/flashes.twig') %} + {% block content %}{% endblock %} + + + + +
+
+ + + + + + + + + + + + + + + + + + + + +{% block scripts %}{% endblock %} + + + + diff --git a/resources/twig/list/journals-tiny.twig b/resources/twig/list/journals-tiny.twig new file mode 100644 index 0000000000..e4ca10fb72 --- /dev/null +++ b/resources/twig/list/journals-tiny.twig @@ -0,0 +1,37 @@ + diff --git a/resources/twig/partials/boxes.twig b/resources/twig/partials/boxes.twig new file mode 100644 index 0000000000..748e3cb960 --- /dev/null +++ b/resources/twig/partials/boxes.twig @@ -0,0 +1,92 @@ + +
+ + + + +
+ diff --git a/resources/twig/partials/flashes.twig b/resources/twig/partials/flashes.twig new file mode 100644 index 0000000000..de601e38c7 --- /dev/null +++ b/resources/twig/partials/flashes.twig @@ -0,0 +1,27 @@ +{% if Session.has('success') %} + +{% endif %} + +{% if Session.has('info') %} + +{% endif %} + +{% if Session.has('warning') %} + +{% endif %} + +{% if Session.has('error') %} + +{% endif %} diff --git a/resources/twig/partials/menu.twig b/resources/twig/partials/menu.twig new file mode 100644 index 0000000000..2c6dfe5056 --- /dev/null +++ b/resources/twig/partials/menu.twig @@ -0,0 +1,182 @@ + + +