Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
message.php
1<?php
10
18
19Loc::loadMessages(__FILE__);
20
26class Message extends Base
27{
35 public function loadConfiguration($id = null, Configuration $configuration = null)
36 {
37 if (!$configuration)
38 {
39 $configuration = new Configuration;
40 }
41
42 if ($id && $this->load($id))
43 {
44 $data = $this->getFields();
45 foreach ($configuration->getOptions() as $option)
46 {
47 $key = $option->getCode();
48 $value = isset($data[$key]) ? $data[$key] : null;
49 if ($option->getType() === $option::TYPE_FILE)
50 {
51 $value = ($value <> '') ? explode(',', $value) : $value;
52 }
53
54 $configuration->set($key, $value);
55 }
56
57 $configuration->setId($id);
58 }
59
60 return $configuration;
61 }
62
69 public function saveConfiguration(Configuration $configuration)
70 {
71 $this->setId($configuration->getId());
72 $result = $configuration->checkOptions();
73 if (!$result->isSuccess())
74 {
75 return $result;
76 }
77
78 $data = [];
79 foreach ($configuration->getOptions() as $option)
80 {
81 $value = $option->getValue();
82 if ($option->getType() === $option::TYPE_FILE)
83 {
84 $value = is_array($value) ? implode(',', $value) : $value;
85 }
86
87 $data[] = [
88 'CODE' => $option->getCode(),
89 'TYPE' => $option->getType(),
90 'VALUE' => $value,
91 ];
92 }
93
94 if (count($data) == 0)
95 {
96 $result->addError(new Error('No options.'));
97 }
98
99 $this->setFields($data)->save();
100
101 if ($this->hasErrors())
102 {
103 $result->addErrors($this->errors->toArray());
104 }
105 else
106 {
107 $configuration->setId($this->getId());
108 }
109
110 return $result;
111 }
112
119 public function copyConfiguration($id)
120 {
121 $copiedId = $this->copyData($id);
122 $result = new Result();
123 $result->setId($copiedId);
124
125 return $result;
126 }
127
134 public function removeConfiguration($id)
135 {
136 $result = static::removeById($id);
137 return $result->isSuccess();
138 }
139
146 public static function removeById($id)
147 {
148 return MessageTable::delete($id);
149 }
150
156 public function getFields(): array
157 {
158 $result = [];
159 $data = $this->getData();
160 foreach ($data['FIELDS'] as $field)
161 {
162 $result[$field['CODE']] = $field['VALUE'];
163 }
164
165 return $result;
166 }
167
174 public function setFields(array $fields)
175 {
176 $this->set('FIELDS', $fields);
177 return $this;
178 }
179
185 public function getUtm(): array
186 {
187 $result = [];
188 $data = $this->getData();
189 foreach ($data['UTM'] as $field)
190 {
191 $result[$field['CODE']] = $field['VALUE'];
192 }
193
194 return $result;
195 }
196
203 public function setUtm(array $utm): Message
204 {
205 $this->set('UTM', $utm);
206 return $this;
207 }
208
214 public function getCode(): ?string
215 {
216 return $this->get('CODE');
217 }
218
225 public function setCode(string $code)
226 {
227 return $this->set('CODE', $code);
228 }
229
235 protected function getDefaultData()
236 {
237 return [
238 'CODE' => '',
239 'FIELDS' => [],
240 'UTM' => [],
241 ];
242 }
243
250 protected function loadData($id)
251 {
252 $data = MessageTable::getRowById($id);
253 if (!is_array($data))
254 {
255 return null;
256 }
257 if ($this->getCode() && $this->getCode() != $data['CODE'])
258 {
259 return null;
260 }
261
262 $data['FIELDS'] = [];
263 $fieldsDb = MessageFieldTable::getList([
264 'select' => ['TYPE', 'CODE', 'VALUE'],
265 'filter' => [
266 '=MESSAGE_ID' => $id,
267 ],
268 ]);
269 while ($field = $fieldsDb->fetch())
270 {
271 $data['FIELDS'][] = $field;
272 }
273
274 return $data;
275 }
276
277 protected function parsePersonalizeList($text)
278 {
279 }
280
288 protected function saveData($id, array $data)
289 {
290 $fields = $data['FIELDS'];
291 $utmTags = $data['UTM'];
292 unset($data['FIELDS']);
293 unset($data['UTM']);
294
295 if (!is_array($fields) && count($fields) === 0)
296 {
297 $this->addError('No message fields.');
298 return $id;
299 }
300
301 $id = $this->saveByEntity(MessageTable::getEntity(), $id, $data);
302 if ($this->hasErrors())
303 {
304 return $id;
305 }
306
307 MessageFieldTable::deleteByMessageId($id);
308 foreach ($fields as $field)
309 {
310 if (!$field['CODE'])
311 {
312 continue;
313 }
314
315 if (in_array($field['CODE'], ['MESSAGE_PERSONALIZE', 'SUBJECT_PERSONALIZE', 'TITLE_PERSONALIZE']))
316 {
317 continue;
318 }
319
320 if (in_array($field['CODE'], ['MESSAGE', 'SUBJECT', 'TITLE']))
321 {
322 preg_match_all("/#([0-9a-zA-Z_.|]+?)#/", $field['VALUE'], $matchesFindPlaceHolders);
323 $matchesFindPlaceHoldersCount = count($matchesFindPlaceHolders[1]);
324 if ($matchesFindPlaceHoldersCount > 0)
325 {
326 $list = json_encode($matchesFindPlaceHolders);
327 MessageFieldTable::add(
328 [
329 'MESSAGE_ID' => $id,
330 'TYPE' => $field['TYPE'],
331 'CODE' => $field['CODE'] . '_PERSONALIZE',
332 'VALUE' => $list,
333 ]
334 );
335 }
336 }
337 MessageFieldTable::add([
338 'MESSAGE_ID' => $id,
339 'TYPE' => $field['TYPE'],
340 'CODE' => $field['CODE'],
341 'VALUE' => $field['VALUE'],
342 ]);
343 }
344
346 if ($utmTags)
347 {
348 foreach ($utmTags as $utm)
349 {
350 if (empty($utm['VALUE']) || empty($utm['CODE']))
351 {
352 continue;
353 }
354 MessageUtmTable::add(
355 [
356 'MESSAGE_ID' => $id,
357 'CODE' => $utm['CODE'],
358 'VALUE' => $utm['VALUE'],
359 ]
360 );
361 }
362 }
363
364 return $id;
365 }
366}
static loadMessages($file)
Definition loc.php:64
loadConfiguration($id=null, Configuration $configuration=null)
Definition message.php:35
saveData($id, array $data)
Definition message.php:288
saveConfiguration(Configuration $configuration)
Definition message.php:69