forked from Isa3v/form.submit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.php
206 lines (175 loc) · 7.89 KB
/
class.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
<?
use \Bitrix\Main\Application;
use \Bitrix\Main\Loader;
class ThelhFeedbackComponent extends CBitrixComponent
{
/**
* Обработка параметров компонента
*/
public function onPrepareComponentParams($params)
{
$result = array(
'IBLOCK_ID' => (int) $params['IBLOCK_ID'],
'WEBFORM_ID' => md5($this->getTemplateName()).'_'.$params['IBLOCK_ID'],
'EVENT' => trim($params['EVENT']),
'CACHE_TYPE' => trim($params['CACHE_TYPE']),
'CACHE_TIME' => intval($params['CACHE_TIME']) > 0 ? intval($params['CACHE_TIME']) : 3600,
'TIME_COOKIE' => intval($params['TIME_COOKIE']) > 0 ? intval($params['TIME_COOKIE']) : 0,
);
$params = array_merge($params, $result);
return $params;
}
/**
* определяет читать данные из кеша или нет
* @return bool
*/
protected function readDataFromCache()
{
global $USER;
if ($this->arParams['CACHE_TYPE'] == 'N') {
return false;
}
if (is_array($this->cacheAddon)) {
$this->cacheAddon[] = $USER->GetUserGroupArray();
} else {
$this->cacheAddon = array($USER->GetUserGroupArray());
}
return !($this->startResultCache(false, $this->cacheAddon, md5(serialize($this->arParams))));
}
/**
* кеширует ключи массива arResult
*/
protected function putDataToCache()
{
$templateCachedData = $this->GetTemplateCachedData();
$this->SetResultCacheKeys($templateCachedData);
}
/**
* Поля веб-формы
* Данные кешируются и не будут заново вызываться при отправке формы
* @return array
*/
public function getFields()
{
$arFields = [];
$resProp = \CIBlockProperty::GetList(["sort"=>"asc", "name"=>"asc"], ["ACTIVE"=>"Y", "IBLOCK_ID" => $this->arParams["IBLOCK_ID"]]);
while ($arProp = $resProp->fetch()){
$arFields[$arProp['CODE']] = $arProp;
if($arProp['PROPERTY_TYPE'] == 'L'){
$resValuesList = \CIBlockPropertyEnum::GetList(Array("DEF"=>"DESC", "SORT"=>"ASC"), Array("IBLOCK_ID" => $this->arParams["IBLOCK_ID"], "CODE"=> $arProp['CODE']));
while($values = $resValuesList->GetNext()){
$arFields[$arProp['CODE']]['VALUES'][$values['ID']] = $values;
}
}
// Привзяка к элементу
if($arProp['PROPERTY_TYPE'] == 'E'){
$resValuesList = \CIBlockElement::GetList([], Array("IBLOCK_ID" => $arProp["LINK_IBLOCK_ID"], "ACTIVE" => "Y"), false, [], ['ID', 'IBLOCK_ID', 'NAME']);
while($ob = $resValuesList->GetNextElement()){
$field = $ob->GetFields();
$field['PEOPERTIES'] = $ob->GetProperties();
$arFields[$arProp['CODE']]['VALUES'][$field['ID']] = $field;
}
}
// Справочник
if($arProp['PROPERTY_TYPE'] == 'S' && $arProp['USER_TYPE'] == 'directory' && \Bitrix\Main\Loader::includeModule("highloadblock")){
$hlblock = \Bitrix\Highloadblock\HighloadBlockTable::getList(['filter' => ['TABLE_NAME' => $arProp['USER_TYPE_SETTINGS']['TABLE_NAME']]])->fetch();
$entityHighloadBlock = \Bitrix\Highloadblock\HighloadBlockTable::compileEntity($hlblock)->getDataClass();
$resHighload = $entityHighloadBlock::getList(["select" => ['*']]);
while($ob = $resHighload->Fetch()){
if(!empty($ob['UF_FILE'])){
$ob['SRC'] = \CFile::GetPath($ob['UF_FILE']);
}
$arFields[$arProp['CODE']]['VALUES'][$ob['ID']] = $ob;
}
}
}
$this->arResult["FIELDS"] = $arFields;
}
/**
* Обработка получения данных формы
* @return mixed
*/
public function requestForm()
{
$context = Application::getInstance()->getContext();
$request = $context->getRequest();
$data = $request->getPostList()->toArray(); // POST параметры
$this->arResult["SEND"] = false;
$this->arResult["ERROR"] = [];
$this->arResult["SEND_FIELDS"] = [];
// Если скрытое поле reviews_iblock соответствует ID инфоблока
if ($request->getPost("webform") == $this->arParams["WEBFORM_ID"] && check_bitrix_sessid()) {
// Если это ajax запрос, то отдаем в arResult переменную
if($request->isAjaxRequest()){
$this->arResult["IS_AJAX"] = "Y";
}
foreach($this->arResult["FIELDS"] as $code => $arField){
// Если поле обязательное и пришло пустым
if($arField['IS_REQUIRED'] == 'Y' && empty($data[$code])){
// Записываем как ошибку
$this->arResult["ERROR"][$code] = "Обязятельное поле \"{$arField['NAME']}\" не заполнено!";
}
// Разбираем поля. Лишнее нам не надо
if(!empty($data[$code])){
$this->arResult["SEND_FIELDS"][$code] = $data[$code];
}
}
// Если были ошибки возвращаем
if(!empty($this->arResult["ERROR"])){
return false;
}
$this->arResult["SEND"] = true;
/**
* Сохранить элемент инфоблока
*/
$arEventFields = array();
if ($this->arParams["IBLOCK_ID"]) {
$arResult = array(
'IBLOCK_ID' => $this->arParams["IBLOCK_ID"],
'ACTIVE' => 'N',
'NAME' => date("Y-m-d H:i:s").(!empty($this->arResult["SEND_FIELDS"]['NAME']) ? " [{$this->arResult["SEND_FIELDS"]['NAME']}]" : ""),
'DATE_ACTIVE_FROM' => ConvertTimeStamp(time(), "FULL"),
'PROPERTY_VALUES' => $this->arResult["SEND_FIELDS"]
);
$element = new \CIBlockElement();
$arEventFields["ID"] = $element->Add($arResult);
/**
* Отправить письмо
*/
if ($this->arParams["EVENT"]) {
foreach ($arResult as $k => $v) {
$arEventFields[strtoupper($k)] = $v;
}
$mailEvent = \Bitrix\Main\Mail\Event::send(array(
"EVENT_NAME" => $this->arParams["EVENT"],
"LID" => SITE_ID,
"C_FIELDS" => $arEventFields['PROPERTY_VALUES']
));
}
}
}
}
/**
* Логика компонента
*/
public function executeComponent()
{
try {
Loader::includeModule('iblock');
if (!$this->readDataFromCache()) {
$this->getFields();
$this->putDataToCache();
// Заканчиваем кеширование
$this->EndResultCache();
}
// Отправка идет без кеширования
$this->requestForm();
// Отправляем шаблон
$this->includeComponentTemplate();
} catch (Exception $e) {
$this->AbortResultCache();
ShowError($e->getMessage());
}
}
}
?>