Changeset 373 for trunk/Common/View.php
- Timestamp:
- Jan 19, 2012, 9:43:36 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/View.php
r372 r373 19 19 define('ViewItemTypeURL', 'URL'); 20 20 define('ViewItemTypeImage', 'Image'); 21 define('ViewItemTypeEnumeration', 'Enumeration'); 21 22 22 class View 23 class View 23 24 { 25 var $Database; 24 26 var $Name; 25 27 var $Title; 26 28 var $Items; 27 29 var $ModelName; 28 var $SubmitText; 29 var $Values = array(); 30 var $OnSubmit = ''; 31 var $Database; 32 30 33 31 function __construct($Database) 34 32 { … … 36 34 } 37 35 38 function AddItemOneToMany($Name, $Title, $TargetModel, $Default)39 {40 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeOneToMany, 'Default' => $Default,41 'TargetModel' => $TargetModel);42 }43 44 function AddItemManyToOne($Name, $Title, $TargetModel, $Default)45 {46 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeManyToOne, 'Default' => $Default,47 'TargetModel' => $TargetModel);48 }49 50 function AddItemInteger($Name, $Title, $Default)51 {52 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeInteger, 'Default' => $Default);53 }54 55 function AddItemFloat($Name, $Title, $Default)56 {57 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeFloat, 'Default' => $Default);58 }59 60 function AddItemText($Name, $Title, $Default)61 {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);68 }69 70 function AddItemBoolean($Name, $Title, $Default)71 {72 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeBoolean, 'Default' => $Default);73 }74 75 function AddItemDate($Name, $Title, $Default)76 {77 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeDate, 'Default' => $Default);78 }79 80 function AddItemTime($Name, $Title, $Default)81 {82 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeTime, 'Default' => $Default);83 }84 85 function AddItemDateTime($Name, $Title, $Default)86 {87 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeDateTime, 'Default' => $Default);88 }89 90 function AddItemIPv4($Name, $Title, $Default)91 {92 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeIPv4, 'Default' => $Default);93 }94 95 function AddItemIPv6($Name, $Title, $Default)96 {97 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeIPv6, 'Default' => $Default);98 }99 100 function AddItemMACAddress($Name, $Title, $Default)101 {102 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeMACAddress, 'Default' => $Default);103 }104 105 function AddItemFileName($Name, $Title, $Default)106 {107 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeFileName, 'Default' => $Default);108 }109 110 function AddItemURL($Name, $Title, $Default)111 {112 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeURL, 'Default' => $Default);113 }114 115 function AddItemPassword($Name, $Title, $Default)116 {117 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypePassword, 'Default' => $Default);118 }119 120 function AddItemImage($Name, $Title, $Default)121 {122 $this->Items[$Name] = array('Name' => $Name, 'Title' => $Title, 'Type' => ViewItemTypeImage, 'Default' => $Default);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 types196 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 password231 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 passwords247 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 } else259 $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 types314 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 }329 }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 36 } 363 37
Note:
See TracChangeset
for help on using the changeset viewer.