HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/limestate-admin/app/Models/ExternalImage.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Orchid\Attachment\Models\Attachment;

/**
 * @property int $id
 * @property string $file
 * @property ?int $file_type
 * @property ?int $attachable_id
 * @property ?string $attachable_type
 * @property ?string $original_filename
 * @property ?string $alt
 * @property ?string $title
 * @property ?string $created_at
 * @property ?string $process
 * @property ?int $sort
 * @property ?int $is_external
 * @property ?int $file_thumb
 * @property ?string $region_district_id
 * @property bool $checked
 * @property ?int $new_id
 */
class ExternalImage extends Model
{

    protected $table = 'attachments_old';

    public $timestamps = false;

    const FILE_TYPE_PHOTO = 'photo';
    const FILE_TYPE_PLAN = 'plan';

    const ATTACHABLE_TYPE_COMPLEX = 'Complex';
    const ATTACHABLE_TYPE_BUILDING = 'Building';
    const ATTACHABLE_TYPE_FLAT = 'Flat';

    protected $fillable = [
        'file',
        'file_type',
        'attachable_id',
        'attachable_type',
        'original_filename',
        'alt',
        'title',
        'created_at',
        'process',
        'sort',
        'is_external',
        'file_thumb',
        'region_district_id',
        'checked',
        'new_id',
    ];

    public static function createFromAttachment(Attachment $attachment, int $attachableId, string $attachableType, string $fileType = self::FILE_TYPE_PHOTO): void
    {
        $externalImage = new ExternalImage();
        $externalImage->file = $attachment->url();
        $externalImage->file_type = $fileType;
        $externalImage->attachable_id = $attachableId;
        $externalImage->attachable_type = $attachableType;
        $externalImage->original_filename = $attachment->original_name;
        $externalImage->is_external = true;
        $externalImage->checked = true;
        $externalImage->new_id = $attachment->id;
        $externalImage->created_at = date('Y-m-d H:i:s');
        $externalImage->save();
    }

    public static function syncNewAttachments(string $attachableType, int $entityId, array $attachmentsIds, string $fileType = self::FILE_TYPE_PHOTO): void
    {
        if (empty($attachmentsIds)) {
            return;
        }

        if (self::FILE_TYPE_PLAN === $fileType) {
            self::query()
                ->where('file_type', $fileType)
                ->where('attachable_type', $attachableType)
                ->where('attachable_id', $entityId)
                ->delete();
        }

        $externalImages = self::query()
            ->where('attachable_type', $attachableType)
            ->where('file_type', $fileType)
            ->whereIn('new_id', $attachmentsIds)
            ->get()->toArray();
        $externalImagesIds = array_column($externalImages, 'new_id');
        $attachmentsIds = array_filter($attachmentsIds, fn($attachment) => !in_array($attachment, $externalImagesIds));
        if (!empty($attachmentsIds)) {
            $attachments = Attachment::query()->whereIn('id', $attachmentsIds)->get();
            foreach ($attachments as $attachment) {
                self::createFromAttachment($attachment, $entityId, $attachableType, $fileType);
            }
        }
    }


    public static function getFlatGallery(Flat $flat): Collection
    {
        $gallery = ExternalImage::query()->select(['file', 'file_thumb'])
            ->where('attachable_type', ExternalImage::ATTACHABLE_TYPE_FLAT)
            ->where('file_type', ExternalImage::FILE_TYPE_PHOTO)
            ->where('is_external', 1)
            ->where('attachable_id', $flat->id)
            ->get();
        if (empty($gallery)) {
            $gallery = ExternalImage::query()->select(['file', 'file_thumb'])
                ->where('attachable_type', ExternalImage::ATTACHABLE_TYPE_BUILDING)
                ->where('file_type', ExternalImage::FILE_TYPE_PHOTO)
                ->where('is_external', 1)
                ->where('attachable_id', $flat->building_id)
                ->get();
        }

        return $gallery;
    }

}