2021-05-31 16:33:02 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Intaro\RetailCrm\Repository;
|
|
|
|
|
|
|
|
use CFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FileRepository
|
|
|
|
* @package Intaro\RetailCrm\Repository
|
|
|
|
*/
|
|
|
|
class FileRepository
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $defaultServerName;
|
2024-04-01 09:34:11 +03:00
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $domainCatalogList = [];
|
2021-05-31 16:33:02 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* FileRepository constructor.
|
|
|
|
* @param string $defaultServerName
|
|
|
|
*/
|
|
|
|
public function __construct(string $defaultServerName)
|
|
|
|
{
|
|
|
|
$this->defaultServerName = $defaultServerName;
|
2024-04-01 09:34:11 +03:00
|
|
|
$this->domainCatalogList = SiteRepository::getDomainList();
|
2021-05-31 16:33:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $fileId
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-04-01 09:34:11 +03:00
|
|
|
public function getImageUrl(?int $fileId, ?int $iBlockId = null): string
|
2021-05-31 16:33:02 +03:00
|
|
|
{
|
|
|
|
if (!$fileId) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$pathImage = CFile::GetPath($fileId);
|
|
|
|
$validation = '/^(http|https):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i';
|
2024-04-01 09:34:11 +03:00
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
if ((bool) preg_match($validation, $pathImage) === false) {
|
2024-04-01 09:34:11 +03:00
|
|
|
return $this->getServerName($iBlockId) . $pathImage;
|
2021-05-31 16:33:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pathImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $product
|
|
|
|
* @param string $pictureProp
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-04-01 09:34:11 +03:00
|
|
|
public function getProductPicture(array $product, string $pictureProp = '', ?int $iBlockId = null): string
|
2021-05-31 16:33:02 +03:00
|
|
|
{
|
|
|
|
$picture = '';
|
|
|
|
$pictureId = $product['PROPERTY_' . $pictureProp . '_VALUE'] ?? null;
|
|
|
|
|
|
|
|
if (isset($product['DETAIL_PICTURE'])) {
|
2024-04-01 09:34:11 +03:00
|
|
|
$picture = $this->getImageUrl($product['DETAIL_PICTURE'], $iBlockId);
|
2021-05-31 16:33:02 +03:00
|
|
|
} elseif (isset($product['PREVIEW_PICTURE'])) {
|
2024-04-01 09:34:11 +03:00
|
|
|
$picture = $this->getImageUrl($product['PREVIEW_PICTURE'], $iBlockId);
|
2021-05-31 16:33:02 +03:00
|
|
|
} elseif ($pictureId !== null) {
|
2024-04-01 09:34:11 +03:00
|
|
|
$picture = $this->getImageUrl($pictureId, $iBlockId);
|
2021-05-31 16:33:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $picture ?? '';
|
|
|
|
}
|
2024-04-01 09:34:11 +03:00
|
|
|
|
|
|
|
public function getServerName(?int $iBlockId): string
|
|
|
|
{
|
|
|
|
return $this->domainCatalogList[$iBlockId] ?? $this->defaultServerName;
|
|
|
|
}
|
2021-05-31 16:33:02 +03:00
|
|
|
}
|