<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

$category = sly_request('rex_file_category', 'int', 0);
$canView  = _WV2_Metainfo::isAllowed('view');
$canEdit  = _WV2_Metainfo::isAllowed('edit');

$languages = array();
$user      = sly_Util_User::getCurrentUser();
$isAdmin   = $user->isAdmin();
$sysLangs  = sly_Util_Language::findAll();
$allowed   = $user->getAllowedCLangs();

foreach ($sysLangs as $id => $lang) {
	if (!in_array($id, $allowed) && !$isAdmin) continue;
	$languages[$id] = sly_html($lang->getName());
}

if (count($languages) > 1) {
	$links = array();
	$get   = 'page=mediapool_detail&amp;file_id='.$medium.'&amp;rex_file_category='.$category;

	foreach ($languages as $id => $name) {
		if ($id == $clang) $links[] = $name;
		else $links[] = '<a href="?clang='.$id.'&amp;'.$get.'">'.$name.'</a>';
	}

	$element = new sly_Form_Text(t('metainfo_languageselect'), implode(' | ', $links));
	$element->setIsHTML(true);

	$form->add($element);
}

$form->addHiddenValue('clang', $clang);
$form->addHiddenValue('wv2metainfo', 1);

$availableInfos = WV2_MetaProvider::getAllMediumMetaInfos();
$assignedData   = WV2_MetaProvider::mediumData($medium, null, null, $clang, true);
$dataToDisplay  = _WV2_Metainfo::getInfosToDisplay($availableInfos, false, $assignedData, $availableInfos);

foreach ($dataToDisplay as $groupName => $group) {
	foreach ($group as $data) {
		$value = $data['value'];
		$info  = $data['info'];

		if ($canEdit) {
			$info->renderForm($form, array($value));
		}
		else {
			$value = $info->deserialize($value);
			$text  = new sly_Form_Text($info->getTitle(), is_array($value) ? implode(', ', $value) : $value);
			$form->add($text);
		}
	}
}
