Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ActivityStorage.php
1<?php
2
4
6
8{
9 private static $instances = [];
10
11 private $tplId;
12 private $name;
13 private $values;
14
15 public static function getInstance(int $tplId, string $name): self
16 {
17 $cacheKey = $tplId . '|' . $name;
18
19 if (!isset(self::$instances[$cacheKey]))
20 {
21 self::$instances[$cacheKey] = new self($tplId, $name);
22 }
23
24 return self::$instances[$cacheKey];
25 }
26
27 private function __construct(int $tplId, string $name)
28 {
29 $this->tplId = $tplId;
30 $this->name = $name;
31 }
32
33 private function __clone()
34 {
35 }
36
37 public function getValue(string $key)
38 {
39 $row = $this->getAll()[$key] ?? null;
40
41 return $row ? $row['value'] : null;
42 }
43
44 public function setValue(string $key, $value): self
45 {
46 $row = $this->getAll()[$key] ?? null;
47
48 if ($row)
49 {
50 if ($value === null)
51 {
52 Entity\ActivityStorageTable::delete($row['id']);
53 unset($this->values[$key]);
54 }
55 else
56 {
57 $this->values[$key]['value'] = $value;
58 Entity\ActivityStorageTable::update($row['id'], ['KEY_VALUE' => Json::encode($value)]);
59 }
60 }
61 else
62 {
63 $this->addValue($key, $value);
64 }
65
66 return $this;
67 }
68
69 protected function getAll()
70 {
71 if ($this->values === null)
72 {
73 $this->values = [];
74 $listResult = Entity\ActivityStorageTable::getList([
75 'filter' => [
76 '=WORKFLOW_TEMPLATE_ID' => $this->tplId,
77 '=ACTIVITY_NAME' => $this->name
78 ]
79 ]);
80
81 foreach ($listResult as $item)
82 {
83 $this->values[$item['KEY_ID']] = [
84 'id' => $item['ID'],
85 'value' => Json::decode($item['KEY_VALUE'])
86 ];
87 }
88 }
89
90 return $this->values;
91 }
92
93 protected function addValue(string $key, $value)
94 {
95 $result = Entity\ActivityStorageTable::add([
96 'WORKFLOW_TEMPLATE_ID' => $this->tplId,
97 'ACTIVITY_NAME' => $this->name,
98 'KEY_ID' => $key,
99 'KEY_VALUE' => Json::encode($value)
100 ]);
101
102 $id = $result->getId();
103
104 if (is_array($this->values))
105 {
106 $this->values[$key] = [
107 'id' => $id,
108 'value' => $value
109 ];
110 }
111
112 return $id;
113 }
114
115 public static function onAfterTemplateDelete(int $id)
116 {
117 $listResult = Entity\ActivityStorageTable::getList([
118 'filter' => [
119 '=WORKFLOW_TEMPLATE_ID' => $id,
120 ],
121 'select' => ['ID']
122 ]);
123
124 foreach ($listResult as $item)
125 {
126 Entity\ActivityStorageTable::delete($item['ID']);
127 }
128 }
129}
static getInstance(int $tplId, string $name)
static decode($data)
Definition json.php:53
static encode($data, $options=null)
Definition json.php:24