complexService = new Complex(); } private function json($data, $status = 200) { http_response_code($status); // header('Content-Type: application/json'); echo json_encode($data); exit; } private function getJsonInput() { return json_decode(file_get_contents('php://input'), true) || []; } // GET /home/{id}/media public function index($params) { $mediaList = $this->complexService->getHouseMedia($params['homeId']); $this->json([ 'status' => true, 'data' => $mediaList ]); } // POST /home/{id}/media public function store($params, $body) { $house_id = $params['homeId']; $input_photos = $body['files']['photos']; $photos = []; $errors = []; $allowedTypes = ['image/png', 'image/jpeg']; $allowedExts = ['png', 'jpg', 'jpeg']; $maxSize = 10 * 1024 * 1024; // 10 MB // TODO change to 10MB if (!empty($input_photos)) { foreach ($input_photos['name'] as $index => $value) { $name = $value; $tmp = $input_photos['tmp_name'][$index]; $type = $input_photos['type'][$index]; $size = $input_photos['size'][$index]; $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); $realType = mime_content_type($tmp); if ( !in_array($realType, $allowedTypes) || !in_array($ext, $allowedExts) ) { $errors[] = [ 'original_name' => $name, 'has_error' => true, 'error_message' => 'Неверный формат (допустимы PNG, JPEG)', ]; continue; } if ($size > $maxSize) { $errors[] = [ 'original_name' => $name, 'has_error' => true, 'error_message' => 'Файл слишком большой (максимум 10 МБ)', ]; continue; } $error = $input_photos['error'][$index]; if ($error !== UPLOAD_ERR_OK) { $errors[] = [ 'original_name' => $input_photos['name'][$index], 'has_error' => true, 'error_message' => 'Ошибка загрузки файла', 'can_retry' => true ]; continue; } $photos[] = [ 'name' => $name, 'tmp_name' => $tmp, 'type' => $type, 'error' => $input_photos['error'][$index], 'size' => $size, ]; } } $uploaded = $this->complexService->addMediaToHouse($house_id, $photos); $result = array_merge($uploaded, $errors); $this->json([ 'status' => true, 'data' => $result ], 201); } // DELETE /home/{homeId}/media/{mediaId} public function delete($homeId, $mediaId) { $result = $this->complexService->deleteMediaFromHouse($homeId, [$mediaId]); $this->json($result); } public function createMediaSet($params, $body) { $house_id = $params['homeId']; if (!$name = $body['fields']['name']) { return $this->json([ 'status' => false, 'message' => 'Name field is required' ]); } $mediaSet = $this->complexService->addMediaSetToHouse($house_id, $name); $this->json([ 'status' => true, 'data' => $mediaSet ]); } public function getMediaSets($params) { $mediaSets = $this->complexService->getMediaSetsForHouse($params['homeId']); $this->json([ 'status' => true, 'data' => $mediaSets ]); } public function deleteMediaSet($house_id, $set_id) { $mediaSets = $this->complexService->deleteMediaSetFromHouse($house_id, $set_id); $this->json($mediaSets); } public function updateMediaSet($params, $body) { $set_id = $params['set_id']; if (isset($body['fields']['name']) && !$body['fields']['name']) { return $this->json(['status' => false]); } $items = isset($body['fields']['items']) ? $body['fields']['items'] : []; $name = isset($body['fields']['name']) ? $body['fields']['name'] : null; $result = $this->complexService->updateMediaSet($set_id, $items, $name); $this->json($result); } public function connectSetToRooms($params, $body) { $set_id = $params['set_id']; $result = $this->complexService->addRoomsToMediaSet($set_id, $body['fields']['room_ids']); return $this->json([ 'status' => true, 'data' => $result ]); } public function getMediaSetForRoom($params) { $roomId = $params['roomId']; $set = $this->complexService->getMediaSetForRoom($roomId); return $this->json([ 'status' => true, 'data' => $set ]); } public function getHouseMediaCompletionPercentage($params) { $homeId = $params['homeId']; $completion = $this->complexService->getHouseMediaCompletionPercentage($homeId); return $this->json([ 'status' => true, 'data' => [ 'completion' => $completion ] ]); } public function findUser($sessionId) { $this->authUser = $this->complexService->findUser($sessionId); return $this->authUser; } public function getMediaThumbnails($params, $queryParams) { // $mediaId = $params['media_id']; $mediaIds = isset($queryParams['media_ids']) ? json_decode($queryParams['media_ids']) : []; return $this->json([ 'status' => true, 'data' => $this->complexService->getMediaThumbnails($mediaIds) ]); } public function findHouseComplexAgency($homeId) { return $this->complexService->findHouseComplexAgency($homeId); } }