1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Param.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Message;
4
5use Bitrix\Im\Text;
6use Bitrix\Main\ORM\Data\DataManager;
7use Bitrix\Main\ORM\Objectify\EntityObject;
8use Bitrix\Im\Model\EO_MessageParam;
9use Bitrix\Im\Model\MessageParamTable;
10use Bitrix\Im\V2\Result;
11use Bitrix\Im\V2\ActiveRecord;
12use Bitrix\Im\V2\RegistryEntry;
13use Bitrix\Im\V2\Common\ActiveRecordImplementation;
14use Bitrix\Im\V2\Common\RegistryEntryImplementation;
15
20{
21 use ActiveRecordImplementation
22 {
23 load as defaultLoad;
24 }
25 use RegistryEntryImplementation;
26
27 public const
28 TYPE_STRING = 'String',
29 TYPE_INT = 'Integer',
30 TYPE_BOOL = 'Boolean',
31 TYPE_STRING_ARRAY = 'ArrayString',
32 TYPE_INT_ARRAY = 'ArrayInteger',
33 TYPE_DATE_TIME = 'DateTime',
34 TYPE_JSON = 'Json'
35 ;
36
37 protected ?string $type = null;
38
39 protected ?int $paramId = null;
40
41 protected ?int $messageId = null;
42
43 protected ?string $name = null;
44
46 protected $value = null;
47
49 protected ?string $jsonValue = null;
50
52 protected $defaultValue = null;
53
57 public function __construct($source = null)
58 {
59 if (!empty($source))
60 {
61 $this->load($source);
62 }
63 }
64
68 public function load($source): Result
69 {
70 $result = $this->defaultLoad($source);
71 if ($result->isSuccess())
72 {
73 $checkType = $this->getType();
74 $this->detectType();
75 if ($this->type !== $checkType)
76 {
77 $this->setValue($this->value);
78 }
79
80 $type = Params::getType($this->name);
81 if (isset($type['loadValueFilter']) && is_callable($type['loadValueFilter']))
82 {
83 $this->value = \call_user_func($type['loadValueFilter'], $this->value);
84 }
85 }
86
87 return $result;
88 }
89
90 //region Param value
91
96 public function setValue($value): self
97 {
98 if ($value === null)
99 {
100 return $this->unsetValue();
101 }
102
103 switch ($this->type)
104 {
105 case self::TYPE_INT:
106 $this->value = (int)$value;
107 break;
108
109 case self::TYPE_BOOL:
110 if (is_string($value))
111 {
112 $this->value = $value === 'Y';
113 }
114 else
115 {
116 $this->value = (bool)$value;
117 }
118 break;
119
120 case self::TYPE_STRING:
121 $this->value = (string)$value;
122 break;
123
124 default:
125 $this->value = $value;
126 }
127
129 if ($this->value === $defaultValue)
130 {
131 return $this->unsetValue();
132 }
133
134 $this->markChanged();
135
136 return $this;
137 }
138
142 public function getDefaultValue()
143 {
144 if ($this->defaultValue === null)
145 {
146 $type = Params::getType($this->name);
147 if (isset($type['default']))
148 {
149 $value = $type['default'];
150
151 switch ($this->type)
152 {
153 case self::TYPE_INT:
154 $this->defaultValue = (int)$value;
155 break;
156
157 case self::TYPE_BOOL:
158 $this->defaultValue = (bool)$value;
159 break;
160
161 case self::TYPE_STRING:
162 $this->defaultValue = (string)$value;
163 break;
164
165 default:
166 $this->defaultValue = $value;
167 }
168 }
169 }
170
171 return $this->defaultValue;
172 }
173
177 public function hasValue(): bool
178 {
179 return $this->value !== null;
180 }
181
185 public function getValue()
186 {
187 if ($this->value === null)
188 {
189 return $this->getDefaultValue();
190 }
191 switch ($this->type)
192 {
193 case self::TYPE_INT:
194 return (int)$this->value;
195
196 case self::TYPE_BOOL:
197 if (is_string($this->value))
198 {
199 $this->value = $this->value === 'Y';
200 }
201 return (bool)$this->value;
202
203 case self::TYPE_STRING:
204 return (string)$this->value;
205
206 default:
207 return $this->value;
208 }
209 }
210
215 public function addValue($value): self
216 {
217 return $this->setValue($value);
218 }
219
223 public function unsetValue(): self
224 {
225 $this->value = null;
226 $this->markDrop();
227
228 if ($this->getRegistry())
229 {
230 unset($this->getRegistry()[$this->getName()]);
231 }
232
233 return $this;
234 }
235
236 public function isHidden(): bool
237 {
238 return Params::getType($this->name)['isHidden'] ?? false;
239 }
240
244 public function toRestFormat()
245 {
246 switch ($this->type)
247 {
248 case self::TYPE_BOOL:
249 return $this->getValue() ? 'Y' : 'N';
250
251 case self::TYPE_INT:
252 return (string)$this->getValue();
253
254 case self::TYPE_STRING:
255 default:
256 return $this->getValue();
257
258 }
259 }
260
264 public function toPullFormat()
265 {
266 return $this->toRestFormat();
267 }
268
269 //endregion
270
271 //region Setters & Getters
272
273 public function setParamId(int $paramId): self
274 {
275 if (!$this->paramId)
276 {
277 $this->paramId = $paramId;
278 }
279 return $this;
280 }
281
282 public function getParamId(): ?int
283 {
284 return $this->paramId;
285 }
286
287 public function setMessageId(int $messageId): self
288 {
289 if ($this->messageId != $messageId)
290 {
291 $this->markChanged();
292 }
293 $this->messageId = $messageId;
294 return $this;
295 }
296
297 public function getMessageId(): ?int
298 {
299 return $this->messageId;
300 }
301
302 public function setName(string $name): self
303 {
304 $name = mb_substr(trim($name), 0, 100);
305 if ($this->name != $name)
306 {
307 $this->markChanged();
308 }
309 $this->name = $name;
310 $this->detectType();
311
312 return $this;
313 }
314
315 public function getName(): ?string
316 {
317 return $this->name;
318 }
319
320 public function saveNameFilter($name): string
321 {
322 $name = $name ?? '';
323 if (is_string($name) && mb_strlen($name) > 100)
324 {
325 $name = mb_substr($name, 0, 100);
326 }
327
328 return $name;
329 }
330
331 public function setType(string $type): self
332 {
333 switch ($type)
334 {
337 break;
338
341 }
342 if ($this->type != $type)
343 {
344 $this->markChanged();
345 }
346 $this->type = $type;
347
348 return $this;
349 }
350
351 public function getType(): string
352 {
353 return $this->type ?? Param::TYPE_STRING;
354 }
355
356 public function detectType(): self
357 {
358 if (empty($this->type) && !empty($this->name))
359 {
360 $type = Params::getType($this->name);
361 $this->setType($type['type'] ?? Param::TYPE_STRING);
362 }
363
364 return $this;
365 }
366
367 public function setJsonValue($value): self
368 {
369 $this->jsonValue = $value;
370 $this->markChanged();
371
372 return $this;
373 }
374
375 public function getJsonValue()
376 {
377 return $this->jsonValue;
378 }
379
380 //endregion
381
382 //region Data storage
383
387 protected static function mirrorDataEntityFields(): array
388 {
389 return [
390 'ID' => [
391 'primary' => true,
392 'field' => 'paramId',
393 'get' => 'getParamId',
394 'set' => 'setParamId',
395 ],
396 'MESSAGE_ID' => [
397 'field' => 'messageId',
398 'set' => 'setMessageId',
399 'get' => 'getMessageId',
400 ],
401 'TYPE' => [
402 'set' => 'setType',
403 'get' => 'getType',
404 ],
405 'PARAM_NAME' => [
406 'field' => 'name',
407 'set' => 'setName',
408 'get' => 'getName',
409 'saveFilter' => 'saveNameFilter',
410 ],
411 'PARAM_VALUE' => [
412 'field' => 'value',
413 'set' => 'setValue',
414 'get' => 'getValue',
415 'saveFilter' => 'saveValueFilter',
416 'loadFilter' => 'loadValueFilter',
417 ],
418 'PARAM_JSON' => [
419 'field' => 'jsonValue',
420 'set' => 'setJsonValue',
421 'get' => 'getJsonValue',
422 'saveFilter' => 'saveJsonFilter',
423 'loadFilter' => 'loadJsonFilter',
424 ],
425 ];
426 }
427
431 public static function getDataClass(): string
432 {
433 return MessageParamTable::class;
434 }
435
439 public function getPrimaryId(): ?int
440 {
441 return $this->getParamId();
442 }
443
448 public function setPrimaryId(int $primaryId): self
449 {
450 return $this->setParamId($primaryId);
451 }
452
453 public function isValid(): Result
454 {
455 return new Result();
456 }
457
462 public function saveValueFilter($value)
463 {
464 $type = Params::getType($this->name);
465
466 if (
467 isset($type['saveValueFilter'])
468 && ($saveFilter = $type['saveValueFilter'])
469 )
470 {
471 if (is_string($saveFilter) && is_callable([$this, $saveFilter]))
472 {
473 $value = $this->$saveFilter($value);
474 }
475 elseif (is_callable($saveFilter))
476 {
477 $value = call_user_func($saveFilter, $value);
478 }
479 }
480 elseif ($type['type'] == Param::TYPE_BOOL)
481 {
482 $value = $value ? 'Y' : 'N';
483 }
484
485 if (is_string($value) && $this->shouldProcessEmoji())
486 {
488 }
489
490 if (is_string($value) && mb_strlen($value) > 100)
491 {
492 $value = mb_substr($value, 0, 97) . '...';
493 }
494
495 return $value;
496 }
497
502 public function loadValueFilter($value)
503 {
504 $type = Params::getType($this->name);
505
506 if (
507 isset($type['loadValueFilter'])
508 && ($loadFilter = $type['loadValueFilter'])
509 )
510 {
511 if (is_string($loadFilter) && is_callable([$this, $loadFilter]))
512 {
513 $value = $this->$loadFilter($value);
514 }
515 elseif (is_callable($loadFilter))
516 {
517 $value = call_user_func($loadFilter, $value);
518 }
519 }
520 elseif (($type['type'] ?? null) == Param::TYPE_BOOL) //TODO replace to normal variant
521 {
522 $value = $value == 'Y';
523 }
524
525 if (is_string($value) && $this->shouldProcessEmoji())
526 {
528 }
529
530 return $value;
531 }
532
537 public function saveJsonFilter($value)
538 {
539 return $value;
540 }
541
546 public function loadJsonFilter($value)
547 {
548 return $value;
549 }
550
551 protected function shouldProcessEmoji(): bool
552 {
553 return in_array($this->getType(), [self::TYPE_STRING, self::TYPE_STRING_ARRAY, self::TYPE_JSON]);
554 }
555
556 //endregion
557}
if($_SERVER $defaultValue['REQUEST_METHOD']==="GET" &&!empty($RestoreDefaults) && $bizprocPerms==="W" &&check_bitrix_sessid())
Определения options.php:32
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
static encodeEmoji($text)
Определения text.php:373
static decodeEmoji($text)
Определения text.php:378
setPrimaryId(int $primaryId)
Определения Param.php:448
const TYPE_BOOL
Определения Param.php:30
unsetValue()
Определения Param.php:223
setParamId(int $paramId)
Определения Param.php:273
const TYPE_DATE_TIME
Определения Param.php:33
setName(string $name)
Определения Param.php:302
getName()
Определения Param.php:315
setJsonValue($value)
Определения Param.php:367
toPullFormat()
Определения Param.php:264
__construct($source=null)
Определения Param.php:57
getPrimaryId()
Определения Param.php:439
detectType()
Определения Param.php:356
const TYPE_INT_ARRAY
Определения Param.php:32
getMessageId()
Определения Param.php:297
string $name
Определения Param.php:43
addValue($value)
Определения Param.php:215
setValue($value)
Определения Param.php:96
string $jsonValue
Определения Param.php:49
const TYPE_STRING
Определения Param.php:28
isValid()
Определения Param.php:453
getType()
Определения Param.php:351
saveNameFilter($name)
Определения Param.php:320
load($source)
Определения Param.php:68
saveJsonFilter($value)
Определения Param.php:537
int $paramId
Определения Param.php:39
getParamId()
Определения Param.php:282
const TYPE_INT
Определения Param.php:29
loadJsonFilter($value)
Определения Param.php:546
loadValueFilter($value)
Определения Param.php:502
$defaultValue
Определения Param.php:52
const TYPE_JSON
Определения Param.php:34
string $type
Определения Param.php:37
setMessageId(int $messageId)
Определения Param.php:287
getValue()
Определения Param.php:185
const TYPE_STRING_ARRAY
Определения Param.php:31
static getDataClass()
Определения Param.php:431
saveValueFilter($value)
Определения Param.php:462
toRestFormat()
Определения Param.php:244
isHidden()
Определения Param.php:236
hasValue()
Определения Param.php:177
int $messageId
Определения Param.php:41
getJsonValue()
Определения Param.php:375
static mirrorDataEntityFields()
Определения Param.php:387
getDefaultValue()
Определения Param.php:142
setType(string $type)
Определения Param.php:331
shouldProcessEmoji()
Определения Param.php:551
static getType(string $paramName)
Определения Params.php:288
Определения result.php:20
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения RegistryEntry.php:6
getRegistry()
$value
Определения Param.php:39
unsetValue()
Определения Param.php:47
setParamId(int $paramId)
Определения Param.php:75
getName()
Определения Param.php:99
const TYPE_INT_ARRAY
Определения Param.php:22
string $name
Определения Param.php:37
setValue($value)
Определения Param.php:163
string $jsonValue
Определения Param.php:38
const TYPE_STRING
Определения Param.php:17
getType()
Определения Param.php:116
int $paramId
Определения Param.php:35
getParamId()
Определения Param.php:70
getValue()
Определения Param.php:141
const TYPE_STRING_ARRAY
Определения Param.php:21
toRestFormat()
Определения Param.php:305
setType(string $type)
Определения Param.php:121
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393