Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
numerator.php
1
<?php
2
namespace
Bitrix\Main\Numerator\Model
;
3
4
use Bitrix\Main\Entity\DataManager;
5
use Bitrix\Main\Entity\DatetimeField;
6
use Bitrix\Main\Entity\IntegerField;
7
use Bitrix\Main\Entity\StringField;
8
use Bitrix\Main\Entity\UpdateResult;
9
use
Bitrix\Main\Error
;
10
use
Bitrix\Main\Localization\Loc
;
11
use
Bitrix\Main\Numerator\Generator\Contract\Sequenceable
;
12
use
Bitrix\Main\Numerator\NumberGeneratorFactory
;
13
use
Bitrix\Main\Numerator\Numerator
;
14
use
Bitrix\Main\SystemException
;
15
use
Bitrix\Main\Type\DateTime
;
16
use
Bitrix\Main\Web\Json
;
17
35
class
NumeratorTable
extends
DataManager
36
{
40
public
static
function
getTableName
()
41
{
42
return
'b_numerator'
;
43
}
44
49
public
static
function
getMap
()
50
{
51
return
[
52
(
new
IntegerField(
'ID'
))
53
->configurePrimary(
true
)
54
->configureAutocomplete(
true
)
55
,
56
(
new
StringField(
'NAME'
))
57
->configureRequired(
true
)
58
,
59
(
new
StringField(
'TEMPLATE'
))
60
->configureRequired(
true
)
61
,
62
(
new
StringField(
'SETTINGS'
))
63
->configureRequired(
true
)
64
,
65
(
new
StringField(
'TYPE'
))
66
->configureDefaultValue(
'DEFAULT'
)
67
,
68
new
DatetimeField(
'CREATED_AT'
),
69
new
IntegerField(
'CREATED_BY'
),
70
new
DatetimeField(
'UPDATED_AT'
),
71
new
IntegerField(
'UPDATED_BY'
),
72
(
new
StringField(
'CODE'
))
73
->configureSize(255)
74
->configureUnique()
75
->configureNullable()
76
->addValidator(
static
function
($value) {
77
if
(is_null($value) || (is_string($value) && !empty($value)))
78
{
79
return
true
;
80
}
81
82
return
'CODE should be either NULL or non-empty string'
;
83
})
84
->addValidator(
static
function
($value, $primary) {
85
if
(!is_string($value))
86
{
87
return
true
;
88
}
89
90
$existingId =
self::getIdByCode
($value);
91
if
(!$existingId)
92
{
93
// no rows with this code exists
94
return
true
;
95
}
96
97
$id = (int)($primary[
'ID'
] ?? 0);
98
if
($id > 0 && $id === $existingId)
99
{
100
return
true
;
101
}
102
103
return
'Entry with the same CODE already exists'
;
104
})
105
,
106
];
107
}
108
112
private
static
function
getCurrentUserId()
113
{
114
global $USER;
115
$userId = 0;
116
if
($USER && is_object($USER) && $USER->isAuthorized())
117
{
118
$userId = $USER->getID();
119
}
120
return
$userId;
121
}
122
131
public
static
function
getNumeratorList
($type, $sort)
132
{
133
$filter = [
'=TYPE'
=> $type];
134
if
($type ==
'ALL'
)
135
{
136
$filter = [];
137
}
138
$params = [
139
'select'
=> [
'*'
],
140
'filter'
=> $filter,
141
];
142
if
($sort)
143
{
144
$params[
'order'
] = $sort;
145
}
146
$results = NumeratorTable::getList($params)->fetchAll();
147
foreach
($results as &$numerator)
148
{
149
$numerator[
'id'
] = $numerator[
'ID'
];
150
$numerator[
'name'
] = $numerator[
'NAME'
];
151
$numerator[
'template'
] = $numerator[
'TEMPLATE'
];
152
$numerator[
'type'
] = $numerator[
'TYPE'
];
153
$numerator[
'code'
] = $numerator[
'CODE'
];
154
}
155
return
$results;
156
}
157
167
public
static
function
saveNumerator
($numeratorId, $numeratorFields)
168
{
169
$fields = [
170
'NAME'
=> $numeratorFields[
'NAME'
],
171
'TEMPLATE'
=> $numeratorFields[
'TEMPLATE'
],
172
'TYPE'
=> $numeratorFields[
'TYPE'
],
173
'SETTINGS'
=> Json::encode($numeratorFields[
'SETTINGS'
]),
174
'UPDATED_AT'
=>
new
DateTime
(),
175
'UPDATED_BY'
=> static::getCurrentUserId(),
176
'CODE'
=> $numeratorFields[
'CODE'
] ??
null
,
177
];
178
if
($numeratorId)
179
{
180
if
(!(
Numerator::load
($numeratorId)))
181
{
182
$result =
new
UpdateResult();
183
$result->addError(
new
Error
(
Loc::getMessage
(
'MAIN_NUMERATOR_EDIT_NUMERATOR_NOT_FOUND_ERROR'
)));
184
return
$result;
185
}
186
187
$updateRes = NumeratorTable::update($numeratorId, $fields);
188
189
if
($updateRes->isSuccess())
190
{
191
$numerator =
Numerator::load
($numeratorId);
192
$isNewNumSequential = $numerator->hasSequentialNumber();
193
if
(!$isNewNumSequential)
194
{
195
NumeratorSequenceTable::deleteByNumeratorId
($numeratorId);
196
}
197
}
198
return
$updateRes;
199
}
200
$fields[
'CREATED_AT'
] =
new
DateTime
();
201
$fields[
'CREATED_BY'
] = static::getCurrentUserId();
202
return
NumeratorTable::add($fields);
203
}
204
212
public
static
function
loadSettings
($numeratorId)
213
{
214
$numerator = static::getList([
215
'select'
=> [
'*'
,],
216
'filter'
=> [
'=ID'
=> $numeratorId],
217
])->fetch();
218
219
if
($numerator)
220
{
221
$result = [];
222
$result[
Numerator::getType
()] = [
223
'idFromDb'
=> $numerator[
'ID'
],
224
'name'
=> $numerator[
'NAME'
],
225
'template'
=> $numerator[
'TEMPLATE'
],
226
'type'
=> $numerator[
'TYPE'
],
227
'code'
=> $numerator[
'CODE'
],
228
];
229
$numeratorGenerators = Json::decode($numerator[
'SETTINGS'
]);
230
$numberGeneratorFactory =
new
NumberGeneratorFactory
();
231
foreach
($numeratorGenerators as $generatorType => $numeratorGenerator)
232
{
233
$class = $numberGeneratorFactory->getClassByType($generatorType);
234
if
(in_array(Sequenceable::class, class_implements($class)))
235
{
236
$numeratorGenerators[$generatorType][
'numeratorId'
] = $numeratorId;
237
}
238
}
239
return
array_merge($result, $numeratorGenerators);
240
}
241
242
return
$numerator;
243
}
244
245
public
static
function
getIdByCode
($code): ?int
246
{
247
$code = (string)$code;
248
if
(empty($code))
249
{
250
return
null
;
251
}
252
253
$row =
254
static::query()
255
->setSelect([
'ID'
])
256
->where(
'CODE'
, $code)
257
->setLimit(1)
258
->setCacheTtl(3600)
259
->fetch()
260
;
261
262
if
($row && isset($row[
'ID'
]))
263
{
264
return
(
int
)$row[
'ID'
];
265
}
266
267
return
null
;
268
}
269
}
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\Numerator\Model\NumeratorSequenceTable\deleteByNumeratorId
static deleteByNumeratorId($id)
Definition
numeratorsequence.php:103
Bitrix\Main\Numerator\Model\NumeratorTable
Definition
numerator.php:36
Bitrix\Main\Numerator\Model\NumeratorTable\loadSettings
static loadSettings($numeratorId)
Definition
numerator.php:212
Bitrix\Main\Numerator\Model\NumeratorTable\getMap
static getMap()
Definition
numerator.php:49
Bitrix\Main\Numerator\Model\NumeratorTable\getIdByCode
static getIdByCode($code)
Definition
numerator.php:245
Bitrix\Main\Numerator\Model\NumeratorTable\getNumeratorList
static getNumeratorList($type, $sort)
Definition
numerator.php:131
Bitrix\Main\Numerator\Model\NumeratorTable\saveNumerator
static saveNumerator($numeratorId, $numeratorFields)
Definition
numerator.php:167
Bitrix\Main\Numerator\Model\NumeratorTable\getTableName
static getTableName()
Definition
numerator.php:40
Bitrix\Main\Numerator\NumberGeneratorFactory
Definition
numbergeneratorfactory.php:12
Bitrix\Main\Numerator\Numerator
Definition
numerator.php:26
Bitrix\Main\Numerator\Numerator\getType
static getType()
Definition
numerator.php:621
Bitrix\Main\Numerator\Numerator\load
static load($numeratorId, $source=null)
Definition
numerator.php:369
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Main\Web\Json
Definition
json.php:11
Bitrix\Main\Numerator\Generator\Contract\Sequenceable
Definition
sequenceable.php:11
Bitrix\Main\Numerator\Model
Definition
numerator.php:2
modules
main
lib
numerator
model
numerator.php
Создано системой
1.10.0