GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

app/Support/ExpandedForm.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * ExpandedForm.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace FireflyIII\Support;
24
25
use Amount as Amt;
26
use Carbon\Carbon;
27
use Eloquent;
28
use FireflyIII\Models\Account;
29
use FireflyIII\Models\AccountType;
30
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
31
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
32
use Illuminate\Support\Collection;
33
use Illuminate\Support\MessageBag;
34
use RuntimeException;
35
use Session;
36
37
/**
38
 * Class ExpandedForm.
39
 */
40
class ExpandedForm
41
{
42
    /**
43
     * @param string $name
44
     * @param null   $value
45
     * @param array  $options
46
     *
47
     * @return string
48
     * @throws \FireflyIII\Exceptions\FireflyException
49
     */
50
    public function amount(string $name, $value = null, array $options = []): string
51
    {
52
        return $this->currencyField($name, 'amount', $value, $options);
53
    }
54
55
    /**
56
     * @param string $name
57
     * @param null   $value
58
     * @param array  $options
59
     *
60
     * @return string
61
     * @throws \FireflyIII\Exceptions\FireflyException
62
     */
63
    public function amountSmall(string $name, $value = null, array $options = []): string
64
    {
65
        return $this->currencyField($name, 'amount-small', $value, $options);
66
    }
67
68
    /**
69
     * @param string $name
70
     * @param null   $value
71
     * @param array  $options
72
     *
73
     * @return string
74
     */
75
    public function assetAccountList(string $name, $value = null, array $options = []): string
76
    {
77
        // properties for cache
78
        $cache = new CacheProperties;
79
        $cache->addProperty('exp-form-asset-list');
80
        $cache->addProperty($name);
81
        $cache->addProperty($value);
82
        $cache->addProperty($options);
83
84
        if ($cache->has()) {
85
            return $cache->get();
86
        }
87
        // make repositories
88
        /** @var AccountRepositoryInterface $repository */
89
        $repository = app(AccountRepositoryInterface::class);
90
        /** @var CurrencyRepositoryInterface $currencyRepos */
91
        $currencyRepos = app(CurrencyRepositoryInterface::class);
92
93
        $assetAccounts   = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
94
        $defaultCurrency = app('amount')->getDefaultCurrency();
95
        $grouped         = [];
96
        // group accounts:
97
        /** @var Account $account */
98
        foreach ($assetAccounts as $account) {
99
            $balance    = app('steam')->balance($account, new Carbon);
100
            $currencyId = (int)$account->getMeta('currency_id');
101
            $currency   = $currencyRepos->findNull($currencyId);
102
            $role       = $account->getMeta('accountRole');
103
            if (0 === strlen($role)) {
104
                $role = 'no_account_type'; // @codeCoverageIgnore
105
            }
106
            if (null === $currency) {
107
                $currency = $defaultCurrency;
108
            }
109
110
            $key                         = (string)trans('firefly.opt_group_' . $role);
111
            $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
112
        }
113
        $res = $this->select($name, $grouped, $value, $options);
114
        $cache->store($res);
115
116
        return $res;
117
    }
118
119
    /**
120
     * @param string $name
121
     * @param null   $value
122
     * @param array  $options
123
     *
124
     * @return string
125
     * @throws \FireflyIII\Exceptions\FireflyException
126
     */
127
    public function balance(string $name, $value = null, array $options = []): string
128
    {
129
        return $this->currencyField($name, 'balance', $value, $options);
130
    }
131
132
    /**
133
     * @param       $name
134
     * @param int   $value
135
     * @param null  $checked
136
     * @param array $options
137
     *
138
     * @return string
139
     *
140
141
     */
142
    public function checkbox(string $name, $value = 1, $checked = null, $options = []): string
143
    {
144
        $options['checked'] = true === $checked ? true : null;
145
        $label              = $this->label($name, $options);
146
        $options            = $this->expandOptionArray($name, $label, $options);
147
        $classes            = $this->getHolderClasses($name);
148
        $value              = $this->fillFieldValue($name, $value);
149
150
        unset($options['placeholder'], $options['autocomplete'], $options['class']);
151
152
        $html = view('form.checkbox', compact('classes', 'name', 'label', 'value', 'options'))->render();
153
154
        return $html;
155
    }
156
157
    /**
158
     * @param       $name
159
     * @param null  $value
160
     * @param array $options
161
     *
162
     * @return string
163
     *
164
165
     */
166
    public function date(string $name, $value = null, array $options = []): string
167
    {
168
        $label   = $this->label($name, $options);
169
        $options = $this->expandOptionArray($name, $label, $options);
170
        $classes = $this->getHolderClasses($name);
171
        $value   = $this->fillFieldValue($name, $value);
172
        unset($options['placeholder']);
173
        $html = view('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render();
174
175
        return $html;
176
    }
177
178
    /**
179
     * @param       $name
180
     * @param array $options
181
     *
182
     * @return string
183
     *
184
185
     */
186
    public function file(string $name, array $options = []): string
187
    {
188
        $label   = $this->label($name, $options);
189
        $options = $this->expandOptionArray($name, $label, $options);
190
        $classes = $this->getHolderClasses($name);
191
        $html    = view('form.file', compact('classes', 'name', 'label', 'options'))->render();
192
193
        return $html;
194
    }
195
196
    /**
197
     * @param       $name
198
     * @param null  $value
199
     * @param array $options
200
     *
201
     * @return string
202
     *
203
204
     */
205
    public function integer(string $name, $value = null, array $options = []): string
206
    {
207
        $label           = $this->label($name, $options);
208
        $options         = $this->expandOptionArray($name, $label, $options);
209
        $classes         = $this->getHolderClasses($name);
210
        $value           = $this->fillFieldValue($name, $value);
211
        $options['step'] = '1';
212
        $html            = view('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render();
213
214
        return $html;
215
    }
216
217
    /**
218
     * @param       $name
219
     * @param null  $value
220
     * @param array $options
221
     *
222
     * @return string
223
     *
224
225
     */
226
    public function location(string $name, $value = null, array $options = []): string
227
    {
228
        $label   = $this->label($name, $options);
229
        $options = $this->expandOptionArray($name, $label, $options);
230
        $classes = $this->getHolderClasses($name);
231
        $value   = $this->fillFieldValue($name, $value);
232
        $html    = view('form.location', compact('classes', 'name', 'label', 'value', 'options'))->render();
233
234
        return $html;
235
    }
236
237
    /**
238
     * Takes any collection and tries to make a sensible select list compatible array of it.
239
     *
240
     * @param \Illuminate\Support\Collection $set
241
     *
242
     * @return array
243
     */
244
    public function makeSelectList(Collection $set): array
245
    {
246
        $selectList = [];
247
        $fields     = ['title', 'name', 'description'];
248
        /** @var Eloquent $entry */
249
        foreach ($set as $entry) {
250
            $entryId = (int)$entry->id;
251
            $title   = null;
252
253
            foreach ($fields as $field) {
254
                if (isset($entry->$field) && null === $title) {
255
                    $title = $entry->$field;
256
                }
257
            }
258
            $selectList[$entryId] = $title;
259
        }
260
261
        return $selectList;
262
    }
263
264
    /**
265
     * @param \Illuminate\Support\Collection $set
266
     *
267
     * @return array
268
     */
269
    public function makeSelectListWithEmpty(Collection $set): array
270
    {
271
        $selectList    = [];
272
        $selectList[0] = '(none)';
273
        $fields        = ['title', 'name', 'description'];
274
        /** @var Eloquent $entry */
275
        foreach ($set as $entry) {
276
            $entryId = (int)$entry->id;
277
            $title   = null;
278
279
            foreach ($fields as $field) {
280
                if (isset($entry->$field) && null === $title) {
281
                    $title = $entry->$field;
282
                }
283
            }
284
            $selectList[$entryId] = $title;
285
        }
286
287
        return $selectList;
288
    }
289
290
    /**
291
     * @param       $name
292
     * @param array $list
293
     * @param null  $selected
294
     * @param array $options
295
     *
296
     * @return string
297
     *
298
299
     */
300
    public function multiCheckbox(string $name, array $list = [], $selected = null, array $options = []): string
301
    {
302
        $label    = $this->label($name, $options);
303
        $options  = $this->expandOptionArray($name, $label, $options);
304
        $classes  = $this->getHolderClasses($name);
305
        $selected = $this->fillFieldValue($name, $selected);
306
307
        unset($options['class']);
308
        $html = view('form.multiCheckbox', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
309
310
        return $html;
311
    }
312
313
    /**
314
     * @param       $name
315
     * @param array $list
316
     * @param null  $selected
317
     * @param array $options
318
     *
319
     * @return string
320
     *
321
322
     */
323
    public function multiRadio(string $name, array $list = [], $selected = null, array $options = []): string
324
    {
325
        $label    = $this->label($name, $options);
326
        $options  = $this->expandOptionArray($name, $label, $options);
327
        $classes  = $this->getHolderClasses($name);
328
        $selected = $this->fillFieldValue($name, $selected);
329
330
        unset($options['class']);
331
        $html = view('form.multiRadio', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
332
333
        return $html;
334
    }
335
336
    /**
337
     * @param string $name
338
     * @param null   $value
339
     * @param array  $options
340
     *
341
     * @return string
342
     *
343
344
     */
345
    public function nonSelectableAmount(string $name, $value = null, array $options = []): string
346
    {
347
        $label            = $this->label($name, $options);
348
        $options          = $this->expandOptionArray($name, $label, $options);
349
        $classes          = $this->getHolderClasses($name);
350
        $value            = $this->fillFieldValue($name, $value);
351
        $options['step']  = 'any';
352
        $selectedCurrency = $options['currency'] ?? Amt::getDefaultCurrency();
0 ignored issues
show
The method getDefaultCurrency() does not exist on FireflyIII\Support\Facades\Amount. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

352
        $selectedCurrency = $options['currency'] ?? Amt::/** @scrutinizer ignore-call */ getDefaultCurrency();
Loading history...
353
        unset($options['currency'], $options['placeholder']);
354
355
        // make sure value is formatted nicely:
356
        if (null !== $value && '' !== $value) {
357
            $value = round($value, $selectedCurrency->decimal_places);
358
        }
359
360
        $html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render();
361
362
        return $html;
363
    }
364
365
    /**
366
     * @param string $name
367
     * @param null   $value
368
     * @param array  $options
369
     *
370
     * @return string
371
     *
372
373
     */
374
    public function nonSelectableBalance(string $name, $value = null, array $options = []): string
375
    {
376
        $label            = $this->label($name, $options);
377
        $options          = $this->expandOptionArray($name, $label, $options);
378
        $classes          = $this->getHolderClasses($name);
379
        $value            = $this->fillFieldValue($name, $value);
380
        $options['step']  = 'any';
381
        $selectedCurrency = $options['currency'] ?? Amt::getDefaultCurrency();
382
        unset($options['currency'], $options['placeholder']);
383
384
        // make sure value is formatted nicely:
385
        if (null !== $value && '' !== $value) {
386
            $decimals = $selectedCurrency->decimal_places ?? 2;
387
            $value    = round($value, $decimals);
388
        }
389
390
        $html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render();
391
392
        return $html;
393
    }
394
395
    /**
396
     * @param string $name
397
     * @param null   $value
398
     * @param array  $options
399
     *
400
     * @return string
401
     *
402
403
     */
404
    public function number(string $name, $value = null, array $options = []): string
405
    {
406
        $label           = $this->label($name, $options);
407
        $options         = $this->expandOptionArray($name, $label, $options);
408
        $classes         = $this->getHolderClasses($name);
409
        $value           = $this->fillFieldValue($name, $value);
410
        $options['step'] = 'any';
411
        unset($options['placeholder']);
412
413
        $html = view('form.number', compact('classes', 'name', 'label', 'value', 'options'))->render();
414
415
        return $html;
416
    }
417
418
    /**
419
     * @param $type
420
     * @param $name
421
     *
422
     * @return string
423
     *
424
425
     */
426
    public function optionsList(string $type, string $name): string
427
    {
428
        $previousValue = null;
429
430
        try {
431
            $previousValue = request()->old('post_submit_action');
432
        } catch (RuntimeException $e) {
433
            // don't care
434
        }
435
436
        $previousValue = $previousValue ?? 'store';
437
        $html          = view('form.options', compact('type', 'name', 'previousValue'))->render();
438
439
        return $html;
440
    }
441
442
    /**
443
     * @param       $name
444
     * @param array $options
445
     *
446
     * @return string
447
     *
448
449
     */
450
    public function password(string $name, array $options = []): string
451
    {
452
        $label   = $this->label($name, $options);
453
        $options = $this->expandOptionArray($name, $label, $options);
454
        $classes = $this->getHolderClasses($name);
455
        $html    = view('form.password', compact('classes', 'name', 'label', 'options'))->render();
456
457
        return $html;
458
    }
459
460
    /**
461
     * @param       $name
462
     * @param array $list
463
     * @param null  $selected
464
     * @param array $options
465
     *
466
     * @return string
467
     *
468
469
     */
470
    public function select(string $name, array $list = [], $selected = null, array $options = []): string
471
    {
472
        $label    = $this->label($name, $options);
473
        $options  = $this->expandOptionArray($name, $label, $options);
474
        $classes  = $this->getHolderClasses($name);
475
        $selected = $this->fillFieldValue($name, $selected);
476
        unset($options['autocomplete'], $options['placeholder']);
477
        $html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
478
479
        return $html;
480
    }
481
482
    /**
483
     * @param       $name
484
     * @param null  $value
485
     * @param array $options
486
     *
487
     * @return string
488
     *
489
490
     */
491
    public function staticText(string $name, $value, array $options = []): string
492
    {
493
        $label   = $this->label($name, $options);
494
        $options = $this->expandOptionArray($name, $label, $options);
495
        $classes = $this->getHolderClasses($name);
496
        $html    = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();
497
498
        return $html;
499
    }
500
501
    /**
502
     * @param       $name
503
     * @param null  $value
504
     * @param array $options
505
     *
506
     * @return string
507
     *
508
509
     */
510
    public function tags(string $name, $value = null, array $options = []): string
511
    {
512
        $label                = $this->label($name, $options);
513
        $options              = $this->expandOptionArray($name, $label, $options);
514
        $classes              = $this->getHolderClasses($name);
515
        $value                = $this->fillFieldValue($name, $value);
516
        $options['data-role'] = 'tagsinput';
517
        $html                 = view('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render();
518
519
        return $html;
520
    }
521
522
    /**
523
     * @param       $name
524
     * @param null  $value
525
     * @param array $options
526
     *
527
     * @return string
528
     *
529
530
     */
531
    public function text(string $name, $value = null, array $options = []): string
532
    {
533
        $label   = $this->label($name, $options);
534
        $options = $this->expandOptionArray($name, $label, $options);
535
        $classes = $this->getHolderClasses($name);
536
        $value   = $this->fillFieldValue($name, $value);
537
        $html    = view('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render();
538
539
        return $html;
540
    }
541
542
    /**
543
     * @param       $name
544
     * @param null  $value
545
     * @param array $options
546
     *
547
     * @return string
548
     *
549
550
     */
551
    public function textarea(string $name, $value = null, array $options = []): string
552
    {
553
        $label           = $this->label($name, $options);
554
        $options         = $this->expandOptionArray($name, $label, $options);
555
        $classes         = $this->getHolderClasses($name);
556
        $value           = $this->fillFieldValue($name, $value);
557
        $options['rows'] = 4;
558
        $html            = view('form.textarea', compact('classes', 'name', 'label', 'value', 'options'))->render();
559
560
        return $html;
561
    }
562
563
    /**
564
     * @param       $name
565
     * @param       $label
566
     * @param array $options
567
     *
568
     * @return array
569
     */
570
    protected function expandOptionArray(string $name, $label, array $options): array
571
    {
572
        $name                    = str_replace('[]', '', $name);
573
        $options['class']        = 'form-control';
574
        $options['id']           = 'ffInput_' . $name;
575
        $options['autocomplete'] = 'off';
576
        $options['placeholder']  = ucfirst($label);
577
578
        return $options;
579
    }
580
581
    /**
582
     * @param $name
583
     * @param $value
584
     *
585
     * @return mixed
586
     */
587
    protected function fillFieldValue(string $name, $value)
588
    {
589
        if (Session::has('preFilled')) {
590
            $preFilled = session('preFilled');
591
            $value     = isset($preFilled[$name]) && null === $value ? $preFilled[$name] : $value;
592
        }
593
        try {
594
            if (null !== request()->old($name)) {
595
                $value = request()->old($name);
596
            }
597
        } catch (RuntimeException $e) {
598
            // don't care about session errors.
599
        }
600
        if ($value instanceof Carbon) {
601
            $value = $value->format('Y-m-d');
602
        }
603
604
        return $value;
605
    }
606
607
    /**
608
     * @param $name
609
     *
610
     * @return string
611
     */
612
    protected function getHolderClasses(string $name): string
613
    {
614
        // Get errors from session:
615
        /** @var MessageBag $errors */
616
        $errors  = session('errors');
617
        $classes = 'form-group';
618
619
        if (null !== $errors && $errors->has($name)) {
620
            $classes = 'form-group has-error has-feedback';
621
        }
622
623
        return $classes;
624
    }
625
626
    /**
627
     * @param $name
628
     * @param $options
629
     *
630
     * @return mixed
631
     */
632
    protected function label(string $name, array $options): string
633
    {
634
        if (isset($options['label'])) {
635
            return $options['label'];
636
        }
637
        $name = str_replace('[]', '', $name);
638
639
        return (string)trans('form.' . $name);
640
    }
641
642
    /**
643
     * @param string $name
644
     * @param string $view
645
     * @param null   $value
646
     * @param array  $options
647
     *
648
     * @return string
649
     *
650
     * @throws \FireflyIII\Exceptions\FireflyException
651
     */
652
    private function currencyField(string $name, string $view, $value = null, array $options = []): string
653
    {
654
        $label           = $this->label($name, $options);
655
        $options         = $this->expandOptionArray($name, $label, $options);
656
        $classes         = $this->getHolderClasses($name);
657
        $value           = $this->fillFieldValue($name, $value);
658
        $options['step'] = 'any';
659
        $defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency();
660
        $currencies      = app('amount')->getAllCurrencies();
661
        unset($options['currency'], $options['placeholder']);
662
663
        // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
664
        $preFilled      = session('preFilled');
665
        $key            = 'amount_currency_id_' . $name;
666
        $sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id;
667
668
        // find this currency in set of currencies:
669
        foreach ($currencies as $currency) {
670
            if ($currency->id === $sentCurrencyId) {
671
                $defaultCurrency = $currency;
672
                break;
673
            }
674
        }
675
676
        // make sure value is formatted nicely:
677
        if (null !== $value && '' !== $value) {
678
            $value = round($value, $defaultCurrency->decimal_places);
679
        }
680
681
        $html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
682
683
        return $html;
684
    }
685
}
686