handler.php
4.06 KB
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
<?php
## params
$mailTo = 'mind.it@ya.ru';
## fields
$name = isset($_POST['name']) ? $_POST['name'] : null;
$phone = isset($_POST['phone']) ? $_POST['phone'] : null;
$city = isset($_POST['city']) ? $_POST['city'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
$till = isset($_POST['till']) ? $_POST['till'] : null;
$from = isset($_POST['from']) ? $_POST['from'] : null;
## additional fields
$type = isset($_POST['type']) ? $_POST['type'] : null;
switch ($type) {
case 'callback':
$data = ['phone' => $phone, 'from' => $from, 'till' => $till];
$callback = new callback($data);
$callback->send($data);
break;
case 'order':
$data = ['name' => $name, 'email' => $email, 'city' => $city,];
$order = new order($data);
$order->send($data);
break;
default:
echo json_encode(array('type'=>'no_template', 'text'=>'Задайте шаблон обработчика!'));
break;
}
class Form{
public static function validate($data, $field = null)
{
switch ($field) {
case 'phone':
if (!preg_match("/^(8|\+7) ? ?\(?(\d{3})\)? ?(\d{3})[ -]?(\d{2})[ -]?(\d{2})/", $data))
die(json_encode(array('type'=>'error_phone', 'text'=>'Телефон введен неверно')));
return $data;
break;
case 'name':
if (mb_strlen($data) <= 1)
die(json_encode(array('type'=>'error_name', 'text'=>'Вы не ввели имя')));
return $data;
break;
case 'email':
if (!preg_match("/.+@.+\..+/i", $data))
die(json_encode(array('type'=>'error_email', 'text'=>'Почтовый адрес введен неверно')));
return $data;
break;
default:
if (strlen($data) < 2)
die(json_encode(array('type'=>'error', 'text'=>"Поле $field пусто")));
return $data;
}
}
public function send(array $data)
{
}
}
class callback extends Form {
public $phone;
public $till;
public $from;
function __construct(array $data)
{
$this->phone = Form::validate($data['phone'], 'phone');
$this->till = $data['till'];
$this->from = $data['from'];
}
public function send(){
global $mailTo;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Landing_Page <testmail@test.ru>';
$subject = "Заявка на франшизу";
$message = 'Телефон: '. $this->phone . '<br>';
$message .= 'Звонить с ' . $this->till . '<br>';
$message .= 'До ' . $this->from . '<br>';
if (mail($mailTo, $subject, $message, $headers)) {
echo json_encode(array('type'=>'success_send', 'text'=>'Ваша заявка успешно отправлена'));
}
}
}
class order extends Form {
public $name;
public $city;
public $email;
function __construct(array $data)
{
$this->name = Form::validate($data['name'], 'name');
$this->city = $data['city'];
$this->email = Form::validate($data['email'], 'email');
}
public function send(){
global $mailTo;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Landing_Page <testmail@test.ru>';
$subject = "Заявка на франшизу";
$message = 'Клиента зовут: '. $this->name . '<br>';
$message .= 'Почтовый ящик: ' . $this->email . '<br>';
$message .= 'Город: ' . $this->city . '<br>';
if (mail($mailTo, $subject, $message, $headers)) {
echo json_encode(array('type'=>'success_send', 'text'=>'Ваша заявка успешно отправлена'));
}
}
}