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

use form\user_create_form;
use form\user_edit_form;
use core\exception\databaseException;
use core\password;
use core\url;

class user_controller extends _controller {
	public function index_action() {
		global $__database;

		$model = array();

		try {
			$query = $__database->prepare("SELECT `user_id`, `date_created`, `date_updated`, `name`, `email`, `active` FROM `users` ORDER BY `name` ASC");
			$query->execute();
			while($record = $query->fetch(\PDO::FETCH_ASSOC)) {
				$record["url_edit"] = url::action("uzytkownicy", "edytuj", $record["user_id"]);
				$record["url_remove"] = url::action("uzytkownicy", "usun", $record["user_id"]);
				$model[] = $record;
			}
		} catch(PDOException $error) {
			throw new databaseException($error->getMessage(), $error->getCode());
		}
		
		return $this->view($model);
	}

	public function create_action() {
		global $__database;

		$form = new user_create_form();
		if($form->valid()) {
			$user_id = $__database->newGuid();
			$password = password::hash($form->password);
			$active = $form->active == "1" ? "1" : "0";

			try {
				$query = $__database->prepare("INSERT INTO `users` (`user_id`, `name`, `email`, `role`, `password`, `active`) VALUES (:user_id, :name, :email, :role, :password, :active)");
				$query->bindParam(":user_id", $user_id);
				$query->bindParam(":name", $form->name);
				$query->bindParam(":email", $form->email);
				$query->bindParam(":role", $form->role);
				$query->bindParam(":password", $password);
				$query->bindParam(":active", $active);
				$query->execute();

				url::redirectAction("uzytkownicy", "edytuj", $user_id);
			} catch(PDOException $error) {
				throw new databaseException($error->getMessage(), $error->getCode());
			}
		}

		return $this->view($form);
	}

	public function edit_action($vars) {
		global $__database;

		$user_id = isset($vars["id"]) ? $vars["id"] : "";
		
		$query = $__database->prepare("SELECT `user_id`, `name`, `email`, `role`, `active` FROM `users` WHERE `user_id`=:user_id");
		$query->bindParam(":user_id", $user_id);
		$query->execute();
		if(!$data = $query->fetch(\PDO::FETCH_ASSOC)) {
			__debug("Record no exist!");
		}

		$form = new user_edit_form($data);
		if($form->valid()) {
			$active = $form->active == "1" ? "1" : "0";

			try {
				$__database->beginTransaction();

				$query = $__database->prepare("UPDATE `users` SET `name`=:name, `email`=:email, `role`=:role, `active`=:active WHERE `user_id`=:user_id");
				$query->bindParam(":name", $form->name);
				$query->bindParam(":email", $form->email);
				$query->bindParam(":role", $form->role);
				$query->bindParam(":active", $active);
				$query->bindParam(":user_id", $user_id);
				$query->execute();
				
				if(!empty($form->password)) {
					$password = password::hash($form->password);
					
					$query = $__database->prepare("UPDATE `users` SET `password`=:password WHERE `user_id`=:user_id");
					$query->bindParam(":password", $password);
					$query->bindParam(":user_id", $user_id);
					$query->execute();
				}

				$__database->commit();

				url::redirectAction("uzytkownicy", "edytuj", $user_id);
			} catch(PDOException $error) {
				$__database->rollBack();

				throw new databaseException($error->getMessage(), $error->getCode());
			}
		}
		
		return $this->view($form);
	}

	public function remove_action($vars) {
		global $__database;

		$user_id = isset($vars["id"]) ? $vars["id"] : "";
		
		$query = $__database->prepare("DELETE FROM `users` WHERE `user_id`=:user_id");
		$query->bindParam(":user_id", $user_id);
		$query->execute();

		url::redirectAction("uzytkownicy");
	}
}
?>