Changeset 922 for trunk/Common/Form/Form.php
- Timestamp:
- Feb 28, 2022, 8:53:22 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/Form/Form.php
r914 r922 2 2 3 3 include_once(dirname(__FILE__).'/Types/Type.php'); 4 include_once(dirname(__FILE__).'/FormManager.php'); 4 5 5 6 /* … … 19 20 */ 20 21 21 22 22 class Form 23 23 { … … 67 67 { 68 68 $Item = $this->Definition['Items'][$Index]; 69 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 70 { 71 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 72 $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); 73 if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') 74 $UseType = 'OneToMany'; 75 else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') 76 $UseType = 'Enumeration'; 77 } else $UseType = $Item['Type']; 69 $UseType = $this->GetItemType($Item); 78 70 return $this->FormManager->Type->ExecuteTypeEvent($UseType, $Event, 79 80 81 71 array('Value' => $this->Values[$Index], 'Name' => $Index, 72 'Type' => $Item['Type'], 'Values' => $this->Values, 73 'Filter' => $this->Values[$Index])); 82 74 } 83 75 … … 94 86 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) 95 87 { 96 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 97 { 98 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 99 $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]); 100 if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') 101 $UseType = 'OneToMany'; 102 else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') 103 $UseType = 'Enumeration'; 104 } else $UseType = $Item['Type']; 88 $UseType = $this->GetItemType($Item); 105 89 $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnView', 106 90 array('Value' => $this->Values[$Index], 'Name' => $Index, … … 150 134 else unset($Parameters['OnPreset']); 151 135 152 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 153 { 154 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 155 $this->FormManager->Type->RegisterType($Item['Type'], '', 156 $this->FormManager->FormTypes[$Item['Type']]); 157 if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') 158 { 159 $UseType = 'OneToMany'; 160 } else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') 161 $UseType = 'Enumeration'; 162 } else $UseType = $Item['Type']; 136 $UseType = $this->GetItemType($Item); 163 137 $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnEdit', $Parameters); 164 138 if (array_key_exists('Suffix', $Item)) $Edit .= $Item['Suffix']; … … 182 156 } 183 157 158 function GetItemType($Item): string 159 { 160 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 161 { 162 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 163 $this->FormManager->Type->RegisterType($Item['Type'], '', 164 $this->FormManager->FormTypes[$Item['Type']]); 165 if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') 166 $UseType = 'OneToMany'; 167 else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') 168 $UseType = 'Enumeration'; 169 } else $UseType = $Item['Type']; 170 171 return $UseType; 172 } 173 184 174 function LoadValuesFromDatabase(string $Id): void 185 175 { 186 176 foreach ($this->Definition['Items'] as $Index => $Item) 187 if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or 188 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and 189 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) 190 { 191 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 192 { 193 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 194 $this->FormManager->Type->RegisterType($Item['Type'], '', 195 $this->FormManager->FormTypes[$Item['Type']]); 196 if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') 197 $UseType = 'OneToMany'; 198 else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') 199 $UseType = 'Enumeration'; 200 } else $UseType = $Item['Type']; 201 if (!array_key_exists('SQL', $Item)) $Item['SQL'] = ''; 202 else $Item['SQL'] = str_replace('#Id', $Id, $Item['SQL']); 203 $Columns[] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnFilterNameQuery', 204 array('Name' => $Index, 'Type' => $Item['Type'], 'SQL' => $Item['SQL'])); 177 { 178 if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or 179 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and 180 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) 181 { 182 $UseType = $this->GetItemType($Item); 183 if (!array_key_exists('SQL', $Item)) $Item['SQL'] = ''; 184 else $Item['SQL'] = str_replace('#Id', $Id, $Item['SQL']); 185 $Columns[] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnFilterNameQuery', 186 array('Name' => $Index, 'Type' => $Item['Type'], 'SQL' => $Item['SQL'])); 187 } 205 188 } 206 189 $Columns = implode(',', $Columns); … … 211 194 $DbRow = $DbResult->fetch_array(); 212 195 foreach ($this->Definition['Items'] as $Index => $Item) 213 if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or 214 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and 215 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) 216 { 217 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 218 { 219 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 220 $this->FormManager->Type->RegisterType($Item['Type'], '', 221 $this->FormManager->FormTypes[$Item['Type']]); 222 if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') 223 $UseType = 'OneToMany'; 224 else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') 225 $UseType = 'Enumeration'; 226 } else $UseType = $Item['Type']; 196 { 197 if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or 198 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and 199 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) 200 { 201 $UseType = $this->GetItemType($Item); 227 202 $this->Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnLoadDb', 228 229 203 array('Value' => $DbRow[$Index], 'Name' => $Index, 204 'Type' => $Item['Type'], 'Values' => $this->Values)); 230 205 $this->ValuesFilter[$Index] = $DbRow[$Index.'_Filter']; 206 } 231 207 } 232 208 } … … 244 220 $Parameters = array('Value' => $this->Values[$Index], 'Name' => $Index, 245 221 'Type' => $Item['Type'], 'Values' => $this->Values); 246 247 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 248 { 249 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 250 $this->FormManager->Type->RegisterType($Item['Type'], '', 251 $this->FormManager->FormTypes[$Item['Type']]); 252 if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference') 253 { 254 $UseType = 'OneToMany'; 255 } 256 else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration') 257 $UseType = 'Enumeration'; 258 } else $UseType = $Item['Type']; 222 $UseType = $this->GetItemType($Item); 259 223 $Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnSaveDb', $Parameters); 260 224 if (($Item['Type'] == 'Password') and ($Values[$Index] == '')) unset($Values[$Index]); … … 264 228 { 265 229 $Values['Id'] = $Id; 266 $ DbResult = $this->Database->insert($this->Definition['Table'], $Values);230 $this->Database->insert($this->Definition['Table'], $Values); 267 231 } else 268 $DbResult = $this->Database->update($this->Definition['Table'], 'Id='.$Id, $Values); 232 $this->Database->update($this->Definition['Table'], 'Id='.$Id, $Values); 233 } 234 235 function HasAllPostVariables(): bool 236 { 237 $Result = array_key_exists('submit', $_POST); 238 239 foreach ($this->Definition['Items'] as $Index => $Item) 240 { 241 if (!array_key_exists('ReadOnly', $Item)) $Item['ReadOnly'] = false; 242 if ($Item['ReadOnly'] == false) 243 if (!array_key_exists('Hidden', $Item) or ($Item['Hidden'] == false)) 244 if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or 245 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and 246 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) 247 { 248 $UseType = $this->GetItemType($Item); 249 if (!$this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnCanLoad', 250 array('Value' => $this->Values[$Index], 'Name' => $Index, 251 'Type' => $Item['Type'], 'Values' => $this->Values, 252 'Filter' => $this->Values[$Index]))) 253 { 254 $Result = false; 255 break; 256 } 257 } 258 } 259 260 return $Result; 269 261 } 270 262 … … 290 282 { 291 283 //if (array_key_exists($Context.$Index, $_POST)) 292 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 293 { 294 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 295 $this->FormManager->Type->RegisterType($Item['Type'], '', 296 $this->FormManager->FormTypes[$Item['Type']]); 297 $CustomType = $this->FormManager->FormTypes[$Item['Type']]['Type']; 298 if ($CustomType == 'Reference') 299 $UseType = 'OneToMany'; 300 else if ($CustomType == 'Enumeration') 301 $UseType = 'Enumeration'; 302 } else $UseType = $Item['Type']; 284 $UseType = $this->GetItemType($Item); 303 285 $Parameters = array('Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values); 304 286 if (array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null']; … … 309 291 } else 310 292 { 311 if (isset($Item['Default'])) { 293 if (isset($Item['Default'])) 294 { 312 295 if (isset($Item['Null']) and ($Item['Null'] == true)) 313 296 $Values[$Index] = null; … … 319 302 } 320 303 304 // Check if filled value is in valid form 321 305 function Validate(): bool 322 306 { 323 307 $Valid = true; 324 308 foreach ($this->Definition['Items'] as $Index => $Item) 325 if ((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or 326 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and 327 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and 328 (!array_key_exists('ReadOnly', $Item) or 329 (array_key_exists('ReadOnly', $Item) and 330 ($Item['ReadOnly'] != true)))) 331 { 309 { 310 if ((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or 311 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and 312 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and 313 (!array_key_exists('ReadOnly', $Item) or 314 (array_key_exists('ReadOnly', $Item) and 315 ($Item['ReadOnly'] != true)))) 316 { 332 317 //if (array_key_exists($Context.$Index, $_POST)) 333 if (array_key_exists($Item['Type'], $this->FormManager->FormTypes)) 334 { 335 if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList)) 336 $this->FormManager->Type->RegisterType($Item['Type'], '', 337 $this->FormManager->FormTypes[$Item['Type']]); 338 $CustomType = $this->FormManager->FormTypes[$Item['Type']]['Type']; 339 if ($CustomType == 'Reference') 340 $UseType = 'OneToMany'; 341 else if ($CustomType == 'Enumeration') 342 $UseType = 'Enumeration'; 343 } else $UseType = $Item['Type']; 344 318 $UseType = $this->GetItemType($Item); 345 319 $Parameters = array('Value' => $this->Values[$Index]); 346 320 if (array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null']; 347 321 else $Parameters['Null'] = false; 348 322 if (!$this->FormManager->Type->ExecuteTypeEvent($UseType, 'Validate', 349 $Parameters)) { 350 $this->ValuesValidate[$Index] = true; 351 $Valid = false; 352 } 353 } 323 $Parameters)) 324 { 325 $this->ValuesValidate[$Index] = true; 326 $Valid = false; 327 } 328 } 329 } 354 330 if ($Valid == false) throw new Exception('not validated'); 355 331 return $Valid; 356 332 } 357 333 } 358 359 334 360 335 function MakeLink(string $Target, string $Title): string … … 386 361 return $Result; 387 362 } 388 389 class FormManager390 {391 public array $Classes;392 public array $FormTypes;393 public Database $Database;394 public Type $Type;395 public string $RootURL;396 public bool $ShowRelation;397 398 function __construct(Database $Database)399 {400 $this->Database = &$Database;401 $this->Classes = array();402 $this->FormTypes = array();403 $this->Type = new Type($this);404 $this->ShowRelation = false;405 }406 407 function RegisterClass(string $Name, array $Class): void408 {409 $this->Classes[$Name] = $Class;410 }411 412 function UnregisterClass(string $Name): void413 {414 unset($this->Classes[$Name]);415 }416 417 function RegisterFormType(string $Name, array $Class): void418 {419 $this->FormTypes[$Name] = $Class;420 }421 422 function UnregisterFormType(string $Name): void423 {424 unset($this->FormTypes[$Name]);425 }426 427 function UpdateSQLMeta(): void428 {429 $this->Database->query('DELETE FROM ModelField');430 $this->Database->query('DELETE FROM Model');431 $this->Database->query('DELETE FROM DataType WHERE Parent IS NOT NULL');432 $this->Database->query('DELETE FROM DataType');433 434 foreach ($this->Type->TypeDefinitionList as $Name => $Type)435 {436 $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Name.'"');437 if ($DbResult->num_rows == 0)438 {439 $this->Database->insert('DataType', array('Name' => $Name,440 'Title' => $Type['Class']));441 } else442 {443 $DbRow = $DbResult->fetch_assoc();444 $this->Database->update('DataType', 'Id='.$DbRow['Id'], array('Name' => $Name,445 'Title' => $Type['Class']));446 }447 }448 449 foreach ($this->Classes as $Class)450 if (!array_key_exists('SQL', $Class) and ($Class['Table'] != ''))451 {452 if (!$this->Database->TableExists($Class['Table'])) continue;453 454 echo($Class['Table'].'<br/>');455 $Module = 1;456 $DbResult = $this->Database->select('Model', 'Id', 'Name="'.$Class['Table'].'"');457 if ($DbResult->num_rows == 0)458 {459 $this->Database->insert('Model', array('Name' => $Class['Table'], 'Title' => $Class['Title'], 'Module' => $Module));460 $Model = $this->Database->insert_id;461 } else462 {463 $DbRow = $DbResult->fetch_assoc();464 $Model = $DbRow['Id'];465 $this->Database->update('Model', 'Id='.$DbRow['Id'], array('Name' => $Class['Table'],466 'Title' => $Class['Title'], 'Module' => $Module));467 }468 469 foreach ($Class['Items'] as $Name => $Field)470 {471 echo($Name.', ');472 $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Field['Type'].'"');473 if ($DbResult->num_rows > 0)474 {475 $DbRow = $DbResult->fetch_assoc();476 $Type = $DbRow['Id'];477 } else {478 $Type = $this->FormTypes[$Field['Type']];479 480 // Search parent type481 $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Type['Type'].'"');482 if ($DbResult->num_rows > 0)483 {484 $DbRow = $DbResult->fetch_assoc();485 $ParentType = $DbRow['Id'];486 } else $ParentType = null;487 488 $this->Database->insert('DataType', array('Name' => $Field['Type'],489 'Title' => '', 'Parent' => $ParentType));490 $Type = $this->Database->insert_id;491 }492 493 $DbResult = $this->Database->select('ModelField', 'Id', '(Name="'.$Name.'") AND (Model='.$Model.')');494 if ($DbResult->num_rows == 0)495 {496 $this->Database->insert('ModelField', array('Name' => $Name,497 'Title' => $Field['Caption'], 'Model' => $Model, 'Type' => $Type));498 } else499 {500 $DbRow = $DbResult->fetch_assoc();501 $this->Database->update('ModelField', 'Id='.$DbRow['Id'], array('Name' => $Name,502 'Title' => $Field['Caption'], 'Model' => $Model, 'Type' => $Type));503 }504 }505 echo('<br/>');506 }507 }508 }
Note:
See TracChangeset
for help on using the changeset viewer.