<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPage.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminNumeric.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/SystemParameters.inc.php" );

	class AdminParametersPage extends BackendPage {
		protected $aPrices = array();

		protected $aCustoms = array();
		protected $customparams = array(
			array( "name" => "max_languages_parttime" ),
			array( "name" => "max_languages_freelancer" ),
			array( "name" => "max_languages_agency" ),
			array( "name" => "half_year_length_in_days" ),
			array( "name" => "whole_year_length_in_days" ),
			array( "name" => "test_email_address" , "width" => 300 ),
			array( "name" => "job_bgcolor_permitted" , "width" => 80 ),
			array( "name" => "job_bgcolor_disabled" , "width" => 80 ),
			array( "name" => "job_bgcolor_special" , "width" => 80 ),
			array( "name" => "job_bgcolor_owned" , "width" => 80 )
		);

		public function __construct( $id = "AdminLanguageEditPage" , $parent = null ) {
			parent::__construct( $id , $parent );
			if (!AdminUserModel::isAdminLoggedIn()) return;

			// generate prices
			$utypes = array( "parttime" , "freelancer" , "agency" );
			$lengths = array( "6m" , "12m" );
			$currencies = array( "huf" , "eur" );
			$prolong_prefixes = array( "" , "prolong_" );
			$sm = new SystemParametersModel();
			foreach ($prolong_prefixes as $pp) {
				foreach ($utypes as	$utype) {
					foreach ($lengths as $length) {
						foreach ($currencies as $currency) {
							$name = sprintf( "%s%s_%s_%s_price" , $pp , $utype , $length , $currency );
							$price = new AdminNumeric( $name , $this );
							$price->setCSSAttribute( "width" , "100px" );	
							$this->aPrices[$name]=$price;
						}
					}
				}
			}

			// generate custom parameters
			foreach ($this->customparams as $wdg) {
				$name = $wdg["name"];
				$width = (isset( $wdg["width"] )) ? $wdg["width"] : 80;

				$type = $sm->getVarType( $name );
				switch ($type) {
				case "integer":
					$custom = new AdminNumeric( $name , $this );
					break;
				default:
					$custom = new AdminTextbox( $name , $this );
					break;
				}
				$custom->setCSSAttribute( "width" , sprintf( "%dpx" , $width ) );
				$this->aCustoms[$name] = $custom;
			}

			// submit
			$this->params_submit = new AdminSubmit( "params_submit" , $this );
			$this->params_submit->setValue( __("Modify","attr") );
		}

		protected function initializeWidgets() {
			$sm = new SystemParametersModel();
			// load prices
			foreach ($this->aPrices as $name => $wdg) $wdg->setValue( $sm->getVar( $name ) );

			// load others
			foreach ($this->aCustoms as $name => $wdg) $wdg->setValue( $sm->getVar( $name ) );

			$names = array();
			foreach ($this->customparams as $wdg) $names[]=$wdg["name"];

			$this->setparam( "customs" , $sm->getVarDescription( $names ) );

			// language editor mode
			$this->setParam( "bLanguageEditorMode" , fo3_get_language_editor_mode() );
		}

		public function handleEvents() {
			$this->refreshChildValuesFromArray();
			if (!AdminUserModel::isAdminLoggedIn()) return parent::handleEvents();
			if (!$this->params_submit->isSubmitted()) return parent::handleEvents();
			$sm = new SystemParametersModel();
			// save prices
			foreach ($this->aPrices as $name => $wdg) $sm->setVar( $name , $wdg->getValue() );
			// save customs
			foreach ($this->aCustoms as $name => $wdg) $sm->setVar( $name , $wdg->getValue() );

			// language editor mode
			fo3_set_language_editor_mode( isset( $this->aP["language_editor_mode"] ) );

			FNTH_refresh();
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->initializeWidgets();
			return parent::render();
		}

	}


?>
