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

75 lines
2.7 KiB
PHP
Raw Normal View History

2021-03-08 13:20:56 +00:00
<?php
2021-03-08 13:52:34 +00:00
require_once "../../../bootstrap.php";
2021-03-09 13:43:39 +00:00
require_once('../Group/Group.php');
2021-03-09 14:04:59 +00:00
require_once('FileHandler.php');
2021-03-08 13:20:56 +00:00
use Backend\Models\GameData;
2021-03-17 08:43:09 +00:00
use Backend\Models\GameJam;
2021-03-09 13:43:39 +00:00
2021-03-09 12:54:20 +00:00
use Backend\Models\Group;
2021-03-08 13:20:56 +00:00
2021-03-17 08:43:09 +00:00
date_default_timezone_set("Europe/Copenhagen");
2021-03-09 12:17:18 +00:00
$isImages = false;
2021-03-09 12:30:21 +00:00
if(isLogin()){
2021-03-09 12:30:21 +00:00
2021-03-09 12:54:20 +00:00
$group = groupViaToken($_SESSION['token']);
2021-03-08 13:20:56 +00:00
2021-03-17 08:43:09 +00:00
$gameJam = GameJam::find($group->game_jam_id);
$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($group->game_data_id)){
if(isset($_POST['submitUpload'])){
//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();
$group->gameData()->associate($gameData);
2021-03-19 12:25:27 +00:00
if(! $group->save()){
2021-03-22 08:43:33 +00:00
echo http_response_code(500);
2021-03-19 12:25:27 +00:00
}else{
2021-03-22 08:43:33 +00:00
echo http_response_code(201);
2021-03-19 12:25:27 +00:00
}
2021-03-09 12:54:20 +00:00
}
2021-03-17 08:43:09 +00:00
}else{
2021-03-22 08:43:33 +00:00
echo http_response_code(400);
2021-03-08 13:20:56 +00:00
}
2021-03-17 08:43:09 +00:00
}else{
2021-03-19 12:25:27 +00:00
echo "Can only upload one file";
2021-03-22 08:43:33 +00:00
echo http_response_code(400);
2021-03-08 13:20:56 +00:00
}
2021-03-10 10:23:40 +00:00
}else{
2021-03-19 12:25:27 +00:00
echo "Can only upload when the game jam has started";
2021-03-22 08:43:33 +00:00
echo http_response_code(400);
2021-03-08 13:20:56 +00:00
}
2021-03-10 10:23:40 +00:00
}else{
2021-03-22 08:43:33 +00:00
echo http_response_code(401);
2021-03-08 13:20:56 +00:00
}