@params $file ['name' => 'name.png', 'type' => '', 'tmp_name' => '', 'size' => 124]
<?php
use Bitrix\UI\Avatar;
class Helper
{
{
return ' data-bx-ui-avatar-editor-info="'.htmlspecialcharsbx(self::getJson($fileId)).'" ';
}
public static function getJson($fileId): string
{
return Main\Web\Json::encode(
static::getData($fileId)
);
}
public static function getData(?
int $fileId): ?array
{
if ($fileId > 0 && ($file = \CFile::GetByID($fileId)->Fetch()))
{
$result = [
'src' => \CFile::GetFileSRC($file),
];
if ($file['VERSION_ORIGINAL_ID'] == $fileId)
{
$originalFile = \CFile::GetByID($fileId, true)->Fetch();
$maskId = null;
if (
($metaData = $file['META'] ? Main\Web\Json::decode($file['META']) : [])
&& is_array($metaData)
&& isset($metaData['maskInfo'])
&& $metaData['maskInfo']['id'] > 0
)
{
$maskId = $metaData['maskInfo']['id'];
}
'select' => ['*'],
'filter' => ['FILE_ID' => $fileId],
'limit' => 1
])->fetch())
{
$maskId = $res['ITEM_ID'];
}
$result = [
'src' => \CFile::GetFileSRC($originalFile),
'maskId' => $maskId
];
}
return $result;
}
return null;
}
public static function save(?
int $originalFileId, array $file, ?Main\Engine\CurrentUser $currentUser =
null): ?int
{
$originalFile = \CFile::GetByID($originalFileId)->Fetch();
if (!$originalFile)
{
return null;
}
$currentUser = is_null($currentUser) ? Main\Engine\CurrentUser::get() : $currentUser;
$consumer = Avatar\Mask\Consumer::createFromId($currentUser->getId());
while (((int)$originalFile['ID'] !== (int)$originalFileId))
{
\CFile::Delete($originalFile['ID']);
$originalFile = \CFile::GetByID($originalFileId)->Fetch();
}
if ($fileIdWithMask = \CFile::SaveFile($file + ['MODULE_ID' => 'ui'], 'ui/masked'))
{
$maskId = isset($file['maskInfo']) ? $file['maskInfo']['id'] : null;
if ($maskId
&& ($maskItem = Avatar\Mask\Item::getInstance($maskId))
&& $maskItem->isReadableBy($consumer)
&& \CFile::AddVersion($originalFileId, $fileIdWithMask, ['maskInfo' => ['id' => $maskId]])->isSuccess()
)
{
$maskItem->applyToFileBy($originalFileId, $fileIdWithMask, $consumer);
$consumer->useRecentlyMaskId($maskItem->getId());
return $fileIdWithMask;
}
\CFile::Delete($fileIdWithMask);
}
return null;
}
public static function getMaskedFile(string $fieldName, ?HttpRequest $request = null): ?array
{
$request = ($request ?? Main\Application::getInstance()->getContext()->getRequest());
$mask = null;
if ($id = $request->getPost(self::REQUEST_FIELD_NAME . $fieldName))
{
$id,
$request->getFile(self::REQUEST_FIELD_NAME),
$request->getPost(self::REQUEST_FIELD_NAME)
);
}
return $mask;
}
public static function getDataFromRequest(
string $fieldName, ?HttpRequest $request =
null): ?array
{
return null;
}
protected static function getMaskFromRequest($id, ?array $rawFiles, ?array $postData): ?array
{
if (!is_array($rawFiles) || !is_array($postData))
{
return null;
}
$orderedFiles = [];
array_walk($rawFiles, function($item, $subField) use (&$orderedFiles) {
foreach ($item as $key => $value)
{
$orderedFiles[$key] = $orderedFiles[$key] ?? [];
$orderedFiles[$key][$subField] = $value;
}
});
$result = null;
if (isset($orderedFiles[$id]))
{
$result = $orderedFiles[$id];
$maskInfo = ($postData[$id] ?? []);
if (isset($maskInfo['maskId']))
{
$result['maskInfo'] = ['id' => $maskInfo['maskId']];
}
}
return $result;
}
public static function addSystemMask(array $file, array $descriptionParams): ?Item
{
$result = Item::create(
new Owner\System(),
$file,
[
'GROUP_ID' => $descriptionParams['GROUP_ID'] ?? null,
'TITLE' => $descriptionParams['TITLE'] ?? null,
'DESCRIPTION' => $descriptionParams['DESCRIPTION'] ?? null,
'SORT' => $descriptionParams['SORT'] ?? 0,
]
)->getData();
return reset($result);
}
public static function setSystemGroup(
string $title, ?
string $description): ?Group
{
}
}
static getList(array $parameters=array())
static createOrGet(DefaultOwner $owner, string $title, ?string $description=null)
static addSystemMask(array $file, array $descriptionParams)
static getDataFromRequest(string $fieldName, ?HttpRequest $request=null)
static getHTMLAttribute($fileId)
static getData(?int $fileId)
static getMaskFromRequest($id, ?array $rawFiles, ?array $postData)
static save(?int $originalFileId, array $file, ?Main\Engine\CurrentUser $currentUser=null)
static setSystemGroup(string $title, ?string $description)