<?php
//priprava dat
$response = $this->response;
$directory = $response["directory"];

$noGroup = new stdClass();
$noGroup->group_name = "-";
$noGroup->id = 0;

//vypis obecnych informaci
$owner = $this->getUser($directory->user_id);

if ($directory->group_id) {
	$thisGroup = $this->getGroup($directory->group_id);
} else {
	$thisGroup = $noGroup;
}
?>
<h1>Adresář <i><?php echo $directory->directory_name; ?></i></h1>
<?php 
//vyhodnoceni opravneni menit skupinu adresare
$changeGroup = false;
$user = Zend_Registry::get("_user");

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

if ($user->id == 1 || $user->id == $owner->id || $role->count()) {
	//uzivatel je valstnik nebo root nebo ma roli
	$changeGroup = true;
}

//vyhodnoceni jestli uzivatel smi zapisovat do materskeho adresare
$changeName = false;
$parent = $response["directParent"];

//kontorla jeslti je uzivatel vlastnikem, rootem nebo ma roli
if ($user->id == $parent->user_id || $user->id == 1 || $role->count()) {
	//uzivatel je automaticky opravnen menit jemno adresare
	$changeName = true;
} else {
	//zkontorluje se opravneni zapisovat
	$changeName = $parent->getPermisions($user->id, Zend_Registry::get("groups"), $parent)->write;
}
?>
<form action="/directory/put/<?php echo $directory->id; ?>.html" method="post">
	<p>
		ID: #<?php echo $directory->id; ?>
	</p>
	<p>
		Uživatel: <strong><a href="/user/get/<?php echo $owner->username; ?>.html"><?php echo $owner->username; ?></a></strong>
	</p>
<?php 
//kontorla jeslti je uzivatel opravnen menit skupinu
if ($changeGroup) {
	//uzivatel je opravnen menit skupinu
	
	//nacteni seznamu skupin podle opravneni uzivatele
	$tableGroups = new Application_Model_UsersGroups();
	
	if ($user->id == 1 || $role->count()) {
		//uzivatel je root nebo admin, nactou se vsechny role
		$groups = $tableGroups->fetchAll(null, "group_name");
	} else {
		//uzivatel ma k dispozici pouze skupiny, ve kterych je clenem
		$groups = $user->findManyToManyRowset($tableGroups, "Application_Model_UsersHasManyUsersGroups", "user", "group");
	}
	
	$groupList = array(0 => "Bez skupiny");
	
	foreach ($groups as $group) {
		$groupList[$group->id] = $group->group_name;
	}
	
	?>
	<p>
		<?php echo $this->formLabel("directory[group_id]", "Skupina: "); ?>
		<?php echo $this->formSelect("directory[group_id]", $directory->group_id, null, $groupList); ?>
	</p>
	<?php 
} else {
	//uzivatel neni opravnen menit skupinu
?>
	<p>
		Skupina: <strong><a href="/group/get/<?php echo $thisGroup->id . "_" . $thisGroup->group_name; ?>"><?php echo $thisGroup->group_name?></a></strong>
	</p>
	<?php 
}

//kontrola jeslti je mozne zmenit jmeno
if ($changeName) {
	?>
	<p>
		<?php echo $this->formLabel("directory[directory_name]", "Jméno adresáře: "); ?>
		<?php echo $this->formText("directory[directory_name]", $directory->directory_name); ?>
	</p>
	<?php 
} else {
	?>
	Jméno adresáře: <strong><?php echo $directory->directory_name; ?></strong>
	<?php 
}

//konotrla jestli uzivtel muze menit alespon jednu vec
if ($changeGroup || $changeName) {
	?>
	<p>
		<?php echo $this->formSubmit("directory[submit]", "Uložit"); ?>
	</p>
	<?php 
}
	?>
</form>
<?php 
//vyhodnoceni jeslti muze uzivatel do adresare zapisovat
if ($response["permisions"]->write || $user->id == 1 || $role->count()) {
	$canWrite = true;
?>
<h2>Vytvořit adresář</h2>
<form action="/directory/post/.html" method="post" name="post-form">
	<p>
		<?php echo $this->formLabel("directory[directory_name]", "Jméno adresáře: ", array("id" => "post-directory-name")); ?>
		<?php echo $this->formText("directory[directory_name]", null, array("id" => "post-directory-name"))?>
		<?php echo $this->formHidden("directory[parent_id]", $directory->id); ?>
	</p>
	<p>
		<?php echo $this->formSubmit("directory[submit]", "Vytvořit")?>
	</p>
</form>
<h2>Přidat dokument</h2>
<form action="/document/post/.html" method="post" enctype="multipart/form-data">
	<p>
		<?php echo $this->formLabel("document[content]", "Soubor s dokumentem: "); ?>
		<?php echo $this->formFile("document[content]"); ?>
	</p>
	<p>
		<?php echo $this->formHidden("document[directory][$directory->id][write]", 1); ?>
		<?php echo $this->formSubmit("document[submit]", "Odeslat na server"); ?>
	</p>
</form>
<?php 
} else {
	$canWrite = false;
}
?>
<h2>Obsah adresáře</h2>
<form action="/directory/put/<?php echo $directory->id; ?>.html" method="post" onsubmit="var uuid = this['document[uuid]'].value; this['document[uuid]'].name='directory[document][' + uuid + '][write]'; this['document[document_name]'].name = 'directory[document][' + uuid + '][document_name]'; return true">
	<table>
		<thead>
			<tr>
				<th>
				Jméno
				</th>
				<th>
				Jméno v adresáři
				</th>
				<th>
				Typ
				</th>
				<th>
				Uživatel
				</th>
				<th>
				Skupina
				</th>
				<th>
				Práva
				</th>
				<th>
				Velikost [B]
				</th>
				<th>
				Vytvořen
				</th>
				<th>
				Zachovat
				</th>
			</tr>
		 </thead>
		 <tbody>
		 	<tr>
		 		<td>
				<a href="">.</a>
				</td>
				<td>
				<a href="">.</a>
				</td>
				<td>
				D
				</td>
				<td>
				<a href="/user/get/<?php echo $owner->username; ?>.html"><?php echo $owner->username; ?></a>
				</td>
				<td>
				<a href="/group/get/<?php echo $directory->group_id; ?>.html"><?php echo $thisGroup->group_name; ?></a>
				</td>
				<td>
				<?php echo $directory->mask; ?>
				</td>
				<td>
				0
				</td>
				<td>
				<?php echo $directory->created_at; ?>
				</td>
				<td>
				-
				</td>
		 	</tr>
		 	<tr>
		 		<td>
				<a href="/directory/get/<?php echo $parent->id; ?>.html">..</a>
				</td>
				<td>
				<a href="/directory/get/<?php echo $parent->id; ?>.html">..</a>
				</td>
				<td>
				D
				</td>
				<td>
				<a href="/user/get/<?php echo $this->getUser($parent->user_id)->username; ?>.html"><?php echo $this->getUser($parent->user_id)->username; ?></a>
				</td>
				<td>
				<a href="/group/get/<?php echo $parent->group_id; ?>.html"><?php echo $this->getGroup($parent->group_id)->group_name; ?></a>
				</td>
				<td>
				<?php echo $parent->mask; ?>
				</td>
				<td>
				0
				</td>
				<td>
				<?php echo $parent->created_at; ?>
				</td>
				<td>
				-
				</td>
		 	</tr>
	<?php 
	//vypsani podadresaru
	foreach ($response["subdirs"] as $dir) {
		?>
			<tr>
		 		<td>
				<a href="/directory/get/<?php echo $dir->id; ?>.html"><?php echo $dir->directory_name; ?></a>
				</td>
				<td>
				<a href="/directory/get/<?php echo $dir->id; ?>.html"><?php echo $dir->directory_name; ?></a>
				</td>
				<td>
				D
				</td>
				<td>
				<a href="/user/get/<?php echo $this->getUser($dir->user_id)->username; ?>.html"><?php echo $this->getUser($dir->user_id)->username; ?></a>
				</td>
				<td>
				<a href="/group/get/<?php echo $dir->group_id; ?>.html"><?php echo $this->getGroup($dir->group_id)->group_name; ?></a>
				</td>
				<td>
				<?php echo $dir->mask; ?>
				</td>
				<td>
				0
				</td>
				<td>
				<?php echo $dir->created_at; ?>
				</td>
				<td>
				-
				</td>
		 	</tr>
		<?php 
	}
	
	//vypsani dokumentu
	foreach ($response["documents"] as $document) {
		?>
		<tr>
		 		<td>
				<a href="/document/get/<?php echo $document->uuid; ?>.html"><?php echo $document->document_name; ?></a>
				</td>
				<td>
				<?php
				//kontrola jeslti uzivatel smi zapisovat 
				if ($canWrite) {
					echo $this->formText("directory[document][$document->uuid][document_name]", $response["documentsNames"][$document->uuid]);
				} else {
					//uzivatel nesmi zapisovat
					echo $response["documentsNames"][$document->uuid] ? $response["documentsNames"][$document->uuid] : "-";
				}
				?>
				</td>
				<td>
				F
				</td>
				<td>
				<a href="/user/get/<?php echo $this->getUser($document->user_id)->username; ?>.html"><?php echo $this->getUser($document->user_id)->username; ?></a>
				</td>
				<td>
				<a href="/group/get/<?php echo $document->group_id; ?>.html"><?php echo $this->getGroup($document->group_id)->group_name; ?></a>
				</td>
				<td>
				<?php echo $document->mask; ?>
				</td>
				<td>
				<?php echo $document->size; ?>
				</td>
				<td>
				<?php echo $document->created_at; ?>
				</td>
				<td>
				<?php echo $this->formCheckbox("directory[document][$document->uuid][write]", 1, array("checked" => "checked")); ?>
				</td>
		 	</tr>
		<?php 
	}
	?>
		 </tbody>
	</table>
	<h3>Vložit existující dokument do adresáře</h3>
	<p>
		<?php echo $this->formLabel("document[uuid]", "UUID dokumentu: ") ?>
		<?php echo $this->formText("document[uuid]"); ?>
	</p>
	<p>
		<?php echo $this->formLabel("document[document_name]", "Jméno dokumentu v adresáři: "); ?>
		<?php echo $this->formText("document[document_name]"); ?>
	</p>
	<p>
		<?php echo $this->formSubmit("directory[submit]", "Uložit změny"); ?>
	</p>
</form>