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.
Completed
Push — master ( 869845...9ac565 )
by James
08:24 queued 03:31
created

ExpandedForm::number()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 3
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
22
declare(strict_types=1);
23
24
namespace FireflyIII\Support;
25
26
use Amount as Amt;
27
use Carbon\Carbon;
28
use Eloquent;
29
use Illuminate\Support\Collection;
30
use Illuminate\Support\MessageBag;
31
use Input;
32
use RuntimeException;
33
use Session;
34
35
/**
36
 * Class ExpandedForm
37
 *
38
 * @package FireflyIII\Support
39
 *
40
 */
41
class ExpandedForm
42
{
43
44
    /**
45
     * @param       $name
46
     * @param null  $value
47
     * @param array $options
48
     *
49
     * @return string
50
     */
51
    public function amount(string $name, $value = null, array $options = []): string
52
    {
53
        return $this->currencyField($name, 'amount', $value, $options);
54
    }
55
56
    /**
57
     * @param       $name
58
     * @param null  $value
59
     * @param array $options
60
     *
61
     * @return string
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       $name
70
     * @param null  $value
71
     * @param array $options
72
     *
73
     * @return string
74
     */
75
    public function balance(string $name, $value = null, array $options = []): string
76
    {
77
        return $this->currencyField($name, 'balance', $value, $options);
78
    }
79
80
    /**
81
     * @param       $name
82
     * @param int   $value
83
     * @param null  $checked
84
     * @param array $options
85
     *
86
     * @return string
87
     */
88
    public function checkbox(string $name, $value = 1, $checked = null, $options = []): string
89
    {
90
        $options['checked'] = $checked === true ? true : null;
91
        $label              = $this->label($name, $options);
92
        $options            = $this->expandOptionArray($name, $label, $options);
93
        $classes            = $this->getHolderClasses($name);
94
        $value              = $this->fillFieldValue($name, $value);
95
96
        unset($options['placeholder'], $options['autocomplete'], $options['class']);
97
98
        $html = view('form.checkbox', compact('classes', 'name', 'label', 'value', 'options'))->render();
99
100
        return $html;
101
    }
102
103
    /**
104
     * @param       $name
105
     * @param null  $value
106
     * @param array $options
107
     *
108
     * @return string
109
     */
110
    public function date(string $name, $value = null, array $options = []): string
111
    {
112
        $label   = $this->label($name, $options);
113
        $options = $this->expandOptionArray($name, $label, $options);
114
        $classes = $this->getHolderClasses($name);
115
        $value   = $this->fillFieldValue($name, $value);
116
        unset($options['placeholder']);
117
        $html = view('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render();
118
119
        return $html;
120
    }
121
122
    /**
123
     * @param       $name
124
     * @param array $options
125
     *
126
     * @return string
127
     */
128
    public function file(string $name, array $options = []): string
129
    {
130
        $label   = $this->label($name, $options);
131
        $options = $this->expandOptionArray($name, $label, $options);
132
        $classes = $this->getHolderClasses($name);
133
        $html    = view('form.file', compact('classes', 'name', 'label', 'options'))->render();
134
135
        return $html;
136
137
    }
138
139
    /**
140
     * @param       $name
141
     * @param null  $value
142
     * @param array $options
143
     *
144
     * @return string
145
     */
146
    public function integer(string $name, $value = null, array $options = []): string
147
    {
148
        $label           = $this->label($name, $options);
149
        $options         = $this->expandOptionArray($name, $label, $options);
150
        $classes         = $this->getHolderClasses($name);
151
        $value           = $this->fillFieldValue($name, $value);
152
        $options['step'] = '1';
153
        $html            = view('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render();
154
155
        return $html;
156
157
    }
158
159
    /**
160
     * @param       $name
161
     * @param null  $value
162
     * @param array $options
163
     *
164
     * @return string
165
     */
166
    public function location(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
        $html    = view('form.location', compact('classes', 'name', 'label', 'value', 'options'))->render();
173
174
        return $html;
175
176
    }
177
178
    /**
179
     *
180
     * Takes any collection and tries to make a sensible select list compatible array of it.
181
     *
182
     * @param \Illuminate\Support\Collection $set
183
     *
184
     * @return array
185
     */
186
    public function makeSelectList(Collection $set): array
187
    {
188
        $selectList = [];
189
        $fields     = ['title', 'name', 'description'];
190
        /** @var Eloquent $entry */
191
        foreach ($set as $entry) {
192
            $entryId = intval($entry->id);
193
            $title   = null;
194
195
            foreach ($fields as $field) {
196
                if (isset($entry->$field) && is_null($title)) {
197
                    $title = $entry->$field;
198
                }
199
            }
200
            $selectList[$entryId] = $title;
201
        }
202
203
        return $selectList;
204
    }
205
206
    /**
207
     * @param \Illuminate\Support\Collection $set
208
     *
209
     * @return array
210
     */
211
    public function makeSelectListWithEmpty(Collection $set): array
212
    {
213
        $selectList    = [];
214
        $selectList[0] = '(none)';
215
        $fields        = ['title', 'name', 'description'];
216
        /** @var Eloquent $entry */
217
        foreach ($set as $entry) {
218
            $entryId = intval($entry->id);
219
            $title   = null;
220
221
            foreach ($fields as $field) {
222
                if (isset($entry->$field) && is_null($title)) {
223
                    $title = $entry->$field;
224
                }
225
            }
226
            $selectList[$entryId] = $title;
227
        }
228
229
        return $selectList;
230
    }
231
232
    /**
233
     * @param       $name
234
     * @param array $list
235
     * @param null  $selected
236
     * @param array $options
237
     *
238
     * @return string
239
     */
240
    public function multiCheckbox(string $name, array $list = [], $selected = null, array $options = []): string
241
    {
242
        $label    = $this->label($name, $options);
243
        $options  = $this->expandOptionArray($name, $label, $options);
244
        $classes  = $this->getHolderClasses($name);
245
        $selected = $this->fillFieldValue($name, $selected);
246
247
        unset($options['class']);
248
        $html = view('form.multiCheckbox', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
249
250
        return $html;
251
    }
252
253
    /**
254
     * @param       $name
255
     * @param array $list
256
     * @param null  $selected
257
     * @param array $options
258
     *
259
     * @return string
260
     */
261
    public function multiRadio(string $name, array $list = [], $selected = null, array $options = []): string
262
    {
263
        $label    = $this->label($name, $options);
264
        $options  = $this->expandOptionArray($name, $label, $options);
265
        $classes  = $this->getHolderClasses($name);
266
        $selected = $this->fillFieldValue($name, $selected);
267
268
        unset($options['class']);
269
        $html = view('form.multiRadio', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
270
271
        return $html;
272
    }
273
274
    /**
275
     * @param string $name
276
     * @param null   $value
277
     * @param array  $options
278
     *
279
     * @return string
280
     */
281
    public function nonSelectableAmount(string $name, $value = null, array $options = []): string
282
    {
283
        $label            = $this->label($name, $options);
284
        $options          = $this->expandOptionArray($name, $label, $options);
285
        $classes          = $this->getHolderClasses($name);
286
        $value            = $this->fillFieldValue($name, $value);
287
        $options['step']  = 'any';
288
        $selectedCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
289
        unset($options['currency']);
290
        unset($options['placeholder']);
291
292
        // make sure value is formatted nicely:
293
        if (!is_null($value) && $value !== '') {
294
            $value = round($value, $selectedCurrency->decimal_places);
295
        }
296
297
298
        $html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render();
299
300
        return $html;
301
    }
302
303
    /**
304
     * @param string $name
305
     * @param null   $value
306
     * @param array  $options
307
     *
308
     * @return string
309
     */
310
    public function nonSelectableBalance(string $name, $value = null, array $options = []): string
311
    {
312
        $label            = $this->label($name, $options);
313
        $options          = $this->expandOptionArray($name, $label, $options);
314
        $classes          = $this->getHolderClasses($name);
315
        $value            = $this->fillFieldValue($name, $value);
316
        $options['step']  = 'any';
317
        $selectedCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
318
        unset($options['currency']);
319
        unset($options['placeholder']);
320
321
        // make sure value is formatted nicely:
322
        if (!is_null($value) && $value !== '') {
323
            $decimals = $selectedCurrency->decimal_places ?? 2;
324
            $value    = round($value, $decimals);
325
        }
326
327
328
        $html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render();
329
330
        return $html;
331
    }
332
333
    /**
334
     * @param string $name
335
     * @param null   $value
336
     * @param array  $options
337
     *
338
     * @return string
339
     */
340
    public function number(string $name, $value = null, array $options = []): string
341
    {
342
        $label           = $this->label($name, $options);
343
        $options         = $this->expandOptionArray($name, $label, $options);
344
        $classes         = $this->getHolderClasses($name);
345
        $value           = $this->fillFieldValue($name, $value);
346
        $options['step'] = 'any';
347
        unset($options['placeholder']);
348
349
        $html = view('form.number', compact( 'classes', 'name', 'label', 'value', 'options'))->render();
350
351
        return $html;
352
    }
353
354
    /**
355
     * @param $type
356
     * @param $name
357
     *
358
     * @return string
359
     */
360
    public function optionsList(string $type, string $name): string
361
    {
362
        $previousValue = null;
363
364
        try {
365
            $previousValue = request()->old('post_submit_action');
366
        } catch (RuntimeException $e) {
367
            // don't care
368
        }
369
370
        $previousValue = is_null($previousValue) ? 'store' : $previousValue;
371
        $html          = view('form.options', compact('type', 'name', 'previousValue'))->render();
372
373
        return $html;
374
    }
375
376
    /**
377
     * @param       $name
378
     * @param array $options
379
     *
380
     * @return string
381
     */
382
    public function password(string $name, array $options = []): string
383
    {
384
        $label   = $this->label($name, $options);
385
        $options = $this->expandOptionArray($name, $label, $options);
386
        $classes = $this->getHolderClasses($name);
387
        $html    = view('form.password', compact('classes', 'name', 'label', 'value', 'options'))->render();
388
389
        return $html;
390
391
    }
392
393
    /**
394
     * @param       $name
395
     * @param array $list
396
     * @param null  $selected
397
     * @param array $options
398
     *
399
     * @return string
400
     */
401
    public function select(string $name, array $list = [], $selected = null, array $options = []): string
402
    {
403
        $label    = $this->label($name, $options);
404
        $options  = $this->expandOptionArray($name, $label, $options);
405
        $classes  = $this->getHolderClasses($name);
406
        $selected = $this->fillFieldValue($name, $selected);
407
        unset($options['autocomplete']);
408
        unset($options['placeholder']);
409
        $html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
410
411
        return $html;
412
    }
413
414
    /**
415
     * @param       $name
416
     * @param null  $value
417
     * @param array $options
418
     *
419
     * @return string
420
     */
421
    public function staticText(string $name, $value, array $options = []): string
422
    {
423
        $label   = $this->label($name, $options);
424
        $options = $this->expandOptionArray($name, $label, $options);
425
        $classes = $this->getHolderClasses($name);
426
        $html    = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();
427
428
        return $html;
429
430
    }
431
432
    /**
433
     * @param       $name
434
     * @param null  $value
435
     * @param array $options
436
     *
437
     * @return string
438
     */
439
    public function tags(string $name, $value = null, array $options = []): string
440
    {
441
        $label                = $this->label($name, $options);
442
        $options              = $this->expandOptionArray($name, $label, $options);
443
        $classes              = $this->getHolderClasses($name);
444
        $value                = $this->fillFieldValue($name, $value);
445
        $options['data-role'] = 'tagsinput';
446
        $html                 = view('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render();
447
448
        return $html;
449
    }
450
451
    /**
452
     * @param       $name
453
     * @param null  $value
454
     * @param array $options
455
     *
456
     * @return string
457
     */
458
    public function text(string $name, $value = null, array $options = []): string
459
    {
460
        $label   = $this->label($name, $options);
461
        $options = $this->expandOptionArray($name, $label, $options);
462
        $classes = $this->getHolderClasses($name);
463
        $value   = $this->fillFieldValue($name, $value);
464
        $html    = view('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render();
465
466
        return $html;
467
468
    }
469
470
    /**
471
     * @param       $name
472
     * @param null  $value
473
     * @param array $options
474
     *
475
     * @return string
476
     */
477
    public function textarea(string $name, $value = null, array $options = []): string
478
    {
479
        $label           = $this->label($name, $options);
480
        $options         = $this->expandOptionArray($name, $label, $options);
481
        $classes         = $this->getHolderClasses($name);
482
        $value           = $this->fillFieldValue($name, $value);
483
        $options['rows'] = 4;
484
        $html            = view('form.textarea', compact('classes', 'name', 'label', 'value', 'options'))->render();
485
486
        return $html;
487
488
    }
489
490
    /**
491
     * @param       $name
492
     * @param       $label
493
     * @param array $options
494
     *
495
     * @return array
496
     */
497
    protected function expandOptionArray(string $name, $label, array $options): array
498
    {
499
        $name                    = str_replace('[]', '', $name);
500
        $options['class']        = 'form-control';
501
        $options['id']           = 'ffInput_' . $name;
502
        $options['autocomplete'] = 'off';
503
        $options['placeholder']  = ucfirst($label);
504
505
        return $options;
506
    }
507
508
    /**
509
     * @param $name
510
     * @param $value
511
     *
512
     * @return mixed
513
     */
514
    protected function fillFieldValue(string $name, $value)
515
    {
516
        if (Session::has('preFilled')) {
517
            $preFilled = session('preFilled');
518
            $value     = isset($preFilled[$name]) && is_null($value) ? $preFilled[$name] : $value;
519
        }
520
        try {
521
            if (!is_null(request()->old($name))) {
522
                $value = request()->old($name);
523
            }
524
        } catch (RuntimeException $e) {
525
            // don't care about session errors.
526
        }
527
        if ($value instanceof Carbon) {
528
            $value = $value->format('Y-m-d');
529
        }
530
531
532
        return $value;
533
    }
534
535
    /**
536
     * @param $name
537
     *
538
     * @return string
539
     */
540
    protected function getHolderClasses(string $name): string
541
    {
542
        /*
543
       * Get errors from session:
544
       */
545
        /** @var MessageBag $errors */
546
        $errors  = session('errors');
547
        $classes = 'form-group';
548
549
        if (!is_null($errors) && $errors->has($name)) {
550
            $classes = 'form-group has-error has-feedback';
551
        }
552
553
        return $classes;
554
    }
555
556
    /**
557
     * @param $name
558
     * @param $options
559
     *
560
     * @return mixed
561
     */
562
    protected function label(string $name, array $options): string
563
    {
564
        if (isset($options['label'])) {
565
            return $options['label'];
566
        }
567
        $name = str_replace('[]', '', $name);
568
569
        return strval(trans('form.' . $name));
570
571
    }
572
573
    /**
574
     * @param string $name
575
     * @param string $view
576
     * @param null   $value
577
     * @param array  $options
578
     *
579
     * @return string
580
     */
581
    private function currencyField(string $name, string $view, $value = null, array $options = []): string
582
    {
583
        $label           = $this->label($name, $options);
584
        $options         = $this->expandOptionArray($name, $label, $options);
585
        $classes         = $this->getHolderClasses($name);
586
        $value           = $this->fillFieldValue($name, $value);
587
        $options['step'] = 'any';
588
        $defaultCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
589
        $currencies      = Amt::getAllCurrencies();
590
        unset($options['currency']);
591
        unset($options['placeholder']);
592
593
        // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
594
        $preFilled      = session('preFilled');
595
        $key            = 'amount_currency_id_' . $name;
596
        $sentCurrencyId = isset($preFilled[$key]) ? intval($preFilled[$key]) : $defaultCurrency->id;
597
598
        // find this currency in set of currencies:
599
        foreach ($currencies as $currency) {
600
            if ($currency->id === $sentCurrencyId) {
601
                $defaultCurrency = $currency;
602
                break;
603
            }
604
        }
605
606
        // make sure value is formatted nicely:
607
        if (!is_null($value) && $value !== '') {
608
            $value = round($value, $defaultCurrency->decimal_places);
609
        }
610
611
612
        $html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
613
614
        return $html;
615
    }
616
}
617