New select options

This commit is contained in:
James Cole 2018-04-29 07:46:14 +02:00
parent c0d715c78a
commit 565cb6d79e
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 82 additions and 46 deletions

View File

@ -27,9 +27,11 @@ use Carbon\Carbon;
use Eloquent;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use RuntimeException;
@ -93,12 +95,51 @@ class ExpandedForm
return $this->currencyField($name, 'amount-small', $value, $options);
}
/**
* @param string $name
* @param $selected
* @param null $options
*
* @return string
* @throws \Throwable
*/
public function assetAccountCheckList(string $name, $options = null): string
{
$options = $options ?? [];
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$selected = request()->old($name) ?? [];
// get all asset accounts:
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$assetAccounts = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
$grouped = [];
// group accounts:
/** @var Account $account */
foreach ($assetAccounts as $account) {
$role = $repository->getMetaValue($account, 'accountRole');
if (null === $role) {
$role = 'no_account_type'; // @codeCoverageIgnore
}
$key = (string)trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $account->name;
}
unset($options['class']);
$html = view('form.assetAccountCheckList', compact('classes','selected', 'name', 'label', 'options', 'grouped'))->render();
return $html;
}
/**
* @param string $name
* @param null $value
* @param array $options
*
* @return string
* @throws \Throwable
*/
public function assetAccountList(string $name, $value = null, array $options = []): string
{
@ -193,19 +234,10 @@ class ExpandedForm
* @param array $options
*
* @return string
* @throws \Throwable
*/
public function currencyList(string $name, $value = null, array $options = []): string
{
// properties for cache
$cache = new CacheProperties;
$cache->addProperty('exp-form-currency-list');
$cache->addProperty($name);
$cache->addProperty($value);
$cache->addProperty($options);
if ($cache->has()) {
return $cache->get();
}
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
@ -217,7 +249,6 @@ class ExpandedForm
$array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')';
}
$res = $this->select($name, $array, $value, $options);
$cache->store($res);
return $res;
}
@ -351,28 +382,6 @@ class ExpandedForm
return $selectList;
}
/**
* @param string $name
* @param array $list
* @param null $selected
* @param array $options
*
* @return string
* @throws \Throwable
*/
public function multiCheckbox(string $name, array $list = [], $selected = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$selected = $this->fillFieldValue($name, $selected);
unset($options['class']);
$html = view('form.multiCheckbox', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
return $html;
}
/**
* @param string $name
* @param array $list
@ -516,6 +525,31 @@ class ExpandedForm
return $html;
}
/**
* @param string $name
* @param null $value
* @param array $options
*
* @return string
* @throws \Throwable
*/
public function ruleGroupList(string $name, $value = null, array $options = []): string
{
/** @var RuleGroupRepositoryInterface $groupRepos */
$groupRepos = app(RuleGroupRepositoryInterface::class);
// get all currencies:
$list = $groupRepos->get();
$array = [];
/** @var RuleGroup $group */
foreach ($list as $group) {
$array[$group->id] = $group->title;
}
$res = $this->select($name, $array, $value, $options);
return $res;
}
/**
* @param string $name
* @param array $list

View File

@ -188,7 +188,7 @@ return [
'is_safe' => [
'date', 'text', 'select', 'balance', 'optionsList', 'checkbox', 'amount', 'tags', 'integer', 'textarea', 'location',
'multiRadio', 'file', 'multiCheckbox', 'staticText', 'amountSmall', 'password', 'nonSelectableBalance', 'nonSelectableAmount',
'number', 'assetAccountList','amountNoCurrency','currencyList'
'number', 'assetAccountList','amountNoCurrency','currencyList','ruleGroupList','assetAccountCheckList'
],
],
'Form' => [

View File

@ -2,18 +2,20 @@
<label for="{{ options.id }}" class="col-sm-4 control-label">{{ label }}</label>
<div class="col-sm-8">
{% for value,description in list %}
{% set options = options|merge({'id': 'ffInput_'~name~'_'~value}) %}
<div class="checkbox">
<label>
{% if value in selected %}
{{ Form.checkbox(name~'[]', value, true, options) }}
{% else %}
{{ Form.checkbox(name~'[]', value, false, options) }}
{% endif %}
{{ description }}
</label>
</div>
{% for groupName, accounts in grouped %}
<strong>{{ groupName }}</strong><br />
{% for id, account in accounts %}
<div class="checkbox" style="margin-left:2em;">
<label>
{% if account in selected or (selected|length == 0 and options.select_all == true) %}
{{ Form.checkbox(name~'[]', id, true, options) }}
{% else %}
{{ Form.checkbox(name~'[]', id, false, options) }}
{% endif %}
{{ account }}
</label>
</div>
{% endfor %}
{% endfor %}
{% include 'form/help' %}
{% include 'form/feedback' %}