<?php
class Group_PutController extends Library_Controller {
	public function __call($methodName, $args) {
		$objectInfo = $this->_parseActionName($methodName);
		$response = new Library_Response();
		
		//kontrola, jestli skupina existuje
		$tableGroups = new Application_Model_UsersGroups();
		
		$group = $tableGroups->find($objectInfo["id"])->current();
		
		if (!$group) {
			$response->setStatus(404, "GROUP_NOT_FOUND");
			$this->_sendToRender($response, $objectInfo["format"]);
			return;
		}
		
		//kontrola opravneni
		if ($this->_user->id != 1) {
			//uzivatel neni root, zkontorluji se role
			$tableRoles = new Application_Model_UsersRoles();
			
			$role = $this->_user->findManyToManyRowset($tableRoles, "Application_Model_UsersHasManyUsersRoles", "user", "role", $tableRoles->select(false)->where("role_name like ?", "GROUP_MANAGER"));

			if (!$role->count()) {
				//role nebyla nalezena, uzivatel nema opravneni
				$response->setStatus(403, "NO_PERMISIONS_TO_CHANGE_GROUP");
				$this->_sendToRender($response, $objectInfo["format"]);
				return;
			}
		}
		
		//kontrola pozadavku na zmenu jmena
		if (isset($_REQUEST["group"]["group_name"])) {
			//pozadavek existuje
			$groupName = strtolower($_REQUEST["group"]["group_name"]);
			
			//kontorla jestli neni prazdny
			$groupName = trim($groupName);
			
			if (empty($groupName)) {
				//nazev skupiny je prazdny
				$response->setStatus(400, "GROUP_NAME_IS_EMPTY");
				$this->_sendToRender($response, $objectInfo["format"]);
				return;
			}
			
			//kontrola vyhrazenych slov
			if ($groupName == "list") {
				//skupina se snazi prejmenovat na vyhrazene slovo
				$response->setStatus(409, "LIST_IS_RESERVED_NAME");
				$this->_sendToRender($response, $objectInfo["format"]);
			}
			
			//nastavnei noveho jmena
			$group->group_name = $groupName;
			$group->save();
		}
		
		//kontorla pozadavku na zapis lidi
		if (isset($_REQUEST["group"]["user"])) {
			//pozadavek na zapis lidi existuje
			$users = (array) $_REQUEST["group"]["user"];
			
			//smazani starych zaznamu
			$tableUsersGroups = new Application_Model_UsersHasManyUsersGroups();
			$tableUsersGroups->delete(array("user_group_id" => $group->id));
			
			//zapsani novych zaznamu
			$data = array(
				"user_id" => 0,
				"user_group_id" => $group->id
			);
			
			foreach ($users as $userId => $toWrite) {
				if ($toWrite) {
					$data["user_id"] = $userId;
					$tableUsersGroups->insert($data);
				}
			}
		}
		
		//nastavnei odpovedi
		$response["group"] = $group;
		$response["users"] = $group->findManyToManyRowset($this->_tableUsers, "Application_Model_UsersHasManyUsersGroups", "group", "user")->toArray();
		
		$response->setOk();
		$this->_sendToRender($response, $objectInfo["format"]);
	}
	
	protected function _sendToRender(Library_Response $response, $format) {
		//zapsani do view
		$this->view->response = $response;
		$response->sendHeader();
		
		if ($format != "html") {
			$this->_helper->layout()->disableLayout();
		}
		
		//vyhodnoceni formatu
		switch ($format) {
			case "html":
				$this->_helper->viewRenderer("put-html");
				break;
			
			case "json":
			
			default:
				$this->_helper->viewRenderer("put-json");
		}
	}
}