Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
workgrouptasksfiltermigration.php
1<?php
2
4
10
11Loc::loadMessages(__FILE__);
12
14{
15 protected static $moduleId = 'socialnetwork';
16 protected static $filterOptionCategory = 'main.ui.filter';
17 protected static $sourceFilterOptionName = '';
18 protected static $targetFilterOptionName = '';
19 protected static $stepperNeedOptionName = '';
20 protected static $stepperDataOptionName = '';
21
22 public function execute(array &$result)
23 {
24 global $DB;
25
26 if (
27 !(
28 static::$sourceFilterOptionName !== ''
29 && static::$targetFilterOptionName !== ''
30 && static::$stepperNeedOptionName !== ''
31 && static::$stepperDataOptionName !== ''
32 && Loader::includeModule(static::$moduleId)
33 && Loader::includeModule('tasks')
34 && Option::get('socialnetwork', static::$stepperNeedOptionName, 'Y') === 'Y'
35 )
36 )
37 {
38 return false;
39 }
40
41 $return = false;
42
43 $params = Option::get('socialnetwork', static::$stepperDataOptionName);
44
45 $params = ($params !== '' ? @unserialize($params, [ 'allowed_classes' => false ]) : []);
46
47 $params = (is_array($params) ? $params : []);
48
49 if (empty($params))
50 {
51 $params = [
52 'lastId' => 0,
53 'number' => 0,
54 'count' => $this->getCount(),
55 ];
56 }
57
58 if ($params['count'] > 0)
59 {
60 $result['title'] = Loc::getMessage('FUPD_WORKGROUP_TASKS_FILTER_MIGRATION_TITLE');
61 $result['progress'] = 1;
62 $result['steps'] = '';
63 $result['count'] = $params['count'];
64
65 $strSql = "
66 SELECT ID, USER_ID, COMMON, VALUE
67 FROM b_user_option
68 WHERE
69 ID > " . (int)$params['lastId'] . "
70 AND CATEGORY = '" . $DB->ForSql(static::$filterOptionCategory) . "'
71 AND NAME = '" . $DB->ForSql(static::$sourceFilterOptionName) . "'
72 ORDER BY ID ASC
73 ";
74 $res = $DB->Query($strSql);
75
76 $found = false;
77 while ($userOptionFields = $res->fetch())
78 {
79 $userId = (int)$userOptionFields['USER_ID'];
80 $common = ($userOptionFields['COMMON'] === 'Y');
81 $value = $userOptionFields['VALUE'];
82
83 $params['number']++;
84 $params['lastId'] = $userOptionFields['ID'];
85
86 $data = @unserialize($value, [ 'allowed_classes' => false ]);
87 if (!is_array($data))
88 {
89 continue;
90 }
91
92 $data = $this->migrate($data, $userId);
93
94 \CUserOptions::SetOption(
95 static::$filterOptionCategory,
96 static::$targetFilterOptionName,
97 $data,
98 $common,
99 $userId
100 );
101
102 $found = true;
103 }
104
105 if ($found)
106 {
107 Option::set('socialnetwork', static::$stepperDataOptionName, serialize($params));
108 $return = true;
109 }
110
111 $result['progress'] = (int)($params['number'] * 100 / $params['count']);
112 $result['steps'] = $params['number'];
113
114 if ($found === false)
115 {
116 Option::delete('socialnetwork', [ 'name' => static::$stepperDataOptionName ]);
117 Option::set('socialnetwork', static::$stepperNeedOptionName, 'N');
118 }
119 }
120
121 return $return;
122 }
123
124 protected function getCount(): int
125 {
126 $counter = 0;
127
128 $res = \CUserOptions::getList(
129 [],
130 [
131 'CATEGORY' => static::$filterOptionCategory,
132 'NAME' => static::$sourceFilterOptionName,
133 ]
134 );
135
136 while ($res->fetch())
137 {
138 $counter++;
139 }
140
141 return $counter;
142 }
143
144 protected function migrate(array $data = [], int $userId = 0): array
145 {
146 $newData = [];
147
148 foreach ($data as $key => $value)
149 {
150 if ($key === 'default_presets')
151 {
152 $newData[$key] = $this->migratePresets($value, $userId);
153 }
154 elseif ($key === 'filters')
155 {
156 $newData[$key] = $this->migrateFilters($value);
157 }
158 elseif ($key === 'default')
159 {
160 $newData[$key] = $this->migratePresetCode($value);
161 }
162 else
163 {
164 $newData[$key] = $value;
165 }
166 }
167
168 return $newData;
169 }
170
171 protected function migratePresets(array $presetsData = [], int $userId = 0): array
172 {
173 static $isExtranetInstalled = null;
174
175 if ($isExtranetInstalled === null)
176 {
177 $isExtranetInstalled = Loader::includeModule('extranet');
178 }
179
180 $newPresetsData = [];
181 $modelPresetData = [];
182
183 if ($userId > 0)
184 {
185 $extranetSiteId = '';
186
187 if (
188 $isExtranetInstalled
189 && !\CExtranet::isIntranetUser(SITE_ID, $userId)
190 )
191 {
192 $extranetSiteId = \CExtranet::getExtranetSiteID();
193 }
194
195 $modelPresetData = \Bitrix\Socialnetwork\Integration\Main\UIFilter\Workgroup::getFilterPresetList([
196 'currentUserId' => $userId,
197 'extranetSiteId' => $extranetSiteId,
198 ]);
199 }
200
201 foreach ($presetsData as $presetCode => $preset)
202 {
203 $newPresetCode = $this->migratePresetCode($presetCode);
204 $newPresetsData[$newPresetCode] = $this->migratePreset($preset);
205 }
206
207 return $this->mergeData($modelPresetData, $newPresetsData);
208 }
209
210 protected function migratePresetCode(string $presetCode = ''): string
211 {
212 $newPresetCode = $presetCode;
213
214 switch ($presetCode)
215 {
216 case 'my':
217 $newPresetCode = 'my';
218 break;
219 case 'active_project':
220 $newPresetCode = 'active';
221 break;
222 case 'inactive_project':
223 $newPresetCode = 'archive';
224 break;
225 default:
226 }
227
228 return $newPresetCode;
229 }
230
231 protected function migratePreset(array $presetData = []): array
232 {
233 $newData = [];
234
235 foreach ($presetData as $key => $value)
236 {
237 if ($key === 'fields')
238 {
239 $newData[$key] = $this->migrateFields($value);
240 }
241 elseif ($key !== 'default')
242 {
243 $newData[$key] = $value;
244 }
245 }
246
247 return $newData;
248 }
249
250 protected function migrateFilters(array $filtersData = []): array
251 {
252 $newFiltersData = [];
253
254 foreach ($filtersData as $filterCode => $filter)
255 {
256 $newFilterCode = $this->migratePresetCode($filterCode);
257 $newFiltersData[$newFilterCode] = $this->migrateFilter($filter);
258 }
259
260 return $newFiltersData;
261 }
262
263 protected function migrateFilter(array $filterData = []): array
264 {
265 $newData = [];
266
267 foreach ($filterData as $key => $value)
268 {
269 if ($key === 'fields')
270 {
271 $newData[$key] = $this->migrateFields($value);
272 }
273 elseif ($key === 'filter_rows')
274 {
275 $newData[$key] = $this->migrateFilterRows((string)$value);
276 }
277 else
278 {
279 $newData[$key] = $value;
280 }
281 }
282
283 return $newData;
284 }
285
286 protected function migrateFilterRows(string $value = ''): string
287 {
288 $newFilterRowsList = [];
289
290 $filterRowsList = explode(',', $value);
291 foreach ($filterRowsList as $filterRow)
292 {
293 switch ($filterRow)
294 {
295 case 'MEMBER_ID':
296 $newFilterRowsList[] = 'MEMBER';
297 break;
298 case 'OWNER_ID':
299 $newFilterRowsList[] = 'OWNER';
300 break;
301 case 'TAGS':
302 $newFilterRowsList[] = 'TAG';
303 break;
304 case 'TYPE':
305 $newFilterRowsList[] = 'VISIBLE';
306 $newFilterRowsList[] = 'OPENED';
307 $newFilterRowsList[] = 'PROJECT';
308 $newFilterRowsList[] = 'EXTRANET';
309 $newFilterRowsList[] = 'LANDING';
310 break;
311 default:
312 $newFilterRowsList[] = $filterRow;
313 }
314 }
315
316 return implode(',', $newFilterRowsList);
317 }
318
319 protected function migrateFields(array $fieldsData = []): array
320 {
321 $newData = [];
322
323 foreach ($fieldsData as $key => $value)
324 {
325 $this->migrateFieldValue($value, $key, $newData);
326 }
327
328 return $newData;
329 }
330
331 protected function migrateFieldKey(string $key = ''): string
332 {
333 $newKey = $key;
334
335 switch ($key)
336 {
337 case 'MEMBER_ID':
338 $newKey = 'MEMBER';
339 break;
340 case 'MEMBER_ID_label':
341 $newKey = 'MEMBER_label';
342 break;
343 case 'OWNER_ID':
344 $newKey = 'OWNER';
345 break;
346 case 'OWNER_ID_label':
347 $newKey = 'OWNER_label';
348 break;
349 case 'TAGS':
350 $newKey = 'TAG';
351 break;
352 case 'TAGS_label':
353 $newKey = 'TAG_label';
354 break;
355 default:
356 }
357
358 return $newKey;
359 }
360
361 protected function migrateFieldValue($value, string $key = '', array &$newData = []): void
362 {
363 switch ($key)
364 {
365 case 'MEMBER_ID':
366 case 'OWNER_ID':
367 $newData[$this->migrateFieldKey($key)] = (!empty($value) ? 'U' . $value : '');
368 break;
369 case 'TYPE':
370 $this->migrateTypeField((string)$value, $newData);
371 break;
372 default:
373 $newData[$this->migrateFieldKey($key)] = $value;
374 }
375 }
376
377 protected function migrateTypeField(string $projectType, array &$newData = []): void
378 {
379 static $typesList = null;
380
381 if ($typesList === null)
382 {
383 $typesList = Helper\Workgroup::getTypes();
384 }
385
386 if (isset($typesList[$projectType]))
387 {
388 if (isset($typesList[$projectType]['VISIBLE']))
389 {
390 $newData['VISIBLE'] = $typesList[$projectType]['VISIBLE'];
391 }
392 if (isset($typesList[$projectType]['OPENED']))
393 {
394 $newData['OPENED'] = $typesList[$projectType]['OPENED'];
395 }
396 if (isset($typesList[$projectType]['PROJECT']))
397 {
398 $newData['PROJECT'] = $typesList[$projectType]['PROJECT'];
399 }
400 if (isset($typesList[$projectType]['EXTERNAL']))
401 {
402 $newData['EXTRANET'] = $typesList[$projectType]['EXTERNAL'];
403 }
404 if (isset($typesList[$projectType]['LANDING']))
405 {
406 $newData['LANDING'] = $typesList[$projectType]['LANDING'];
407 }
408 }
409 }
410
411 protected function mergeData($modelPresetData, $newPresetsData)
412 {
413 foreach ($modelPresetData as $code => $modelPreset)
414 {
415 if (!isset($newPresetsData[$code]))
416 {
417 $newPresetsData[$code] = $modelPreset;
418 }
419 }
420
421 return $newPresetsData;
422 }
423}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29