1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/Types/Type.php');
|
---|
4 | include_once(dirname(__FILE__).'/Module.php');
|
---|
5 | include_once(dirname(__FILE__).'/Html.php');
|
---|
6 |
|
---|
7 | class Form extends Module
|
---|
8 | {
|
---|
9 | var $Definition = array();
|
---|
10 | var $Values = array();
|
---|
11 | var $OnSubmit = '';
|
---|
12 |
|
---|
13 | function __construct($System, $FormClass)
|
---|
14 | {
|
---|
15 | parent::__construct($System);
|
---|
16 | $this->Definition = $FormClass;
|
---|
17 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
18 | {
|
---|
19 | $this->Values[$Index] = '';
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | function ShowTable()
|
---|
24 | {
|
---|
25 | $Output = $this->ShowTableBlock();
|
---|
26 | return($Output);
|
---|
27 | }
|
---|
28 |
|
---|
29 | function ShowTableBlock($Context = '')
|
---|
30 | {
|
---|
31 | global $FormTypes;
|
---|
32 |
|
---|
33 | $Table = array(
|
---|
34 | 'Header' => array('Položka', 'Hodnota'),
|
---|
35 | 'Rows' => array(),
|
---|
36 | );
|
---|
37 | if($Context != '') $Context = $Context.'-';
|
---|
38 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
39 | {
|
---|
40 | if($Item['Type'] != 'Hidden')
|
---|
41 | {
|
---|
42 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
43 | $Edit = ExecuteTypeEvent($this->System, $Item['Type'], 'OnView',
|
---|
44 | array('Name' => $Index, 'Value' => $this->Values[$Index], 'Type' => $Item['Type']));
|
---|
45 | array_push($Table['Rows'], array($Item['Caption'], $Edit));
|
---|
46 | }
|
---|
47 | }
|
---|
48 | $Html = new Html();
|
---|
49 | $Output = '<div style="text-align: center">'.$this->Definition['Title'].'</div>'.$Html->Table($Table, 'WideTable');
|
---|
50 | return($Output);
|
---|
51 | }
|
---|
52 |
|
---|
53 | function ShowEditForm()
|
---|
54 | {
|
---|
55 | if(!array_key_exists('SubmitText', $this->Definition)) $this->Definition['SubmitText'] = 'Uložit';
|
---|
56 | $Output = '<form class="Form" action="'.$this->OnSubmit.'" method="post">'.$this->ShowEditBlock().'<div><input type="submit" value="'.$this->Definition['SubmitText'].'" /></div></form>';
|
---|
57 | return($Output);
|
---|
58 | }
|
---|
59 |
|
---|
60 | function ShowEditBlock($Context = '')
|
---|
61 | {
|
---|
62 | global $FormTypes;
|
---|
63 |
|
---|
64 | $Table = array(
|
---|
65 | //'Header' => array('Položka', 'Hodnota'),
|
---|
66 | 'Rows' => array(),
|
---|
67 | );
|
---|
68 | if($Context != '') $Context = $Context.'-';
|
---|
69 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
70 | {
|
---|
71 | if($Item['Type'] != 'Hidden')
|
---|
72 | {
|
---|
73 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
74 | $Edit = ExecuteTypeEvent($this->System, $Item['Type'], 'OnEdit', array('Name' => $Index, 'Value' => $this->Values[$Index], 'Type' => $Item['Type']));
|
---|
75 | array_push($Table['Rows'], array($Item['Caption'].':', $Edit));
|
---|
76 | }
|
---|
77 | }
|
---|
78 | $Html = new Html();
|
---|
79 | $Output = '<fieldset><legend>'.$this->Definition['Title'].'</legend>'.$Html->Table($Table, 'BasicTable').
|
---|
80 | '</fieldset>';
|
---|
81 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
82 | if($Item['Type'] == 'Hidden') $Output .= ExecuteTypeEvent($this->System, $Item['Type'], 'OnEdit', array('Name' => $Index, 'Value' => $this->Values[$Index], 'Type' => $Item['Type']));
|
---|
83 | return($Output);
|
---|
84 | }
|
---|
85 |
|
---|
86 | function LoadValuesFromDatabase($Id)
|
---|
87 | {
|
---|
88 | $DbResult = $this->Database->query('SELECT T.* FROM '.$this->Definition['Table'].' AS T WHERE T.Id='.$Id);
|
---|
89 | if($DbResult->num_rows > 0) $DbRow = $DbResult->fetch_assoc();
|
---|
90 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
91 | {
|
---|
92 | if($Item['Type'] != 'Hidden')
|
---|
93 | if($DbResult->num_rows > 0) $this->Values[$Index] = $DbRow[$Index];
|
---|
94 | else $this->Values[$Index] = '';
|
---|
95 | }
|
---|
96 | return($DbResult->num_rows > 0);
|
---|
97 | }
|
---|
98 |
|
---|
99 | function SaveValuesToDatabase($Id)
|
---|
100 | {
|
---|
101 | if($Id == 0)
|
---|
102 | {
|
---|
103 | $this->Values['Id'] = $Id;
|
---|
104 | $DbResult = $this->Database->replace($this->Definition['Table'], $this->Values);
|
---|
105 | } else
|
---|
106 | $DbResult = $this->Database->update($this->Definition['Table'], 'Id='.$Id, $this->Values);
|
---|
107 | }
|
---|
108 |
|
---|
109 | function LoadValuesFromForm()
|
---|
110 | {
|
---|
111 | $this->Values = $this->LoadValuesFromFormBlock();
|
---|
112 | }
|
---|
113 |
|
---|
114 | function LoadValuesFromFormBlock($Context = '')
|
---|
115 | {
|
---|
116 | global $FormTypes;
|
---|
117 |
|
---|
118 | if($Context != '') $Context = $Context.'-';
|
---|
119 | $Values = array();
|
---|
120 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
121 | {
|
---|
122 | if(array_key_exists($Context.$Index, $_POST))
|
---|
123 | $Values[$Index] = ExecuteTypeEvent($this->System, $Item['Type'], 'OnLoad',
|
---|
124 | array('Name' => $Index, 'Type' => $Item['Type']));
|
---|
125 | }
|
---|
126 | return($Values);
|
---|
127 | }
|
---|
128 | }
|
---|