Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
globalvartable.php
1<?php
2
4
8
9\Bitrix\Bizproc\Workflow\Type\GlobalsManager::loadLanguageFile();
10
28{
29 public static function getTableName(): string
30 {
31 return 'b_bp_global_var';
32 }
33
34 public static function getMap(): array
35 {
36 return [
37 'ID' => [
38 'data_type' => 'string',
39 'primary' => true,
40 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_ID_TITLE'),
41 ],
42 'NAME' => [
43 'data_type' => 'string',
44 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_NAME_TITLE'),
45 ],
46 'DESCRIPTION' => [
47 'data_type' => 'string',
48 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_DESCRIPTION_TITLE'),
49 ],
50 'PROPERTY_TYPE' => [
51 'data_type' => 'string',
52 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_PROPERTY_TYPE_TITLE'),
53 ],
54 'IS_REQUIRED' => [
55 'data_type' => 'boolean',
56 'values' => ['N', 'Y'],
57 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_IS_REQUIRED_TITLE'),
58 ],
59 'IS_MULTIPLE' => [
60 'data_type' => 'boolean',
61 'values' => ['N', 'Y'],
62 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_IS_MULTIPLE_TITLE'),
63 ],
64 'PROPERTY_OPTIONS' => [
65 'data_type' => 'string',
66 'serialized' => true,
67 'validation' => [__CLASS__, 'validateTextField'],
68 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_PROPERTY_OPTIONS_TITLE'),
69 ],
70 'PROPERTY_SETTINGS' => [
71 'data_type' => 'string',
72 'serialized' => true,
73 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_PROPERTY_SETTINGS_TITLE'),
74 ],
75 'PROPERTY_VALUE' => [
76 'data_type' => 'string',
77 'serialized' => true,
78 'validation' => [__CLASS__, 'validateTextField'],
79 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_PROPERTY_VALUE_TITLE'),
80 ],
81 'CREATED_DATE' => [
82 'data_type' => 'datetime',
83 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_CREATED_DATE_TITLE'),
84 ],
85 'CREATED_BY' => [
86 'data_type' => 'integer',
87 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_CREATED_BY_TITLE'),
88 ],
89 'VISIBILITY' => [
90 'data_type' => 'string',
91 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_VISIBILITY_TITLE'),
92 ],
93 'MODIFIED_DATE' => [
94 'data_type' => 'datetime',
95 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_MODIFIED_DATE_TITLE'),
96 ],
97 'MODIFIED_BY' => [
98 'data_type' => 'integer',
99 'title' => Main\Localization\Loc::getMessage('BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_MODIFIED_BY_TITLE'),
100 ],
101 ];
102 }
103
104 public static function validateTextField()
105 {
106 return [
107 function($value, $primary, $row, \Bitrix\Main\ORM\Fields\Field $field)
108 {
109 $errorMsg = Main\Localization\Loc::getMessage(
110 'BIZPROC_WF_TYPE_GLOBALS_MANAGER_COLUMN_PROPERTIES_TEXT_FIELD_LENGTH_ERROR',
111 ['#FIELD_TITLE#' => $field->getTitle()]
112 );
113
114 return strlen(serialize($value)) < 65535 ? true : $errorMsg;
115 }
116 ];
117 }
118
124 public static function upsertByProperty(string $varId, array $property, int $userId = null)
125 {
126 $property = static::normalizePropertyForUpsert($property, $userId);
127
128 // PROPERTY_SETTINGS ?
129 $fields = [
130 'NAME' => trim($property['Name']),
131 'DESCRIPTION' => $property['Description'],
132 'PROPERTY_TYPE' => $property['Type'],
133 'IS_REQUIRED' => $property['Required'] ? 'Y' : 'N',
134 'IS_MULTIPLE' => $property['Multiple'] ? 'Y' : 'N',
135 'PROPERTY_OPTIONS' => $property['Options'],
136 'PROPERTY_VALUE' => $property['Default'],
137 'VISIBILITY' => $property['Visibility'],
138 'CREATED_BY' => $property['CreatedBy'],
139 'CREATED_DATE' => $property['CreatedDate'],
140 'MODIFIED_DATE' => $property['ModifiedDate'],
141 'MODIFIED_BY' => $property['ModifiedBy'],
142 ];
143
144 if ($userId === null) // update by robot
145 {
146 unset($fields['CREATED_BY'], $fields['CREATED_DATE'], $fields['MODIFIED_BY'], $fields['MODIFIED_DATE']);
147 }
148
149 $oldProperty = static::getByPrimary($varId)->fetch();
150 if ($oldProperty)
151 {
152 if (isset($oldProperty['CREATED_BY']))
153 {
154 unset($fields['CREATED_BY'], $fields['CREATED_DATE']);
155 }
156
157 $result = static::update($varId, $fields);
158 }
159 else
160 {
161 $fields['ID'] = $varId;
162 $result = static::add($fields);
163 }
164
165 return $result;
166 }
167
168 public static function convertToProperty(array $fields): array
169 {
170 // Settings ?
171 return [
172 'Name' => $fields['NAME'],
173 'Description' => $fields['DESCRIPTION'],
174 'Type' => $fields['PROPERTY_TYPE'],
175 'Required' => \CBPHelper::getBool($fields['IS_REQUIRED']),
176 'Multiple' => \CBPHelper::getBool($fields['IS_MULTIPLE']),
177 'Options' => $fields['PROPERTY_OPTIONS'],
178 'Default' => $fields['PROPERTY_VALUE'],
179 'CreatedBy' => (int)$fields['CREATED_BY'],
180 'CreatedDate' => $fields['CREATED_DATE'],
181 'Visibility' => $fields['VISIBILITY'],
182 'ModifiedBy' => (int)$fields['MODIFIED_BY'],
183 'ModifiedDate' => $fields['MODIFIED_DATE'],
184 ];
185 }
186
187 private static function normalizePropertyForUpsert($property, int $userId = null): array
188 {
189 $normalized = [];
190 $normalizedAsField = FieldType::normalizeProperty($property);
191
192 $normalized['Visibility'] = isset($property['Visibility']) ? (string)$property['Visibility'] : 'GLOBAL';
193 $normalized['ModifiedBy'] = $userId;
194 $normalized['CreatedBy'] = $userId;
195 $normalized['ModifiedDate'] = new Main\Type\DateTime();
196 $normalized['CreatedDate'] = $normalized['ModifiedDate'];
197
198 return array_merge($normalized, $normalizedAsField);
199 }
200
201 public static function onBeforeUpdate(Event $event)
202 {
203 $result = new Main\ORM\EventResult();
204 $result->unsetFields(['PROPERTY_TYPE', 'IS_REQUIRED', 'IS_MULTIPLE', 'VISIBILITY']);
205
206 return $result;
207 }
208}
static normalizeProperty($property)
static upsertByProperty(string $varId, array $property, int $userId=null)