-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReservation.module
359 lines (302 loc) · 13.7 KB
/
Reservation.module
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/**
* Class Reservation
*
* Sample reservation module.
*
*/
class Reservation extends WireData implements Module {
//------------------------------------
// Modules information
// -----------------------------------
public static function getModuleInfo()
{
return array(
'title' => 'Reservation',
'version' => 003,
'summary' => 'Handles reservation process',
'singular' => false,
'autoload' => false,
'requires' => array()
);
}
/**
* Populate the default config data
*
* ProcessWire will automatically overwrite it with anything the user has specifically configured.
* This is done in construct() rather than init() because ProcessWire populates config data after
* construct(), but before init().
*
*/
public function __construct() {
foreach(self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
}
/**
* Method to initialize the module.
*
* While the method is required, if you don't need it, then just leave the implementation blank.
*
* This is called after ProcessWire's API is fully ready for use and hooks. It is called at the end of the
* bootstrap process. This is before PW has started retrieving or rendering a page. If you need to have the
* API ready with the $page ready as well, then see the ready() method below this one.
*
*/
public function init()
{
// TODO: Implement init() method.
}
static public function getDefaultData() {
$defaultData = array(
'confirmationUrlSegment' => "confirmation",
'completedUrlSegment' => "completed",
'customThankyou' => 0,
'noOfChildren' => array(),
'noOfAdults' => array()
);
foreach(self::getDefaultFields() as $key => $arr) {
if ($arr['defaults']['visible']) $defaultData[$key] = 1;
$reqKey = $key . 'Required';
if ($arr['defaults']['required']) $defaultData[$reqKey] = 1;
$defaultData['label'] = $arr['label'];
}
// ----------------------------------------
// Setting up labels for children drop-down
// ----------------------------------------
$maxChildren = 20;
for($i = 0; $i<=$maxChildren;$i++)
{
if ($i === 0) $defaultData['noOfChildren'][] = '0 ' . __('children');
else if ($i === 1) $defaultData['noOfChildren'][] = '1 ' . __('children');
else $defaultData['noOfChildren'][] = "{$i} " . __('children');
}
// ----------------------------------------
// Setting up labels for adult drop-down
// ----------------------------------------
$maxPeople = 20;
for($i = 1; $i<=$maxPeople;$i++)
{
if ($i === 1) $defaultData['noOfAdults'][$i] = '1 ' . __('person');
else $defaultData['noOfAdults'][$i] = "{$i} " . __('person');
}
return $defaultData;
}
static public function getDefaultFields() {
// ------------------------------
// Property for required input fields
// ------------------------------
$required = array(
'visible' => 1,
'required' => 1
);
// ------------------------------
// Properties for optional input fields
// ------------------------------
$visible = array(
'visible' => 1,
'required' => 0
);
// ------------------------------
// Properties for the hidden input fields
// ------------------------------
$hidden = array(
'visible' => 0,
'required' => 0
);
return array (
'res_first_name' => array('label' => __('First name'), 'defaults' => $required),
'res_last_name' => array('label' => __('Last name'), 'defaults' => $required),
'res_daytime_phone' => array('label' => __('Daytime phone'), 'defaults' => $required),
'res_email' => array('label' => __('Email'), 'defaults' => $required),
'res_arrival_date' => array('label' => __('Arrival date'), 'defaults' => $required),
'res_departure_date' => array('label' => __('Departure date'), 'defaults' => $required),
'res_no_of_adults' => array('label' => __('Number of adults'), 'defaults' => $required),
'res_no_of_children' => array('label' => __('Number of children'), 'defaults' => $required),
'res_comments' => array('label' => __('Comments'), 'defaults' => $visible),
);
}
public function renderReservation() {
if(!$this->input->urlSegment1) {
if ($this->input->post->submit) {
$orderArray = $this->validateInformation(true);
if($orderArray['valid']) {
$orderArray = $this->validateInformation(true);
if($orderArray['valid']) {
$this->session->redirect("./{$this->confirmationUrlSegment}/");
}
}
}else if ($this->input->post->reset){
$this->resetFields();
$this->session->redirect("./");
} else {
$this->validateInformation(false);
}
$out = $this->renderReservationForm();
}
else if ($this->input->urlSegment1 == $this->confirmationUrlSegment) {
$out = $this->renderConfirmation();
if ($this->input->post->submit) {
$orderArray = $this->session->orderArray;
$this->sendEmail($orderArray);
$this->session->remove('orderArray');
$this->session->redirect("../{$this->completedUrlSegment}/");
}
}
else if ($this->input->urlSegment1 == $this->completedUrlSegment) {
$out = $this->renderReservationComplete();
}
return $out;
}
protected function renderReservationForm() {
//Create delegate template
$template = new TemplateFile(__DIR__. "/markup/reservation-form.inc");
$template->setArray(
array(
'title' => $this->_('Order your reservation'),
'summary' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt eleifend fringilla. Suspendisse pharetra justo mi, sit amet tincidunt odio porttitor et. Vestibulum posuere lacus a purus posuere molestie sit amet vel nulla',
'form' => $this->session->orderArray,
'noOfChildren' => $this->noOfChildren,
'noOfAdults' => $this->noOfAdults
)
);
//assign the rendered view to content variable to show up on _done.php
$content = $template->render();
return $content;
}
protected function renderConfirmation() {
//Create delegate template
$template = new TemplateFile(__DIR__. "/markup/confirm-reservation-form.inc");
$template->setArray(
array(
'title' => $this->_('Confirm your details'),
'summary' => sprintf($this->_('Please confirm your details and click `%s` button to proceed. If you missed something please update your information by clicking `%s`.'), $this->_('Confirm'), $this->_('Edit order')),
'order' => $this->session->orderArray,
)
);
//assign the rendered view to content variable to show up on _done.php
$content = $template->render();
return $content;
}
protected function renderReservationComplete(){
$config = wire('config');
//Create delegate template
$template = new TemplateFile(__DIR__. "/markup/reservation-complete.inc");
$template->setArray(
array(
'order' => $this->session->orderArray,
'title' => $this->_('Your reservation successful'),
'summary' => $this->_('Thank you for your order. We will contact you in soon.'),
'homepage' => $config->urls->root,
)
);
//assign the rendered view to content variable to show up on _done.php
$content = $template->render();
return $content;
}
public function resetFields() {
$fields = self::getDefaultFields();
$orderArray = Array();
$orderArray['valid'] = true;
foreach($fields as $key => $field) {
//--------------------------------------------
// If not visible field, don't add to array
//--------------------------------------------
// if ($this->$key != 1) continue;
//--------------------------------------------
// Set the form item type and default to input
//--------------------------------------------
if (isset($field['type'])) $orderArray['fields'][$key]['type'] = $field['type'];
else $orderArray['fields'][$key]['type'] = 'text';
//--------------------------------------------
// Set empty value for every field
//--------------------------------------------
$orderArray['fields'][$key]['value'] = '';
//--------------------------------------------
// Set default label for the field
//--------------------------------------------
if (isset($field['label'])) $orderArray['fields'][$key]['label'] = $field['label'];
else $orderArray['fields'][$key]['label'] = $this->_('No label added');
$orderArray['fields'][$key]['error'] = '';
//--------------------------------------------
// And if we have value saved on session, empty it
//--------------------------------------------
if(isset($this->session->orderArray['fields'][$key])) {
$orderArray['fields'][$key]['value'] = '';
}
}
$this->session->set('orderArray', $orderArray);
}
public function validateInformation($validate = true) {
$fields = self::getDefaultFields();
$orderArray = Array();
$orderArray['valid'] = true;
foreach($fields as $key => $field) {
//--------------------------------------------
// If not visible field, don't add to array
//--------------------------------------------
// if ($this->$key != 1) continue;
//--------------------------------------------
// Set the form item type and default to input
//--------------------------------------------
if (isset($field['type'])) $orderArray['fields'][$key]['type'] = $field['type'];
else $orderArray['fields'][$key]['type'] = 'text';
//--------------------------------------------
// Set empty value for every field
//--------------------------------------------
$orderArray['fields'][$key]['value'] = '';
//--------------------------------------------
// Set default label for the field
//--------------------------------------------
if (isset($field['label'])) $orderArray['fields'][$key]['label'] = $field['label'];
else $orderArray['fields'][$key]['label'] = $this->_('No label added');
$orderArray['fields'][$key]['error'] = '';
//--------------------------------------------
// And if we have value saved on session, update it
//--------------------------------------------
if(isset($this->session->orderArray['fields'][$key])) {
$orderArray['fields'][$key]['value'] = $this->session->orderArray['fields'][$key]['value'];
}
}
if ($validate) {
foreach($orderArray['fields'] as $key => $field) {
$reqKey = $key . 'Required';
$field['value'] = $this->input->post->$key;
$orderArray['fields'][$key]['value'] = $this->sanitizer->text($field['value']);
if($key == 'res_email' && !empty($this->input->post->rs_email)) {
if($this->sanitizer->email($field['value']) == '') {
$orderArray['valid'] = false;
$orderArray['fields'][$key]['error'] = $this->_("Email is not in right format.");
}
}
if ($this->$reqKey && $orderArray['fields'][$key]['value'] == '') {
$orderArray['valid'] = false;
$orderArray['fields'][$key]['error'] = $orderArray['fields'][$key]['label'] . ' '. $this->_("is a required");
}
$this->session->set('orderArray', $orderArray);
}
}
if (!isset($this->session->orderArray['valid'])) $this->session->set('orderArray', $orderArray);
return $orderArray;
}
protected function ___sendEmail($order) {
$message = '';
$fromEmail = 'mearch.com';
$subject = 'Web reservation request';
$message = "<html><head></head><body>$message</body></html>";
$headers = "Content-Type: text/html;";
if($fromEmail) $headers = "From: $fromEmail\n$headers";
if (isset($order['fields'])) {
$message .= "<h3>Web reservation request</h3>";
$message .= "<table border='1'>";
foreach ($order['fields'] as $field){
$message .= "<tr>";
$message .= "<th>" . $field['label'] ."</th>";
$message .= "<td>" . $field['value'] ."</td>";
$message .= "</tr>";
}
$message .= "</table>";
}
mail($order['fields']['res_email']['value'], $subject, $message, $headers);
}
}