-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailformControl.php
277 lines (199 loc) · 5.89 KB
/
MailformControl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
use \Nette\Application\UI\Control,
\Nette\Application\UI\Form,
\Nette\Mail\Message;
/**
* MailformControl
*
* @author Michal Mikoláš <nanuqcz@gmail.com>
*/
class MailformControl extends Control
{
/** @var string path to the template file */
protected $templatePath;
/** @var string */
protected $bodyHeader;
/** @var string */
protected $bodyFooter;
/** @var Message */
protected $message;
/** @var string */
protected $flashSuccess;
/** @var Form */
protected $form;
public function __construct()
{
parent::__construct();
$this->templatePath = '';
$this->form = new Form();
$this->bodyHeader = '';
$this->bodyFooter = '';
$this->message = new Message();
$this->flashSuccess = '';
}
/**
* @return void
*/
public function render()
{
$this->amendValues();
$this->template->setFile($this->templatePath);
$this->template->render();
}
/**
* @return Form
*/
protected function createComponentForm()
{
if (count($this->form->controls) == 0) {
$this->setForm( $this->createDefaultForm() );
}
return $this->form;
}
/**
* @param Form
* @return void
*/
public function handleFormSubmitted($form)
{
$this->amendValues();
// Create mail body
$values = $form->getValues();
$mailBody = $this->bodyHeader;
$mailBody .= '<table>';
foreach ($form->controls as $control) {
if ($control->label) {
$mailBody .= '
<tr>
<th>' . $control->label . '</th>
<td>' . nl2br($control->value) . '</td>
</tr>
';
}
}
$mailBody .= '</table>';
$mailBody .= $this->bodyFooter;
// Send e-mail
$this->message->htmlBody = $mailBody;
$this->message->send();
// Redirect
$this->flashMessage($this->flashSuccess);
$this->redirect('this');
}
/********************* Control setup *********************/
/**
* @param string
* @return MailformControl
*/
public function setTemplate($template)
{
$this->templatePath = $template;
return $this;
}
/**
* @param string
* @param string
* @return MailformControl
*/
public function setFrom($email, $name = NULL)
{
$this->message->setFrom($email, $name);
return $this;
}
/**
* @param string
* @return MailformControl
*/
public function addTo($email, $name = NULL)
{
$this->message->addTo($email, $name);
return $this;
}
/**
* @param string
* @return MailformControl
*/
public function setSubject($subject)
{
$this->message->setSubject($subject);
return $this;
}
/**
* @param string
* @return MailformControl
*/
public function setBodyHeader($bodyHeader)
{
$this->bodyHeader = $bodyHeader;
return $this;
}
/**
* @param string
* @return MailformControl
*/
public function setBodyFooter($bodyFooter)
{
$this->bodyFooter = $bodyFooter;
return $this;
}
/**
* @return Form
*/
public function getForm()
{
return $this->form;
}
/**
* @param Form
* @return MailformControl
*/
public function setForm(Form $form)
{
$this->form = $form;
$this->form->onSuccess[] = callback($this, 'handleFormSubmitted');
return $this;
}
/**
* @return Message
*/
public function getMessage()
{
return $this->message;
}
/********************* Class methods *********************/
/**
* @return void
*/
protected function amendValues()
{
// TemplatePath
if ($this->templatePath == '') $this->templatePath = __DIR__ . '/MailformControl.latte';
// Message
if ($this->message->from == '') {
$this->message->from = 'info@' . $_SERVER['HTTP_HOST'];
}
if (count($this->message->getHeader('to')) == 0) {
$this->message->addTo('info@' . $_SERVER['HTTP_HOST']);
}
if ($this->message->subject == '') {
$this->message->subject = 'Byl vyplněn e-mailový formulář.';
}
// FlashSuccess
if ($this->flashSuccess == '') $this->flashSuccess = 'Zpráva byla úspěšně odeslána.';
}
/**
* @return Form
*/
protected function createDefaultForm()
{
$form = new Form();
$form->addText('name', 'Jméno: ')
->addRule(Form::FILLED, 'Vyplňte prosím jméno.');
$form->addText('email', 'E-mail: ')
->addRule(Form::EMAIL, 'Vyplňte prosím e-mail.');
$form->addTextarea('message', 'Zpráva: ')
->addRule(Form::FILLED, 'Vyplňte prosím obsah zprávy.');
$form->addSubmit('send', 'Odeslat');
return $form;
}
}