1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
basecontext.php
См. документацию.
1<?php
2
4
7
13
16{
17 public const EMPTY_CONTEXT_ID = 0; // Context with no attributes.
18
19 protected $id = null;
20 protected array $attributes = [];
21
22 public function setId(?int $id): void
23 {
24 $this->id = $id;
25 }
26
27 public function getId(): ?int
28 {
29 return $this->id;
30 }
31
39 public function addCounter($day, $name, $value = null)
40 {
41 if (!($day instanceof Date))
42 throw new ArgumentTypeException('day', '\Bitrix\Main\Type\Date');
43
44 if (! is_string($name))
45 throw new ArgumentTypeException('name', 'string');
46
47 if (! is_numeric($value))
48 throw new ArgumentTypeException('value', 'numeric');
49
50 if (($id = $this->id) === null)
51 throw new SystemException('Cannot add counter without context!');
52
53 static $types;
54 if (! $types)
55 {
56 $types = CounterManager::getTypes();
57 }
58
59 if (! $type = $types[$name])
60 throw new SystemException("Undefined counter '$name' type!");
61
62 if (! $type['ACTIVE'])
63 return;
64
65 // save to database
66
67 $primary = array(
68 'DAY' => $day,
69 'CONTEXT_ID' => $id,
70 'NAME' => $name
71 );
72
73 $data = array('VALUE' => new DB\SqlExpression('?# + ?f', 'VALUE', $value));
74
75 $result = ContextCounterDayTable::update($primary, $data);
76
77 if ($result->getAffectedRowsCount() === 0)
78 {
79 try
80 {
81 $result = ContextCounterDayTable::add($primary + array('VALUE' => $value));
82 }
83 catch (DB\SqlQueryException $e)
84 {
85 $result = ContextCounterDayTable::update($primary, $data);
86 }
87 }
88
89 $result->isSuccess(); // TODO isSuccess
90 }
91
100 public function subCounter($day, $name, $value = 1)
101 {
102 return $this->addCounter($day, $name, -$value);
103 }
104
111 public function setAttribute($name, $value = null)
112 {
113 if (! is_string($name))
114 throw new ArgumentTypeException('name', 'string');
115
116 if (! (is_scalar($value) || is_null($value)))
117 throw new ArgumentTypeException('name', 'scalar');
118
119 if ($this->id !== null)
120 throw new SystemException('Cannot set attribute for existent context!');
121
122 static $types;
123 if (! $types)
124 {
126 }
127
128 if (! $type = $types[$name])
129 throw new SystemException("Undefined attribute '$name' type!");
130
131 // set attribute
132
133 $this->attributes[$name] = $value;
134 }
135
137 protected function save()
138 {
139 if (($id =& $this->id) !== null)
140 throw new SystemException('Cannot save existent context!');
141
142 $id = self::EMPTY_CONTEXT_ID;
143
144 if ($attributes = $this->attributes)
145 {
146 // leave only one attribute in group
147
148 static $groupedTypes;
149
150 if (! $groupedTypes)
151 {
152 $groupedTypes = AttributeManager::getGroupedTypes();
153 }
154
155 foreach ($groupedTypes as $types)
156 {
157 $intersection = array_intersect_key($types, $attributes);
158
159 if (count($intersection) > 1)
160 {
161 array_shift($intersection);
162
163 foreach ($intersection as $name => $type)
164 {
165 unset($attributes[$name]);
166 }
167 }
168 }
169
170 // save to database
171
172 $snapshot = self::getSnapshot($attributes);
173
174 $query = array(
175 'limit' => 1,
176 'select' => array('ID'),
177 'filter' => array('=SNAPSHOT' => $snapshot),
178 );
179
180 if ($row = ContextTable::getList($query)->fetch())
181 {
182 $id = (int) $row['ID'];
183 }
184 elseif (Option::get('conversion', 'CONTEXT_TABLE') != 'locked') // TODO remove if
185 {
186 try
187 {
188 $result = ContextTable::add(array('SNAPSHOT' => $snapshot));
189
190 if ($result->isSuccess())
191 {
192 $id = $result->getId();
193
194 foreach ($attributes as $name => $value)
195 {
196 // TODO resetContext if not success and return null!!!
197 $result = ContextAttributeTable::add(array(
198 'CONTEXT_ID' => $id,
199 'NAME' => $name,
200 'VALUE' => (string) $value, // can be null!
201 ));
202 }
203 }
204 else
205 {
206 throw new DB\SqlQueryException();
207 }
208 }
209 catch (DB\SqlQueryException $e)
210 {
211 if ($row = ContextTable::getList($query)->fetch())
212 {
213 $id = (int) $row['ID'];
214 }
215 }
216 }
217 }
218 }
219
220 static private function getSnapshot(array $attributes)
221 {
222 $keys = array();
223
224 foreach ($attributes as $name => $value)
225 {
226 $keys []= $name.':'.$value;
227 }
228
229 sort($keys);
230
231 $string = implode(';', $keys);
232
233 return md5($string).md5(strrev($string));
234 }
235}
$type
Определения options.php:106
subCounter($day, $name, $value=1)
Определения basecontext.php:100
addCounter($day, $name, $value=null)
Определения basecontext.php:39
setAttribute($name, $value=null)
Определения basecontext.php:111
static getTypes(array $filter=null)
Определения typemanager.php:49
Определения date.php:9
$data['IS_AVAILABLE']
Определения .description.php:13
</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
$query
Определения get_search.php:11
$name
Определения menu_edit.php:35
Определения arrayresult.php:2
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936