1 | <?php
|
---|
2 |
|
---|
3 | class FormManager
|
---|
4 | {
|
---|
5 | public array $Classes;
|
---|
6 | public array $FormTypes;
|
---|
7 | public Database $Database;
|
---|
8 | public Type $Type;
|
---|
9 | public string $RootURL;
|
---|
10 | public bool $ShowRelation;
|
---|
11 |
|
---|
12 | function __construct(Database $Database)
|
---|
13 | {
|
---|
14 | $this->Database = &$Database;
|
---|
15 | $this->Classes = array();
|
---|
16 | $this->FormTypes = array();
|
---|
17 | $this->Type = new Type($this);
|
---|
18 | $this->ShowRelation = false;
|
---|
19 | }
|
---|
20 |
|
---|
21 | function RegisterClass(string $Name, array $Class): void
|
---|
22 | {
|
---|
23 | $this->Classes[$Name] = $Class;
|
---|
24 | }
|
---|
25 |
|
---|
26 | function UnregisterClass(string $Name): void
|
---|
27 | {
|
---|
28 | unset($this->Classes[$Name]);
|
---|
29 | }
|
---|
30 |
|
---|
31 | function RegisterFormType(string $Name, array $Class): void
|
---|
32 | {
|
---|
33 | $this->FormTypes[$Name] = $Class;
|
---|
34 | }
|
---|
35 |
|
---|
36 | function UnregisterFormType(string $Name): void
|
---|
37 | {
|
---|
38 | unset($this->FormTypes[$Name]);
|
---|
39 | }
|
---|
40 |
|
---|
41 | function UpdateSQLMeta(): void
|
---|
42 | {
|
---|
43 | $this->Database->query('DELETE FROM ModelField');
|
---|
44 | $this->Database->query('DELETE FROM Model');
|
---|
45 | $this->Database->query('DELETE FROM DataType WHERE Parent IS NOT NULL');
|
---|
46 | $this->Database->query('DELETE FROM DataType');
|
---|
47 |
|
---|
48 | foreach ($this->Type->TypeDefinitionList as $Name => $Type)
|
---|
49 | {
|
---|
50 | $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Name.'"');
|
---|
51 | if ($DbResult->num_rows == 0)
|
---|
52 | {
|
---|
53 | $this->Database->insert('DataType', array('Name' => $Name,
|
---|
54 | 'Title' => $Type['Class']));
|
---|
55 | } else
|
---|
56 | {
|
---|
57 | $DbRow = $DbResult->fetch_assoc();
|
---|
58 | $this->Database->update('DataType', 'Id='.$DbRow['Id'], array('Name' => $Name,
|
---|
59 | 'Title' => $Type['Class']));
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | foreach ($this->Classes as $Class)
|
---|
64 | if (!array_key_exists('SQL', $Class) and ($Class['Table'] != ''))
|
---|
65 | {
|
---|
66 | if (!$this->Database->TableExists($Class['Table'])) continue;
|
---|
67 |
|
---|
68 | echo($Class['Table'].'<br/>');
|
---|
69 | $Module = 1;
|
---|
70 | $DbResult = $this->Database->select('Model', 'Id', 'Name="'.$Class['Table'].'"');
|
---|
71 | if ($DbResult->num_rows == 0)
|
---|
72 | {
|
---|
73 | $this->Database->insert('Model', array('Name' => $Class['Table'], 'Title' => $Class['Title'], 'Module' => $Module));
|
---|
74 | $Model = $this->Database->insert_id;
|
---|
75 | } else
|
---|
76 | {
|
---|
77 | $DbRow = $DbResult->fetch_assoc();
|
---|
78 | $Model = $DbRow['Id'];
|
---|
79 | $this->Database->update('Model', 'Id='.$DbRow['Id'], array('Name' => $Class['Table'],
|
---|
80 | 'Title' => $Class['Title'], 'Module' => $Module));
|
---|
81 | }
|
---|
82 |
|
---|
83 | foreach ($Class['Items'] as $Name => $Field)
|
---|
84 | {
|
---|
85 | echo($Name.', ');
|
---|
86 | $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Field['Type'].'"');
|
---|
87 | if ($DbResult->num_rows > 0)
|
---|
88 | {
|
---|
89 | $DbRow = $DbResult->fetch_assoc();
|
---|
90 | $Type = $DbRow['Id'];
|
---|
91 | } else {
|
---|
92 | $Type = $this->FormTypes[$Field['Type']];
|
---|
93 |
|
---|
94 | // Search parent type
|
---|
95 | $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Type['Type'].'"');
|
---|
96 | if ($DbResult->num_rows > 0)
|
---|
97 | {
|
---|
98 | $DbRow = $DbResult->fetch_assoc();
|
---|
99 | $ParentType = $DbRow['Id'];
|
---|
100 | } else $ParentType = null;
|
---|
101 |
|
---|
102 | $this->Database->insert('DataType', array('Name' => $Field['Type'],
|
---|
103 | 'Title' => '', 'Parent' => $ParentType));
|
---|
104 | $Type = $this->Database->insert_id;
|
---|
105 | }
|
---|
106 |
|
---|
107 | $DbResult = $this->Database->select('ModelField', 'Id', '(Name="'.$Name.'") AND (Model='.$Model.')');
|
---|
108 | if ($DbResult->num_rows == 0)
|
---|
109 | {
|
---|
110 | $this->Database->insert('ModelField', array('Name' => $Name,
|
---|
111 | 'Title' => $Field['Caption'], 'Model' => $Model, 'Type' => $Type));
|
---|
112 | } else
|
---|
113 | {
|
---|
114 | $DbRow = $DbResult->fetch_assoc();
|
---|
115 | $this->Database->update('ModelField', 'Id='.$DbRow['Id'], array('Name' => $Name,
|
---|
116 | 'Title' => $Field['Caption'], 'Model' => $Model, 'Type' => $Type));
|
---|
117 | }
|
---|
118 | }
|
---|
119 | echo('<br/>');
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|