Game-Jaming/Backend/Controllers/FileHandler/upload.php

91 lines
3.4 KiB
PHP

<?php
require_once "../../../bootstrap.php";
require_once('../Group/Group.php');
require_once('FileHandler.php');
use Backend\Models\GameData;
use Backend\Models\GameJam;
use Backend\Models\Registration;
use Backend\Models\Group;
date_default_timezone_set("Europe/Copenhagen");
$isImages = false;
if (isLogin()) {
if (isset($_POST['submitUpload'])) {
$group = groupViaToken($_SESSION['token']);
$gameJam = GameJam::find($_POST['gameJamId']);
if ($gameJam === null) {
http_response_code(400);
echo json_encode(["message" => "gameJam not found"]);
exit();
}
$registration = Registration::where('game_jam_id', $gameJam->id)->where("group_id", $group->id)->first();
if ($registration === null) {
http_response_code(401);
echo json_encode(["message" => "group not registered for that game jam"]);
exit();
}
$gameJamStartTime = strtotime($gameJam->start_time);
$gameJamEndTime = strtotime($gameJam->end_time);
$date = date('Y/m/d H:i:s', time());
$currentTime = strtotime($date);
if ($gameJamStartTime <= $currentTime && $gameJamEndTime >= $currentTime) {
if (!isset($registration->game_data_id)) {
//Get the data from the user form
$gameFile = $_FILES['gameFile'];
$desc = $_POST['description'];
$title = $_POST['gameTitle'];
$thumbnail = $_FILES['thumbnailFile'];
$isWebBased = isset($_POST['isWebBased']);
$gameFileName = $_FILES['gameFile']['name'];//Game name
$gameFileTmp = $_FILES['gameFile']['tmp_name'];//Tmp location of the file
$gameFileError = $_FILES['gameFile']['error'];//File error
$thumbnailFileName = $_FILES['thumbnailFile']['name'];//Game name
$thumbnailFileTmp = $_FILES['thumbnailFile']['tmp_name'];//Tmp location of the file
$thumbnailFileError = $_FILES['thumbnailFile']['error'];//File error
if ($gameFileError === 0) {
$gameData = new GameData();
$gameData->game_name = $title;
$gameData->game_link = ZipFileHandler($gameFileName, $gameFileTmp);
$gameData->description = $desc;
if (isset($thumbnail) && $thumbnailFileError === 0) {
$gameData->img = imagesFileHandler($thumbnailFileName, $thumbnailFileTmp);
}
$gameData->is_web_Based = $isWebBased;
$gameData->save();
$registration->gameData()->associate($gameData);
if (!$registration->save()) {
http_response_code(500);
} else {
http_response_code(201);
}
} else {
http_response_code(500);
}
} else {
http_response_code(400);
echo json_encode(["message" => "file already uploaded "]);
}
} else {
http_response_code(400);
echo json_encode(["message" => "Can only upload when the game jam has started"]);
}
} else {
http_response_code(400);
}
} else {
http_response_code(401);
echo json_encode(["message" => "is not login"]);
}