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

use PDO;

class copy_controller extends _controller {
	function index_action($vars) {
		global $__database;
		
		$fromUser = $vars["from"];
		$toUser = $vars["to"];

		$presentations = array();

		$query = $__database->prepare("SELECT * FROM `presentations` WHERE `user_id`=:user_id");
		$query->bindParam(":user_id", $fromUser);
		$query->execute();
		$presentations = $query->fetchAll(PDO::FETCH_ASSOC);
		
		if(count($presentations) == 0) {
			__debug("Brak prezentacji");
		}

		foreach($presentations as $presentation) {
			$presentationId = $__database->newGuid();
			$accessKey = $__database->newPresentationCode();

			$query = $__database->prepare("INSERT INTO `presentations` (`presentation_id`, `user_id`, `date_created`, `date_updated`, `accessKey`, `name`, `tempo`, `results`, `isOpen`) VALUES (:presentation_id, :user_id, :date_created, :date_updated, :accessKey, :name, :tempo, :results, :isOpen)");
			$query->bindParam(":presentation_id", $presentationId);
			$query->bindParam(":user_id", $toUser);
			$query->bindParam(":date_created", $presentation["date_created"]);
			$query->bindParam(":date_updated", $presentation["date_updated"]);
			$query->bindParam(":accessKey", $accessKey);
			$query->bindParam(":name", $presentation["name"]);
			$query->bindParam(":tempo", $presentation["tempo"]);
			$query->bindParam(":results", $presentation["results"]);
			$query->bindParam(":isOpen", $presentation["isOpen"]);
			if($query->execute()) {
				$query = $__database->prepare("SELECT * FROM `boards` WHERE `presentation_id`=:presentation_id");
				$query->bindParam(":presentation_id", $presentation["presentation_id"]);
				$query->execute();
				$boards = $query->fetchAll(PDO::FETCH_ASSOC);

				if(count($boards) == 0) {
					continue;
				}

				foreach($boards as $board) {
					$boardId = $__database->newGuid();

					$query = $__database->prepare("INSERT INTO `boards` (`board_id`, `presentation_id`, `date_create`, `date_updated`, `type`, `name`, `preview`, `image`, `video`, `config`, `reactions`, `order`) VALUES (:board_id, :presentation_id, :date_create, :date_updated, :type, :name, :preview, :image, :video, :config, :reactions, :order)");
					$query->bindParam(":board_id", $boardId);
					$query->bindParam(":presentation_id", $presentationId);
					$query->bindParam(":date_create", $board["date_create"]);
					$query->bindParam(":date_updated", $board["date_updated"]);
					$query->bindParam(":type", $board["type"]);
					$query->bindParam(":name", $board["name"]);
					$query->bindParam(":preview", $board["preview"]);
					$query->bindParam(":image", $board["image"]);
					$query->bindParam(":video", $board["video"]);
					$query->bindParam(":config", $board["config"]);
					$query->bindParam(":reactions", $board["reactions"]);
					$query->bindParam(":order", $board["order"]);
					if($query->execute()) {
						$query = $__database->prepare("INSERT INTO `boards_options` (`option_id`, `board_id`, `text`, `correct`, `image`, `order`) SELECT UUID() AS `option_id`, '{$boardId}' AS `board_id`, `text`, `correct`, `image`, `order` FROM `boards_options` WHERE `board_id`=:board_id");
						$query->bindParam(":board_id", $board["board_id"]);
						$query->execute();
					}
				}
			}
		}
	}
}
?>