Files
2025-08-02 16:30:27 +02:00

206 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\GraphQLBundle\ApiShared\Util;
use KupShop\GraphQLBundle\ApiAdmin\Types\MutateResponse;
use KupShop\GraphQLBundle\ApiAdmin\Types\PhotoTranslationInput;
use KupShop\GraphQLBundle\ApiShared\ApiUtil;
use KupShop\GraphQLBundle\ApiShared\Types\Photo\Input\PhotoInput;
use KupShop\GraphQLBundle\ApiShared\Types\Photo\Input\PhotosInput;
use KupShop\I18nBundle\Translations\PhotosTranslation;
use KupShop\KupShopBundle\Util\ObjectUtil;
use Query\Operator;
use Symfony\Contracts\Service\Attribute\Required;
class PhotoUtil
{
public const OBJECT_PRODUCT = 'product';
public const OBJECT_SELLER = 'seller';
private PhotosTranslation $photosTranslation;
#[Required]
public function setPhotosTranslation(PhotosTranslation $photosTranslation): void
{
$this->photosTranslation = $photosTranslation;
}
public function updateObjectPhotos(string $object, int $objectId, array|PhotosInput $photosInput): void
{
$photos = $photosInput;
$delete = false;
if ($photosInput instanceof PhotosInput) {
$photos = ObjectUtil::isPropertyInitialized($photosInput, 'photos') ? $photosInput->photos : null;
if (ObjectUtil::isPropertyInitialized($photosInput, 'delete')) {
$delete = (bool) $photosInput->delete;
}
}
sqlGetConnection()->transactional(function () use ($object, $objectId, $photos, $delete) {
$tableName = "photos_{$object}s_relation";
if ($delete) {
sqlQueryBuilder()
->delete($tableName)
->where(Operator::equals(['id_'.$object => $objectId]))
->execute();
}
if ($photos === null) {
return;
}
foreach ($photos as $key => $photo) {
// try to download, create and insert photos relation
try {
if (!($photoId = $this->createOrUpdatePhoto($photo))) {
return;
}
$position = $photo->position === null ? 999 : $photo->position;
$showInLead = $position == 0 ? 'Y' : 'N';
$existsAndX = [
'id_photo' => $photoId,
'id_'.$object => $objectId,
];
if ($object === self::OBJECT_PRODUCT) {
$existsAndX['id_variation'] = null;
}
$exists = sqlQueryBuilder()
->select('id_photo')
->from("photos_{$object}s_relation")
->where(Operator::equalsNullable($existsAndX))
->execute()->fetchOne();
if ($exists) {
sqlQueryBuilder()
->update("photos_{$object}s_relation")
->directValues(
[
'show_in_lead' => $showInLead,
'position' => $position,
]
)
->where(Operator::equalsNullable($existsAndX))
->execute();
} else {
sqlQueryBuilder()
->insert("photos_{$object}s_relation")
->directValues(
[
'id_photo' => $photoId,
'id_'.$object => $objectId,
'show_in_lead' => $showInLead,
'position' => $position,
]
)
->execute();
}
} catch (\Throwable) {
}
}
\Photos::checkLeadPhoto("photos_{$object}s_relation", 'id_'.$object, $objectId);
});
}
public function createOrUpdatePhoto(PhotoInput $photo): ?int
{
if (!($photoId = $this->getDownloader()->importProductImage($photo->url, true))) {
return null;
}
$update = [];
if (ObjectUtil::isPropertyInitialized($photo, 'description')) {
$update['descr'] = $photo->description ?: '';
}
if (ObjectUtil::isPropertyInitialized($photo, 'centerPoint')) {
$update['date_update'] = (new \DateTime())->format('Y-m-d H:i:s');
$update['data'] = [
'center_point' => [
'x' => $photo->centerPoint?->x,
'y' => $photo->centerPoint?->y,
],
];
}
if (!empty($update)) {
if (!empty($update['data'])) {
$update['data'] = json_encode($this->getUpdatedPhotoData($photoId, $update['data']));
}
sqlQueryBuilder()
->update('photos')
->directValues($update)
->where(Operator::equals(['id' => $photoId]))
->execute();
if (!empty($update['date_update'])) {
$this->clearPhotoThumbnails($photoId);
}
}
return $photoId;
}
public function translatePhoto(PhotoTranslationInput $input): MutateResponse
{
ApiUtil::validateTranslationLanguage($input->language);
if (ObjectUtil::isPropertyInitialized($input, 'descr')) {
$photoTranslation['descr'] = $input->descr;
}
$result = true;
if (!empty($photoTranslation)) {
$result = $this->photosTranslation->saveSingleObject(
$input->language,
$input->photoId,
$photoTranslation
);
}
return new MutateResponse($result);
}
private function clearPhotoThumbnails(int $photoId): void
{
$photo = new \Photos();
$photo->newImage($photoId);
$photo->clearThumbnails();
}
private function getUpdatedPhotoData(int $photoId, array $updateData): array
{
$data = sqlQueryBuilder()
->select('data')
->from('photos')
->where(Operator::equals(['id' => $photoId]))
->sendToMaster()
->execute()->fetchOne();
$data = json_decode($data ?: '', true) ?: [];
return array_merge($data, $updateData);
}
private function getDownloader(): \Downloader
{
static $downloader;
if (!$downloader) {
$downloader = new \Downloader();
$downloader->setMethod('curl');
}
return $downloader;
}
}