<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
$lang_info=Db::i()->selectRow("SELECT `key`, lang_name, lang_id FROM ?_langs WHERE lang_id=?", $_GET['var1']);
if (!$lang_info) return LabCMS::i()->error(_m('Язык не найден'));

//addBreadcrumb($lang_info['lang_name'], _url($lang_info['lang_id'], 'files'));

Tpl::i()->assign('lang', $lang_info);

$mod_template="edit_text.tpl";

if ($_SERVER['REQUEST_METHOD']=='POST') {
	User::checkToken();
	$array=array();
	foreach ($_POST['val'] as $key=>$var) {
		if (!$_POST['del'][$key]) {
			Lang_Generic::updateString($lang_info['key'], $_POST['keys'][$key], $var, $_POST['context'][$key]);
		} else {
			Lang_Generic::deleteString($lang_info['key'], $_POST['keys'][$key], $_POST['context'][$key]);
		}
	}
	refresh('', _url());
}

$target=Lang_Generic::getLang($lang_info['key']);
$default=Lang_Generic::getDefaultLang();
$merge=merge_langs($default, $target);

if (count($merge)) {
	foreach ($merge as $key=>$var) {
		foreach ($var as $context=>$value) {
			Tpl::i()->append(
				'strings',
				array(
					'key'=>$key,
					'context'=>$context,
					'value'=>empty($target[$key][$context])?'':$target[$key][$context],
					'active'=>(
						array_key_exists($key, $default)
						&& array_key_exists($context, $default[$key])
					)
				)
			);
		}
	}
}

function merge_langs($default, $target)
{
	if (count($default)) foreach ($default as $key=>$val) {
		foreach ($val as $context=>$v) {
			if (!array_key_exists($key, $target)
				|| !array_key_exists($context, $target[$key])) $target[$key][$context]='';
		}
	}
	ksort($target);
	return $target;
}
