<?php

/**
 * class presentation_controller
 *
 * Description for class presentation_controller
 *
 * @author:
*/
namespace area\presenter\controller;

use area\presenter\form\presentation_create_form;
use area\presenter\form\presentation_edit_form;
use core\exception\databaseException;
use core\encryption;
use core\presentation;
use core\response;
use core\url;
use core\user;

class presentation_controller extends _controller {
	private $p = null;

	public function __construct() {
		global $__routing;

		parent::__construct();

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

		$this->p = new presentation($id);
		if(!empty($id) && !$this->p->existsOwner()) {
			$this->viewError("404");
		}
	}

	public function create_action() {
		if(!isset($_POST["__action"])) {
			$this->viewError("404");
		}

		$form = new presentation_create_form();
		if($form->valid()) {
			$presentationId = $this->p->create($form->name, $form->tempo);
			if($presentationId !== false) {
				response::json(array("status" => "success", "url" => url::action("prezentacje", "edytuj", $presentationId)));
			}
		}

		response::json(array("status" => "error", "errors" => $form->getAllErrors()));
	}

	public function copy_action() {
		if(!isset($_POST["__action"])) {
			$this->viewError("404");
		}

		$form = new presentation_create_form();
		if($form->valid()) {
			$presentationId = $this->p->copy($form->name, $form->tempo);
			if($presentationId !== false) {
				response::json(array("status" => "success", "url" => url::action("prezentacje", "edytuj", $presentationId)));
			}
		}

		response::json(array("status" => "error", "errors" => $form->getAllErrors()));
	}

	public function removeResults_action($vars) {
		if(!isset($_POST["__action"]) || !isset($vars["id"])) {
			$this->viewError("404");
		}

		$newPresentationId = $this->p->copy($this->p->presentationName, $this->p->presentationTempo);
		if($newPresentationId !== false) {
			$this->p->modificationName("[archiwum] ".$this->p->presentationName." - ".date("Y-m-d H:i"));

			response::json(array("status" => "success", "url" => url::action("prezentacje", "edytuj", $newPresentationId)));
		}

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

	public function edit_action($vars) {
		$this->setVariable("presentation_types", $this->p->typesList());
		return $this->view($this->p->getOwner(), null, "layoutEmpty");
	}

	public function save_action() {
		if(!isset($_POST["__action"])) {
			$this->viewError("404");
		}
		
		$form = new presentation_edit_form();
		if($form->valid()) {
			$status = $this->p->modification($form->name, $form->tempo, $form->results);
			if($status !== false) {
				response::json(array("status" => "success"));
			}
		}
		
		response::json(array("status" => "error", "errors" => $form->getAllErrors()));
	}

	public function play_action() {
		$this->p->runPlay();
		
		$this->setVariable("countSlideQuiz", $this->p->countSlideQuiz());
		$this->setVariable("userId", user::identifier());
		return $this->view($this->p->getOwner(true), null, "layoutEmpty");
	}

	public function off_action() {
		$this->p->stopPlay();
	}

	public function remove_action() {
		if(!isset($_POST["__action"])) {
			$this->viewError("404");
		}

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

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

	public function sort_action() {
		if(!isset($_POST["slide"]) || count($_POST["slide"]) == 0) {
			response::json(array("status" => "error"));
		}

		if($this->p->slideSort($_POST["slide"])) {
			response::json(array("status" => "success"));
		}

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