Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basecontext.php
1<?php
2
4
7
13
16{
17 const EMPTY_CONTEXT_ID = 0; // Context with no attributes.
18
19 protected $id = null;
20 protected $attributes = array();
21
29 public function addCounter($day, $name, $value = null)
30 {
31 if (!($day instanceof Date))
32 throw new ArgumentTypeException('day', '\Bitrix\Main\Type\Date');
33
34 if (! is_string($name))
35 throw new ArgumentTypeException('name', 'string');
36
37 if (! is_numeric($value))
38 throw new ArgumentTypeException('value', 'numeric');
39
40 if (($id = $this->id) === null)
41 throw new SystemException('Cannot add counter without context!');
42
43 static $types;
44 if (! $types)
45 {
46 $types = CounterManager::getTypes();
47 }
48
49 if (! $type = $types[$name])
50 throw new SystemException("Undefined counter '$name' type!");
51
52 if (! $type['ACTIVE'])
53 return;
54
55 // save to database
56
57 $primary = array(
58 'DAY' => $day,
59 'CONTEXT_ID' => $id,
60 'NAME' => $name
61 );
62
63 $data = array('VALUE' => new DB\SqlExpression('?# + ?f', 'VALUE', $value));
64
65 $result = ContextCounterDayTable::update($primary, $data);
66
67 if ($result->getAffectedRowsCount() === 0)
68 {
69 try
70 {
71 $result = ContextCounterDayTable::add($primary + array('VALUE' => $value));
72 }
73 catch (DB\SqlQueryException $e)
74 {
75 $result = ContextCounterDayTable::update($primary, $data);
76 }
77 }
78
79 $result->isSuccess(); // TODO isSuccess
80 }
81
90 public function subCounter($day, $name, $value = 1)
91 {
92 return $this->addCounter($day, $name, -$value);
93 }
94
101 public function setAttribute($name, $value = null)
102 {
103 if (! is_string($name))
104 throw new ArgumentTypeException('name', 'string');
105
106 if (! (is_scalar($value) || is_null($value)))
107 throw new ArgumentTypeException('name', 'scalar');
108
109 if ($this->id !== null)
110 throw new SystemException('Cannot set attribute for existent context!');
111
112 static $types;
113 if (! $types)
114 {
116 }
117
118 if (! $type = $types[$name])
119 throw new SystemException("Undefined attribute '$name' type!");
120
121 // set attribute
122
123 $this->attributes[$name] = $value;
124 }
125
127 protected function save()
128 {
129 if (($id =& $this->id) !== null)
130 throw new SystemException('Cannot save existent context!');
131
133
134 if ($attributes = $this->attributes)
135 {
136 // leave only one attribute in group
137
138 static $groupedTypes;
139
140 if (! $groupedTypes)
141 {
142 $groupedTypes = AttributeManager::getGroupedTypes();
143 unset($groupedTypes[null]);
144 }
145
146 foreach ($groupedTypes as $types)
147 {
148 $intersection = array_intersect_key($types, $attributes);
149
150 if (count($intersection) > 1)
151 {
152 array_shift($intersection);
153
154 foreach ($intersection as $name => $type)
155 {
156 unset($attributes[$name]);
157 }
158 }
159 }
160
161 // save to database
162
163 $snapshot = self::getSnapshot($attributes);
164
165 $query = array(
166 'limit' => 1,
167 'select' => array('ID'),
168 'filter' => array('=SNAPSHOT' => $snapshot),
169 );
170
171 if ($row = ContextTable::getList($query)->fetch())
172 {
173 $id = (int) $row['ID'];
174 }
175 elseif (Option::get('conversion', 'CONTEXT_TABLE') != 'locked') // TODO remove if
176 {
177 try
178 {
179 $result = ContextTable::add(array('SNAPSHOT' => $snapshot));
180
181 if ($result->isSuccess())
182 {
183 $id = $result->getId();
184
185 foreach ($attributes as $name => $value)
186 {
187 // TODO resetContext if not success and return null!!!
188 $result = ContextAttributeTable::add(array(
189 'CONTEXT_ID' => $id,
190 'NAME' => $name,
191 'VALUE' => (string) $value, // can be null!
192 ));
193 }
194 }
195 else
196 {
197 throw new DB\SqlQueryException();
198 }
199 }
200 catch (DB\SqlQueryException $e)
201 {
202 if ($row = ContextTable::getList($query)->fetch())
203 {
204 $id = (int) $row['ID'];
205 }
206 }
207 }
208 }
209 }
210
211 static private function getSnapshot(array $attributes)
212 {
213 $keys = array();
214
215 foreach ($attributes as $name => $value)
216 {
217 $keys []= $name.':'.$value;
218 }
219
220 sort($keys);
221
222 $string = implode(';', $keys);
223
224 return md5($string).md5(strrev($string));
225 }
226}
addCounter($day, $name, $value=null)
static getTypes(array $filter=null)