Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
restrictiontable.php
1<?php
2
4
15
31{
37 public static function getTableName(): string
38 {
39 return 'b_messageservice_restriction';
40 }
41
47 public static function getMap(): array
48 {
49 return [
50 'ID' => (new IntegerField('ID', []))
51 ->configurePrimary(true)
52 ->configureAutocomplete(true)
53 ,
54 'CODE' => (new StringField('CODE', [
55 'validation' => function()
56 {
57 return[
58 new LengthValidator(null, 128),
59 ];
60 },
61 ]))
62 ->configureRequired(true)
63 ,
64 'COUNTER' => (new IntegerField('COUNTER', [])),
65 'DATE_CREATE' => (new DateField('DATE_CREATE', []))
66 ->configureRequired(true)
67 ,
68 'ADDITIONAL_PARAMS' => (new ArrayField('ADDITIONAL_PARAMS', []))
69 ->configureSerializeCallback(static function($value) {
70 $preparedValue = [];
71 foreach($value as $entity)
72 {
73 $preparedValue[] = "|$entity|";
74 }
75 $result = implode(' ', $preparedValue);
76
77 return $result;
78 })
79 ->configureUnserializeCallback(static function($value) {
80 if ((string)$value === '')
81 {
82 return [];
83 }
84
85 $result = [];
86 foreach(explode(' ', $value) as $entity)
87 {
88 $result[] = trim($entity, '|');
89 }
90
91 return $result;
92 })
93 ,
94 ];
95 }
96
102 public static function updateCounter(string $filteringCode, int $filteringCounter): bool
103 {
104 $entity = static::getEntity();
105 $table = static::getTableName();
106
107 $filter = Query::filter()
108 ->where('CODE', $filteringCode)
109 ->where('COUNTER','<=', $filteringCounter)
110 ->where('DATE_CREATE', new Date())
111 ;
112
113 $where = Query::buildFilterSql($entity, $filter);
114
115 if ($where !== '')
116 {
117 $where = ' WHERE ' . $where;
118 }
119
120 $helper = Application::getConnection()->getSqlHelper();
121 $tableName = $helper->quote($table);
122 $updateCounter = (new SqlExpression("?# = ?# + 1", 'COUNTER', 'COUNTER'))->compile();
123
124 $sql = "UPDATE {$tableName} SET {$updateCounter} {$where}";
125
126 Application::getConnection()->queryExecute($sql);
127
128 return Application::getConnection()->getAffectedRowsCount() === 1;
129 }
130
138 public static function updateCounterWithParam(string $code, int $limit, string $additionalParam): bool
139 {
140 $entity = static::getEntity();
141 $table = static::getTableName();
142 $encodedAdditionalParam = self::getMap()['ADDITIONAL_PARAMS']->encode([$additionalParam]);
143
144 $filter = Query::filter()
145 ->where('CODE', $code)
146 ->where('COUNTER','<=', $limit)
147 ->where('DATE_CREATE', new Date())
148 ;
149
150 $where = Query::buildFilterSql($entity, $filter);
151
152 if ($where !== '')
153 {
154 $where = ' WHERE ' . $where;
155 }
156
157 $helper = Application::getConnection()->getSqlHelper();
158 $tableName = $helper->quote($table);
159
160 // If got duplicate by code+date,
161 // when check for the same substring in ADDITIONAL_PARAMS,
162 // then don't touch COUNTER and ADDITIONAL_PARAMS,
163 // in otherwise increment COUNTER and append ADDITIONAL_PARAMS
164
165 $updateCounter = (new SqlExpression(
166 "?# = (CASE WHEN POSITION(?s IN ?#) = 0 THEN ?# + 1 ELSE ?# END)",
167 'COUNTER',
168 $encodedAdditionalParam,
169 'ADDITIONAL_PARAMS',
170 'COUNTER',
171 'COUNTER'
172 ))->compile();
173
174 $updateAdditionParams = (new SqlExpression(
175 "?# = (CASE WHEN POSITION(?s IN ?#) = 0 THEN CONCAT_WS(' ', ?#, ?s) ELSE ?# END)",
176 'ADDITIONAL_PARAMS',
177 $encodedAdditionalParam,
178 'ADDITIONAL_PARAMS',
179 'ADDITIONAL_PARAMS',
180 $encodedAdditionalParam,
181 'ADDITIONAL_PARAMS'
182 ))->compile();
183
184 $sql = "UPDATE {$tableName} SET {$updateCounter}, {$updateAdditionParams} {$where}";
185
186 Application::getConnection()->queryExecute($sql);
187
188 return Application::getConnection()->getAffectedRowsCount() === 1;
189 }
190
195 public static function insertCounter(string $code): void
196 {
197 $helper = Application::getConnection()->getSqlHelper();
198 $table = static::getTableName();
199
200 $sql = $helper->prepareMerge(
201 $table,
202 ['CODE', 'DATE_CREATE'],
203 [
204 'CODE' => $code,
205 'DATE_CREATE' => new Date(),
206 'COUNTER' => 1,
207 'ADDITIONAL_PARAMS' => '',
208 ],
209 [
210 'COUNTER' => new SqlExpression("?#.?# + 1", $table, 'COUNTER')
211 ]
212 )[0];
213
214 Application::getConnection()->queryExecute($sql);
215 }
216
222 public static function insertCounterWithParam(string $code, string $additionalParam): void
223 {
224 $helper = Application::getConnection()->getSqlHelper();
225 $table = static::getTableName();
226 $additionalParam = self::getMap()['ADDITIONAL_PARAMS']->encode([$additionalParam]);
227
228 $sql = $helper->prepareMerge(
229 $table,
230 ['CODE', 'DATE_CREATE'],
231 [
232 'CODE' => $code,
233 'DATE_CREATE' => new Date(),
234 'COUNTER' => 1,
235 'ADDITIONAL_PARAMS' => $additionalParam,
236 ],
237 [
238 'COUNTER' => new SqlExpression(
239 "(CASE WHEN POSITION(?s IN ?#.?#) = 0 THEN ?#.?# + 1 ELSE ?#.?# END)",
240 $additionalParam,
241 $table, 'ADDITIONAL_PARAMS',
242 $table, 'COUNTER',
243 $table, 'COUNTER'
244 ),
245 'ADDITIONAL_PARAMS' => new SqlExpression(
246 "(CASE WHEN POSITION(?s IN ?#.?#) = 0 THEN CONCAT_WS(' ', ?#.?#, ?s) ELSE ?#.?# END)",
247 $additionalParam,
248 $table, 'ADDITIONAL_PARAMS',
249 $table, 'ADDITIONAL_PARAMS',
250 $additionalParam,
251 $table, 'ADDITIONAL_PARAMS'
252 )
253 ]
254 )[0];
255
256 Application::getConnection()->queryExecute($sql);
257 }
258}
static getConnection($name="")
static updateCounter(string $filteringCode, int $filteringCounter)
static updateCounterWithParam(string $code, int $limit, string $additionalParam)
static insertCounterWithParam(string $code, string $additionalParam)