|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Investform module for webcms2. |
|
5
|
|
|
* Copyright (c) @see LICENSE |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace FrontendModule\InvestformModule; |
|
9
|
|
|
|
|
10
|
|
|
use WebCMS\InvestformModule\Common\FutureValueOfAnnuityCalculator; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Description of InvestformPresenter |
|
14
|
|
|
* |
|
15
|
|
|
* @author Tomas Voslar <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class CalculatorPresenter extends BasePresenter |
|
18
|
|
|
{ |
|
19
|
|
|
private $id; |
|
20
|
|
|
|
|
21
|
|
|
private $fvoa; |
|
22
|
|
|
|
|
23
|
|
|
private $year; |
|
24
|
|
|
|
|
25
|
|
|
protected function startup() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::startup(); |
|
28
|
|
|
|
|
29
|
|
|
foreach(range(200000, 3000000, 100000) as $number) { |
|
30
|
|
|
$amountItems[$number] = \WebCMS\Helpers\SystemHelper::price($number); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function beforeRender() |
|
37
|
|
|
{ |
|
38
|
|
|
parent::beforeRender(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function createComponentForm() |
|
42
|
|
|
{ |
|
43
|
|
|
$form = $this->createForm('form-submit'); |
|
44
|
|
|
|
|
45
|
|
|
$form->addSelect('amount', 'Amount', $this->amountItems) |
|
46
|
|
|
->setAttribute('placeholder', 'Type investement amount'); |
|
|
|
|
|
|
47
|
|
|
$form->addSelect('length', 'Length', array(3 => 'Tříletý', 5 => 'Pětiletý')); |
|
48
|
|
|
$form->addText('date', 'Date')->setRequired('Date is mandatory.'); |
|
49
|
|
|
|
|
50
|
|
|
$form->addHidden('secured'); |
|
51
|
|
|
|
|
52
|
|
|
$form->addSubmit('calculate', 'Calculate'); |
|
53
|
|
|
|
|
54
|
|
|
$form->onSuccess[] = callback($this, 'formSubmitted'); |
|
55
|
|
|
|
|
56
|
|
|
return $form; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function formSubmitted($form) |
|
60
|
|
|
{ |
|
61
|
|
|
$values = $form->getValues(); |
|
62
|
|
|
|
|
63
|
|
|
$from = strtotime($values->date); |
|
64
|
|
|
|
|
65
|
|
|
if ($values->length == 5) { |
|
66
|
|
|
if ($values->secured) { |
|
67
|
|
|
$year = 2020; |
|
68
|
|
|
$to = strtotime('2020-11-30'); |
|
69
|
|
|
} else { |
|
70
|
|
|
$year = 2019; |
|
71
|
|
|
$to = strtotime('2019-11-30'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} else { |
|
75
|
|
|
$year = 2017; |
|
76
|
|
|
$to = strtotime('2017-10-30'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$length = ($to - $from) / 60 / 60 / 24 / 365; |
|
80
|
|
|
|
|
81
|
|
|
$this->year = $year; |
|
82
|
|
|
$this->fvoa = new FutureValueOfAnnuityCalculator($values->amount, $length); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function renderDefault($id) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!is_object($this->fvoa)) { |
|
88
|
|
|
$this->fvoa = new FutureValueOfAnnuityCalculator(0, 3); |
|
89
|
|
|
$this->year = ""; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->template->year = $this->year; |
|
|
|
|
|
|
93
|
|
|
$this->template->fvoa = $this->fvoa; |
|
|
|
|
|
|
94
|
|
|
$this->template->id = $id; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.