<?php
$response = $this->response;

/* @var $master Application_Model_Row_Directory */
$master = $response["master"];
/* @var $document Application_Model_Row_Directory */
$document = $response["document"];
?>
<h1>Dokument <?php echo $document->document_name; ?></h1>
<p>
	Vytvořen: <strong><?php echo $document->created_at; ?></strong>
</p>
<p>
	UUID: <strong><?php echo $document->uuid; ?></strong>
</p>
<p>
	Uživatel: <strong><a href="/user/get/<?php echo $this->getUser($master->user_id)->username; ?>.html"><?php echo $this->getUser($master->user_id)->username; ?></a></strong>
</p>
<p>
	Skupina: <strong><a href="/group/get/<?php echo $this->getGroup($master->group_id)->id ?>.html"><?php echo $this->getGroup($master->group_id)->group_name; ?></a></strong>
</p>
<p>
	Velikost dokumentu: <strong><?php echo $document->size; ?></strong> [B]
</p>
<?php 
//vyhodnoceni moznosti zapisovat do souboru
$user = Zend_Registry::get("_user");
$groups = Zend_Registry::get("_groups");

//nacteni role
$tableRoles = new Application_Model_UsersRoles();
$role = $user->findManyToManyRowset($tableRoles, "Application_Model_UsersHasManyUsersRoles", "user", "role", $tableRoles->select(false)->where("role_name like ?", "DOCUMENT_MANAGER"));

$writeAble = ($user->id == 1 || $role->count() || $master->getPermisions($user->id, $groups)->write) && $document->is_latest;

if ($writeAble) {
	//do dokumentu je mozne zapisovat
	?>
<form action="/document/put/<?php echo $master->uuid; ?>.html" method="post" enctype="multipart/form-data">
	<p>
		<?php echo $this->formLabel("document[mime_type]", "MIME typ: ")?>
		<?php echo $this->formText("document[mime_type]", $master->mime_type); ?>
	</p>
	<p>
		<?php echo $this->formLabel("document[document_name]", "Jméno dokumentu: ")?>
		<?php echo $this->formText("document[document_name]", $master->document_name); ?>
	</p>
	<p>
		<?php echo $this->formLabel("document[mask]", "Maska oprávnění: ")?>
		<?php echo $this->formText("document[mask]", $master->mask); ?>
	</p>
	<p>
		<?php echo $this->formLabel("document[content]", "Nový obsah dokumentu"); ?>
		<?php echo $this->formFile("document[content]"); ?>
	</p>
	<p>
		<?php echo $this->formSubmit("document[submit]", "Zapsat změny"); ?>
	</p>
</form>
	<?php 
} else {
	//do dokumentu neni mozne zapisovat
?>
<p>
	MIME typ: <strong><?php echo $document->mime_type; ?></strong>
</p>
<p>
	Jméno dokumentu: <strong><?php echo $document->document_name; ?></strong>
</p>
<p>
	Maska oprávnění: <strong><?php echo $master->mask; ?></strong>
</p>
<?php 
	//kontorla jeslti je dokument aktualni
	if ($document->is_latest) {
?>
<p>
	<strong>Dokument je aktuální</strong>
</p>
<?php 		
	} else {
?>
<p>
	Na aktuální verzi dokumentu se dostanete <a href="/document/get/<?php echo $master->uuid; ?>.html">tudy</a>
</p>
<?php 	
	}
	
}

//vyhodnoceni jeslti je uzivatel opravnen cist dokument
$readAble = $user->id == 1 || $role->count() || $master->getPermisions($user->id, $groups)->read;

if ($readAble) {
	?>
<p>
	Stáhnout dokumentu můžete <a href="/document/get/<?php echo $document->uuid; ?>">zde</a>
</p>
	<?php 
}
?>
<h2>Adresáře dokumentu</h2>
<ul>
	<?php 
	foreach ($response["directories"] as $directory) {
		?>
	<li>
		<a href="/directory/get/<?php echo $directory->id; ?>.html"><?php echo $directory->directory_name; ?></a>
	</li>
		<?php 
	}
	?>
</ul>
<h2>Historie změn</h2>
<ul>
	<?php 
	foreach ($response["history"] as $history) {
		?>
	<li>
		<a href="/document/get/<?php echo $history->document_old_uuid; ?>.html"><?php echo $history->created_at; ?></a>
	</li>
		<?php 
	}
	?>
	<li>
		<a href="/document/get/<?php echo $master->uuid; ?>.html"><?php echo $master->created_at; ?></a>
	</li>
</ul>