Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
setting.php
1<?php
2
4
7
12{
13 public const VERSION = 2;
14 public const SETTING_MANIFEST = 'SETTING_MANIFEST';
15 public const MANIFEST_CODE = 'MANIFEST_CODE';
16 public const SETTING_RATIO = 'SETTING_RATIO';
17 public const SETTING_APP_INFO = 'APP_INFO';
18 public const SETTING_USER_ID = 'USER_ID';
19 public const SETTING_UNINSTALL_APP_CODE = 'UNINSTALL_APP_CODE';
20 public const SETTING_EXPORT_ARCHIVE_NAME = 'EXPORT_ARCHIVE_NAME';
21 public const SETTING_ACTION_INFO = 'ACTION_INFO';
22 public const SETTING_ACTION_ADDITIONAL_OPTION = 'ACTION_ADDITIONAL_OPTION';
23 public const SETTING_NOTICE_COLLECTION = 'NOTICE_COLLECTION';
24 public const SETTING_FINISH_DATA = 'FINISH_DATA';
25 private const TTL_CONTEXT = 259200;
26
27 private $context = 'null';
28 private $multipleCode = [];
29
33 public function __construct($context)
34 {
35 if ($context != '')
36 {
37 $context = preg_replace('/[^a-zA-Z0-9_]/', '', $context);
38 $this->context = $context;
39 }
40 }
41
46 public function getContext(): string
47 {
48 return $this->context;
49 }
50
51 public function addMultipleCode($code): bool
52 {
53 $this->multipleCode[] = $code;
54 return true;
55 }
56
57 public function getMultipleCode()
58 {
59 return $this->multipleCode;
60 }
61
70 public function set($code, $data)
71 {
72 $id = 0;
73 if (!in_array($code, $this->multipleCode, true))
74 {
75 $res = StorageTable::getList(
76 [
77 'filter' => [
78 '=CONTEXT' => $this->context,
79 '=CODE' => $code,
80 ],
81 ]
82 );
83 if ($item = $res->fetch())
84 {
85 StorageTable::deleteFile($item);
86 $id = $item['ID'];
87 }
88 }
89
90 $save = [
91 'CREATE_TIME' => new DateTime(),
92 'CONTEXT' => $this->context,
93 'CODE' => $code,
94 'DATA' => $data
95 ];
96 if ($id > 0)
97 {
98 $result = StorageTable::update($id, $save);
99 }
100 else
101 {
102 $result = StorageTable::add($save);
103 }
104
105 return $result->isSuccess();
106 }
107
114 public function get($code)
115 {
116 $result = null;
117 $now = new DateTime();
118 $isMultiple = in_array($code, $this->multipleCode, true);
119
120 $res = StorageTable::getList(
121 [
122 'filter' => [
123 '=CONTEXT' => $this->context,
124 '=CODE' => $code,
125 ],
126 ]
127 );
128 while ($item = $res->fetch())
129 {
130 $item['CREATE_TIME']->add(self::TTL_CONTEXT . 'second');
131 if ($item['CREATE_TIME'] > $now)
132 {
133 if (!$isMultiple)
134 {
135 $result = $item['DATA'];
136 break;
137 }
138
139 $result[$item['ID']] = $item['DATA'];
140 }
141 else
142 {
143 StorageTable::deleteFile($item);
144 StorageTable::delete($item['ID']);
145 }
146 }
147
148 return $result;
149 }
150
156 public function delete($code)
157 {
158 StorageTable::deleteByFilter(
159 [
160 '=CONTEXT' => $this->context,
161 '=CODE' => $code,
162 ]
163 );
164
165 return true;
166 }
167
171 public function deleteFull()
172 {
173 StorageTable::deleteByFilter(
174 [
175 '=CONTEXT' => $this->context,
176 ]
177 );
178
179 return true;
180 }
181}