<?php
/**
 * class slide_controller
 *
 * Description for class slide_controller
 *
 * @author:
*/
namespace area\presenter\controller;

use core\file;
use core\presentation;
use core\response;
use core\url;

class slide_controller extends _controller {
	private $p = null;

	public function __construct() {
		global $__routing;

		parent::__construct();

		$presentationId = isset($__routing[1]["pid"]) ? $__routing[1]["pid"] : "";
		$slideId = isset($__routing[1]["id"]) ? $__routing[1]["id"] : "";

		$this->p = new presentation($presentationId, $slideId);
		if(!$this->p->existsOwner()) {
			$this->viewError("404");
		}
	}

	public function create_action() {
		$slideId = $this->p->slideCreate();
		if($slideId === false) {
			response::json(array("status" => false));
		}

		response::json(array("status" => true, "board_id" => $slideId));
	}

	public function copy_action() {
		$slideId = $this->p->slideCopy();
		if($slideId === false) {
			response::json(array("status" => false));
		}

		response::json(array("status" => true, "board" => $slideId));
	}

	public function save_action() {
		$result = $this->p->slideSave();
		if($result === false) {
			response::json(array("status" => false));
		}

		$this->p->loadSlideData();

		$show = $this->p->slideShow();
		if($show === false) {
			$show = "";
		}

		response::json(array("status" => true, "preview" => $show));
	}

	public function type_action() {
		$type = isset($_POST["type"]) ? trim(stripslashes($_POST["type"])) : "";
		if(empty($type)) {
			response::json(array("status" => false));
		}
		
		$form = $this->p->slideType($type);
		if($form === false) {
			response::json(array("status" => false));
		}

		response::json(array("status" => true, "view" => $form["form"], "script" => $form["script"], "preview" => $form["preview"], "lock" => $form["lock"]));
	}

	public function show_action() {
		$show = $this->p->slideShow();
		if($show === false) {
			response::json(array("status" => false));
		}

		response::json(array("status" => true, "show" => $show));
	}

	public function preview_action() {
		$image = isset($_POST["image"]) ? $_POST["image"] : "";
		if(empty($image)) {
			response::json(array("status" => false));
		}
		
		$preview = $this->p->slideImage($image);
		if($preview === false) {
			response::json(array("status" => false));
		}

		response::json(array("status" => true, "preview" => $preview));
	}

	public function remove_action() {
		if(!isset($_POST["__action"])) {
			response::json(array("status" => false));
		}

		if($this->p->slideRemove() == true) {
			response::json(array("status" => true));
		}

		response::json(array("status" => false));
	}

	public function removeResults_action() {
		if(!isset($_POST["__action"])) {
			response::json(array("status" => false));
		}

		if($this->p->slideResultsRemove() == true) {
			$this->p->loadSlideData();
			response::json(array("status" => true, "preview" => $this->p->slidePreview()));
		}

		response::json(array("status" => false));
	}

	public function play_action() {
		$play = $this->p->slidePlay(true);
		if($play !== false) {
			response::json(array("status" => true, "content" => $play));
		}

		response::json(array("status" => false));
	}

	public function resultsQuiz_action() {
		$result = $this->p->slideQuizResults(true);
		if($result !== false) {
			response::json(array("status" => true, "chart" => $result["chart"], "rank" => $result["rank"], "users" => $result["users"]));
		}

		response::json(array("status" => false));
	}

	public function saveImage_action() {
		global $__database;

		$fieldName = isset($_POST["fieldName"]) ? strip_tags(trim(stripslashes($_POST["fieldName"]))) : "";
		
		if(empty($fieldName)) {
			response::json(array("status" => "error", "error" => ""));
		}

		if(intval($_SERVER["CONTENT_LENGTH"]) > 0 && count($_POST) === 0) {
			response::json(array("status" => "error", "error" => "Maksymalny rozmiar wysyłanego pliku: ".ini_get("upload_max_filesize")."B"));
		}
		
		if(!isset($_FILES[$fieldName])) {
			response::json(array("status" => "error", "error" => "Wybierz plik"));
		}

		$fileUpload = $_FILES[$fieldName];
		$fileName = $fileUpload["name"];
		$fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
		
		switch($fileUpload["error"]) {
			case 0:
				$validExtensions = array("jpg", "jpeg", "png", "gif");
				if(array_search($fileExtension, $validExtensions) === false) {
					response::json(array("status" => "error", "error" => "Nieprawidłowy format pliku. Obsługiwane formaty plików to: ".implode(", ", $validExtensions)));
				}
				break;
			case 1:
				response::json(array("status" => "error", "error" => "Maksymalny rozmiar wysyłanego pliku: ".ini_get("upload_max_filesize")."B"));
				break;
			case 2:
				response::json(array("status" => "error", "error" => "Maksymalny rozmiar wysyłanego pliku: ".file::sizeToString($_POST["MAX_FILE_SIZE"])));
				break;
			case 3:
				response::json(array("status" => "error", "error" => "Plik przesłany tylko częściowo"));
				break;
			case 4:
				response::json(array("status" => "error", "error" => "Wybierz plik"));
				break;
			case 6:
				response::json(array("status" => "error", "error" => "Brak katalogu tymczasowego"));
				break;
			default:
				response::json(array("status" => "error", "error" => "Plik nie został zapisany na dysku"));
				break;
		}

		$fileUniqueName = $__database->newGuid().".".$fileExtension;

		if(!move_uploaded_file($fileUpload["tmp_name"], "public/upload/slides/".$fileUniqueName)) {
			response::json(array("status" => "error", "error" => "Plik nie został zapisany na dysku"));
		}

		response::json(array("status" => "success", "value" => $fileUniqueName, "name" => $fileName, "path" => url::root()."public/upload/slides/".$fileUniqueName));
	}
}
?>