Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Helper.php
1<?php
3
6use Bitrix\UI\Avatar;
7
8class Helper
9{
10 public const REQUEST_FIELD_NAME = 'ui_avatar_editor';
11 public static function getHTMLAttribute($fileId)
12 {
13 return ' data-bx-ui-avatar-editor-info="'.htmlspecialcharsbx(self::getJson($fileId)).'" ';
14 }
15
16 public static function getJson($fileId): string
17 {
18 return Main\Web\Json::encode(
19 static::getData($fileId)
20 );
21 }
22
23 public static function getData(?int $fileId): ?array
24 {
25 if ($fileId > 0 && ($file = \CFile::GetByID($fileId)->Fetch()))
26 {
27 $result = [
28 // 'name' => $file['FILE_NAME'],
29 // 'width' => $file['WIDTH'],
30 // 'height' => $file['HEIGHT'],
31 // 'size' => $file['FILE_SIZE'],
32 // 'type' => $file['CONTENT_TYPE'],
33 'src' => \CFile::GetFileSRC($file),
34 // 'meta' => $file['META']
35 ];
36
37 if ($file['VERSION_ORIGINAL_ID'] == $fileId)
38 {
39 $originalFile = \CFile::GetByID($fileId, true)->Fetch();
40 $maskId = null;
41 if (
42 ($metaData = $file['META'] ? Main\Web\Json::decode($file['META']) : [])
43 && is_array($metaData)
44 && isset($metaData['maskInfo'])
45 && $metaData['maskInfo']['id'] > 0
46 )
47 {
48 $maskId = $metaData['maskInfo']['id'];
49 }
50 else if ($res = Avatar\Model\ItemToFileTable::getList([
51 'select' => ['*'],
52 'filter' => ['FILE_ID' => $fileId],
53 'limit' => 1
54 ])->fetch())
55 {
56 $maskId = $res['ITEM_ID'];
57 }
58
59 $result = [
60 // 'name' => $originalFile['FILE_NAME'],
61 // 'width' => $originalFile['WIDTH'],
62 // 'height' => $originalFile['HEIGHT'],
63 // 'size' => $originalFile['FILE_SIZE'],
64 // 'type' => $originalFile['CONTENT_TYPE'],
65 'src' => \CFile::GetFileSRC($originalFile),
66 'maskId' => $maskId
67 ];
68 }
69 return $result;
70 }
71 return null;
72 }
73
74 public static function save(?int $originalFileId, array $file, ?Main\Engine\CurrentUser $currentUser = null): ?int
75 {
76 $originalFile = \CFile::GetByID($originalFileId)->Fetch();
77 if (!$originalFile)
78 {
79 return null;
80 }
81 $currentUser = is_null($currentUser) ? Main\Engine\CurrentUser::get() : $currentUser;
82 $consumer = Avatar\Mask\Consumer::createFromId($currentUser->getId());
83 while (((int)$originalFile['ID'] !== (int)$originalFileId))
84 {
85 \CFile::Delete($originalFile['ID']);
86 $originalFile = \CFile::GetByID($originalFileId)->Fetch();
87 }
88 if ($fileIdWithMask = \CFile::SaveFile($file + ['MODULE_ID' => 'ui'], 'ui/masked'))
89 {
90 $maskId = isset($file['maskInfo']) ? $file['maskInfo']['id'] : null;
91 if ($maskId
92 && ($maskItem = Avatar\Mask\Item::getInstance($maskId))
93 && $maskItem->isReadableBy($consumer)
94 && \CFile::AddVersion($originalFileId, $fileIdWithMask, ['maskInfo' => ['id' => $maskId]])->isSuccess()
95 )
96 {
97 $maskItem->applyToFileBy($originalFileId, $fileIdWithMask, $consumer);
98 $consumer->useRecentlyMaskId($maskItem->getId());
99 return $fileIdWithMask;
100 }
101 \CFile::Delete($fileIdWithMask);
102 }
103 return null;
104 }
105
106 public static function getMaskedFile(string $fieldName, ?HttpRequest $request = null): ?array
107 {
109 $request = ($request ?? Main\Application::getInstance()->getContext()->getRequest());
110 $mask = null;
111 if ($id = $request->getPost(self::REQUEST_FIELD_NAME . $fieldName))
112 {
114 $id,
115 $request->getFile(self::REQUEST_FIELD_NAME),
116 $request->getPost(self::REQUEST_FIELD_NAME)
117 );
118 }
119 return $mask;
120 }
121
128 public static function getDataFromRequest(string $fieldName, ?HttpRequest $request = null): ?array
129 {
130 return null;
131 }
132 protected static function getMaskFromRequest($id, ?array $rawFiles, ?array $postData): ?array
133 {
134 if (!is_array($rawFiles) || !is_array($postData))
135 {
136 return null;
137 }
138
139 $orderedFiles = [];
140 array_walk($rawFiles, function($item, $subField) use (&$orderedFiles) {
141 foreach ($item as $key => $value)
142 {
143 $orderedFiles[$key] = $orderedFiles[$key] ?? [];
144 $orderedFiles[$key][$subField] = $value;
145 }
146 });
147 $result = null;
148 if (isset($orderedFiles[$id]))
149 {
150 $result = $orderedFiles[$id];
151 $maskInfo = ($postData[$id] ?? []);
152 if (isset($maskInfo['maskId']))
153 {
154 $result['maskInfo'] = ['id' => $maskInfo['maskId']];
155 }
156 }
157 return $result;
158 }
177 public static function addSystemMask(array $file, array $descriptionParams): ?Item
178 {
179 $result = Item::create(
180 new Owner\System(),
181 $file,
182 [
183 'GROUP_ID' => $descriptionParams['GROUP_ID'] ?? null,
184 'TITLE' => $descriptionParams['TITLE'] ?? null,
185 'DESCRIPTION' => $descriptionParams['DESCRIPTION'] ?? null,
186 'SORT' => $descriptionParams['SORT'] ?? 0,
187 ]
188 )->getData();
189
190 return reset($result);
191 }
192
193 public static function setSystemGroup(string $title, ?string $description): ?Group
194 {
195 return Group::createOrGet(new Owner\System(), $title, $description);
196 }
197}
static getList(array $parameters=array())
static createOrGet(DefaultOwner $owner, string $title, ?string $description=null)
Definition Group.php:73
static addSystemMask(array $file, array $descriptionParams)
Definition Helper.php:177
static getDataFromRequest(string $fieldName, ?HttpRequest $request=null)
Definition Helper.php:128
static getHTMLAttribute($fileId)
Definition Helper.php:11
static getData(?int $fileId)
Definition Helper.php:23
static getMaskFromRequest($id, ?array $rawFiles, ?array $postData)
Definition Helper.php:132
static save(?int $originalFileId, array $file, ?Main\Engine\CurrentUser $currentUser=null)
Definition Helper.php:74
static setSystemGroup(string $title, ?string $description)
Definition Helper.php:193
static getJson($fileId)
Definition Helper.php:16