FormManager = &$FormManager; $this->Database = $FormManager->Database; $this->Definition = array(); $this->Values = array(); $this->ValuesFilter = array(); $this->ValuesValidate = array(); $this->OnSubmit = ''; } function LoadDefaults() { foreach($this->Definition['Items'] as $Index => $Item) if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) { if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default']; } } function SetClass($Name) { $this->Definition = &$this->FormManager->Classes[$Name]; $this->LoadDefaults(); } function ShowViewForm() { $Table = array( //'Header' => array('Položka', 'Hodnota'), 'Rows' => array(), ); foreach($this->Definition['Items'] as $Index => $Item) if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) { if(array_key_exists($Item['Type'], $this->FormManager->FormTypes)) { if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') $UseType = 'OneToMany'; else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') $UseType = 'Enumeration'; } else $UseType = $Item['Type']; $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnView', array('Value' => $this->Values[$Index], 'Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values, 'Filter' => $this->ValuesFilter[$Index])); if(array_key_exists('Suffix', $Item)) $Edit .= ' '.$Item['Suffix']; if(!$this->FormManager->Type->IsHidden($UseType)) array_push($Table['Rows'], array($Item['Caption'].':', $Edit)); } $Output = '
'.$this->Definition['Title'].''.Table($Table). '
'; return($Output); } function ShowEditForm() { if(!array_key_exists('SubmitText', $this->Definition)) $this->Definition['SubmitText'] = 'Uložit'; $Output = '
'.$this->ShowEditBlock(). '
'. '
'; return($Output); } function ShowEditBlock($Context = '') { $Hidden = ''; $IsHidden = false; $Table = array( //'Header' => array('Položka', 'Hodnota'), 'Rows' => array(), ); if($Context != '') $Context = $Context.'-'; foreach($this->Definition['Items'] as $Index => $Item) { if(!array_key_exists('ReadOnly', $Item)) $Item['ReadOnly'] = false; if($Item['ReadOnly'] == false) if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) { $Parameters = array('Value' => $this->Values[$Index], 'Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values); if(array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null']; else unset($Parameters['Null']); if(array_key_exists('OnPreset', $Item)) $Parameters['OnPreset'] = $Item['OnPreset']; else unset($Parameters['OnPreset']); if(array_key_exists($Item['Type'], $this->FormManager->FormTypes)) { if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') { $UseType = 'OneToMany'; } else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') $UseType = 'Enumeration'; } else $UseType = $Item['Type']; $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnEdit', $Parameters); if(array_key_exists('Suffix', $Item)) $Edit .= $Item['Suffix']; $Caption = $Item['Caption'].':'; if(array_key_exists($Index, $this->ValuesValidate) and $this->ValuesValidate[$Index]) { $Format = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'GetValidationFormat', array()); if($Format != '') $Caption .= '
'.$Format.''; $Caption = ''.$Caption.''; } if(!$this->FormManager->Type->IsHidden($UseType)) array_push($Table['Rows'], array($Caption, $Edit)); else $Hidden .= $Edit; } } $Output = '
'.$this->Definition['Title'].''.Table($Table). $Hidden.'
'; return($Output); } function LoadValuesFromDatabase($Id) { foreach($this->Definition['Items'] as $Index => $Item) if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) { if(array_key_exists($Item['Type'], $this->FormManager->FormTypes)) { if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') $UseType = 'OneToMany'; else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') $UseType = 'Enumeration'; } else $UseType = $Item['Type']; if(!array_key_exists('SQL', $Item)) $Item['SQL'] = ''; else $Item['SQL'] = str_replace('#Id', $Id, $Item['SQL']); $Columns[] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnFilterNameQuery', array('Name' => $Index, 'Type' => $Item['Type'], 'SQL' => $Item['SQL'])); } $Columns = implode(',', $Columns); if(array_key_exists('SQL', $this->Definition)) $SourceTable = '('.$this->Definition['SQL'].') AS `TX`'; else $SourceTable = '`'.$this->Definition['Table'].'` AS `TX`'; $DbResult = $this->Database->query('SELECT '.$Columns.' FROM '.$SourceTable.' WHERE `TX`.`Id`='.$Id); $DbRow = $DbResult->fetch_array(); foreach($this->Definition['Items'] as $Index => $Item) if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) { if(array_key_exists($Item['Type'], $this->FormManager->FormTypes)) { if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') $UseType = 'OneToMany'; else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') $UseType = 'Enumeration'; } else $UseType = $Item['Type']; $this->Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnLoadDb', array('Value' => $DbRow[$Index], 'Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values)); $this->ValuesFilter[$Index] = $DbRow[$Index.'_Filter']; } } function SaveValuesToDatabase($Id) { $Values = array(); foreach($this->Definition['Items'] as $Index => $Item) { if(!array_key_exists('ReadOnly', $Item)) $Item['ReadOnly'] = false; if($Item['ReadOnly'] == false) if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) { $Parameters = array('Value' => $this->Values[$Index], 'Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values); if(array_key_exists($Item['Type'], $this->FormManager->FormTypes)) { if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') { $UseType = 'OneToMany'; } else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') $UseType = 'Enumeration'; } else $UseType = $Item['Type']; $Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnSaveDb', $Parameters); if(($Item['Type'] == 'Password') and ($Values[$Index] == '')) unset($Values[$Index]); } } if($Id == 0) { $Values['Id'] = $Id; $DbResult = $this->Database->insert($this->Definition['Table'], $Values); } else $DbResult = $this->Database->update($this->Definition['Table'], 'Id='.$Id, $Values); } function LoadValuesFromForm() { $this->Values = $this->LoadValuesFromFormBlock(); } function LoadValuesFromFormBlock($Context = '') { if($Context != '') $Context = $Context.'-'; $Values = array(); foreach($this->Definition['Items'] as $Index => $Item) if((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and (!array_key_exists('ReadOnly', $Item) or (array_key_exists('ReadOnly', $Item) and ($Item['ReadOnly'] != true)))) { //if(array_key_exists($Context.$Index, $_POST)) if(array_key_exists($Item['Type'], $this->FormManager->FormTypes)) { if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); $CustomType = $this->FormManager->FormTypes[$Item['Type']]['Type']; if($CustomType == 'Reference') $UseType = 'OneToMany'; else if($CustomType == 'Enumeration') $UseType = 'Enumeration'; } else $UseType = $Item['Type']; $Parameters = array('Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values); if(array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null']; else unset($Parameters['Null']); $Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnLoad', $Parameters); } return($Values); } function Validate() { $Valid = true; foreach($this->Definition['Items'] as $Index => $Item) if((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and (!array_key_exists('ReadOnly', $Item) or (array_key_exists('ReadOnly', $Item) and ($Item['ReadOnly'] != true)))) { //if(array_key_exists($Context.$Index, $_POST)) if(array_key_exists($Item['Type'], $this->FormManager->FormTypes)) { if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); $CustomType = $this->FormManager->FormTypes[$Item['Type']]['Type']; if($CustomType == 'Reference') $UseType = 'OneToMany'; else if($CustomType == 'Enumeration') $UseType = 'Enumeration'; } else $UseType = $Item['Type']; $Parameters = array('Value' => $this->Values[$Index]); if(array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null']; else $Parameters['Null'] = false; if(!$this->FormManager->Type->ExecuteTypeEvent($UseType, 'Validate', $Parameters)) { $this->ValuesValidate[$Index] = true; $Valid = false; } } if($Valid == false) throw new Exception('not validated'); return($Valid); } } function MakeLink($Target, $Title) { return(''.$Title.''); } function Table($Table) { $Result = ''; if(array_key_exists('Header', $Table)) { $Result .= ''; foreach($Table['Header'] as $Item) $Result .= ''; $Result .= ''; } foreach($Table['Rows'] as $Row) { $Result .= ''; foreach($Row as $Index => $Item) { if($Index == 0) $Class = ' class="Header"'; else $Class = ''; $Result .= ''.$Item.''; } $Result .= ''; } $Result .= '
'.$Item.'
'; return($Result); } class FormManager { var $Classes; var $FormTypes; var $Database; var $Type; var $RootURL; var $ShowRelation; function __construct($Database) { $this->Database = &$Database; $this->Classes = array(); $this->FormTypes = array(); $this->Type = new Type($this); $this->ShowRelation = false; } function RegisterClass($Name, $Class) { $this->Classes[$Name] = $Class; } function UnregisterClass($Name) { unset($this->Classes[$Name]); } function RegisterFormType($Name, $Class) { $this->FormTypes[$Name] = $Class; } function UnregisterFormType($Name) { unset($this->FormTypes[$Name]); } }