source: trunk/includes/PageEdit.php

Last change on this file was 893, checked in by chronos, 21 months ago
  • Fixed: Class types casting for better type checking.
  • Fixed: XML direct export.
  • Modified: User class instance moved from Core class to ModuleUser class.
File size: 5.7 KB
Line 
1<?php
2
3class PageEdit extends Page
4{
5 public string $Table;
6 public string $TableSQL;
7 public array $Definition;
8 public array $ItemActions;
9 public bool $AllowEdit;
10
11 function __construct($System)
12 {
13 parent::__construct($System);
14 $this->Table = '';
15 $this->Definition = array();
16 $this->ItemActions = array(
17 array('Name' => T('View'), 'URL' => '?action=view&amp;id=#Id'),
18 );
19 $this->AllowEdit = false;
20 if ($this->AllowEdit) $this->ItemActions[] = array('Name' => T('Delete'), 'URL' => '?action=remove&amp;id=#Id');
21 }
22
23 function Show(): string
24 {
25 $Output = '';
26 if (array_key_exists('action', $_GET))
27 {
28 if ($_GET['action'] == 'add') $Output .= $this->AddItem();
29 else if ($_GET['action'] == 'view') $Output .= $this->ViewItem();
30 //else if ($_GET['action'] == 'edit') $Output .= $this->ModifyItem();
31 //else if ($_GET['action'] == 'remove') $Output .= $this->RemoveItem();
32 else if ($_GET['action'] == 'delete') $Output .= $this->DeleteItem();
33 else $Output .= ShowMessage(T('Unknown action'), MESSAGE_CRITICAL);
34 } else $Output .= $this->ViewList();
35 return $Output;
36 }
37
38 function ViewItem()
39 {
40 $DbResult = $this->Database->query('SELECT * FROM ('.$this->TableSQL.') AS `T` WHERE `Id`='.$_GET['id']);
41 if ($DbResult->num_rows > 0)
42 {
43 $DbRow = $DbResult->fetch_assoc();
44
45 $Output = T('Item').
46 '<table>';
47 foreach ($this->Definition as $DefIndex => $Def)
48 {
49 $Output .= '<tr><td>'.$Def['Title'].':</td><td>'.$this->ViewControl($Def['Type'], $DefIndex, $DbRow[$DefIndex]).'</tr>';
50 }
51 $Output .= '<tr>'.
52 '</table>';
53 } else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
54 if ($this->AllowEdit)
55 {
56 $Output .= '<a href="?action=add">'.T('Add').'</a> ';
57 $Output .= '<a href="?action=edit&amp;id='.$_GET['id'].'">'.T('Edit').'</a> ';
58 $Output .= '<a href="?action=delete&amp;id='.$_GET['id'].'">'.T('Add').'</a> ';
59 }
60 $Output .= '<a href="?">'.T('List').'</a><br/>';
61 return $Output;
62 }
63
64 function ViewList()
65 {
66 $DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$this->TableSQL.') AS `T`');
67 $DbRow = $DbResult->fetch_row();
68 $PageList = GetPageList($DbRow[0]);
69 $Output = $PageList['Output'];
70
71 $Output .= '<table class="BaseTable">';
72 $TableColumns = array();
73 foreach ($this->Definition as $Index => $Def)
74 if ($Def['InList'])
75 $TableColumns[] = array('Name' => $Index, 'Title' => $Def['Title']);
76 if (count($this->ItemActions) > 0)
77 $TableColumns[] = array('Name' => '', 'Title' => 'Akce');
78
79 $Order = GetOrderTableHeader($TableColumns, 'Name', 0);
80 $Output .= $Order['Output'];
81
82 $DbResult = $this->Database->query('SELECT * FROM ('.$this->Table.') '.$Order['SQL'].$PageList['SQLLimit']);
83 while ($Item = $DbResult->fetch_assoc())
84 {
85 $Output .= '<tr>';
86 foreach ($this->Definition as $Index => $Def)
87 if ($Def['InList'])
88 {
89 if ($Def['Type'] == 'URL') $Output .= '<td><a href="'.$Item[$Index].'">'.$Item[$Index].'</a></td>';
90 else $Output .= '<td>'.$Item[$Index].'</td>';
91 }
92 if (count($this->ItemActions) > 0)
93 {
94 $Output .= '<td>';
95 foreach ($this->ItemActions as $Index => $Action)
96 {
97 $URL = $Action['URL'];
98 if (strpos($URL, '#Id')) $URL = str_replace('#Id', $Item['Id'], $URL);
99 $Output .= '<a href="'.$URL.'">'.$Action['Name'].'</a> ';
100 }
101 $Output .= '</td>';
102 }
103 $Output .= '</tr>';
104 }
105 $Output .= '</table>';
106 if ($this->AllowEdit) $Output .= '<a href="?action=add">'.T('Add').'</a><br/>';
107 return $Output;
108 }
109
110 function AddItem()
111 {
112 $Output = '';
113 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
114 if ($User->Licence(LICENCE_USER))
115 {
116 if (array_key_exists('finish', $_GET))
117 {
118 $Items = array();
119 foreach ($this->Definition as $Index => $Def)
120 {
121 $Items[$Index] = $_POST[$Index];
122 }
123 $this->Database->insert($this->Table, $Items);
124 $Output = ShowMessage(T('Item added'), MESSAGE_INFORMATION);
125 } else
126 {
127 $Output .= '<form action="?action=add&amp;finish=1" method="post">'.
128 '<fieldset><legend>'.T('New item').'</legend>'.
129 '<table>';
130 foreach ($this->Definition as $DefIndex => $Def)
131 {
132 $Output .= '<tr><td>'.$Def['Title'].':</td><td>'.$this->GetControl($Def['Type'], $DefIndex, '').'</tr>';
133 }
134 $Output .= '<tr><td colspan="2"><input type="submit" value="'.T('Add').'" /></td></tr>'.
135 '</table>'.
136 '</fieldset>'.
137 '</form>';
138 }
139 } else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
140 return $Output;
141 }
142
143 function DeleteItem()
144 {
145 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
146 if ($User->Licence(LICENCE_USER))
147 {
148 $this->Database->query('DELETE FROM `'.$this->Table.'` WHERE (`User`='.$User->Id.') AND (`Id`='.($_GET['id'] * 1).')');
149 $Output = ShowMessage(T('Record removed'));
150 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
151 return $Output;
152 }
153
154 function GetControl($Type, $Name, $Value)
155 {
156 if ($Type == 'Text') $Output = '<input type="text" name="'.$Name.'" value="'.$Value.'"/>';
157 else if ($Type == 'Boolean') $Output = '<input type="checkbox" name="'.$Name.'"/>';
158 else $Output = '<input type="text" name="'.$Name.'" value="'.$Value.'"/>';
159 return $Output;
160 }
161
162 function ViewControl($Type, $Name, $Value)
163 {
164 if ($Type == 'Text') $Output = $Value;
165 else if ($Type == 'URL') $Output = '<a href="'.$Value.'">'.$Value.'</a>';
166 else if ($Type == 'Boolean') $Output = $Value;
167 else $Output = $Value;
168 return $Output;
169 }
170}
Note: See TracBrowser for help on using the repository browser.