<?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
*/
if ($mode=="delete") {
	Db::i()->query("DELETE FROM ?_langs WHERE lang_id=?", $_GET['var1']);
	Db::i()->query("UPDATE ?_tree SET lang_id=0 WHERE lang_id=?", $_GET['var1']);
	LabCMS::i()->reset_cache('all');
	refresh();
}

$form=getEditForm();

if ($form->validate()) {
	if ($mode=="add") {
		Db::i()->query("INSERT INTO ?_langs (lang_name, `key`) VALUES(?, ?)", $_POST['lang_name'], $_POST['key']);
	}
	if ($mode=="edit") {
		Db::i()->query(
			"UPDATE ?_langs SET lang_name=?, `key`=? WHERE lang_id=?",
			$_POST['lang_name'], $_POST['key'], $_GET['var1']
		);
	}
	refresh();
}


$mod_template="edit_lang.tpl";

if ($mode=="edit") {
	$form->AddDataSource(
		Factory::create(
			'HTML_QuickForm2_DataSource_Array',
			Db::i()->selectRow("SELECT lang_id, lang_name, `key` FROM ?_langs WHERE lang_id=?", $_GET['var1'])
		)
	);
}

Tpl::i()->assign("form", renderForm($form));

function getEditForm()
{
	$form = Factory::create('HTML_QuickForm2', null, 'post', array('action'=>''));
	$token=$form->addElement('hidden', 'token', array('value'=>User::i()->token));
	$name=$form->addElement('text', 'lang_name', null, array('label' => _m('Название языка')));
	$key=$form->addElement('text', 'key', null, array('label' => _m('Обозначение языка (например ru)')));
	$submit=$form->addElement('submit', null, array('value'=>_m('Сохранить')));
	
	$name->addRule(
		'required',
		_m('Название языка не может быть пустым!')
	);
	$key->addRule(
		'required',
		_m('Обозначение языка не может быть пустым!')
	);
	return $form;
}

function renderForm($form)
{
	$renderer = HTML_QuickForm2_Renderer::factory(config::get('core.form_renderer'));
	$renderer=$form->render($renderer);
	LabCMS::i()->skins[LabCMS::i()->getSkin()]['js']=
		LabCMS::i()->skins[LabCMS::i()->getSkin()]['js']
		+$renderer->getJSLibs(false, false);
	return $renderer;
}