Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
numerator.php
1<?php
3
4use Bitrix\Main\Entity\DataManager;
5use Bitrix\Main\Entity\DatetimeField;
6use Bitrix\Main\Entity\IntegerField;
7use Bitrix\Main\Entity\StringField;
8use Bitrix\Main\Entity\UpdateResult;
17
35class 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 {
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}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static saveNumerator($numeratorId, $numeratorFields)
static load($numeratorId, $source=null)