Changeset 372 for trunk/Common/View.php
- Timestamp:
- Jan 19, 2012, 1:58:03 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/View.php
r370 r372 1 1 <?php 2 2 3 define('View TypeDate', 'Date');4 define('View TypeTime', 'Time');5 define('View TypeDateTime', 'DateTime');6 define('View TypeText', 'Text');7 define('View TypeString', 'String');8 define('View TypeBoolean', 'Boolean');9 define('View TypeInteger', 'Integer');10 define('View TypeFloat', 'Float');11 define('View TypeOneToMany', 'OneToMany');12 define('View TypeManyToOne', 'ManyToOne');13 define('View TypeManyToMany', 'ManyToMany');14 define('View TypeIPv4', 'IPv4');15 define('View TypeIPv6', 'IPv6');16 define('View TypeMACAddrees', 'MACAddress');17 define('View TypeFileName', 'FileName');18 define('View TypePassword', 'Password');19 define('View TypeURL', 'URL');20 define('View TypeImage', 'Image');3 define('ViewItemTypeDate', 'Date'); 4 define('ViewItemTypeTime', 'Time'); 5 define('ViewItemTypeDateTime', 'DateTime'); 6 define('ViewItemTypeText', 'Text'); 7 define('ViewItemTypeString', 'String'); 8 define('ViewItemTypeBoolean', 'Boolean'); 9 define('ViewItemTypeInteger', 'Integer'); 10 define('ViewItemTypeFloat', 'Float'); 11 define('ViewItemTypeOneToMany', 'OneToMany'); 12 define('ViewItemTypeManyToOne', 'ManyToOne'); 13 define('ViewItemTypeManyToMany', 'ManyToMany'); 14 define('ViewItemTypeIPv4', 'IPv4'); 15 define('ViewItemTypeIPv6', 'IPv6'); 16 define('ViewItemTypeMACAddrees', 'MACAddress'); 17 define('ViewItemTypeFileName', 'FileName'); 18 define('ViewItemTypePassword', 'Password'); 19 define('ViewItemTypeURL', 'URL'); 20 define('ViewItemTypeImage', 'Image'); 21 21 22 22 class View … … 27 27 var $ModelName; 28 28 var $SubmitText; 29 var $Values = array(); 30 var $OnSubmit = ''; 31 var $Database; 29 32 33 function __construct($Database) 34 { 35 $this->Database = &$Database; 36 } 37 30 38 function AddItemOneToMany($Name, $Title, $TargetModel, $Default) 31 39 { 32 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeOneToMany, 'Default' => $Default,40 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeOneToMany, 'Default' => $Default, 33 41 'TargetModel' => $TargetModel); 34 42 } … … 36 44 function AddItemManyToOne($Name, $Title, $TargetModel, $Default) 37 45 { 38 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' => ItemTypeOneToMany, 'Default' => $Default,46 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeManyToOne, 'Default' => $Default, 39 47 'TargetModel' => $TargetModel); 40 48 } … … 42 50 function AddItemInteger($Name, $Title, $Default) 43 51 { 44 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeInteger, 'Default' => $Default);52 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeInteger, 'Default' => $Default); 45 53 } 46 54 47 55 function AddItemFloat($Name, $Title, $Default) 48 56 { 49 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeFloat, 'Default' => $Default);57 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeFloat, 'Default' => $Default); 50 58 } 51 59 52 60 function AddItemText($Name, $Title, $Default) 53 61 { 54 $this->Items[] = array('Name' => 'Name', 'Title' => $Title, 'Type' => ItemTypeText, 'Default' => $Default); 62 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeText, 'Default' => $Default); 63 } 64 65 function AddItemString($Name, $Title, $Default) 66 { 67 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeString, 'Default' => $Default); 55 68 } 56 69 57 70 function AddItemBoolean($Name, $Title, $Default) 58 71 { 59 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeBoolean, 'Default' => $Default);72 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeBoolean, 'Default' => $Default); 60 73 } 61 74 62 75 function AddItemDate($Name, $Title, $Default) 63 76 { 64 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeDate, 'Default' => $Default);77 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeDate, 'Default' => $Default); 65 78 } 66 79 67 80 function AddItemTime($Name, $Title, $Default) 68 81 { 69 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeTime, 'Default' => $Default);82 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeTime, 'Default' => $Default); 70 83 } 71 84 72 85 function AddItemDateTime($Name, $Title, $Default) 73 86 { 74 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeDateTime, 'Default' => $Default);87 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeDateTime, 'Default' => $Default); 75 88 } 76 89 77 90 function AddItemIPv4($Name, $Title, $Default) 78 91 { 79 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeIPv4, 'Default' => $Default);92 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeIPv4, 'Default' => $Default); 80 93 } 81 94 82 95 function AddItemIPv6($Name, $Title, $Default) 83 96 { 84 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeIPv6, 'Default' => $Default);97 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeIPv6, 'Default' => $Default); 85 98 } 86 99 87 100 function AddItemMACAddress($Name, $Title, $Default) 88 101 { 89 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeMACAddress, 'Default' => $Default);102 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeMACAddress, 'Default' => $Default); 90 103 } 91 104 92 105 function AddItemFileName($Name, $Title, $Default) 93 106 { 94 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeFileName, 'Default' => $Default);107 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeFileName, 'Default' => $Default); 95 108 } 96 109 97 110 function AddItemURL($Name, $Title, $Default) 98 111 { 99 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeURL, 'Default' => $Default);112 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeURL, 'Default' => $Default); 100 113 } 101 114 102 115 function AddItemPassword($Name, $Title, $Default) 103 116 { 104 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypePassword, 'Default' => $Default);117 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypePassword, 'Default' => $Default); 105 118 } 106 119 107 120 function AddItemImage($Name, $Title, $Default) 108 121 { 109 $this->Items[ ] = array('Name' => 'Name', 'Title' => $Title, 'Type' =>ItemTypeImage, 'Default' => $Default);122 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeImage, 'Default' => $Default); 110 123 } 124 125 function ShowEditForm() 126 { 127 if($this->SubmitText == '') $this->SubmitText = 'Uložit'; 128 $Output = '<form class="Form" action="'.$this->OnSubmit.'" method="post">'.$this->ShowEditBlock().'<div><input type="submit" value="'.$this->SubmitText.'" /></div></form>'; 129 return($Output); 130 } 131 132 function ShowEditBlock($Context = '') 133 { 134 $Table = array( 135 //'Header' => array('Položka', 'Hodnota'), 136 'Rows' => array(), 137 ); 138 if($Context != '') $Context = $Context.'-'; 139 foreach($this->Items as $Index => $Item) 140 { 141 if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default']; 142 switch($Item['Type']) 143 { 144 case ViewItemTypeBoolean: 145 if($this->Values[$Index] == 0) $Checked = ''; else $Checked = ' CHECKED'; 146 $Edit = '<input type="checkbox" name="'.$Context.$Index.'"'.$Checked.' />'; 147 break; 148 case ViewItemTypeString: 149 $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />'; 150 break; 151 case ViewItemTypeText: 152 $Edit = '<textarea style="width: 98%; height: 200px;" name="'.$Context.$Index.'">'.$this->Values[$Index].'</textarea>'; 153 break; 154 case ViewItemTypePassword: 155 $Edit = '<input style="width: 98%;" type="password" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />'; 156 break; 157 case ViewItemTypeInteger: 158 $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />'; 159 break; 160 case ViewItemTypeFloat: 161 $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />'; 162 break; 163 case ViewItemTypeTime: 164 if($this->Values[$Index] == 'Now') $this->Values[$Index] = date('j.n.Y'); 165 $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />'; 166 break; 167 case ViewItemTypeOneToMany: 168 $Edit = '<select style="width: 100%;" name="'.$Context.$Index.'">'; 169 $DbResult = $this->Database->select($Item['TargetModel'], 'Id, Name', '1 ORDER BY Name'); 170 while($Row = $DbResult->fetch_assoc()) 171 { 172 if($Row['Id'] == $this->Values[$Index]) $Selected = ' selected="selected"'; 173 else $Selected = ''; 174 $Edit .= '<option value="'.$Row['Id'].'"'.$Selected.'>'.$Row['Id'].': '.$Row['Name'].'</option>'; 175 } 176 $Edit .= '</select>'; 177 break; 178 case 'Array': 179 $Form = new Form($Item['ItemClass']); 180 $Edit = '<script type="text/javascript" id="syndication">'. 181 "var Count = 0;". 182 "function AddItem() {". 183 "Count = Count + 1;". 184 "var newcontent = document.createElement('div');". 185 "newcontent.id = '".$Context.$Item['ItemClass']."-' + Count;". 186 "newcontent.innerHTML = '<input type=\"hidden\" name=\"".$Context.$Item['ItemClass']."-' + Count + '\">".$Form->ShowEditBlock($Context.$Item['ItemClass']."-' + Count + '")."';". 187 "var scr = document.getElementById('syndication');". 188 "scr.parentNode.insertBefore(newcontent, scr); }". 189 '</script>'; 190 $Edit .= '<form><input type="button" onclick="AddItem();" value="Přidat položku" /></form>'; 191 break; 192 default: 193 if(array_key_exists($Item['Type'], $FormTypes)) 194 { 195 // Custom types 196 switch($FormTypes[$Item['Type']]['Type']) 197 { 198 case 'Enumeration': 199 $Edit = '<select style="width: 100%;" name="'.$Context.$Index.'">'; 200 foreach($FormTypes[$Item['Type']]['States'] as $StateIndex => $StateName) 201 { 202 if($this->Values[$Index] == $StateIndex) $Selected = 'selected="selected"'; 203 else $Selected = ''; 204 $Edit .= '<option value="'.$StateIndex.'"'.$Selected.'>'.$StateName.'</option>'; 205 } 206 $Edit .= '</select>'; 207 break; 208 default: 209 $Edit = 'Neznámý typ'; 210 } 211 } else $Edit = 'Neznámý typ'; 212 } 213 array_push($Table['Rows'], array($Item['Title'].':', $Edit)); 214 } 215 $Output = '<fieldset><legend>'.$this->Title.'</legend>'.Table($Table). 216 '</fieldset>'; 217 return($Output); 218 } 219 220 function LoadValuesFromDatabase($Id) 221 { 222 $DbResult = $this->Database->query('SELECT T.* FROM '.$this->ModelName.' AS T WHERE T.Id='.$Id); 223 $DbRow = $DbResult->fetch_array(); 224 foreach($this->Items as $Index => $Item) 225 { 226 $this->Values[$Index] = $DbRow[$Index]; 227 switch($Item['Type']) 228 { 229 case ViewItemTypePassword: 230 if($Item['Type'] == ViewItemTypePassword) $this->Values[$Index] = ''; // Dont show password 231 break; 232 case ViewItemTypeTime: 233 $this->Values[$Index] == MysqlDateTimeToTime($this->Values[$Index]); 234 break; 235 } 236 } 237 } 238 239 function SaveValuesToDatabase($Id) 240 { 241 foreach($this->Items as $Index => $Item) 242 { 243 switch($Item['Type']) 244 { 245 case ViewItemTypePassword: 246 if($this->Values[$Index] == '') unset($this->Values[$Index]); // Do not modify empty passwords 247 else $this->Values[$Index] = sha1($this->Values[$Index]); 248 break; 249 case ViewItemTypeTime: 250 $this->Values[$Index] = TimeToMysqlDateTime($this->Values[$Index]); 251 break; 252 } 253 } 254 if($Id == 0) 255 { 256 $this->Values['Id'] = $Id; 257 $DbResult = $this->Database->replace($this->ModelName, $this->Values); 258 } else 259 $DbResult = $this->Database->update($this->ModelName, 'Id='.$Id, $this->Values); 260 //echo($this->Database->LastQuery); 261 } 262 263 function LoadValuesFromForm() 264 { 265 $this->Values = $this->LoadValuesFromFormBlock(); 266 } 267 268 function LoadValuesFromFormBlock($Context = '') 269 { 270 if($Context != '') $Context = $Context.'-'; 271 $Values = array(); 272 foreach($this->Items as $Index => $Item) 273 { 274 if(array_key_exists($Context.$Index, $_POST)) 275 switch($Item['Type']) 276 { 277 case ViewItemTypeBoolean: 278 if(array_key_exists($Context.$Index, $_POST)) $Values[$Index] = 1; 279 else $Values[$Index] = 0; 280 break; 281 case ViewItemTypeString: 282 $Values[$Index] = $_POST[$Context.$Index]; 283 break; 284 case ViewItemTypeText: 285 $Values[$Index] = $_POST[$Context.$Index]; 286 break; 287 case ViewItemTypePassword: 288 $Values[$Index] = $_POST[$Context.$Index]; 289 break; 290 case ViewItemTypeInteger: 291 $Values[$Index] = $_POST[$Context.$Index]; 292 break; 293 case ViewItemTypeFloat: 294 $Values[$Index] = $_POST[$Context.$Index]; 295 break; 296 case ViewItemTypeTime: 297 $Values[$Index] = explode('.', $_POST[$Context.$Index]); 298 $Values[$Index] = mktime(0, 0, 0, $Values[$Index][1], $Values[$Index][0], $Values[$Index][2]); 299 break; 300 case 'Array': 301 $I = 1; 302 //echo('Expect: '.$Context.$Item['ItemClass'].'-'.$I.'<br />'); 303 while(isset($_POST[$Context.$Item['ItemClass'].'-'.$I])) 304 { 305 $Form = new Form($Item['ItemClass']); 306 $Values[$Index][] = $Form->LoadValuesFromFormBlock($Context.$Item['ItemClass'].'-'.$I); 307 $I++; 308 } 309 break; 310 default: 311 if(array_key_exists($Item['Type'], $FormTypes)) 312 { 313 // Custom types 314 switch($FormTypes[$Item['Type']]['Type']) 315 { 316 case 'Enumeration': 317 $Values[$Index] = $_POST[$Context.$Index]; 318 break; 319 case 'Reference': 320 $Values[$Index] = $_POST[$Context.$Index]; 321 break; 322 default: 323 } 324 } 325 } 326 } 327 return($Values); 328 } 111 329 } 112 330 331 function MakeLink($Target, $Title) 332 { 333 return('<a href="'.$Target.'">'.$Title.'</a>'); 334 } 335 336 function Table($Table) 337 { 338 $Result = '<table class="BasicTable">'; 339 if(array_key_exists('Header', $Table)) 340 { 341 $Result .= '<tr>'; 342 foreach($Table['Header'] as $Item) 343 $Result .= '<th>'.$Item.'</th>'; 344 $Result .= '</tr>'; 345 } 346 foreach($Table['Rows'] as $Row) 347 { 348 $Result .= '<tr>'; 349 foreach($Row as $Index => $Item) 350 { 351 if($Index == 0) $Class = ' class="Header"'; else $Class = ''; 352 $Result .= '<td'.$Class.' style="width: '.(floor(100 / count($Row))).'%">'.$Item.'</td>'; 353 } 354 $Result .= '</tr>'; 355 } 356 $Result .= '</table>'; 357 return($Result); 358 } 359 360 function ShowEditTable($ClassName, $Values) 361 { 362 } 363 113 364 ?>
Note:
See TracChangeset
for help on using the changeset viewer.