Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
3
9
10abstract class Base
11{
12 protected static ?bool $bitrix24Included = null;
13
14 protected static array $errors = [];
15
16 public static function create(array $config): Main\Result
17 {
18 static::clearErrors();
19 static::checkRequiredModules();
20 if (!static::isSuccess())
21 {
22 return static::getErrorResult();
23 }
24
25 $config = static::verifyConfig($config);
26 if (!static::isSuccess())
27 {
28 return static::getErrorResult();
29 }
30
31 return static::internalCreate($config);
32 }
33
34 abstract protected static function internalCreate(array $config): Main\Result;
35
36 protected static function verifyConfig(array $config): array
37 {
38 return $config;
39 }
40
41 public static function isAllowed(): bool
42 {
43 return true;
44 }
45
46 protected static function addError(string $error): void
47 {
48 $error = trim($error);
49 if ($error !== '')
50 {
51 self::$errors[] = $error;
52 }
53 }
54
55 protected static function getErrors(): array
56 {
57 return self::$errors;
58 }
59
60 protected static function clearErrors(): void
61 {
62 self::$errors = [];
63 }
64
65 protected static function isSuccess(): bool
66 {
67 return empty(self::$errors);
68 }
69
70 protected static function getErrorResult(): Main\Result
71 {
72 $result = new Main\Result();
73 foreach (static::getErrors() as $value)
74 {
75 $result->addError(new Main\Error($value));
76 }
77 static::clearErrors();
78 return $result;
79 }
80
81 protected static function checkRequiredModules(): void
82 {
83 if (self::$bitrix24Included === null)
84 {
85 self::$bitrix24Included = Loader::includeModule('bitrix24');
86 }
87 }
88
89 protected static function isBitrix24(): bool
90 {
93 }
94
95 public static function getDefaultSettings(): ?array
96 {
97 return [];
98 }
99
105 protected static function getModuleTasks(string $moduleId, array $filter = []): array
106 {
107 $result = [];
108
109 $filter['=MODULE_ID'] = $moduleId;
110
111 $iterator = TaskTable::getList([
112 'select' => [
113 'ID',
114 'LETTER',
115 ],
116 'filter' => $filter,
117 ]);
118 while ($row = $iterator->fetch())
119 {
120 $result[$row['LETTER']] = $row['ID'];
121 }
122
123 return $result;
124 }
125
130 protected static function loadUserFieldRow(array $field): ?array
131 {
132 $iterator = Main\UserFieldTable::getList([
133 'select' => ['*'],
134 'filter' => [
135 '=ENTITY_ID' => $field['ENTITY_ID'],
136 '=FIELD_NAME' => $field['FIELD_NAME'],
137 ],
138 ]);
139 $row = $iterator->fetch();
140 unset($iterator);
141 if (!empty($row))
142 {
143 $row['ID'] = (int)$row['ID'];
144
145 return $row;
146 }
147
148 return null;
149 }
150
151 protected static function isExistsUserFieldRow(array $field): bool
152 {
153 return (static::loadUserFieldRow($field) !== null);
154 }
155
160 protected static function createUserField(array $field): Main\Result
161 {
162 $result = new Main\Result();
163
164 $userField = new \CUserTypeEntity();
165
166 $row = static::loadUserFieldRow($field);
167 $id = 0;
168 if (!empty($row))
169 {
170 $row['ID'] = (int)$row['ID'];
171 if ($userField->Update($row['ID'], $field))
172 {
173 $id = $row['ID'];
174 }
175 }
176 else
177 {
178 $id = (int)$userField->Add($field);
179 }
180 unset($row);
181 if ($id <= 0)
182 {
183 $application = UserFieldHelper::getInstance()->getApplication();
184 $exception = $application->GetException();
185 $error = $exception instanceof \CAdminException
186 ? $exception->GetString()
188 'BX_CATALOG_PRODUCT_SYSTEMFIELD_BASE_ERR_CREATE_UF_COMMON',
189 ['#FIELD_NAME#' => $field['FIELD_NAME']]
190 )
191 ;
192 $result->addError(new Main\Error(
193 $error,
194 $field['FIELD_NAME']
195 ));
196 unset($error, $exception);
197 }
198 else
199 {
200 $result->setData(['ID' => $id]);
201 }
202
203 return $result;
204 }
205
206
207 public static function getGridAction(array $config): ?array
208 {
209 static::clearErrors();
210 static::checkRequiredModules();
211 if (!static::isSuccess())
212 {
213 return null;
214 }
215
216 return static::internalGridAction($config);
217 }
218
219 protected static function internalGridAction(array $config): ?array
220 {
221 return null;
222 }
223
224 protected static function getEmptyListValueDescription(): array
225 {
226 return [
227 'VALUE' => '0',
228 'NAME' => Loc::getMessage('CATALOG_PRODUCT_SYSTEMFIELD_TYPE_BASE_EMPTY_LIST_VALUE'),
229 ];
230 }
231}
static getModuleTasks(string $moduleId, array $filter=[])
Definition base.php:105
static isExistsUserFieldRow(array $field)
Definition base.php:151
static internalGridAction(array $config)
Definition base.php:219
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29