<?php
abstract class MyWeb_File_Form_AbstractIni extends MyWeb_File_File
{
	
	protected $_typeFromRequired = array('edit','new','login');
	protected $_currentTypeForm = NULL;
	
	/**
	 * nastavi policka jako vyzadovana => je nutne je vyplnit
	 * Enter description here ...
	 */
	protected function setRequiredInput($nameColumn, $currentType, $type)
	{
		$stringRequired = "";
		if (!is_array($type)){
			throw new MyWeb_Model_Exception('Class: MyWeb_File_Form_AbstractIni Method: setRequiredInput $type is not array');
		}
		if (in_array($currentType, $type)){
			$stringRequired ="elements.".$nameColumn.".Options.required = true \n";
		}
		return $stringRequired;
	}
	
	/**
	 * matoda prida do ini file validatory k prvkum podle db
	 * Enter description here ...
	 */
	protected function addValidators($typeColumn,$nameColumn,$value)
	{
		$size = NULL;
		$dataType = NULL;
		$stringFile = '';
		$inputType = 'text';
		
		if (!empty($value)){
			$inputType = 'hidden';
		}
		// vyextrhuje z varchar(45) velikost 45
			if (substr_count($typeColumn, '(') == 1) {
				$size=intval(
							substr(
								$typeColumn,
								strpos($typeColumn,'(')+1,
								strpos($typeColumn,')')-1
								)
							);
			}
				
			// odstranim zavorky a prazdna mista
			$position = strpos($typeColumn, '(');
			if ($position){
				$dataType = substr_replace($typeColumn, '', $position, strlen($typeColumn));
			}else {
				$dataType = $typeColumn;
			}
			$dataType = strtolower(trim($dataType));
			$nameElement = $nameColumn.$value;
				
				
			$stringFile.="\nelements.".$nameColumn.$value.".Type = ".$inputType." \n";
			
			if (strcmp($inputType, 'hidden') !== 0) {
				//$stringFile.="elements.".$nameColumn.$value.".Options.required = true \n";
				$stringFile.="elements.".$nameColumn.$value.".Options.label = ".$nameColumn."\n";
				$stringFile.=$this->setRequiredInput($nameColumn.$value, $this->_currentTypeForm, $this->_typeFromRequired);
			if (!is_null($size)) {
				$stringFile.="elements.".$nameColumn.$value.".Options.size = ".$size."\n";
				$stringFile.="elements.".$nameColumn.$value.".Options.maxlength = ".$size."\n";
			}
			$stringFile.= "\n";
			
			// nastaveni validatoru
			switch (1) {
				case substr_count($dataType,'char'):
					//echo "<p>".$dataType."char".$nameColumn."</p>";
					if (!is_null($size)) {
						$stringFile.="elements.".$nameElement.".options.validators.length.validator = \"StringLength\" \n";
						$stringFile.="elements.".$nameElement.".options.validators.length.options.1 = 2 \n";
						$stringFile.="elements.".$nameElement.".options.validators.length.options.2 = ".$size." \n";
					}
					$stringFile.="elements.".$nameElement.".options.validators.alnum.validator = \"Alnum\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.alnum.options.1 = true \n\n";
					break;
				case substr_count($dataType,"text"):
					//echo "<p>"."text".$nameColumn."</p>";
					if (!is_null($size)) {
						$stringFile.= "\n";
						$stringFile.="elements.".$nameElement.".options.validators.length.validator = \"StringLength\" \n";
						$stringFile.="elements.".$nameElement.".options.validators.length.options.1 = 2 \n";
						$stringFile.="elements.".$nameElement.".options.validators.length.options.2 = ".$size." \n";
					}
					$stringFile.="elements.".$nameElement.".options.validators.alnum.validator = \"Alnum\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.alnum.options.1 = true \n\n";
					break;
				// numeric datatypes	
				case substr_count($dataType,"bit"):
					//echo "<p>"."bit".$nameColumn."</p>";
					$stringFile.="elements.".$nameElement.".options.validators.digits.validator = \"Digits\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.digits.options.1 = true \n\n";
					break;
				case substr_count($dataType,"int"):
					//echo "<p>"."int".$nameColumn."</p>";
					$stringFile.="elements.".$nameElement.".options.validators.int.validator = \"Int\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.digits.validator = \"Digits\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.digits.options.1 = true \n\n";
					break;
				case substr_count($dataType,"float"):
					//echo "<p>"."float".$nameColumn."</p>";
					$stringFile.="elements.".$nameElement.".options.validators.float.validator = \"Float\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.digits.validator = \"Digits\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.digits.options.1 = true \n\n";
					break;
				case substr_count($dataType,"double"):
					//echo "<p>"."double".$nameColumn."</p>";
					$stringFile.="elements.".$nameElement.".options.validators.float.validator = \"Float\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.digits.validator = \"Digits\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.digits.options.1 = true \n\n";
				case substr_count($dataType,"bool"):
					//echo "<p>"."bool".$nameColumn."</p>";
					break;
				case substr_count($dataType,"dec"):
					//echo "<p>"."dec".$nameColumn."</p>";
					$stringFile.="elements.".$nameElement.".options.validators.digits.validator = \"Digits\" \n";
					$stringFile.="elements.".$nameElement.".options.validators.digits.options.1 = true \n\n";
					break;
			}
			}
			return $stringFile;
	}
}