-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadTableVisitor.cs
269 lines (242 loc) · 9.63 KB
/
ReadTableVisitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ABC
{
public class ReadTableVisitor : IVisitor
{
private TextWriter _writer;
private ICollection<Table> _tables;
private int _indentation = 0;
private string _indent = " ";
public ReadTableVisitor(TextWriter writer, ICollection<Table> tables)
{
_writer = writer;
_tables = tables;
}
public void ConstructTabs(Table table)
{
if(_tables.Count > 0)
{
_writer.WriteLine("<div class='row' style='margin-top:70px;'>");
_writer.WriteLine("<ul class='nav nav-tabs'>");
foreach(var t in _tables)
{
if(t.Name == table.Name)
{
_writer.WriteLine($"<li class='active'><a href='#'>{t.Name}</a></li>");
continue;
}
string path = $"../{t.Name.ToLower()}/{t.Name.ToLower()}.php";
_writer.WriteLine($"<li><a href='{path}'>{t.Name}</a></li>");
}
_writer.WriteLine("</ul>");
_writer.WriteLine("</div>");
}
}
public string GetSelectQuery(Table table)
{
StringBuilder sb = new StringBuilder();
int i = 0;
foreach(var col in table.Columns.Values)
{
if(!col.Hide || col.IsPrimaryKey){
if(i > 0)
{
sb.Append(",");
}
sb.Append($"{col.Name}");
i++;
}
}
return sb.ToString();
}
public string GetWhereClause(Table table)
{
if(table.HideReg.Count == 0)
{
return string.Empty;
}
StringBuilder sb = new StringBuilder($"WHERE {table.PrimaryKey.Name} NOT IN (");
int i = 0;
foreach(var hide in table.HideReg)
{
if(i > 0)
{
sb.Append(",");
}
sb.Append(hide);
i++;
}
sb.Append(")");
return sb.ToString();
}
public void AddButon(Table table)
{
if(table.ShowNew)
{
_writer.WriteLine($"<a href='add{table.Name.ToLower()}.php' class='btn btn-success' style='margin-top: 40px; padding-left: 39px; padding-right: 39px;'>Añadir {table.Name}</a>");
}
}
public string GetColumnArray(Column col)
{
StringBuilder sb = new StringBuilder();
int i = 0;
foreach(var option in col.Options)
{
if(i > 0)
{
sb.Append(",");
}
sb.Append($"\"{option.Key}\" => \"{option.Value}\"");
i++;
}
return sb.ToString();
}
public void ConstructTable(Table table)
{
if(table.Columns.Count > 0)
{
_writer.WriteLine("<div class='row' style='margin-top:30px;'>");
_writer.WriteLine("<div class='table-responsive'>");
_writer.WriteLine("<table class='table'>");
_writer.WriteLine("<thead>");
_writer.WriteLine("<tr>");
foreach(var col in table.Columns.Values)
{
Console.WriteLine(col.IsPrimaryKey);
if(col.IsPrimaryKey && !table.ShowId)
{
continue;
}
if(!col.Hide){
Console.WriteLine($"{col.Header} hidden");
_writer.WriteLine($"<th>{col.Header}</th>");
}
}
if(table.ShowEdit)
{
_writer.WriteLine($"<th></th>");
}
if(table.ShowDelete)
{
_writer.WriteLine($"<th></th>");
}
_writer.WriteLine("</tr>");
_writer.WriteLine("</thead>");
_writer.WriteLine("<tbody>");
_writer.WriteLine("<?php");
_writer.WriteLine($"$resultSet = $db->squery_rows('SELECT {GetSelectQuery(table)} FROM {table.Name} {GetWhereClause(table)}', array());");
_writer.WriteLine("while($data = mysqli_fetch_assoc($resultSet)){");
_writer.WriteLine("echo '<tr>';");
foreach(var col in table.Columns.Values)
{
if(col.IsPrimaryKey && !table.ShowId)
{
continue;
}
if(!col.Hide){
if(col.IsForeignKey && col.IsForeignKeyMapped)
{
ForeignKey key = col.ForeignKeyMap;
_writer.WriteLine($"$fkquery = \"SELECT {key.Column} FROM {key.ToTable.Name} WHERE {key.ToTable.PrimaryKey.Name} = '{SharedContainer.AppConfigInstance.WildCard}'\";");
_writer.WriteLine($"$fkresult = $db->qarray($fkquery, array($data['{col.Name}']));");
_writer.WriteLine($"echo '<td>'.$fkresult['{key.Column}'].'</td>';");
continue;
}
if(col.IsDropdown){
_writer.WriteLine($"$array = array({GetColumnArray(col)});");
_writer.WriteLine($"echo '<td>'.$array[$data['{col.Name}']].'</td>';");
}
else{
_writer.WriteLine($"echo '<td>'.$data['{col.Name}'].'</td>';");
}
}
}
if(table.ShowEdit){
_writer.WriteLine($"echo '<td><a href=\"edit{table.Name}.php?pk='.$data['{table.PrimaryKey.Name}'].'\" class=\"btn btn-primary\" style=\"padding-left: 46px; padding-right: 46px;\">Editar</a></td>';");
}
if(table.ShowDelete){
_writer.WriteLine($"echo '<td><a href=\"{table.Name}.php?pk='.$data['{table.PrimaryKey.Name}'].'\" onclick=\"return confirm(\\'Seguro quieres borrar este registro?\\');\" class=\"btn btn-danger\" style=\"padding-left: 39px; padding-right: 39px;\">Eliminar</a></td>';");
}
_writer.WriteLine("echo '</tr>';}");
_writer.WriteLine("?>");
_writer.WriteLine("</tbody>");
_writer.WriteLine("</table>");
_writer.WriteLine("</div>");
_writer.WriteLine("</div>");
}
}
public void Visit(Table table)
{
WriteHeader(table);
ConstructTabs(table);
AddButon(table);
ConstructTable(table);
WriteFooter();
// _writer.WriteLine($"Table: {table.Name}, ShowId: {table.ShowId}, ShowNew: {table.ShowNew}, ShowEdit: {table.ShowEdit}, ShowDelete: {table.ShowDelete}");
// foreach(var hide in table.HideReg)
// {
// _writer.WriteLine($"HideReg: {hide}");
// }
// foreach(var column in table.Columns.Values)
// {
// column.Accept(this);
// }
}
public void Visit(Column column)
{
// _writer.WriteLine($"Column: {column.Name}, tipo: {column.DataType}, header: {column.Header}, readOnly: {column.ReadOnly}, hide: {column.Hide}, isDropdown: {column.IsDropdown}");
// foreach(var option in column.Options)
// {
// _writer.WriteLine($"Database Value: {option.Key}, New Value: {option.Value}");
// }
}
private void Indent()
{
_indentation++;
}
private void Unindent()
{
_indentation--;
}
private void WriteIndentation()
{
for (int i = 0; i < _indentation; i++)
{
_writer.Write(_indent);
}
}
private void WriteHeader(Table table)
{
_writer.WriteLine("<?php");
Indent();
WriteIndentation();
_writer.WriteLine($"include_once(\"../header.php\");");
Unindent();
WriteIndentation();
_writer.WriteLine("if(!empty( $_GET['pk'])){");
_writer.WriteLine($"$query = \"DELETE FROM {table.Name} WHERE {table.PrimaryKey.Name} = \" . '' . \"{SharedContainer.AppConfigInstance.WildCard}\";");
_writer.WriteLine($"$result = $db->squery_rows($query, array($_GET['pk']));");
_writer.WriteLine("if($result == 1){");
_writer.WriteLine($"header('Location: {table.Name}.php');");
_writer.WriteLine("}");
_writer.WriteLine("else{");
_writer.WriteLine("echo 'ERROR al eliminar registro. Vuelva a intentarlo.';");
_writer.WriteLine("}");
_writer.WriteLine("}");
_writer.WriteLine("?>");
}
private void WriteFooter()
{
_writer.WriteLine("<?php");
Indent();
WriteIndentation();
_writer.WriteLine($"include_once(\"../footer.php\");");
Unindent();
WriteIndentation();
_writer.WriteLine("?>");
}
}
}