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/elite/coordParser/CommimMapController.php
<?php

namespace coordParser;

class CommimMapController
{

    const ACTION_INIT = 'init';
    const ACTION_GET = 'get';
    const ACTION_SMART_FILTER = 'filter';
    const ACTION_FAVORITE = 'favorite';
    const ACTION_CHANGE_LONGITUDE = 'change_longitude';
    const ACTION_CHANGE_LATITUDE = 'change_latitude';
    const ACTION_GET_POLYGON = 'get_poly';

    private CommimAddress $commimAddress;

    private function getFromRequest(string $field)
    {
        return $_POST[$field] ?? $_GET[$field] ?? null;
    }

    private function response(int $code, array $response = []): void
    {
        header('HTTP/1.1 ' . $code);
        header('Content-type: application/json');

        echo json_encode($response, JSON_UNESCAPED_UNICODE) . PHP_EOL;

        exit;
    }

    private function errorResponse(int $code, ?string $message = null): void
    {
        $this->response($code, ['error' => $message ?? 'Неизвестная ошибка']);
    }

    private function badRequest(?string $message = null): void
    {
        $this->errorResponse(400, $message ?? 'Неверный запрос');
    }

    private function notFound(?string $message = null): void
    {
        $this->errorResponse(404, $message ?? 'Не найдено');
    }

    private function success($data = null)
    {
        $response = ['success' => true];
        if (!empty($data)) {
            $response['data'] = $data;
        }

        $this->response(200, $response);
    }


    public function __construct()
    {
        $action = $this->getFromRequest('action');
        if (!isset($action)) {
            $this->badRequest('Необходимо указать значение action');
        }

        $this->commimAddress = new CommimAddress();

        switch ($action) {
            case self::ACTION_INIT:
                $this->init();
                break;
            case self::ACTION_GET:
                $this->get();
                break;
            case self::ACTION_SMART_FILTER:
                $this->smartFilter();
                break;
            case self::ACTION_FAVORITE:
                $this->favorite();
                break;
            case self::ACTION_CHANGE_LONGITUDE:
                $this->changeLongitude();
                break;
            case self::ACTION_CHANGE_LATITUDE:
                $this->changeLatitude();
                break;
            case self::ACTION_GET_POLYGON:
                $this->getPolygon();
                break;
            default:
                $this->badRequest('Некорректное значение action');
        }
    }

    private function init(): void
    {
        $cacheFileName = __DIR__ . '/filterCacheJson.json';
        if (file_exists($cacheFileName)) {
            $response = json_decode(file_get_contents($cacheFileName));
        } else {
            $response = $this->commimAddress->getFilters();
            file_put_contents($cacheFileName, json_encode($response));
        }

        $this->success($response);
    }

    private function get(): void
    {
        $filters = [];
        $filters['type'] = $this->getFromRequest('type');
        $filters['pravo'] = $this->getFromRequest('pravo');
        $filters['encumbrance_name'] = $this->getFromRequest('encumbranceName');
        $filters['purpose_name'] = $this->getFromRequest('purposeName');
        $filters['minArea'] = $this->getFromRequest('minArea');
        $filters['maxArea'] = $this->getFromRequest('maxArea');

        $filters = array_filter($filters);

        $result = $this->commimAddress->find($filters);

        $response = [
            'count' => count($result),
            'totalCount' => $this->commimAddress->lastRequestTotalCount,
            'data' => $result,
        ];

        $this->response(200, $response);
    }

    private function smartFilter(): void
    {
        $filters = [];
        $filters['type'] = $this->getFromRequest('type');
        $filters['pravo'] = $this->getFromRequest('pravo');
        $filters['encumbrance_name'] = $this->getFromRequest('encumbranceName');
        $filters['purpose_name'] = $this->getFromRequest('purposeName');
        $filters['minArea'] = $this->getFromRequest('minArea');
        $filters['maxArea'] = $this->getFromRequest('maxArea');

        $filters = array_filter($filters);

        $filtersData = $this->commimAddress->getFilters($filters);

        $this->response(200, $filtersData);
    }

    private function checkAddress()
    {
        $objId = $this->getFromRequest('id');
        if (empty($objId)) {
            $this->badRequest('Необходимо указать id');
        }

        $address = $this->commimAddress->findOne($objId);
        if (empty($address)) {
            $this->notFound();
        }

        return $address;
    }

    private function favorite(): void
    {
        $address = $this->checkAddress();

        $choice = $this->getFromRequest('choice');
        if (!isset($choice)) {
            $this->badRequest('Необходимо указать значение choice');
        } elseif (!in_array($choice, ['0', '1'])) {
            $this->badRequest('Поле choice может принимать значения 0 и 1');
        }

        if ($address->choice == $choice) {
            $this->badRequest('Состояние адреса не поменялось');
        }

        $this->commimAddress->updateChoice($address->obj_id, (bool) $choice);

        $this->success();
    }

    private function changeLongitude(): void
    {
        $address = $this->checkAddress();

        $value = $this->getFromRequest('longitude');
        if (!isset($value)) {
            $this->badRequest('Необходимо указать значение longitude');
        } elseif (!is_numeric($value)) {
            $this->badRequest('Поле longitude должно быть дробным числом');
        }

        $this->commimAddress->updateLongitude($address->obj_id, (float) $value);

        $this->success();
    }

    private function changeLatitude(): void
    {
        $address = $this->checkAddress();

        $value = $this->getFromRequest('latitude');
        if (!isset($value)) {
            $this->badRequest('Необходимо указать значение latitude');
        } elseif (!is_numeric($value)) {
            $this->badRequest('Поле longitude должно быть дробным числом');
        }

        $this->commimAddress->updateLatitude($address->obj_id, (float) $value);

        $this->success();
    }

    private function getPolygon(): void
    {
        $address = $this->checkAddress();
        $coords = [];
        if (!empty($address->coords)) {
            $coords = json_decode($address->coords);
        }

        $this->response(200, ['coords' => $coords]);
    }

}