1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
basecontext.php
См. документацию.
1
<?php
2
3
namespace
Bitrix\Conversion\Internals
;
4
5
use
Bitrix\Conversion\CounterManager
;
6
use
Bitrix\Conversion\AttributeManager
;
7
8
use
Bitrix\Main\DB
;
9
use
Bitrix\Main\Type\Date
;
10
use
Bitrix\Main\Config\Option
;
11
use
Bitrix\Main\SystemException
;
12
use
Bitrix\Main\ArgumentTypeException
;
13
15
class
BaseContext
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
{
125
$types =
AttributeManager::getTypes
();
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
$type
Определения
options.php:106
Bitrix\Conversion\AttributeManager
Определения
attributemanager.php:6
Bitrix\Conversion\AttributeManager\getGroupedTypes
static getGroupedTypes()
Определения
attributemanager.php:12
Bitrix\Conversion\CounterManager
Определения
countermanager.php:6
Bitrix\Conversion\Internals\BaseContext
Определения
basecontext.php:16
Bitrix\Conversion\Internals\BaseContext\setId
setId(?int $id)
Определения
basecontext.php:22
Bitrix\Conversion\Internals\BaseContext\subCounter
subCounter($day, $name, $value=1)
Определения
basecontext.php:100
Bitrix\Conversion\Internals\BaseContext\getId
getId()
Определения
basecontext.php:27
Bitrix\Conversion\Internals\BaseContext\EMPTY_CONTEXT_ID
const EMPTY_CONTEXT_ID
Определения
basecontext.php:17
Bitrix\Conversion\Internals\BaseContext\addCounter
addCounter($day, $name, $value=null)
Определения
basecontext.php:39
Bitrix\Conversion\Internals\BaseContext\setAttribute
setAttribute($name, $value=null)
Определения
basecontext.php:111
Bitrix\Conversion\Internals\BaseContext\$attributes
array $attributes
Определения
basecontext.php:20
Bitrix\Conversion\Internals\BaseContext\$id
$id
Определения
basecontext.php:19
Bitrix\Conversion\Internals\BaseContext\save
save()
Определения
basecontext.php:137
Bitrix\Conversion\Internals\TypeManager\getTypes
static getTypes(array $filter=null)
Определения
typemanager.php:49
Bitrix\Main\ArgumentTypeException
Определения
ArgumentTypeException.php:9
Bitrix\Main\Config\Option
Определения
option.php:15
Bitrix\Main\DB\SqlExpression
Определения
sqlexpression.php:21
Bitrix\Main\DB\SqlQueryException
Определения
sqlqueryexception.php:9
Bitrix\Main\SystemException
Определения
SystemException.php:9
Bitrix\Main\Type\Date
Определения
date.php:9
$data
$data['IS_AVAILABLE']
Определения
.description.php:13
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
$result
$result
Определения
get_property_values.php:14
$query
$query
Определения
get_search.php:11
$name
$name
Определения
menu_edit.php:35
Bitrix\Conversion\Internals
Определения
basecontext.php:3
Bitrix\Main\DB
Определения
arrayresult.php:2
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
count
</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
bitrix
modules
conversion
lib
internals
basecontext.php
Создано системой
1.14.0