-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnippet.php
145 lines (128 loc) · 3.8 KB
/
Snippet.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
<?php
namespace ddSendFeedback;
class Snippet extends \DDTools\Snippet {
protected
$version = '2.9.0',
$params = [
//Defaults
'result_titleSuccess' => null,
'result_titleFail' => null,
'result_messageSuccess' => null,
'result_messageFail' => null,
'senders' => null,
],
$paramsTypes = [
'senders' => 'objectArray',
]
;
/**
* prepareParams
* @version 1.0.1 (2024-07-13)
*
* @param $params {stdClass|arrayAssociative|stringJsonObject|stringQueryFormatted}
*
* @return {void}
*/
protected function prepareParams($params = []){
//Call base method
parent::prepareParams($params);
//Получаем язык админки
$lang = \ddTools::$modx->getConfig('manager_language');
//Если язык русский
if(
$lang == 'russian-UTF8'
|| $lang == 'russian'
){
if (is_null($this->params->result_titleSuccess)){
$this->params->result_titleSuccess = 'Заявка успешно отправлена';
}
if (is_null($this->params->result_titleFail)){
$this->params->result_titleFail = 'Непредвиденная ошибка =(';
}
if (is_null($this->params->result_messageSuccess)){
$this->params->result_messageSuccess = 'Наш специалист свяжется с вами в ближайшее время.';
}
if (is_null($this->params->result_messageFail)){
$this->params->result_messageFail = 'Во время отправки заявки что-то произошло.<br />Пожалуйста, попробуйте чуть позже.';
}
}else{
if (is_null($this->params->result_titleSuccess)){
$this->params->result_titleSuccess = 'Message sent successfully';
}
if (is_null($this->params->result_titleFail)){
$this->params->result_titleFail = 'Unexpected error =(';
}
if (is_null($this->params->result_messageSuccess)){
$this->params->result_messageSuccess = 'We will contact you later';
}
if (is_null($this->params->result_messageFail)){
$this->params->result_messageFail = 'Something happened while sending the message.<br />Please try again later.';
}
}
}
/**
* run
* @version 1.0.3 (2024-07-13)
*
* @return {string}
*/
public function run(){
$result = new \DDTools\Response();
//Senders is required parameter
if (\ddTools::isEmpty($this->params->senders)){
$result->setMeta([
'success' => false,
]);
}else{
$outputMessages = [
'titles' => [
0 => $this->params->result_titleFail,
1 => $this->params->result_titleSuccess,
],
'messages' => [
0 => $this->params->result_messageFail,
1 => $this->params->result_messageSuccess,
],
];
$sendResults = [];
//Iterate through all senders to create their instances
foreach(
$this->params->senders
as $senderName
=> $senderParams
){
$sender = \ddSendFeedback\Sender\Sender::createChildInstance([
'name' => $senderName,
'params' => $senderParams,
]);
//Send message (items with integer keys are not overwritten)
$sendResults = array_merge(
$sendResults,
$sender->send()
);
}
//Fail by default
$sendResults_status = 0;
//Перебираем все статусы отправки
foreach (
$sendResults
as $sendResults_item
){
//Запоминаем
$sendResults_status = intval($sendResults_item);
//Если не отправлось хоть на один адрес, считаем, что всё плохо
if ($sendResults_status == 0){
break;
}
}
$result->setMeta([
'success' => boolval($sendResults_status),
'message' => [
'content' => $outputMessages['messages'][$sendResults_status],
'title' => $outputMessages['titles'][$sendResults_status],
],
]);
}
return $result->toJSON();
}
}