<?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
 */

$infos     = WV2_MetaProvider::getMetaInfosForArticleType($articleType);
$metainfos = array_keys($infos);
$canView   = _WV2_Metainfo::isAllowed('view');
$canEdit   = _WV2_Metainfo::isAllowed('edit');

$lastGroup = null;
if ($canView || $canEdit) {
	foreach ($dataToDisplay as $groupName => $group) {
		foreach ($group as $data) {
			$value    = $data['value'];
			$info     = $data['info'];
			$metainfo = $info->getName();
			$hidden   = !in_array($metainfo, $metainfos);
			$style    = $hidden ? ' style="display:none"' : '';

			if ($lastGroup != $groupName) {
				$form->add(new sly_Form_Fragment('</div'));
				$form->beginFieldset($groupName, 'group_'.uniqid());
				$form->add(new sly_Form_Fragment('<div class="wv2_frontend_meta">'));
				$lastGroup = $groupName;
			}

			$fragment = new sly_Form_Fragment('<div id="wv2_metainfo'.$info->getID().'_row" class="wv2_metainfo_row"'.$style.'>');

			$form->add($fragment);

			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);
			}

			$form->add(new sly_Form_Fragment('</div>'));
		}
	}
}
