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-api/Models/Attachment.php
<?php

use Gumlet\ImageResize;

class Attachment extends Db_Model
{

    /** @deprecated */
    static $table_name = 'attachments_old';
    static $tableName = 'attachments';
    static $relationTable = 'attachmentable';
    static $thumbsTable = 'attachment_thumbs';

    static $defaultThumbType = 'medium_large';

    static $attachmentableTypeComplex = 'App\Models\Complex';
    static $attachmentableTypeFlat = 'App\Models\Flat';
    static $attachmentableTypeBuilding = 'App\Models\Building';

    private static function generateImagePath(string $path, string $name, string $extension): string
    {
        return ADMIN_HOST . '/storage/' . $path . $name . '.' . $extension;
    }

    private static function generateThumbImagePath(string $path, string $name, string $extension,  $type = ''): string
    {
        return ADMIN_HOST . '/storage/thumbs/' . $path . $name . '_' . $type . '.' . $extension;
    }

    /**
     * @deprecated
     */
    public static function clear($attachableType, $attachableId, $fileType, $isExternal = 1)
    {
        self::delete([
            'attachable_type' => $attachableType,
            'attachable_id' => $attachableId,
            'file_type' => $fileType,
            'is_external' => $isExternal,
        ]);
    }

    /**
     * @deprecated
     */
    public static function clearWithout($attachableType, $attachableId, $fileType, $withoutIds)
    {
        if (!empty($withoutIds)) {
            $sql = 'DELETE FROM ' . self::$table_name . ' WHERE '
                . "attachable_type = '" . $attachableType . "'"
                . " AND attachable_id = " . $attachableId
                . " AND file_type = '" . $fileType . "'"
                . " AND is_external = 1"
                . " AND id NOT IN (" . implode(',', $withoutIds) . ")";
            self::query($sql);
        } else {
            self::clear($attachableType, $attachableId, $fileType);
        }
    }

    /**
     * @deprecated
     */
    public static function getId($url, $attachableType, $attachableId)
    {
        return self::$db->select('id')->from(self::$table_name)
            ->where('file = ?', $url)
            ->where('is_external = ?', 1)
            ->where('attachable_type = ?', $attachableType)
            ->where('attachable_id = ?', $attachableId)
            ->fetchOne();
    }

    private static function getGalleryOfType($type, $entityId): array
    {
        if (empty($entityId) || empty($type)) {
            return [];
        }

        $attachments = self::$db
            ->select('a.id', 'a.path', 'a.name', 'a.extension', 't.path AS thumb_path', 't.name AS thumb_name', 't.extension AS thumb_extension', 't.type AS thumb_type')
            ->from(self::$tableName . ' a')
            ->join(self::$relationTable . ' r', 'r.attachment_id = a.id')
            ->joinLeft(self::$thumbsTable . ' t', 't.attachment_id = a.id')
            ->where('r.attachmentable_type = ?', $type)
            ->where('r.attachmentable_id = ?', $entityId)
            ->order('a.sort ASC, r.id ASC')
            ->fetchAll();

        if (empty($attachments)) {
            return [];
        }

        $images = [];
        $imagesThumbs = [];
        foreach ($attachments as $attachment) {
            $images[$attachment->id] = (object) [
                'file' => self::generateImagePath($attachment->path, $attachment->name, $attachment->extension),
                'file_thumb' => $attachment->thumb_name ? self::generateImagePath($attachment->thumb_path, $attachment->thumb_name, $attachment->thumb_extension) : null,
            ];
            $imagesThumbs[$attachment->id]['origin'] = self::generateImagePath($attachment->path, $attachment->name, $attachment->extension);
            $imagesThumbs[$attachment->id][$attachment->thumb_type] = self::generateThumbImagePath($attachment->path, $attachment->name, $attachment->extension, $attachment->thumb_type);
        }
        foreach ($imagesThumbs as $attachmentId => $thumbs) {
            $images[$attachmentId]->full_thumbs[] = $thumbs;
        }

        return $images;
    }

    public static function getComplexGallery($complexId): array
    {
        return self::getGalleryOfType(self::$attachmentableTypeComplex, $complexId);
    }

    public static function getFlatGallery($flatId): array
    {
        if (empty($flatId)) {
            return [];
        }

        $flat = Flat::find($flatId);

        if (empty($flat)) {
            return [];
        }

        $images = self::getGalleryOfType(self::$attachmentableTypeFlat, $flatId);

        if (empty($images) && !empty($flat->building_id)) {
            $images = self::getGalleryOfType(self::$attachmentableTypeBuilding, $flat->building_id);
        }

        return $images;
    }

    public static function getBuildingGallery($buildingId): array
    {
        if (empty($buildingId)) {
            return [];
        }

        $building = Building::find($buildingId);

        if (empty($building)) {
            return [];
        }

        return self::getGalleryOfType(self::$attachmentableTypeBuilding, $buildingId);
    }

    public static function getFlatPlan($flatId)
    {
        if (empty($flatId)) {
            return null;
        }

        $attachment = self::$db->select('a.id', 'a.path', 'a.name', 'a.extension', 't.path AS thumb_path', 't.name AS thumb_name', 't.extension AS thumb_extension')
            ->from(self::$tableName . ' a')
            ->join(Flat::$table_name . ' f', 'f.plan_id = a.id')
            ->joinLeft(self::$thumbsTable . ' t', 't.attachment_id = a.id AND t.type = \'' . self::$defaultThumbType . '\'')
            ->where('f.id = ?', $flatId)
            ->fetchRow();

        if (empty($attachment)) {
            return null;
        }

        return (object) [
            'file' => self::generateImagePath($attachment->path, $attachment->name, $attachment->extension),
            'file_thumb' => $attachment->thumb_name ? self::generateImagePath($attachment->thumb_path, $attachment->thumb_name, $attachment->thumb_extension) : null,
        ];
    }

    public static function getFlatFloorPlan($flatId)
    {
        return null;
    }

    public static function getBuildingFloorsPlans($buildingId): array
    {
        return [];
    }

    /**
     * @deprecated
     */
    public static function createWithThumbnail($attachment)
    {

        try {
            if ($attachment['is_external']) {
                $filePath = File::urlToTmp($attachment['file']);
                $image = new ImageResize(ROOT . '/tmp/' . $filePath);
                File::clearTmp($filePath);
            } else {
                $image = new ImageResize(ROOT . $attachment['file']);
            }

            $image->resizeToLongSide(500);
            $targetFilePath = File::generateFilePath($attachment['original_filename']);
            $image->save($targetFilePath['path']);

            $attachment['file_thumb'] = $targetFilePath['url'];
        } catch (Exception $e) {
            // Nothing
            echo $e->getMessage() . PHP_EOL;
        }

        return Attachment::create($attachment);
    }
}