<?php

namespace Modules\Admin\Cities;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Creates the form for city insert/update.
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		// load globals
		$CFG   = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// load the configurations for this form
		$cfg = \Entity\City::loadCFG();

		$form = \Engine\Form\General::create();
		$form->setStyle('width', '300');
		$form->addContainer(\Engine\Form\Container\Fieldset::create());

		// country code
		$fld = new \App\Form\Input\Autocomplete_Country($this->lang['index']['filters']['country'], 'countryCode', $this->values['countryCode']);
		$fld->importFromConfig($cfg['countryCode']);

		if ($this->controller->getAction()=='update') {
			$fld->setDisabled();
		}
		$form->addElement($fld);

		// city name
		if ($CFG['useAlternativeNames']['cities']) {
			$fld = new \App\Form\Input\AlternativeText($this->lang['formFields']['name'], 'name', $this->values['name']);
		}
		else {
			$fld = new \Engine\Form\Input\Text($this->lang['formFields']['name'], 'name', $this->values['name']);
		}

		$fld->importFromConfig($cfg['name']);
		$form->addElement($fld);

		// city code3
		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['code3'], 'code3', $this->values['code3']);
		$fld->importFromConfig($cfg['code3']);
		$form->addElement($fld);

		// city code3 cyrillic
		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['codeLocal'], 'codeLocal', $this->values['codeLocal']);
		$fld->importFromConfig($cfg['codeLocal']);
		$form->addElement($fld);

		// city district
		if ($CFG['useAlternativeNames']['cityDistricts']) {
			$fld = new \App\Form\Input\AlternativeText($this->lang['formFields']['district'], 'district', $this->values['district']);
		}
		else {
			$fld = new \Engine\Form\Input\Text($this->lang['formFields']['district'], 'district', $this->values['district']);
		}

		$fld->importFromConfig($cfg['district']);
		$form->addElement($fld);

		// city region
		if ($CFG['useAlternativeNames']['cityRegions']) {
			$fld = new \App\Form\Input\AlternativeText($this->lang['formFields']['region'], 'region', $this->values['region']);
		}
		else {
			$fld = new \Engine\Form\Input\Text($this->lang['formFields']['region'], 'region', $this->values['region']);
		}
		$fld->importFromConfig($cfg['region']);
		$form->addElement($fld);

		// area code
		$fld = new \Engine\Form\Input\Text($this->lang['formFields']['areaCode'], 'areaCode', $this->values['areaCode']);
		$fld->importFromConfig($cfg['areaCode']);
		$form->addElement($fld);

		// city id (for update operation)
		if ($this->controller->getAction()=='update') {
			$form->addElement(new \Engine\Form\Input\Hidden('', 'id', $this->values['id']));
		}

		// parent TODO: needed anymore?
		$form->addElement(new \Engine\Form\Input\Hidden('', 'parent', $this->values['parent']));

		// submit button
		$form->addElement(new \Engine\Form\Input\Hidden('', $this->submitButtonName, 1));

		return $form;

	}

}