Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
agreement.php
1<?php
9
17
18Loc::loadLanguageFile(__FILE__);
19
25{
26 const ACTIVE = 'Y';
27 const NOT_ACTIVE = 'N';
28 const TYPE_STANDARD = 'S';
29 const TYPE_CUSTOM = 'C';
30
32 protected $id = null;
33
35 protected $errors;
36
38 protected $data = array(
39 'ACTIVE' => self::ACTIVE,
40 'TYPE' => self::TYPE_CUSTOM,
41 );
42
44 protected $replace = array();
45
47 protected $intl;
48
50 protected $dataProvider;
51
52 private $isAgreementTextHtml;
53
59 public static function getActiveList()
60 {
61 $result = array();
62 $list = Internals\AgreementTable::getList(array(
63 'select' => array('ID', 'NAME'),
64 'filter' => array('=ACTIVE' => 'Y'),
65 'order' => array('ID' => 'DESC')
66 ));
67 foreach ($list as $item)
68 {
69 $result[$item['ID']] = $item['NAME'];
70 }
71
72 return $result;
73 }
74
80 public static function getTypeNames()
81 {
82 return array(
83 self::TYPE_CUSTOM => Loc::getMessage('MAIN_USER_CONSENT_AGREEMENT_TYPE_N'),
84 self::TYPE_STANDARD => Loc::getMessage('MAIN_USER_CONSENT_AGREEMENT_TYPE_S'),
85 );
86 }
87
94 public function __construct($id, array $replace = array())
95 {
96 $this->errors = new ErrorCollection();
97 $this->intl = new Intl();
98 $this->load($id);
99 $this->setReplace($replace);
100
101 $this->isAgreementTextHtml = ($this->data['IS_AGREEMENT_TEXT_HTML'] == 'Y');
102 }
103
110 public function load($id)
111 {
112 $this->id = null;
113 if (!$id)
114 {
115 $this->errors->setError(new Error('Parameter `Agreement ID` required.'));
116 return false;
117 }
118
119 $data = Internals\AgreementTable::getRowById($id);
120 if (!$data)
121 {
122 $this->errors->setError(new Error("Agreement with id `$id` not found."));
123 return false;
124 }
125
126 $this->data = $data;
127 $this->id = $id;
128 $this->intl->load($this->data['LANGUAGE_ID']);
129
130 return true;
131 }
132
139 public function setReplace(array $replace)
140 {
141 $this->replace = $replace;
142 return $this;
143 }
144
150 public function getErrors()
151 {
152 return $this->errors->toArray();
153 }
154
160 public function hasErrors()
161 {
162 return !$this->errors->isEmpty();
163 }
164
170 public function getId()
171 {
172 return $this->id;
173 }
174
180 public function getData()
181 {
182 return $this->data;
183 }
184
191 public function setData(array $data)
192 {
193 unset($data['ID']);
194 $this->data = $data;
195
196 $this->isAgreementTextHtml = ($this->data['IS_AGREEMENT_TEXT_HTML'] == 'Y');
197 }
198
205 public function mergeData(array $data)
206 {
207 $this->setData($data + $this->data);
208 }
209
215 public function save()
216 {
217 $this->errors->clear();
219
220 $fields = $data['FIELDS'];
221 unset($data['FIELDS']);
222
223 if(!$this->check())
224 {
225 return;
226 }
227
228 if ($this->isAgreementTextHtml)
229 {
230 (new \CBXSanitizer)->sanitizeHtml($data['AGREEMENT_TEXT']);
231 }
232
233 if($this->id)
234 {
235 $result = Internals\AgreementTable::update($this->id, $data);
236 }
237 else
238 {
239 $data['DATE_INSERT'] = new DateTime();
240 $result = Internals\AgreementTable::add($data);
241 $this->id = $result->getId();
242 }
243
244 if(!$result->isSuccess())
245 {
246 return;
247 }
248
249 Internals\FieldTable::setConsentFields($this->id, $fields);
250 }
251
257 protected function check()
258 {
260 $data['DATE_INSERT'] = new DateTime();
261
262 //$fields = $data['FIELDS'];
263 unset($data['FIELDS']);
264
265 $result = new ORM\Data\Result;
266 Internals\AgreementTable::checkFields($result, $this->id, $data);
267 if (!$result->isSuccess())
268 {
269 $this->errors->add($result->getErrors());
270 }
271
272 return $result->isSuccess();
273 }
274
280 public function isExist()
281 {
282 return $this->id > 0;
283 }
284
290 public function isActive()
291 {
292 return ($this->data['ACTIVE'] == self::ACTIVE);
293 }
294
295 public function isAgreementTextHtml(): bool
296 {
297 return $this->isAgreementTextHtml;
298 }
299
305 protected function isCustomType()
306 {
307 return ($this->data['TYPE'] != self::TYPE_STANDARD);
308 }
309
315 public function getTitle()
316 {
317 return trim($this->getTitleFromText($this->getText()));
318 }
319
320 protected static function getTitleFromText($text)
321 {
322 $text = trim($text);
323 $maxLength = 50;
324 $pos = min(
325 mb_strpos($text, "\n")?: 50,
326 mb_strpos($text, "<br>")?: 50,
327 mb_strpos($text, ".")?: 50,
328 $maxLength
329 );
330
331 return mb_substr($text, 0, $pos);
332 }
333
340 public function getText($cutTitle = false)
341 {
342 $text = $this->getContent($cutTitle);
343
344 return ($this->isAgreementTextHtml ? strip_tags($text) : $text);
345 }
346
351 public function getHtml()
352 {
353 $text = $this->getContent();
354
355 $text = ($this->isAgreementTextHtml ? $text : nl2br($text));
356 $sanitizer = new \CBXSanitizer;
357 $sanitizer->setLevel(\CBXSanitizer::SECURE_LEVEL_MIDDLE);
358 $sanitizer->allowAttributes([
359 'target' => [
360 'tag' => function ($tag)
361 {
362 return $tag === 'a';
363 },
364 'content' => function ($tag)
365 {
366 return true;
367 },
368 ]
369 ]);
370
371 return $sanitizer->sanitizeHtml($text);
372 }
373
374 private function getContent($cutTitle = false)
375 {
376 if ($this->isCustomType())
377 {
378 return $this->data['AGREEMENT_TEXT'];
379 }
380
381 $text = $this->intl->getText();
382
383 $replaceData = array();
384 $replaceData = $replaceData + $this->replace;
385 $replaceData = $replaceData + $this->getDataProviderValues();
386 $replaceData = $replaceData + $this->getReplaceFieldValues();
387
388 $text = Text::replace($text, $replaceData, true);
389 $text = trim($text);
390 if ($cutTitle)
391 {
392 $title = self::getTitleFromText($text);
393 if (mb_strlen($title) !== 50 && $title === mb_substr($text, 0, mb_strlen($title)))
394 {
395 $text = trim(mb_substr($text, mb_strlen($title)));
396 }
397 }
398
399 return $text;
400 }
401
407 public function getLabelText()
408 {
409 return str_replace('%', '', $this->getLabel());
410 }
411
417 public function getUrl()
418 {
419 return ($this->data['USE_URL'] === 'Y' && $this->data['URL'])
420 ? (new Uri($this->data['URL']))->getLocator()
421 : null;
422 }
423
429 public function getLabel()
430 {
431 $text = $this->isCustomType() ? $this->data['LABEL_TEXT'] : $this->intl->getLabelText();
432 $text = Text::replace($text, $this->replace);
433
434 if ($this->data['USE_URL'] !== 'Y' || !$this->data['URL'])
435 {
436 return str_replace('%', '', $text);
437 }
438
439 $text = trim(trim($text), "%");
440 $text = explode('%', $text);
441 $text = array_filter($text);
442
444 switch (count($text))
445 {
446 case 0:
447 case 1:
448 $text = array_merge([''], $text, ['']);
449 break;
450
451 case 2:
452 $text[] = '';
453 break;
454
455 case 3:
456 break;
457
458 default:
459 $text = array_merge(
460 array_slice($text, 0, 2),
461 [implode('', array_slice($text, 2))]
462 );
463 break;
464 }
465
466 return implode('%', $text);
467 }
468
474 protected function getDataProviderValues()
475 {
476 if (!$this->dataProvider)
477 {
478 $providerCode = $this->data['DATA_PROVIDER'] ?? null;
479 $this->dataProvider = DataProvider::getByCode($providerCode);
480 }
481
482 if ($this->dataProvider)
483 {
484 return $this->dataProvider->getData();
485 }
486
487 return array();
488 }
489
495 public function getFields()
496 {
497 $result = array();
498 $fields = $this->intl->getFields();
499 $fieldValues = FieldTable::getConsentFields($this->id);
500 foreach ($fields as $field)
501 {
502 $fieldCode = $field['CODE'];
503 $field['VALUE'] = $fieldValues[$fieldCode] ?? '';
504 $result[$fieldCode] = $field;
505 }
506
507 return $result;
508 }
509
515 public function getFieldValues()
516 {
517 $result = array();
518 $fields = $this->intl->getFields();
519 $fieldValues = FieldTable::getConsentFields($this->id);
520 foreach ($fields as $field)
521 {
522 $fieldCode = $field['CODE'];
523 $result[$fieldCode] = $fieldValues[$fieldCode] ?? '';
524 }
525
526 return $result;
527 }
528
529 protected function getReplaceFieldValues()
530 {
531 $result = $this->getFieldValues();
532 $fields = $this->intl->getFields();
533
534 // set default values to result
535 foreach ($fields as $field)
536 {
537 if (!isset($field['DEFAULT_VALUE']) || !$field['DEFAULT_VALUE'])
538 {
539 continue;
540 }
541
542 if (isset($result[$field['CODE']]) && $result[$field['CODE']])
543 {
544 continue;
545 }
546
547 $result[$field['CODE']] = $field['DEFAULT_VALUE'];
548 }
549
550 // set values to result
551 foreach ($fields as $field)
552 {
553 if ($field['TYPE'] != 'enum' || empty($field['TEXT']))
554 {
555 continue;
556 }
557
558 $fieldCode = $field['CODE'];
559
560 $valueAsText = null;
561 foreach ($field['ITEMS'] as $item)
562 {
563 // detect text item
564 if (isset($item['IS_TEXT']) && $item['IS_TEXT'])
565 {
566 continue;
567 }
568
569 if ($result[$fieldCode] == $item['CODE'])
570 {
571 $valueAsText = $item['NAME'];
572 }
573 }
574
575 if (!$valueAsText)
576 {
577 continue;
578 }
579
580 $result[$field['TEXT']] = Text::formatArrayToText(array($valueAsText));
581 }
582
583 return $result;
584 }
585}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29