zet-lib 1.4.6 → 1.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Form.js +1 -1
- package/lib/zAppRouter.js +83 -16
- package/lib/zGeneratorRouter.js +983 -843
- package/package.json +1 -1
package/lib/zGeneratorRouter.js
CHANGED
|
@@ -1,74 +1,110 @@
|
|
|
1
|
-
const express = require(
|
|
2
|
-
const router = express.Router()
|
|
3
|
-
const csrf = require(
|
|
4
|
-
const csrfProtection = csrf({ cookie: true })
|
|
5
|
-
const fs = require(
|
|
6
|
-
const axios = require(
|
|
7
|
-
const pm2 = require(
|
|
8
|
-
const uglifyJS = require(
|
|
9
|
-
const { minify } = require(
|
|
10
|
-
const zRoute = require(
|
|
11
|
-
const connection = require(
|
|
12
|
-
const Util = require(
|
|
13
|
-
const moduleLib = require(
|
|
14
|
-
const myCache = require(
|
|
15
|
-
const ejs = require(
|
|
16
|
-
var app = express()
|
|
17
|
-
const path = require(
|
|
18
|
-
|
|
19
|
-
const nots = [
|
|
1
|
+
const express = require("express");
|
|
2
|
+
const router = express.Router();
|
|
3
|
+
const csrf = require("csurf");
|
|
4
|
+
const csrfProtection = csrf({ cookie: true });
|
|
5
|
+
const fs = require("fs-extra");
|
|
6
|
+
const axios = require("axios");
|
|
7
|
+
const pm2 = require("pm2");
|
|
8
|
+
const uglifyJS = require("uglify-js");
|
|
9
|
+
const { minify } = require("html-minifier-terser");
|
|
10
|
+
const zRoute = require("./zRoute");
|
|
11
|
+
const connection = require("./connection");
|
|
12
|
+
const Util = require("./Util");
|
|
13
|
+
const moduleLib = require("./moduleLib");
|
|
14
|
+
const myCache = require("./cache");
|
|
15
|
+
const ejs = require("ejs");
|
|
16
|
+
var app = express();
|
|
17
|
+
const path = require("path");
|
|
18
|
+
|
|
19
|
+
const nots = [
|
|
20
|
+
"index",
|
|
21
|
+
"uploads",
|
|
22
|
+
"js",
|
|
23
|
+
"css",
|
|
24
|
+
"log",
|
|
25
|
+
"generator",
|
|
26
|
+
"zmenu",
|
|
27
|
+
"zgenerator",
|
|
28
|
+
"zfields",
|
|
29
|
+
"zrole",
|
|
30
|
+
"zfunction",
|
|
31
|
+
"zgrid",
|
|
32
|
+
"zgrid_default",
|
|
33
|
+
"zpage",
|
|
34
|
+
"zlayout",
|
|
35
|
+
"zerror",
|
|
36
|
+
"zuser_company",
|
|
37
|
+
];
|
|
20
38
|
//const nots = [];
|
|
21
39
|
//const generatorUrl = 'http://localhost:3005';
|
|
22
|
-
const generatorUrl =
|
|
40
|
+
const generatorUrl = "https://generator.cmsqu.com";
|
|
23
41
|
|
|
24
|
-
const bodyView = Util.readFile(
|
|
42
|
+
const bodyView = Util.readFile(
|
|
43
|
+
require.resolve(__dirname + "/views/generator.ejs")
|
|
44
|
+
);
|
|
25
45
|
|
|
26
|
-
router.get(
|
|
27
|
-
let table = req.query.table ||
|
|
28
|
-
route =
|
|
46
|
+
router.get("/", csrfProtection, async (req, res) => {
|
|
47
|
+
let table = req.query.table || "",
|
|
48
|
+
route = "",
|
|
29
49
|
jsonData = {},
|
|
30
50
|
zForms = {},
|
|
31
51
|
relations = {},
|
|
32
|
-
approvalDatas = { is_approval: false }
|
|
33
|
-
const MYMODELS = myCache.get(
|
|
34
|
-
let datas = await axios.post(`${generatorUrl}/api/generator/index`)
|
|
35
|
-
jsonData = datas.data
|
|
36
|
-
jsonData.rows = await zfieldsKeys()
|
|
52
|
+
approvalDatas = { is_approval: false };
|
|
53
|
+
const MYMODELS = myCache.get("MYMODELS");
|
|
54
|
+
let datas = await axios.post(`${generatorUrl}/api/generator/index`);
|
|
55
|
+
jsonData = datas.data;
|
|
56
|
+
jsonData.rows = await zfieldsKeys();
|
|
37
57
|
if (table) {
|
|
38
|
-
jsonData.datas = jsonData.rows[table] || {}
|
|
58
|
+
jsonData.datas = jsonData.rows[table] || {};
|
|
39
59
|
}
|
|
40
|
-
jsonData.fields = await connection.query(connection.showFullFields(table))
|
|
41
|
-
zForms.obj = {}
|
|
42
|
-
let sorting =
|
|
60
|
+
jsonData.fields = await connection.query(connection.showFullFields(table));
|
|
61
|
+
zForms.obj = {};
|
|
62
|
+
let sorting = "";
|
|
43
63
|
if (table) {
|
|
44
|
-
let contentScript = `var ZFIELDS = ${JSON.stringify(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
64
|
+
let contentScript = `var ZFIELDS = ${JSON.stringify(
|
|
65
|
+
jsonData.rows,
|
|
66
|
+
null,
|
|
67
|
+
2
|
|
68
|
+
)};`;
|
|
69
|
+
moduleLib.addScript(req, res, contentScript);
|
|
70
|
+
moduleLib.editor(req, res);
|
|
71
|
+
let MYMODEL_APPROVERS = MYMODELS["zapprovals"];
|
|
48
72
|
let zfieldData = await connection.result({
|
|
49
|
-
table:
|
|
73
|
+
table: "zfields",
|
|
50
74
|
where: {
|
|
51
75
|
table: table,
|
|
52
76
|
},
|
|
53
|
-
})
|
|
54
|
-
sorting = JSON.stringify(zfieldData.sorting)
|
|
55
|
-
approvalDatas = MYMODEL_APPROVERS.datas
|
|
77
|
+
});
|
|
78
|
+
sorting = JSON.stringify(zfieldData.sorting);
|
|
79
|
+
approvalDatas = MYMODEL_APPROVERS.datas;
|
|
56
80
|
if (zfieldData) {
|
|
57
|
-
let approvalJSON = zfieldData.approval_json || {}
|
|
58
|
-
approvalDatas.is_approval = zfieldData.is_approval || false
|
|
59
|
-
approvalDatas.title = approvalJSON.title ||
|
|
60
|
-
approvalDatas.type = approvalJSON.type || 1
|
|
61
|
-
approvalDatas.approvers = approvalJSON.approvers
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
81
|
+
let approvalJSON = zfieldData.approval_json || {};
|
|
82
|
+
approvalDatas.is_approval = zfieldData.is_approval || false;
|
|
83
|
+
approvalDatas.title = approvalJSON.title || "";
|
|
84
|
+
approvalDatas.type = approvalJSON.type || 1;
|
|
85
|
+
approvalDatas.approvers = approvalJSON.approvers
|
|
86
|
+
? approvalJSON.approvers
|
|
87
|
+
: [];
|
|
88
|
+
approvalDatas.knowings = approvalJSON.knowings || [];
|
|
89
|
+
approvalDatas.template = approvalJSON.content || "";
|
|
90
|
+
zForms = await zRoute.formsFieldSync(
|
|
91
|
+
req,
|
|
92
|
+
res,
|
|
93
|
+
MYMODEL_APPROVERS,
|
|
94
|
+
approvalDatas
|
|
95
|
+
);
|
|
65
96
|
}
|
|
66
97
|
}
|
|
67
98
|
|
|
68
|
-
const jsonDatas = jsonData.datas
|
|
69
|
-
const lock = nots.includes(table) ? 1 : 0
|
|
70
|
-
const jsonDatasJson =
|
|
71
|
-
|
|
99
|
+
const jsonDatas = jsonData.datas;
|
|
100
|
+
const lock = nots.includes(table) ? 1 : 0;
|
|
101
|
+
const jsonDatasJson =
|
|
102
|
+
jsonDatas.hasOwnProperty("json") && jsonDatas.json ? jsonDatas.json : {};
|
|
103
|
+
const checkDummy =
|
|
104
|
+
Object.prototype.hasOwnProperty.call(jsonDatasJson, "dummy") &&
|
|
105
|
+
jsonDatasJson.dummy
|
|
106
|
+
? "checked"
|
|
107
|
+
: "";
|
|
72
108
|
const renderData = {
|
|
73
109
|
datas: jsonDatas,
|
|
74
110
|
table: table,
|
|
@@ -83,218 +119,256 @@ router.get('/', csrfProtection, async (req, res) => {
|
|
|
83
119
|
sorting: sorting,
|
|
84
120
|
lock: lock,
|
|
85
121
|
routeName: res.locals.routeName,
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const bodyHTML = ejs.render(bodyView, renderData)
|
|
89
|
-
const endHTML = ejs.render(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const bodyHTML = ejs.render(bodyView, renderData);
|
|
125
|
+
const endHTML = ejs.render(
|
|
126
|
+
Util.readFile(require.resolve(__dirname + "/views/generatorjs.ejs")),
|
|
127
|
+
renderData
|
|
128
|
+
);
|
|
129
|
+
let dataRender = renderData;
|
|
130
|
+
dataRender.bodyHTML = bodyHTML;
|
|
131
|
+
dataRender.endHTML = endHTML;
|
|
132
|
+
res.render(
|
|
133
|
+
require.resolve(__dirname + "/views/generator_layout.ejs"),
|
|
134
|
+
dataRender
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
router.post("/fields", async (req, res) => {
|
|
97
139
|
try {
|
|
98
|
-
let body = req.body
|
|
99
|
-
let table = body.table ||
|
|
140
|
+
let body = req.body;
|
|
141
|
+
let table = body.table || "";
|
|
100
142
|
let result = {},
|
|
101
143
|
fields = {},
|
|
102
|
-
rowsFields = []
|
|
144
|
+
rowsFields = [];
|
|
103
145
|
if (table) {
|
|
104
|
-
result = await connection.result({
|
|
105
|
-
|
|
106
|
-
|
|
146
|
+
result = await connection.result({
|
|
147
|
+
table: "zfields",
|
|
148
|
+
where: { table: table },
|
|
149
|
+
});
|
|
150
|
+
fields = await connection.query(connection.showFullFields(table));
|
|
151
|
+
rowsFields = await connection.query(connection.describeTable(table));
|
|
107
152
|
}
|
|
108
153
|
let objData = {
|
|
109
154
|
result: result,
|
|
110
155
|
fields: fields,
|
|
111
156
|
rowsFields: rowsFields,
|
|
112
157
|
table: table,
|
|
113
|
-
}
|
|
114
|
-
let datas = await axios.post(
|
|
115
|
-
|
|
158
|
+
};
|
|
159
|
+
let datas = await axios.post(
|
|
160
|
+
`${generatorUrl}/api/generator/modal`,
|
|
161
|
+
objData
|
|
162
|
+
);
|
|
163
|
+
res.json(datas.data);
|
|
116
164
|
} catch (e) {
|
|
117
|
-
console.log(e)
|
|
118
|
-
res.json(e.toString())
|
|
165
|
+
console.log(e);
|
|
166
|
+
res.json(e.toString());
|
|
119
167
|
}
|
|
120
|
-
})
|
|
168
|
+
});
|
|
121
169
|
|
|
122
170
|
/*
|
|
123
171
|
Create new Module/Table
|
|
124
172
|
*/
|
|
125
|
-
router.post(
|
|
173
|
+
router.post("/", csrfProtection, async (req, res) => {
|
|
126
174
|
try {
|
|
127
|
-
let results = await axios.post(
|
|
128
|
-
|
|
175
|
+
let results = await axios.post(
|
|
176
|
+
`${generatorUrl}/api/generator/create`,
|
|
177
|
+
req.body
|
|
178
|
+
);
|
|
179
|
+
let datas = results.data;
|
|
129
180
|
if (datas.status == 0) {
|
|
130
|
-
res.json(datas)
|
|
181
|
+
res.json(datas);
|
|
131
182
|
} else {
|
|
132
183
|
if (nots.includes(datas.post.table)) {
|
|
133
|
-
return res.json(Util.flashError(
|
|
184
|
+
return res.json(Util.flashError("Table is locked"));
|
|
134
185
|
}
|
|
135
186
|
await connection.insert({
|
|
136
|
-
table:
|
|
187
|
+
table: "zfields",
|
|
137
188
|
data: datas.post,
|
|
138
|
-
})
|
|
139
|
-
await connection.query(datas.sql)
|
|
140
|
-
res.json(datas)
|
|
189
|
+
});
|
|
190
|
+
await connection.query(datas.sql);
|
|
191
|
+
res.json(datas);
|
|
141
192
|
}
|
|
142
193
|
} catch (err) {
|
|
143
|
-
res.json(Util.flashError(err.toString()))
|
|
194
|
+
res.json(Util.flashError(err.toString()));
|
|
144
195
|
}
|
|
145
|
-
})
|
|
196
|
+
});
|
|
146
197
|
|
|
147
|
-
router.post(
|
|
198
|
+
router.post("/reset", async (req, res) => {
|
|
148
199
|
try {
|
|
149
|
-
let json = Util.jsonSuccess(
|
|
150
|
-
let table = req.body.table
|
|
200
|
+
let json = Util.jsonSuccess("Reset Success");
|
|
201
|
+
let table = req.body.table;
|
|
151
202
|
if (nots.includes(table)) {
|
|
152
|
-
return res.json(Util.flashError(
|
|
203
|
+
return res.json(Util.flashError("Table is locked"));
|
|
153
204
|
}
|
|
154
205
|
if (table) {
|
|
155
|
-
let results = await axios.post(
|
|
156
|
-
|
|
206
|
+
let results = await axios.post(
|
|
207
|
+
`${generatorUrl}/api/generator/reset`,
|
|
208
|
+
req.body
|
|
209
|
+
);
|
|
210
|
+
let datas = results.data;
|
|
157
211
|
await connection.update({
|
|
158
|
-
table:
|
|
212
|
+
table: "zfields",
|
|
159
213
|
data: datas,
|
|
160
214
|
where: {
|
|
161
215
|
table: table,
|
|
162
216
|
},
|
|
163
|
-
})
|
|
217
|
+
});
|
|
164
218
|
} else {
|
|
165
|
-
json = Util.flashError(
|
|
219
|
+
json = Util.flashError("error");
|
|
166
220
|
}
|
|
167
|
-
res.json(json)
|
|
221
|
+
res.json(json);
|
|
168
222
|
} catch (e) {
|
|
169
|
-
console.log(e.toString())
|
|
170
|
-
json = Util.flashError(
|
|
171
|
-
res.json(json)
|
|
223
|
+
console.log(e.toString());
|
|
224
|
+
json = Util.flashError("error");
|
|
225
|
+
res.json(json);
|
|
172
226
|
}
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
router.post(
|
|
176
|
-
let json = { status: 0, title:
|
|
177
|
-
let body = req.body
|
|
178
|
-
let table = body.table ||
|
|
179
|
-
let title = body.title ||
|
|
180
|
-
let tabs = body.tabs || []
|
|
181
|
-
if (table ==
|
|
182
|
-
json.title =
|
|
183
|
-
return res.send(json)
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
router.post("/tabs", async (req, res) => {
|
|
230
|
+
let json = { status: 0, title: "error", url: "" };
|
|
231
|
+
let body = req.body;
|
|
232
|
+
let table = body.table || "";
|
|
233
|
+
let title = body.title || "";
|
|
234
|
+
let tabs = body.tabs || [];
|
|
235
|
+
if (table == "") {
|
|
236
|
+
json.title = "table is empty";
|
|
237
|
+
return res.send(json);
|
|
184
238
|
}
|
|
185
|
-
if (title ==
|
|
186
|
-
json.title =
|
|
187
|
-
return res.send(json)
|
|
239
|
+
if (title == "") {
|
|
240
|
+
json.title = "title is empty";
|
|
241
|
+
return res.send(json);
|
|
188
242
|
}
|
|
189
243
|
if (nots.includes(table)) {
|
|
190
|
-
return res.json(Util.flashError(
|
|
244
|
+
return res.json(Util.flashError("Table is locked"));
|
|
191
245
|
}
|
|
192
|
-
let post = {}
|
|
193
|
-
post.table = table
|
|
194
|
-
post.tabs = JSON.stringify(tabs)
|
|
195
|
-
post.name = title
|
|
196
|
-
if (body.hasOwnProperty(
|
|
197
|
-
post.json = JSON.stringify(body.json)
|
|
246
|
+
let post = {};
|
|
247
|
+
post.table = table;
|
|
248
|
+
post.tabs = JSON.stringify(tabs);
|
|
249
|
+
post.name = title;
|
|
250
|
+
if (body.hasOwnProperty("json")) {
|
|
251
|
+
post.json = JSON.stringify(body.json);
|
|
198
252
|
} else {
|
|
199
|
-
post.json = JSON.stringify({ dummy: 0 })
|
|
253
|
+
post.json = JSON.stringify({ dummy: 0 });
|
|
200
254
|
}
|
|
201
255
|
let results = await connection.results({
|
|
202
|
-
table:
|
|
256
|
+
table: "zfields",
|
|
203
257
|
where: {
|
|
204
258
|
table: table,
|
|
205
259
|
},
|
|
206
|
-
})
|
|
260
|
+
});
|
|
207
261
|
|
|
208
262
|
if (results.length == 0) {
|
|
209
|
-
await connection.insert({ table:
|
|
263
|
+
await connection.insert({ table: "zfields", data: post });
|
|
210
264
|
} else {
|
|
211
265
|
//original data
|
|
212
|
-
let tabOri = results[0].tabs || []
|
|
213
|
-
let detailOri = results[0].details || {}
|
|
214
|
-
let leftOri = results[0].left || []
|
|
215
|
-
let rightOri = results[0].right || []
|
|
216
|
-
let oneColumnOri = results[0].one_column || []
|
|
217
|
-
let objOri = {}
|
|
218
|
-
let arrayOri = {}
|
|
266
|
+
let tabOri = results[0].tabs || [];
|
|
267
|
+
let detailOri = results[0].details || {};
|
|
268
|
+
let leftOri = results[0].left || [];
|
|
269
|
+
let rightOri = results[0].right || [];
|
|
270
|
+
let oneColumnOri = results[0].one_column || [];
|
|
271
|
+
let objOri = {};
|
|
272
|
+
let arrayOri = {};
|
|
219
273
|
tabOri.map((item, index) => {
|
|
220
|
-
objOri[item] = `arr${index}
|
|
221
|
-
arrayOri[item] = index
|
|
222
|
-
})
|
|
274
|
+
objOri[item] = `arr${index}`;
|
|
275
|
+
arrayOri[item] = index;
|
|
276
|
+
});
|
|
223
277
|
//end original data
|
|
224
278
|
if (tabOri.length > 0) {
|
|
225
279
|
if (JSON.stringify(tabs) !== JSON.stringify(tabOri)) {
|
|
226
|
-
let details = {}
|
|
280
|
+
let details = {};
|
|
227
281
|
//remove Object in array
|
|
228
|
-
let left = leftOri.filter((item) => typeof item !==
|
|
229
|
-
right = rightOri.filter((item) => typeof item !==
|
|
230
|
-
oneColumn =
|
|
282
|
+
let left = leftOri.filter((item) => typeof item !== "object") || [],
|
|
283
|
+
right = rightOri.filter((item) => typeof item !== "object") || [],
|
|
284
|
+
oneColumn =
|
|
285
|
+
oneColumnOri.filter((item) => typeof item !== "object") || [];
|
|
231
286
|
|
|
232
|
-
details.notabs = detailOri.notabs
|
|
287
|
+
details.notabs = detailOri.notabs;
|
|
233
288
|
tabs.map((item, index) => {
|
|
234
|
-
let key = `arr${index}
|
|
235
|
-
let valueIndex = arrayOri[item]
|
|
289
|
+
let key = `arr${index}`;
|
|
290
|
+
let valueIndex = arrayOri[item];
|
|
236
291
|
//details
|
|
237
|
-
details[key] = detailOri[objOri[item]] || []
|
|
292
|
+
details[key] = detailOri[objOri[item]] || [];
|
|
238
293
|
//left
|
|
239
|
-
let objLeft =
|
|
240
|
-
|
|
294
|
+
let objLeft =
|
|
295
|
+
leftOri[valueIndex] &&
|
|
296
|
+
leftOri[valueIndex].hasOwnProperty(`arr${arrayOri[item]}`)
|
|
297
|
+
? leftOri[valueIndex][`arr${arrayOri[item]}`]
|
|
298
|
+
: [];
|
|
299
|
+
left.push({ [key]: objLeft });
|
|
241
300
|
//right
|
|
242
|
-
let objRight =
|
|
243
|
-
|
|
301
|
+
let objRight =
|
|
302
|
+
rightOri[valueIndex] &&
|
|
303
|
+
rightOri[valueIndex].hasOwnProperty(`arr${arrayOri[item]}`)
|
|
304
|
+
? rightOri[valueIndex][`arr${arrayOri[item]}`]
|
|
305
|
+
: [];
|
|
306
|
+
right.push({ [key]: objRight });
|
|
244
307
|
//one column
|
|
245
|
-
let objOneColumn =
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
308
|
+
let objOneColumn =
|
|
309
|
+
oneColumnOri[valueIndex] &&
|
|
310
|
+
oneColumnOri[valueIndex].hasOwnProperty(`arr${arrayOri[item]}`)
|
|
311
|
+
? oneColumnOri[valueIndex][`arr${arrayOri[item]}`]
|
|
312
|
+
: [];
|
|
313
|
+
oneColumn.push({ [key]: objOneColumn });
|
|
314
|
+
});
|
|
315
|
+
post.details = JSON.stringify(details);
|
|
316
|
+
post.left = JSON.stringify(left);
|
|
317
|
+
post.right = JSON.stringify(right);
|
|
318
|
+
post.one_column = JSON.stringify(oneColumn);
|
|
252
319
|
}
|
|
253
320
|
}
|
|
254
|
-
await connection.update({
|
|
321
|
+
await connection.update({
|
|
322
|
+
table: "zfields",
|
|
323
|
+
where: { id: results[0].id },
|
|
324
|
+
data: post,
|
|
325
|
+
});
|
|
255
326
|
}
|
|
256
327
|
|
|
257
|
-
json.status = 1
|
|
258
|
-
json.url =
|
|
259
|
-
res.send(json)
|
|
260
|
-
})
|
|
328
|
+
json.status = 1;
|
|
329
|
+
json.url = "/generator?table=" + table;
|
|
330
|
+
res.send(json);
|
|
331
|
+
});
|
|
261
332
|
|
|
262
333
|
//after drag and drop then save
|
|
263
334
|
// and generate
|
|
264
|
-
router.post(
|
|
335
|
+
router.post("/save_and_generate", csrfProtection, async (req, res) => {
|
|
265
336
|
try {
|
|
266
337
|
if (nots.includes(req.body.table)) {
|
|
267
|
-
return res.json(Util.flashError(
|
|
338
|
+
return res.json(Util.flashError("Table is locked"));
|
|
268
339
|
}
|
|
269
340
|
//console.log(JSON.stringify(req.body))
|
|
270
|
-
const json = await generate(req, res)
|
|
271
|
-
res.json(json)
|
|
341
|
+
const json = await generate(req, res);
|
|
342
|
+
res.json(json);
|
|
272
343
|
} catch (e) {
|
|
273
|
-
console.log(e)
|
|
274
|
-
res.status(200).json({ message: e +
|
|
344
|
+
console.log(e);
|
|
345
|
+
res.status(200).json({ message: e + "" });
|
|
275
346
|
}
|
|
276
|
-
})
|
|
347
|
+
});
|
|
277
348
|
|
|
278
349
|
const generate = async (req, res) => {
|
|
279
|
-
const body = req.body
|
|
280
|
-
const table = body.table
|
|
281
|
-
const MYMODELS = myCache.get(
|
|
282
|
-
let MYMODEL = {}
|
|
283
|
-
let chains = []
|
|
284
|
-
let datas
|
|
285
|
-
let results
|
|
286
|
-
let dummy = false
|
|
350
|
+
const body = req.body;
|
|
351
|
+
const table = body.table;
|
|
352
|
+
const MYMODELS = myCache.get("MYMODELS");
|
|
353
|
+
let MYMODEL = {};
|
|
354
|
+
let chains = [];
|
|
355
|
+
let datas;
|
|
356
|
+
let results;
|
|
357
|
+
let dummy = false;
|
|
287
358
|
|
|
288
359
|
//save data into table zfields
|
|
289
360
|
//console.log(JSON.stringify(body.others))
|
|
290
|
-
const others = body.others
|
|
361
|
+
const others = body.others;
|
|
291
362
|
//return;
|
|
292
|
-
await saveToZFields(body)
|
|
363
|
+
await saveToZFields(body);
|
|
293
364
|
|
|
294
|
-
let exist = await connection.query(connection.describeTable(table))
|
|
365
|
+
let exist = await connection.query(connection.describeTable(table));
|
|
295
366
|
if (exist.length) {
|
|
296
|
-
body.rows = await connection.results({
|
|
297
|
-
|
|
367
|
+
body.rows = await connection.results({
|
|
368
|
+
table: "zfields",
|
|
369
|
+
where: { table: table },
|
|
370
|
+
});
|
|
371
|
+
body.checks = await connection.query(`SELECT 'public.${table}'::regclass`);
|
|
298
372
|
} else {
|
|
299
373
|
let sql = `
|
|
300
374
|
CREATE TABLE ${table} (
|
|
@@ -308,129 +382,140 @@ const generate = async (req, res) => {
|
|
|
308
382
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
309
383
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
310
384
|
CONSTRAINT fk_${table}_company_id FOREIGN KEY (company_id) REFERENCES zcompany (id)
|
|
311
|
-
)
|
|
312
|
-
await connection.query(sql)
|
|
313
|
-
body.checks = await connection.query(`SELECT 'public.${table}'::regclass`)
|
|
314
|
-
body.rows = await connection.results({
|
|
385
|
+
);`;
|
|
386
|
+
await connection.query(sql);
|
|
387
|
+
body.checks = await connection.query(`SELECT 'public.${table}'::regclass`);
|
|
388
|
+
body.rows = await connection.results({
|
|
389
|
+
table: "zfields",
|
|
390
|
+
where: { table: table },
|
|
391
|
+
});
|
|
315
392
|
}
|
|
316
|
-
body.others = others
|
|
317
|
-
results = await axios.post(`${generatorUrl}/api/generator/properties`, body)
|
|
318
|
-
datas = results.data
|
|
319
|
-
if (datas.create_table.hasOwnProperty(
|
|
320
|
-
await connection.query(datas.create_table.sql)
|
|
321
|
-
return generate(body)
|
|
393
|
+
body.others = others;
|
|
394
|
+
results = await axios.post(`${generatorUrl}/api/generator/properties`, body);
|
|
395
|
+
datas = results.data;
|
|
396
|
+
if (datas.create_table.hasOwnProperty("sql")) {
|
|
397
|
+
await connection.query(datas.create_table.sql);
|
|
398
|
+
return generate(body);
|
|
322
399
|
}
|
|
323
400
|
|
|
324
401
|
let result = await connection.update({
|
|
325
|
-
table:
|
|
402
|
+
table: "zfields",
|
|
326
403
|
data: datas.updateFields.post,
|
|
327
404
|
where: {
|
|
328
405
|
id: datas.updateFields.id,
|
|
329
406
|
},
|
|
330
|
-
})
|
|
331
|
-
let resultJSON = Object.prototype.hasOwnProperty.call(result,
|
|
332
|
-
|
|
333
|
-
|
|
407
|
+
});
|
|
408
|
+
let resultJSON = Object.prototype.hasOwnProperty.call(result, "json")
|
|
409
|
+
? result.json
|
|
410
|
+
: {};
|
|
411
|
+
if (resultJSON && Object.prototype.hasOwnProperty.call(resultJSON, "dummy")) {
|
|
412
|
+
dummy = resultJSON.dummy == 1 ? true : false;
|
|
334
413
|
}
|
|
335
|
-
let properties = result.properties
|
|
414
|
+
let properties = result.properties;
|
|
336
415
|
//update if any chains
|
|
337
416
|
for (let key in properties) {
|
|
338
417
|
//override
|
|
339
|
-
properties[key].values.required = properties[key].values.required
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
properties[key].values.
|
|
418
|
+
properties[key].values.required = properties[key].values.required
|
|
419
|
+
? true
|
|
420
|
+
: false;
|
|
421
|
+
properties[key].values.unique = properties[key].values.unique
|
|
422
|
+
? true
|
|
423
|
+
: false;
|
|
424
|
+
var isChain = properties[key].values.isChain ? true : false;
|
|
425
|
+
properties[key].values.isChain = isChain;
|
|
343
426
|
if (isChain) {
|
|
344
|
-
chains.push(key)
|
|
427
|
+
chains.push(key);
|
|
345
428
|
}
|
|
346
429
|
}
|
|
347
430
|
if (chains.length && chains.length < 2) {
|
|
348
|
-
return Util.flashError(
|
|
431
|
+
return Util.flashError("Chains min 2 fields number...");
|
|
349
432
|
}
|
|
350
433
|
if (chains.length) {
|
|
351
|
-
let prop = chainings(table, chains)
|
|
434
|
+
let prop = chainings(table, chains);
|
|
352
435
|
for (const key in prop) {
|
|
353
|
-
if (prop[key].hasOwnProperty(
|
|
354
|
-
chains = Object.keys(prop[key].chains)
|
|
436
|
+
if (prop[key].hasOwnProperty("chains")) {
|
|
437
|
+
chains = Object.keys(prop[key].chains);
|
|
355
438
|
if (chains.length) {
|
|
356
|
-
properties[key].values.chains = prop[key].chains
|
|
439
|
+
properties[key].values.chains = prop[key].chains;
|
|
357
440
|
}
|
|
358
441
|
}
|
|
359
442
|
}
|
|
360
443
|
}
|
|
361
444
|
//update properties
|
|
362
445
|
result = await connection.update({
|
|
363
|
-
table:
|
|
446
|
+
table: "zfields",
|
|
364
447
|
data: {
|
|
365
448
|
properties: JSON.stringify(properties),
|
|
366
449
|
},
|
|
367
450
|
where: {
|
|
368
451
|
table: table,
|
|
369
452
|
},
|
|
370
|
-
})
|
|
453
|
+
});
|
|
371
454
|
//end chains
|
|
372
455
|
|
|
373
456
|
//generate file models routes views
|
|
374
|
-
body.columns = await connection.query(connection.describeTable(table))
|
|
375
|
-
body.constraintList = await connection.query(
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
457
|
+
body.columns = await connection.query(connection.describeTable(table));
|
|
458
|
+
body.constraintList = await connection.query(
|
|
459
|
+
connection.constraintList(table)
|
|
460
|
+
);
|
|
461
|
+
body.result = result;
|
|
462
|
+
results = await axios.post(`${generatorUrl}/api/generator/generate`, body);
|
|
463
|
+
datas = results.data;
|
|
464
|
+
let files = datas.files;
|
|
380
465
|
for (let key in files) {
|
|
381
|
-
let filesKey = files[key]
|
|
382
|
-
if (key ==
|
|
383
|
-
if (filesKey.hasOwnProperty(
|
|
384
|
-
const sql = filesKey.sql || []
|
|
466
|
+
let filesKey = files[key];
|
|
467
|
+
if (key == "model") {
|
|
468
|
+
if (filesKey.hasOwnProperty("sql")) {
|
|
469
|
+
const sql = filesKey.sql || [];
|
|
385
470
|
for (let i = 0; i < sql.length; i++) {
|
|
386
|
-
console.log(i +
|
|
387
|
-
await connection.query(sql[i])
|
|
471
|
+
console.log(i + "sql : " + sql[i]);
|
|
472
|
+
await connection.query(sql[i]);
|
|
388
473
|
}
|
|
389
474
|
}
|
|
390
|
-
if (filesKey.hasOwnProperty(
|
|
391
|
-
const alter = filesKey.alter || []
|
|
475
|
+
if (filesKey.hasOwnProperty("alter")) {
|
|
476
|
+
const alter = filesKey.alter || [];
|
|
392
477
|
for (let i = 0; i < alter.length; i++) {
|
|
393
|
-
console.log(alter[i])
|
|
394
|
-
await connection.query(alter[i])
|
|
478
|
+
console.log(alter[i]);
|
|
479
|
+
await connection.query(alter[i]);
|
|
395
480
|
}
|
|
396
481
|
}
|
|
397
|
-
if (filesKey.hasOwnProperty(
|
|
398
|
-
const index = filesKey.index || []
|
|
482
|
+
if (filesKey.hasOwnProperty("index")) {
|
|
483
|
+
const index = filesKey.index || [];
|
|
399
484
|
for (let i = 0; i < index.length; i++) {
|
|
400
|
-
console.log(index[i])
|
|
401
|
-
await connection.query(index[i])
|
|
485
|
+
console.log(index[i]);
|
|
486
|
+
await connection.query(index[i]);
|
|
402
487
|
}
|
|
403
488
|
}
|
|
404
|
-
if (filesKey.hasOwnProperty(
|
|
405
|
-
let fk = filesKey.fk || []
|
|
489
|
+
if (filesKey.hasOwnProperty("fk")) {
|
|
490
|
+
let fk = filesKey.fk || [];
|
|
406
491
|
for (let i = 0; i < fk.length; i++) {
|
|
407
|
-
console.log(fk[i])
|
|
408
|
-
await connection.query(fk[i])
|
|
492
|
+
console.log(fk[i]);
|
|
493
|
+
await connection.query(fk[i]);
|
|
409
494
|
}
|
|
410
495
|
}
|
|
411
|
-
if (filesKey.hasOwnProperty(
|
|
412
|
-
const defaultValue = filesKey.defaultValue || []
|
|
496
|
+
if (filesKey.hasOwnProperty("defaultValue")) {
|
|
497
|
+
const defaultValue = filesKey.defaultValue || [];
|
|
413
498
|
for (let i = 0; i < defaultValue.length; i++) {
|
|
414
|
-
console.log(defaultValue[i])
|
|
415
|
-
await connection.query(defaultValue[i])
|
|
499
|
+
console.log(defaultValue[i]);
|
|
500
|
+
await connection.query(defaultValue[i]);
|
|
416
501
|
}
|
|
417
502
|
}
|
|
418
503
|
//console.log(JSON.stringify(filesKey.modelObj))
|
|
419
504
|
//check if has chains here
|
|
420
|
-
MYMODEL = filesKey.modelObj
|
|
505
|
+
MYMODEL = filesKey.modelObj;
|
|
421
506
|
//delete/clean zgrid after changes
|
|
422
|
-
await connection.delete({
|
|
507
|
+
/* await connection.delete({
|
|
423
508
|
table: 'zgrid',
|
|
424
509
|
where: {
|
|
425
510
|
route_name: table,
|
|
426
511
|
},
|
|
427
|
-
})
|
|
512
|
+
}) */
|
|
428
513
|
await connection.delete({
|
|
429
|
-
table:
|
|
514
|
+
table: "zgrid_default",
|
|
430
515
|
where: {
|
|
431
516
|
table: table,
|
|
432
517
|
},
|
|
433
|
-
})
|
|
518
|
+
});
|
|
434
519
|
//insert into zgrid default
|
|
435
520
|
let datagrid = {
|
|
436
521
|
company_id: 1,
|
|
@@ -438,51 +523,61 @@ const generate = async (req, res) => {
|
|
|
438
523
|
created_by: 1,
|
|
439
524
|
created_at: Util.now(),
|
|
440
525
|
updated_at: Util.now(),
|
|
441
|
-
}
|
|
442
|
-
datagrid.table = table
|
|
443
|
-
datagrid.visibles = JSON.stringify(MYMODEL.grids.visibles)
|
|
444
|
-
datagrid.invisibles = JSON.stringify(MYMODEL.grids.invisibles)
|
|
445
|
-
datagrid.fields = JSON.stringify([
|
|
526
|
+
};
|
|
527
|
+
datagrid.table = table;
|
|
528
|
+
datagrid.visibles = JSON.stringify(MYMODEL.grids.visibles);
|
|
529
|
+
datagrid.invisibles = JSON.stringify(MYMODEL.grids.invisibles);
|
|
530
|
+
datagrid.fields = JSON.stringify([
|
|
531
|
+
...MYMODEL.grids.visibles,
|
|
532
|
+
...MYMODEL.grids.invisibles,
|
|
533
|
+
]);
|
|
446
534
|
await connection.insert({
|
|
447
|
-
table:
|
|
535
|
+
table: "zgrid_default",
|
|
448
536
|
data: datagrid,
|
|
449
|
-
})
|
|
450
|
-
|
|
451
|
-
fs.writeFileSync(
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
let
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
fs.writeFileSync(
|
|
540
|
+
`${dirRoot}/models/${filesKey.filename}`,
|
|
541
|
+
filesKey.content
|
|
542
|
+
);
|
|
543
|
+
console.log(`The file model of ${filesKey.filename} is saved!`);
|
|
544
|
+
|
|
545
|
+
let widgets = MYMODEL.widgets;
|
|
546
|
+
let objChains = {};
|
|
547
|
+
let chainsTable = [];
|
|
548
|
+
let isChain = false;
|
|
458
549
|
for (let key in widgets) {
|
|
459
|
-
let hasChains = widgets[key].hasOwnProperty(
|
|
550
|
+
let hasChains = widgets[key].hasOwnProperty("isChain")
|
|
551
|
+
? widgets[key].isChain
|
|
552
|
+
: false;
|
|
460
553
|
if (hasChains) {
|
|
461
|
-
isChain = true
|
|
462
|
-
if (widgets[key].hasOwnProperty(
|
|
463
|
-
chainsTable.push(widget_table)
|
|
464
|
-
var widget_table = widgets[key].table
|
|
554
|
+
isChain = true;
|
|
555
|
+
if (widgets[key].hasOwnProperty("table")) {
|
|
556
|
+
chainsTable.push(widget_table);
|
|
557
|
+
var widget_table = widgets[key].table;
|
|
465
558
|
objChains[widget_table] = {
|
|
466
559
|
key: key,
|
|
467
560
|
changes: [],
|
|
468
|
-
}
|
|
561
|
+
};
|
|
469
562
|
}
|
|
470
563
|
}
|
|
471
564
|
}
|
|
472
565
|
|
|
473
566
|
if (isChain) {
|
|
474
567
|
for (let key in objChains) {
|
|
475
|
-
const model_table = MYMODELS[key] // require(`./../models/${key}`);
|
|
568
|
+
const model_table = MYMODELS[key]; // require(`./../models/${key}`);
|
|
476
569
|
for (let q in model_table.widgets) {
|
|
477
|
-
if (model_table.widgets[q].hasOwnProperty(
|
|
570
|
+
if (model_table.widgets[q].hasOwnProperty("table")) {
|
|
478
571
|
if (Util.in_array(model_table.widgets[q].table, chainsTable)) {
|
|
479
572
|
const objChanges = {
|
|
480
573
|
target: objChains[key].key,
|
|
481
574
|
table: key,
|
|
482
575
|
column: q,
|
|
483
576
|
name: widgets[objChains[key].key].fields[1],
|
|
484
|
-
}
|
|
485
|
-
objChains[model_table.widgets[q].table].changes.push(
|
|
577
|
+
};
|
|
578
|
+
objChains[model_table.widgets[q].table].changes.push(
|
|
579
|
+
objChanges
|
|
580
|
+
);
|
|
486
581
|
}
|
|
487
582
|
}
|
|
488
583
|
}
|
|
@@ -490,32 +585,32 @@ const generate = async (req, res) => {
|
|
|
490
585
|
//console.log(JSON.stringify(objChains))
|
|
491
586
|
//update model
|
|
492
587
|
for (let key in objChains) {
|
|
493
|
-
const keykey = objChains[key].key
|
|
494
|
-
widgets[keykey].chains = objChains[key].changes
|
|
588
|
+
const keykey = objChains[key].key;
|
|
589
|
+
widgets[keykey].chains = objChains[key].changes;
|
|
495
590
|
}
|
|
496
|
-
MYMODEL.widgets = widgets
|
|
497
|
-
let newModel = `module.exports = ${Util.newLine}${Util.tab}{${Util.newLine}
|
|
591
|
+
MYMODEL.widgets = widgets;
|
|
592
|
+
let newModel = `module.exports = ${Util.newLine}${Util.tab}{${Util.newLine}`;
|
|
498
593
|
//newModel += JSON.stringify(MYMODEL, null, 2)
|
|
499
|
-
newModel += zRoute.buildFileModel(MYMODEL)
|
|
500
|
-
newModel += `${Util.tab}}
|
|
594
|
+
newModel += zRoute.buildFileModel(MYMODEL);
|
|
595
|
+
newModel += `${Util.tab}};`;
|
|
501
596
|
|
|
502
|
-
fs.writeFileSync(`${dirRoot}/models/${filesKey.filename}`, newModel)
|
|
597
|
+
fs.writeFileSync(`${dirRoot}/models/${filesKey.filename}`, newModel);
|
|
503
598
|
}
|
|
504
599
|
//check model if it has select relation in concat
|
|
505
|
-
await scanning(MYMODEL, result)
|
|
600
|
+
await scanning(MYMODEL, result);
|
|
506
601
|
}
|
|
507
602
|
//generate views
|
|
508
|
-
let DIR_VIEW = `${dirRoot}/views/${table}
|
|
509
|
-
if (key ==
|
|
603
|
+
let DIR_VIEW = `${dirRoot}/views/${table}`;
|
|
604
|
+
if (key == "views") {
|
|
510
605
|
if (!fs.existsSync(DIR_VIEW)) {
|
|
511
|
-
fs.mkdirSync(DIR_VIEW)
|
|
606
|
+
fs.mkdirSync(DIR_VIEW);
|
|
512
607
|
}
|
|
513
|
-
let compilesJS = [
|
|
608
|
+
let compilesJS = ["createjs.ejs", "updatejs.ejs", "importjs.ejs"];
|
|
514
609
|
if (!dummy) {
|
|
515
610
|
for (const q in filesKey) {
|
|
516
|
-
console.log(q)
|
|
611
|
+
console.log(q);
|
|
517
612
|
if (Util.in_array(q, compilesJS)) {
|
|
518
|
-
fs.writeFileSync(`${DIR_VIEW}/${q}`, filesKey[q])
|
|
613
|
+
fs.writeFileSync(`${DIR_VIEW}/${q}`, filesKey[q]);
|
|
519
614
|
} else {
|
|
520
615
|
fs.writeFileSync(
|
|
521
616
|
`${DIR_VIEW}/${q}`,
|
|
@@ -524,317 +619,337 @@ const generate = async (req, res) => {
|
|
|
524
619
|
minifyCSS: true,
|
|
525
620
|
collapseWhitespace: true,
|
|
526
621
|
})
|
|
527
|
-
)
|
|
622
|
+
);
|
|
528
623
|
}
|
|
529
|
-
console.log(`The files views of ${q} is saved!`)
|
|
624
|
+
console.log(`The files views of ${q} is saved!`);
|
|
530
625
|
}
|
|
531
626
|
}
|
|
532
627
|
}
|
|
533
628
|
if (!dummy) {
|
|
534
|
-
if (key ==
|
|
535
|
-
fs.writeFileSync(
|
|
536
|
-
|
|
629
|
+
if (key == "route") {
|
|
630
|
+
fs.writeFileSync(
|
|
631
|
+
`${dirRoot}/routes/${filesKey.filename}`,
|
|
632
|
+
filesKey.content
|
|
633
|
+
);
|
|
634
|
+
console.log(`The files route of ${filesKey.filename} is saved!`);
|
|
537
635
|
}
|
|
538
636
|
}
|
|
539
637
|
}
|
|
540
638
|
|
|
541
639
|
//make a directory
|
|
542
640
|
if (!dummy) {
|
|
543
|
-
let path_script = `${dirRoot}/public/runtime/script/${table}
|
|
544
|
-
Util.dirExist(path_script, true)
|
|
545
|
-
let path_head = `${dirRoot}/public/runtime/head/${table}
|
|
546
|
-
Util.dirExist(path_head, true)
|
|
547
|
-
let path_end = `${dirRoot}/public/runtime/end/${table}
|
|
548
|
-
Util.dirExist(path_end, true)
|
|
641
|
+
let path_script = `${dirRoot}/public/runtime/script/${table}`;
|
|
642
|
+
Util.dirExist(path_script, true);
|
|
643
|
+
let path_head = `${dirRoot}/public/runtime/head/${table}`;
|
|
644
|
+
Util.dirExist(path_head, true);
|
|
645
|
+
let path_end = `${dirRoot}/public/runtime/end/${table}`;
|
|
646
|
+
Util.dirExist(path_end, true);
|
|
549
647
|
//we need to generate javascript code into runtime
|
|
550
|
-
let relations = await zRoute.relations(req, res, MYMODEL.table)
|
|
648
|
+
let relations = await zRoute.relations(req, res, MYMODEL.table);
|
|
551
649
|
//add script
|
|
552
|
-
let jsObj = zRoute.generateJS(req, res, MYMODEL, relations)
|
|
650
|
+
let jsObj = zRoute.generateJS(req, res, MYMODEL, relations);
|
|
553
651
|
//minify script
|
|
554
|
-
let script = uglifyJS.minify(jsObj.script)
|
|
652
|
+
let script = uglifyJS.minify(jsObj.script);
|
|
555
653
|
if (script.error) {
|
|
556
|
-
console.log(jsObj.script)
|
|
557
|
-
throw script.error
|
|
654
|
+
console.log(jsObj.script);
|
|
655
|
+
throw script.error;
|
|
558
656
|
}
|
|
559
|
-
let time = new Date().getTime()
|
|
560
|
-
Util.deleteAllFiles(path_script)
|
|
561
|
-
Util.deleteAllFiles(path_head)
|
|
562
|
-
Util.deleteAllFiles(path_end)
|
|
563
|
-
Util.writeFile(`${path_script}/${time}.js`, script.code)
|
|
564
|
-
Util.writeFile(`${path_head}/head.txt`, jsObj.head)
|
|
565
|
-
Util.writeFile(`${path_end}/end.txt`, jsObj.end)
|
|
657
|
+
let time = new Date().getTime();
|
|
658
|
+
Util.deleteAllFiles(path_script);
|
|
659
|
+
Util.deleteAllFiles(path_head);
|
|
660
|
+
Util.deleteAllFiles(path_end);
|
|
661
|
+
Util.writeFile(`${path_script}/${time}.js`, script.code);
|
|
662
|
+
Util.writeFile(`${path_head}/head.txt`, jsObj.head);
|
|
663
|
+
Util.writeFile(`${path_end}/end.txt`, jsObj.end);
|
|
566
664
|
}
|
|
567
665
|
|
|
568
666
|
//restart pm2 module in production
|
|
569
|
-
if (process.env.NODE_ENV ==
|
|
667
|
+
if (process.env.NODE_ENV == "production") {
|
|
570
668
|
pm2.connect(function (err) {
|
|
571
669
|
if (err) {
|
|
572
|
-
console.log(err.toString())
|
|
670
|
+
console.log(err.toString());
|
|
573
671
|
}
|
|
574
672
|
pm2.restart(process.env.PM2_NAME, (err, proc) => {
|
|
575
673
|
//io.to(room).emit("message","Restart done")
|
|
576
|
-
})
|
|
577
|
-
})
|
|
674
|
+
});
|
|
675
|
+
});
|
|
578
676
|
}
|
|
579
677
|
|
|
580
|
-
return Util.jsonSuccess(
|
|
581
|
-
}
|
|
678
|
+
return Util.jsonSuccess("Success");
|
|
679
|
+
};
|
|
582
680
|
|
|
583
|
-
router.post(
|
|
584
|
-
let body = req.body
|
|
585
|
-
let table = body.table
|
|
586
|
-
let details = body.details
|
|
681
|
+
router.post("/savetabs", async (req, res) => {
|
|
682
|
+
let body = req.body;
|
|
683
|
+
let table = body.table;
|
|
684
|
+
let details = body.details;
|
|
587
685
|
let left = [],
|
|
588
686
|
right = [],
|
|
589
687
|
tabLeft = {},
|
|
590
|
-
tabRight = {}
|
|
591
|
-
let json = { status: 0, title:
|
|
592
|
-
let obj = {}
|
|
593
|
-
obj.notabs = []
|
|
594
|
-
let labels = {}
|
|
595
|
-
let hasTab = false
|
|
688
|
+
tabRight = {};
|
|
689
|
+
let json = { status: 0, title: "error", message: "error", url: "" };
|
|
690
|
+
let obj = {};
|
|
691
|
+
obj.notabs = [];
|
|
692
|
+
let labels = {};
|
|
693
|
+
let hasTab = false;
|
|
596
694
|
if (nots.includes(table)) {
|
|
597
|
-
return res.json(Util.flashError(
|
|
695
|
+
return res.json(Util.flashError("Table is locked"));
|
|
598
696
|
}
|
|
599
697
|
|
|
600
|
-
let rows = await connection.results({
|
|
698
|
+
let rows = await connection.results({
|
|
699
|
+
table: "zfields",
|
|
700
|
+
where: { table: table },
|
|
701
|
+
});
|
|
601
702
|
if (rows.length > 0) {
|
|
602
|
-
let tabsValue = rows[0].tabs +
|
|
703
|
+
let tabsValue = rows[0].tabs + "";
|
|
603
704
|
if (tabsValue.length > 5) {
|
|
604
|
-
hasTab = true
|
|
605
|
-
let tabs = rows[0].tabs
|
|
705
|
+
hasTab = true;
|
|
706
|
+
let tabs = rows[0].tabs;
|
|
606
707
|
for (let i = 0; i < tabs.length; i++) {
|
|
607
|
-
obj[
|
|
608
|
-
tabLeft[
|
|
609
|
-
tabRight[
|
|
708
|
+
obj["arr" + i] = [];
|
|
709
|
+
tabLeft["arr" + i] = [];
|
|
710
|
+
tabRight["arr" + i] = [];
|
|
610
711
|
}
|
|
611
712
|
}
|
|
612
713
|
}
|
|
613
714
|
|
|
614
715
|
for (let i = 0; i < details.length; i++) {
|
|
615
|
-
let name = details[i].name
|
|
616
|
-
let value = details[i].value
|
|
617
|
-
if (name.indexOf(
|
|
618
|
-
let explode = name.split(
|
|
619
|
-
obj[explode[1]].push(explode[0])
|
|
620
|
-
labels[explode[0]] = value
|
|
716
|
+
let name = details[i].name;
|
|
717
|
+
let value = details[i].value;
|
|
718
|
+
if (name.indexOf("___") > -1) {
|
|
719
|
+
let explode = name.split("___");
|
|
720
|
+
obj[explode[1]].push(explode[0]);
|
|
721
|
+
labels[explode[0]] = value;
|
|
621
722
|
} else {
|
|
622
|
-
if (name ==
|
|
623
|
-
if (value.indexOf(
|
|
624
|
-
var explode = value.split(
|
|
625
|
-
tabLeft[explode[1]].push(explode[0])
|
|
723
|
+
if (name == "LEFT") {
|
|
724
|
+
if (value.indexOf("___") > -1) {
|
|
725
|
+
var explode = value.split("___");
|
|
726
|
+
tabLeft[explode[1]].push(explode[0]);
|
|
626
727
|
} else {
|
|
627
|
-
left.push(value)
|
|
728
|
+
left.push(value);
|
|
628
729
|
}
|
|
629
|
-
} else if (name ==
|
|
630
|
-
if (value.indexOf(
|
|
631
|
-
let explode = value.split(
|
|
632
|
-
tabRight[explode[1]].push(explode[0])
|
|
730
|
+
} else if (name == "RIGHT") {
|
|
731
|
+
if (value.indexOf("___") > -1) {
|
|
732
|
+
let explode = value.split("___");
|
|
733
|
+
tabRight[explode[1]].push(explode[0]);
|
|
633
734
|
} else {
|
|
634
|
-
right.push(value)
|
|
735
|
+
right.push(value);
|
|
635
736
|
}
|
|
636
737
|
} else {
|
|
637
|
-
obj[
|
|
638
|
-
labels[name] = value
|
|
738
|
+
obj["notabs"].push(name);
|
|
739
|
+
labels[name] = value;
|
|
639
740
|
}
|
|
640
741
|
}
|
|
641
742
|
}
|
|
642
|
-
let post = {}
|
|
643
|
-
post.labels = JSON.stringify(labels)
|
|
644
|
-
post.details = JSON.stringify(obj)
|
|
645
|
-
post.left = hasTab ? JSON.stringify(tabLeft) : JSON.stringify(left)
|
|
646
|
-
post.right = hasTab ? JSON.stringify(tabRight) : JSON.stringify(right)
|
|
743
|
+
let post = {};
|
|
744
|
+
post.labels = JSON.stringify(labels);
|
|
745
|
+
post.details = JSON.stringify(obj);
|
|
746
|
+
post.left = hasTab ? JSON.stringify(tabLeft) : JSON.stringify(left);
|
|
747
|
+
post.right = hasTab ? JSON.stringify(tabRight) : JSON.stringify(right);
|
|
647
748
|
//console.log(post.left)
|
|
648
749
|
//console.log(JSON.stringify(post))
|
|
649
750
|
try {
|
|
650
751
|
if (rows.length > 0) {
|
|
651
|
-
await connection.update({
|
|
752
|
+
await connection.update({
|
|
753
|
+
table: "zfields",
|
|
754
|
+
data: post,
|
|
755
|
+
where: { id: rows[0].id },
|
|
756
|
+
});
|
|
652
757
|
} else {
|
|
653
|
-
post.table = table
|
|
654
|
-
await connection.insert({ table:
|
|
758
|
+
post.table = table;
|
|
759
|
+
await connection.insert({ table: "zfields", data: post });
|
|
655
760
|
}
|
|
656
|
-
json = { status: 1, title:
|
|
761
|
+
json = { status: 1, title: "Success", message: "Success", url: "" };
|
|
657
762
|
} catch (error) {
|
|
658
|
-
json = Util.flashError(error.sqlMessage)
|
|
763
|
+
json = Util.flashError(error.sqlMessage);
|
|
659
764
|
}
|
|
660
765
|
|
|
661
|
-
res.send(json)
|
|
662
|
-
})
|
|
766
|
+
res.send(json);
|
|
767
|
+
});
|
|
663
768
|
|
|
664
769
|
const buildJoin = async (MYMODEL, result) => {
|
|
665
|
-
let joins = result.joins || {}
|
|
666
|
-
let table = MYMODEL.table
|
|
667
|
-
let table_joins = []
|
|
668
|
-
let join_list = []
|
|
770
|
+
let joins = result.joins || {};
|
|
771
|
+
let table = MYMODEL.table;
|
|
772
|
+
let table_joins = [];
|
|
773
|
+
let join_list = [];
|
|
669
774
|
|
|
670
775
|
if (result.joins && Object.keys(joins).length > 0) {
|
|
671
|
-
let newDataObj = {}
|
|
776
|
+
let newDataObj = {};
|
|
672
777
|
for (let key in joins) {
|
|
673
|
-
let THEIR_MODEL = require(`${dirRoot}/models/${key}`)
|
|
778
|
+
let THEIR_MODEL = require(`${dirRoot}/models/${key}`);
|
|
674
779
|
//console.log(THEIR_MODEL)
|
|
675
|
-
let arr = joins[key] || []
|
|
676
|
-
table_joins.push(key)
|
|
780
|
+
let arr = joins[key] || [];
|
|
781
|
+
table_joins.push(key);
|
|
677
782
|
if (arr.length > 0) {
|
|
678
783
|
arr.map((item) => {
|
|
679
784
|
//console.log(item)
|
|
680
785
|
//console.log(typeof item)
|
|
681
|
-
const originKey = item.key.replace(`${key}___`,
|
|
682
|
-
console.log(originKey)
|
|
683
|
-
newDataObj[item.key] = item.value
|
|
684
|
-
MYMODEL.keys.push(item.key)
|
|
786
|
+
const originKey = item.key.replace(`${key}___`, "");
|
|
787
|
+
console.log(originKey);
|
|
788
|
+
newDataObj[item.key] = item.value;
|
|
789
|
+
MYMODEL.keys.push(item.key);
|
|
685
790
|
//MYMODEL.keysExcel.push(item.key)
|
|
686
|
-
MYMODEL.labels[item.key] = item.value
|
|
687
|
-
MYMODEL.fields[item.key] = THEIR_MODEL.fields[originKey]
|
|
688
|
-
MYMODEL.options[item.key] = THEIR_MODEL.options[originKey]
|
|
689
|
-
MYMODEL.grids.invisibles.push(item.key)
|
|
690
|
-
MYMODEL.datas[item.key] = THEIR_MODEL.datas[originKey]
|
|
691
|
-
MYMODEL.widgets[item.key] = THEIR_MODEL.widgets[originKey]
|
|
692
|
-
join_list.push(item.key)
|
|
693
|
-
})
|
|
791
|
+
MYMODEL.labels[item.key] = item.value;
|
|
792
|
+
MYMODEL.fields[item.key] = THEIR_MODEL.fields[originKey];
|
|
793
|
+
MYMODEL.options[item.key] = THEIR_MODEL.options[originKey];
|
|
794
|
+
MYMODEL.grids.invisibles.push(item.key);
|
|
795
|
+
MYMODEL.datas[item.key] = THEIR_MODEL.datas[originKey];
|
|
796
|
+
MYMODEL.widgets[item.key] = THEIR_MODEL.widgets[originKey];
|
|
797
|
+
join_list.push(item.key);
|
|
798
|
+
});
|
|
694
799
|
}
|
|
695
800
|
}
|
|
696
801
|
|
|
697
|
-
let sql = []
|
|
802
|
+
let sql = [];
|
|
698
803
|
//get all foreign key
|
|
699
|
-
let rows = await connection.query(connection.foreignKeyList(table))
|
|
700
|
-
let foreign_table_name = {}
|
|
701
|
-
let foreignTables = []
|
|
804
|
+
let rows = await connection.query(connection.foreignKeyList(table));
|
|
805
|
+
let foreign_table_name = {};
|
|
806
|
+
let foreignTables = [];
|
|
702
807
|
rows.map((row) => {
|
|
703
|
-
foreignTables.push(row.foreign_table_name)
|
|
704
|
-
foreign_table_name[
|
|
705
|
-
|
|
808
|
+
foreignTables.push(row.foreign_table_name);
|
|
809
|
+
foreign_table_name[
|
|
810
|
+
row.foreign_table_name
|
|
811
|
+
] = ` LEFT JOIN ${row.foreign_table_name} ON ${row.foreign_table_name}.id = ${table}.${row.column_name} `;
|
|
812
|
+
});
|
|
706
813
|
|
|
707
814
|
//step by step
|
|
708
|
-
let table_steps = []
|
|
815
|
+
let table_steps = [];
|
|
709
816
|
table_joins.map((item) => {
|
|
710
817
|
if (foreignTables.includes(item)) {
|
|
711
|
-
sql.push(foreign_table_name[item])
|
|
712
|
-
table_steps.push(item)
|
|
818
|
+
sql.push(foreign_table_name[item]);
|
|
819
|
+
table_steps.push(item);
|
|
713
820
|
}
|
|
714
|
-
})
|
|
821
|
+
});
|
|
715
822
|
|
|
716
823
|
if (table_steps.length > 0) {
|
|
717
824
|
for (let table_steps_item of table_steps) {
|
|
718
|
-
let rows = await connection.query(
|
|
719
|
-
|
|
720
|
-
|
|
825
|
+
let rows = await connection.query(
|
|
826
|
+
connection.foreignKeyList(table_steps_item)
|
|
827
|
+
);
|
|
828
|
+
let foreign_table_name = {};
|
|
829
|
+
let foreignTables = [];
|
|
721
830
|
rows.map((row) => {
|
|
722
|
-
foreignTables.push(row.foreign_table_name)
|
|
723
|
-
foreign_table_name[
|
|
724
|
-
|
|
725
|
-
|
|
831
|
+
foreignTables.push(row.foreign_table_name);
|
|
832
|
+
foreign_table_name[
|
|
833
|
+
row.foreign_table_name
|
|
834
|
+
] = ` LEFT JOIN ${row.foreign_table_name} ON ${row.foreign_table_name}.id = ${table_steps_item}.${row.column_name} `;
|
|
835
|
+
});
|
|
836
|
+
console.log(foreignTables);
|
|
726
837
|
table_joins.map((item) => {
|
|
727
838
|
if (!table_steps.includes(item)) {
|
|
728
839
|
if (foreignTables.includes(item)) {
|
|
729
|
-
sql.push(foreign_table_name[item])
|
|
730
|
-
table_steps.push(item)
|
|
840
|
+
sql.push(foreign_table_name[item]);
|
|
841
|
+
table_steps.push(item);
|
|
731
842
|
}
|
|
732
843
|
}
|
|
733
|
-
})
|
|
844
|
+
});
|
|
734
845
|
}
|
|
735
846
|
}
|
|
736
847
|
|
|
737
848
|
if (table_steps.length > 0) {
|
|
738
849
|
for (let table_steps_item of table_steps) {
|
|
739
|
-
let rows = await connection.query(
|
|
740
|
-
|
|
741
|
-
|
|
850
|
+
let rows = await connection.query(
|
|
851
|
+
connection.foreignKeyList(table_steps_item)
|
|
852
|
+
);
|
|
853
|
+
let foreign_table_name = {};
|
|
854
|
+
let foreignTables = [];
|
|
742
855
|
rows.map((row) => {
|
|
743
|
-
foreignTables.push(row.foreign_table_name)
|
|
744
|
-
foreign_table_name[
|
|
745
|
-
|
|
856
|
+
foreignTables.push(row.foreign_table_name);
|
|
857
|
+
foreign_table_name[
|
|
858
|
+
row.foreign_table_name
|
|
859
|
+
] = ` LEFT JOIN ${row.foreign_table_name} ON ${row.foreign_table_name}.id = ${table_steps_item}.${row.column_name} `;
|
|
860
|
+
});
|
|
746
861
|
//console.log(foreignTables)
|
|
747
862
|
table_joins.map((item) => {
|
|
748
863
|
if (!table_steps.includes(item)) {
|
|
749
864
|
if (foreignTables.includes(item)) {
|
|
750
|
-
sql.push(foreign_table_name[item])
|
|
751
|
-
table_steps.push(item)
|
|
865
|
+
sql.push(foreign_table_name[item]);
|
|
866
|
+
table_steps.push(item);
|
|
752
867
|
}
|
|
753
868
|
}
|
|
754
|
-
})
|
|
869
|
+
});
|
|
755
870
|
}
|
|
756
871
|
}
|
|
757
872
|
|
|
758
873
|
//add join
|
|
759
|
-
let JOINS_DATA = {}
|
|
760
|
-
JOINS_DATA.tables = table_joins
|
|
761
|
-
JOINS_DATA.list = join_list
|
|
762
|
-
JOINS_DATA.data = joins
|
|
763
|
-
JOINS_DATA.sql = sql
|
|
874
|
+
let JOINS_DATA = {};
|
|
875
|
+
JOINS_DATA.tables = table_joins;
|
|
876
|
+
JOINS_DATA.list = join_list;
|
|
877
|
+
JOINS_DATA.data = joins;
|
|
878
|
+
JOINS_DATA.sql = sql;
|
|
764
879
|
|
|
765
|
-
MYMODEL.joins = JOINS_DATA
|
|
880
|
+
MYMODEL.joins = JOINS_DATA;
|
|
766
881
|
}
|
|
767
|
-
return MYMODEL
|
|
768
|
-
}
|
|
882
|
+
return MYMODEL;
|
|
883
|
+
};
|
|
769
884
|
|
|
770
885
|
const columnLR = (items, dataName) => {
|
|
771
|
-
return `<div class="col-md-6"><ol class="mydragable divboxlittle" data-name="${dataName}">${items}</ol></div
|
|
772
|
-
}
|
|
886
|
+
return `<div class="col-md-6"><ol class="mydragable divboxlittle" data-name="${dataName}">${items}</ol></div>`;
|
|
887
|
+
};
|
|
773
888
|
|
|
774
889
|
const columnOne = (items) => {
|
|
775
|
-
return `<div class="row sortable"><i class="fa fa-arrows icon-float"></i><div class="col-md-12"><ol class="mydragable divboxlittle" data-name="ONE_COLUMN">${items}</ol></div></div
|
|
776
|
-
}
|
|
890
|
+
return `<div class="row sortable"><i class="fa fa-arrows icon-float"></i><div class="col-md-12"><ol class="mydragable divboxlittle" data-name="ONE_COLUMN">${items}</ol></div></div>`;
|
|
891
|
+
};
|
|
777
892
|
|
|
778
893
|
const reformatProperties = async (position, item, table, arr) => {
|
|
779
|
-
const newData = JSON.stringify(Util.arrayDelete(arr, item))
|
|
894
|
+
const newData = JSON.stringify(Util.arrayDelete(arr, item));
|
|
780
895
|
const data = {
|
|
781
896
|
[position]: newData,
|
|
782
|
-
}
|
|
897
|
+
};
|
|
783
898
|
await connection.update({
|
|
784
|
-
table:
|
|
899
|
+
table: "zfields",
|
|
785
900
|
data: data,
|
|
786
901
|
where: {
|
|
787
902
|
table: table,
|
|
788
903
|
},
|
|
789
|
-
})
|
|
790
|
-
}
|
|
904
|
+
});
|
|
905
|
+
};
|
|
791
906
|
|
|
792
|
-
router.delete(
|
|
793
|
-
let table = req.body.table
|
|
794
|
-
let json = Util.jsonSuccess(
|
|
907
|
+
router.delete("/delete-table", csrfProtection, async (req, res) => {
|
|
908
|
+
let table = req.body.table;
|
|
909
|
+
let json = Util.jsonSuccess("Successfully delete module");
|
|
795
910
|
if (Util.in_array(table, nots)) {
|
|
796
|
-
return res.json(Util.flashError(
|
|
911
|
+
return res.json(Util.flashError("Can not be deleted!"));
|
|
797
912
|
}
|
|
798
913
|
if (nots.includes(table)) {
|
|
799
|
-
return res.json(Util.flashError(
|
|
914
|
+
return res.json(Util.flashError("Table is locked"));
|
|
800
915
|
}
|
|
801
916
|
try {
|
|
802
917
|
await connection.delete({
|
|
803
|
-
table:
|
|
918
|
+
table: "zfields",
|
|
804
919
|
where: {
|
|
805
920
|
table: table,
|
|
806
921
|
},
|
|
807
|
-
})
|
|
922
|
+
});
|
|
808
923
|
//await connection.query("DELETE FROM zfields WHERE `table` = ?", [table]);
|
|
809
|
-
await connection.query(`DROP table "${table}";`)
|
|
924
|
+
await connection.query(`DROP table "${table}";`);
|
|
810
925
|
//delete file
|
|
811
|
-
let modelFile = dirRoot +
|
|
926
|
+
let modelFile = dirRoot + "/models/" + table + ".js";
|
|
812
927
|
fs.stat(modelFile, function (err, stats) {
|
|
813
|
-
console.log(stats) //here we got all information of file in stats variable
|
|
928
|
+
console.log(stats); //here we got all information of file in stats variable
|
|
814
929
|
if (err) {
|
|
815
|
-
return console.error(err)
|
|
930
|
+
return console.error(err);
|
|
816
931
|
} else {
|
|
817
932
|
fs.unlink(modelFile, function (err) {
|
|
818
|
-
if (err) return console.log(err)
|
|
819
|
-
console.log(
|
|
820
|
-
})
|
|
933
|
+
if (err) return console.log(err);
|
|
934
|
+
console.log("file deleted successfully");
|
|
935
|
+
});
|
|
821
936
|
}
|
|
822
|
-
})
|
|
937
|
+
});
|
|
823
938
|
|
|
824
|
-
let routesFile = dirRoot +
|
|
939
|
+
let routesFile = dirRoot + "/routes/" + table + ".js";
|
|
825
940
|
fs.stat(routesFile, function (err, stats) {
|
|
826
|
-
console.log(stats) //here we got all information of file in stats variable
|
|
941
|
+
console.log(stats); //here we got all information of file in stats variable
|
|
827
942
|
if (err) {
|
|
828
|
-
return console.error(err)
|
|
943
|
+
return console.error(err);
|
|
829
944
|
} else {
|
|
830
945
|
fs.unlink(routesFile, function (err) {
|
|
831
|
-
if (err) return console.log(err)
|
|
832
|
-
console.log(
|
|
833
|
-
})
|
|
946
|
+
if (err) return console.log(err);
|
|
947
|
+
console.log("file deleted successfully");
|
|
948
|
+
});
|
|
834
949
|
}
|
|
835
|
-
})
|
|
950
|
+
});
|
|
836
951
|
|
|
837
|
-
let runtimeFile = dirRoot +
|
|
952
|
+
let runtimeFile = dirRoot + "/public/runtime/script/" + table;
|
|
838
953
|
fs.rm(
|
|
839
954
|
runtimeFile,
|
|
840
955
|
{
|
|
@@ -842,13 +957,13 @@ router.delete('/delete-table', csrfProtection, async (req, res) => {
|
|
|
842
957
|
},
|
|
843
958
|
(error) => {
|
|
844
959
|
if (error) {
|
|
845
|
-
console.log(error)
|
|
960
|
+
console.log(error);
|
|
846
961
|
} else {
|
|
847
|
-
console.log(
|
|
962
|
+
console.log("Recursive: Directories Deleted!");
|
|
848
963
|
}
|
|
849
964
|
}
|
|
850
|
-
)
|
|
851
|
-
let runtimeFileHead = dirRoot +
|
|
965
|
+
);
|
|
966
|
+
let runtimeFileHead = dirRoot + "/public/runtime/head/" + table;
|
|
852
967
|
fs.rm(
|
|
853
968
|
runtimeFileHead,
|
|
854
969
|
{
|
|
@@ -856,13 +971,13 @@ router.delete('/delete-table', csrfProtection, async (req, res) => {
|
|
|
856
971
|
},
|
|
857
972
|
(error) => {
|
|
858
973
|
if (error) {
|
|
859
|
-
console.log(error)
|
|
974
|
+
console.log(error);
|
|
860
975
|
} else {
|
|
861
|
-
console.log(
|
|
976
|
+
console.log("Recursive: Directories Deleted!");
|
|
862
977
|
}
|
|
863
978
|
}
|
|
864
|
-
)
|
|
865
|
-
let runtimeFileEnd = dirRoot +
|
|
979
|
+
);
|
|
980
|
+
let runtimeFileEnd = dirRoot + "/public/runtime/end/" + table;
|
|
866
981
|
fs.rm(
|
|
867
982
|
runtimeFileEnd,
|
|
868
983
|
{
|
|
@@ -870,19 +985,19 @@ router.delete('/delete-table', csrfProtection, async (req, res) => {
|
|
|
870
985
|
},
|
|
871
986
|
(error) => {
|
|
872
987
|
if (error) {
|
|
873
|
-
console.log(error)
|
|
988
|
+
console.log(error);
|
|
874
989
|
} else {
|
|
875
|
-
console.log(
|
|
990
|
+
console.log("Recursive: Directories Deleted!");
|
|
876
991
|
}
|
|
877
992
|
}
|
|
878
|
-
)
|
|
993
|
+
);
|
|
879
994
|
|
|
880
995
|
await connection.delete({
|
|
881
|
-
table:
|
|
996
|
+
table: "zgrid",
|
|
882
997
|
where: { route_name: table },
|
|
883
|
-
})
|
|
998
|
+
});
|
|
884
999
|
if (table) {
|
|
885
|
-
const viewsFile = dirRoot +
|
|
1000
|
+
const viewsFile = dirRoot + "/views/" + table;
|
|
886
1001
|
fs.rm(
|
|
887
1002
|
viewsFile,
|
|
888
1003
|
{
|
|
@@ -890,255 +1005,273 @@ router.delete('/delete-table', csrfProtection, async (req, res) => {
|
|
|
890
1005
|
},
|
|
891
1006
|
(error) => {
|
|
892
1007
|
if (error) {
|
|
893
|
-
console.log(error)
|
|
1008
|
+
console.log(error);
|
|
894
1009
|
} else {
|
|
895
|
-
console.log(
|
|
1010
|
+
console.log("Recursive: Directories Deleted!");
|
|
896
1011
|
}
|
|
897
1012
|
}
|
|
898
|
-
)
|
|
1013
|
+
);
|
|
899
1014
|
}
|
|
900
1015
|
await connection.delete({
|
|
901
|
-
table:
|
|
1016
|
+
table: "zgrid_default",
|
|
902
1017
|
where: { table: table },
|
|
903
|
-
})
|
|
1018
|
+
});
|
|
904
1019
|
} catch (e) {
|
|
905
|
-
json = Util.flashError(e.toString(),
|
|
1020
|
+
json = Util.flashError(e.toString(), "");
|
|
906
1021
|
}
|
|
907
1022
|
|
|
908
|
-
res.json(json)
|
|
909
|
-
})
|
|
1023
|
+
res.json(json);
|
|
1024
|
+
});
|
|
910
1025
|
|
|
911
|
-
router.post(
|
|
1026
|
+
router.post("/add_field", csrfProtection, async (req, res) => {
|
|
912
1027
|
try {
|
|
913
|
-
const body = req.body
|
|
914
|
-
console.log(JSON.stringify(body))
|
|
915
|
-
const table = body.table
|
|
1028
|
+
const body = req.body;
|
|
1029
|
+
console.log(JSON.stringify(body));
|
|
1030
|
+
const table = body.table;
|
|
916
1031
|
if (nots.includes(table)) {
|
|
917
|
-
return res.json(Util.flashError(
|
|
1032
|
+
return res.json(Util.flashError("Table is locked"));
|
|
918
1033
|
}
|
|
919
|
-
body.zFields = await connection.results({
|
|
920
|
-
|
|
921
|
-
|
|
1034
|
+
body.zFields = await connection.results({
|
|
1035
|
+
table: "zfields",
|
|
1036
|
+
where: { table: table },
|
|
1037
|
+
});
|
|
1038
|
+
const results = await axios.post(
|
|
1039
|
+
`${generatorUrl}/api/generator/create_field`,
|
|
1040
|
+
body
|
|
1041
|
+
);
|
|
1042
|
+
const datas = results.data;
|
|
922
1043
|
if (datas.status == 1) {
|
|
923
|
-
await connection.update({
|
|
1044
|
+
await connection.update({
|
|
1045
|
+
table: "zfields",
|
|
1046
|
+
where: { table: table },
|
|
1047
|
+
data: datas.post,
|
|
1048
|
+
});
|
|
924
1049
|
}
|
|
925
|
-
res.json(datas)
|
|
1050
|
+
res.json(datas);
|
|
926
1051
|
} catch (e) {
|
|
927
|
-
res.json(Util.flashError(e.toString()))
|
|
1052
|
+
res.json(Util.flashError(e.toString()));
|
|
928
1053
|
}
|
|
929
|
-
})
|
|
1054
|
+
});
|
|
930
1055
|
|
|
931
|
-
router.post(
|
|
932
|
-
let others = req.body.others
|
|
933
|
-
let table = req.body.table
|
|
1056
|
+
router.post("/add_container", async (req, res) => {
|
|
1057
|
+
let others = req.body.others;
|
|
1058
|
+
let table = req.body.table;
|
|
934
1059
|
//console.log(JSON.stringify(req.body))
|
|
935
1060
|
try {
|
|
936
1061
|
if (others && table) {
|
|
937
1062
|
await connection.update({
|
|
938
|
-
table:
|
|
1063
|
+
table: "zfields",
|
|
939
1064
|
data: {
|
|
940
1065
|
others: others,
|
|
941
1066
|
},
|
|
942
1067
|
where: {
|
|
943
1068
|
table: table,
|
|
944
1069
|
},
|
|
945
|
-
})
|
|
1070
|
+
});
|
|
946
1071
|
}
|
|
947
1072
|
} catch (e) {
|
|
948
|
-
return res.json(Util.flashError(e.toString()))
|
|
1073
|
+
return res.json(Util.flashError(e.toString()));
|
|
949
1074
|
}
|
|
950
1075
|
|
|
951
|
-
res.json(Util.jsonSuccess(
|
|
952
|
-
})
|
|
1076
|
+
res.json(Util.jsonSuccess("Success"));
|
|
1077
|
+
});
|
|
953
1078
|
|
|
954
|
-
router.post(
|
|
1079
|
+
router.post("/setting_field", csrfProtection, async (req, res) => {
|
|
955
1080
|
if (nots.includes(req.body.table)) {
|
|
956
|
-
return res.json(Util.flashError(
|
|
1081
|
+
return res.json(Util.flashError("Table is locked"));
|
|
957
1082
|
}
|
|
958
|
-
const body = req.body
|
|
959
|
-
const table = body.table
|
|
960
|
-
const html =
|
|
961
|
-
body.result = await connection.result({
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
1083
|
+
const body = req.body;
|
|
1084
|
+
const table = body.table;
|
|
1085
|
+
const html = "";
|
|
1086
|
+
body.result = await connection.result({
|
|
1087
|
+
table: "zfields",
|
|
1088
|
+
where: { table: table },
|
|
1089
|
+
});
|
|
1090
|
+
const results = await axios.post(
|
|
1091
|
+
`${generatorUrl}/api/generator/modal_settings`,
|
|
1092
|
+
body
|
|
1093
|
+
);
|
|
1094
|
+
const datas = results.data;
|
|
1095
|
+
res.json(datas.html);
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
router.post("/save_setting", async (req, res) => {
|
|
1099
|
+
const html = "",
|
|
1100
|
+
table = req.query.table || "";
|
|
1101
|
+
let json = Util.jsonSuccess("Success to update");
|
|
971
1102
|
try {
|
|
972
1103
|
if (nots.includes(req.body.table)) {
|
|
973
|
-
return res.json(Util.flashError(
|
|
1104
|
+
return res.json(Util.flashError("Table is locked"));
|
|
974
1105
|
}
|
|
975
|
-
const post = req.body
|
|
1106
|
+
const post = req.body;
|
|
976
1107
|
let data = await connection.result({
|
|
977
|
-
table:
|
|
1108
|
+
table: "zfields",
|
|
978
1109
|
where: { table: table },
|
|
979
|
-
})
|
|
980
|
-
let properties = data.properties ? data.properties : {}
|
|
981
|
-
let propertyKey =
|
|
1110
|
+
});
|
|
1111
|
+
let properties = data.properties ? data.properties : {};
|
|
1112
|
+
let propertyKey = "";
|
|
982
1113
|
for (var key in post) {
|
|
983
|
-
propertyKey = key
|
|
1114
|
+
propertyKey = key;
|
|
984
1115
|
}
|
|
985
1116
|
|
|
986
|
-
properties[propertyKey].values = post[propertyKey]
|
|
1117
|
+
properties[propertyKey].values = post[propertyKey];
|
|
987
1118
|
let insert = {
|
|
988
1119
|
properties: JSON.stringify(properties),
|
|
989
|
-
}
|
|
1120
|
+
};
|
|
990
1121
|
await connection.update({
|
|
991
|
-
table:
|
|
1122
|
+
table: "zfields",
|
|
992
1123
|
data: insert,
|
|
993
1124
|
where: { table: table },
|
|
994
|
-
})
|
|
1125
|
+
});
|
|
995
1126
|
} catch (e) {
|
|
996
|
-
json = Util.flashError(e.toString())
|
|
1127
|
+
json = Util.flashError(e.toString());
|
|
997
1128
|
}
|
|
998
1129
|
|
|
999
|
-
res.json(json)
|
|
1000
|
-
})
|
|
1130
|
+
res.json(json);
|
|
1131
|
+
});
|
|
1001
1132
|
|
|
1002
|
-
router.delete(
|
|
1003
|
-
let json = Util.jsonSuccess(
|
|
1133
|
+
router.delete("/delete_field", csrfProtection, async (req, res) => {
|
|
1134
|
+
let json = Util.jsonSuccess("Success to update");
|
|
1004
1135
|
try {
|
|
1005
1136
|
if (nots.includes(req.body.table)) {
|
|
1006
|
-
return res.json(Util.flashError(
|
|
1137
|
+
return res.json(Util.flashError("Table is locked"));
|
|
1007
1138
|
}
|
|
1008
1139
|
const table = req.body.table,
|
|
1009
|
-
name = req.body.name
|
|
1140
|
+
name = req.body.name;
|
|
1010
1141
|
let data = await connection.result({
|
|
1011
|
-
table:
|
|
1142
|
+
table: "zfields",
|
|
1012
1143
|
where: { table: table },
|
|
1013
|
-
})
|
|
1014
|
-
let properties = data.properties ? data.properties : {}
|
|
1015
|
-
delete properties[name]
|
|
1016
|
-
let labels = data.labels ? data.labels : {}
|
|
1017
|
-
delete labels[name]
|
|
1018
|
-
|
|
1019
|
-
let left = data.left ? data.left : []
|
|
1020
|
-
left = Util.arrayDelete(left, name)
|
|
1021
|
-
let right = data.right ? data.right : []
|
|
1022
|
-
right = Util.arrayDelete(right, name)
|
|
1023
|
-
let oneColumn = data.one_column ? data.one_column : []
|
|
1024
|
-
oneColumn = Util.arrayDelete(oneColumn, name)
|
|
1025
|
-
let details = data.details ? data.details : {}
|
|
1026
|
-
let detailsTemp = {}
|
|
1144
|
+
});
|
|
1145
|
+
let properties = data.properties ? data.properties : {};
|
|
1146
|
+
delete properties[name];
|
|
1147
|
+
let labels = data.labels ? data.labels : {};
|
|
1148
|
+
delete labels[name];
|
|
1149
|
+
|
|
1150
|
+
let left = data.left ? data.left : [];
|
|
1151
|
+
left = Util.arrayDelete(left, name);
|
|
1152
|
+
let right = data.right ? data.right : [];
|
|
1153
|
+
right = Util.arrayDelete(right, name);
|
|
1154
|
+
let oneColumn = data.one_column ? data.one_column : [];
|
|
1155
|
+
oneColumn = Util.arrayDelete(oneColumn, name);
|
|
1156
|
+
let details = data.details ? data.details : {};
|
|
1157
|
+
let detailsTemp = {};
|
|
1027
1158
|
for (var keys in details) {
|
|
1028
|
-
detailsTemp[keys] = Util.arrayDelete(details[keys], name)
|
|
1159
|
+
detailsTemp[keys] = Util.arrayDelete(details[keys], name);
|
|
1029
1160
|
}
|
|
1030
|
-
let newProperty = {}
|
|
1031
|
-
newProperty.properties = JSON.stringify(properties)
|
|
1032
|
-
newProperty.labels = JSON.stringify(labels)
|
|
1033
|
-
newProperty.left = JSON.stringify(left)
|
|
1034
|
-
newProperty.right = JSON.stringify(right)
|
|
1035
|
-
newProperty.one_column = JSON.stringify(oneColumn)
|
|
1036
|
-
newProperty.details = JSON.stringify(detailsTemp)
|
|
1037
|
-
newProperty.sorting = JSON.stringify(data.sorting)
|
|
1161
|
+
let newProperty = {};
|
|
1162
|
+
newProperty.properties = JSON.stringify(properties);
|
|
1163
|
+
newProperty.labels = JSON.stringify(labels);
|
|
1164
|
+
newProperty.left = JSON.stringify(left);
|
|
1165
|
+
newProperty.right = JSON.stringify(right);
|
|
1166
|
+
newProperty.one_column = JSON.stringify(oneColumn);
|
|
1167
|
+
newProperty.details = JSON.stringify(detailsTemp);
|
|
1168
|
+
newProperty.sorting = JSON.stringify(data.sorting);
|
|
1038
1169
|
await connection.update({
|
|
1039
|
-
table:
|
|
1170
|
+
table: "zfields",
|
|
1040
1171
|
data: newProperty,
|
|
1041
1172
|
where: { table: table },
|
|
1042
|
-
})
|
|
1173
|
+
});
|
|
1043
1174
|
//DROP COULUMN
|
|
1044
1175
|
//ALTER TABLE table_name DROP COLUMN IF EXISTS column_name;
|
|
1045
|
-
await connection.query(
|
|
1176
|
+
await connection.query(
|
|
1177
|
+
`ALTER TABLE "${table}" DROP COLUMN IF EXISTS "${name}";`
|
|
1178
|
+
);
|
|
1046
1179
|
} catch (err) {
|
|
1047
|
-
json = Util.flashError(err.toString())
|
|
1048
|
-
console.log(err)
|
|
1049
|
-
console.log(
|
|
1180
|
+
json = Util.flashError(err.toString());
|
|
1181
|
+
console.log(err);
|
|
1182
|
+
console.log("ada error di update zfields");
|
|
1050
1183
|
}
|
|
1051
|
-
res.json(json)
|
|
1052
|
-
})
|
|
1053
|
-
|
|
1054
|
-
router.post(
|
|
1055
|
-
const html =
|
|
1056
|
-
table = req.body.table ||
|
|
1057
|
-
name = req.body.name ||
|
|
1058
|
-
id = req.body.id || 0
|
|
1059
|
-
let json = Util.jsonSuccess(
|
|
1184
|
+
res.json(json);
|
|
1185
|
+
});
|
|
1186
|
+
|
|
1187
|
+
router.post("/tab_rename", csrfProtection, async (req, res) => {
|
|
1188
|
+
const html = "",
|
|
1189
|
+
table = req.body.table || "",
|
|
1190
|
+
name = req.body.name || "",
|
|
1191
|
+
id = req.body.id || 0;
|
|
1192
|
+
let json = Util.jsonSuccess("Success to rename");
|
|
1060
1193
|
try {
|
|
1061
|
-
if (table ==
|
|
1062
|
-
return res.json(Util.flashError(
|
|
1194
|
+
if (table == "") {
|
|
1195
|
+
return res.json(Util.flashError("table empty"));
|
|
1063
1196
|
}
|
|
1064
|
-
if (name ==
|
|
1065
|
-
return res.json(Util.flashError(
|
|
1197
|
+
if (name == "") {
|
|
1198
|
+
return res.json(Util.flashError("name empty"));
|
|
1066
1199
|
}
|
|
1067
1200
|
let data = await connection.result({
|
|
1068
|
-
table:
|
|
1201
|
+
table: "zfields",
|
|
1069
1202
|
where: { table: table },
|
|
1070
|
-
})
|
|
1071
|
-
const tabs = data.tabs || []
|
|
1072
|
-
let arr = []
|
|
1203
|
+
});
|
|
1204
|
+
const tabs = data.tabs || [];
|
|
1205
|
+
let arr = [];
|
|
1073
1206
|
tabs.forEach(function (item, index) {
|
|
1074
1207
|
if (index == id) {
|
|
1075
|
-
arr.push(name)
|
|
1208
|
+
arr.push(name);
|
|
1076
1209
|
} else {
|
|
1077
|
-
arr.push(item)
|
|
1210
|
+
arr.push(item);
|
|
1078
1211
|
}
|
|
1079
|
-
})
|
|
1212
|
+
});
|
|
1080
1213
|
let post = {
|
|
1081
1214
|
tabs: JSON.stringify(arr),
|
|
1082
|
-
}
|
|
1215
|
+
};
|
|
1083
1216
|
await connection.update({
|
|
1084
|
-
table:
|
|
1217
|
+
table: "zfields",
|
|
1085
1218
|
data: post,
|
|
1086
1219
|
where: {
|
|
1087
1220
|
table: table,
|
|
1088
1221
|
},
|
|
1089
|
-
})
|
|
1222
|
+
});
|
|
1090
1223
|
} catch (e) {
|
|
1091
|
-
json = Util.flashError(e.toString())
|
|
1224
|
+
json = Util.flashError(e.toString());
|
|
1092
1225
|
}
|
|
1093
|
-
res.json(json)
|
|
1094
|
-
})
|
|
1226
|
+
res.json(json);
|
|
1227
|
+
});
|
|
1095
1228
|
|
|
1096
1229
|
const zfields = async () => {
|
|
1097
1230
|
return await connection.results({
|
|
1098
1231
|
select: `id, "table", "name", "route",tabs,labels, details, "left","right",one_column, sorting, properties,others,json`,
|
|
1099
|
-
table:
|
|
1100
|
-
orderBy: [
|
|
1101
|
-
})
|
|
1102
|
-
}
|
|
1232
|
+
table: "zfields",
|
|
1233
|
+
orderBy: ["table", "asc"],
|
|
1234
|
+
});
|
|
1235
|
+
};
|
|
1103
1236
|
|
|
1104
1237
|
const zfieldsKeys = async () => {
|
|
1105
|
-
return Util.arrayToObject(await zfields(),
|
|
1106
|
-
}
|
|
1238
|
+
return Util.arrayToObject(await zfields(), "table");
|
|
1239
|
+
};
|
|
1107
1240
|
|
|
1108
|
-
router.post(
|
|
1109
|
-
const table = req.body.table
|
|
1110
|
-
let json = Util.flashError(
|
|
1241
|
+
router.post("/load-form", async (req, res) => {
|
|
1242
|
+
const table = req.body.table;
|
|
1243
|
+
let json = Util.flashError("error");
|
|
1111
1244
|
const result = await connection.result({
|
|
1112
|
-
table:
|
|
1245
|
+
table: "zfields",
|
|
1113
1246
|
where: {
|
|
1114
1247
|
table: table,
|
|
1115
1248
|
},
|
|
1116
|
-
})
|
|
1249
|
+
});
|
|
1117
1250
|
if (result.id) {
|
|
1118
|
-
json = Util.jsonSuccess(
|
|
1119
|
-
json.data = result
|
|
1251
|
+
json = Util.jsonSuccess("success");
|
|
1252
|
+
json.data = result;
|
|
1120
1253
|
}
|
|
1121
|
-
res.json(json)
|
|
1122
|
-
})
|
|
1254
|
+
res.json(json);
|
|
1255
|
+
});
|
|
1123
1256
|
|
|
1124
1257
|
//save to table zfields
|
|
1125
1258
|
var saveToZFields = async (body) => {
|
|
1126
|
-
const table = body.table
|
|
1127
|
-
let is_approval = body.is_approval
|
|
1128
|
-
if (typeof body.is_approval ==
|
|
1129
|
-
if (body.is_approval ==
|
|
1130
|
-
is_approval = 0
|
|
1131
|
-
} else if (body.is_approval ==
|
|
1132
|
-
is_approval = 1
|
|
1259
|
+
const table = body.table;
|
|
1260
|
+
let is_approval = body.is_approval;
|
|
1261
|
+
if (typeof body.is_approval == "string") {
|
|
1262
|
+
if (body.is_approval == "false") {
|
|
1263
|
+
is_approval = 0;
|
|
1264
|
+
} else if (body.is_approval == "true") {
|
|
1265
|
+
is_approval = 1;
|
|
1133
1266
|
}
|
|
1134
1267
|
}
|
|
1135
1268
|
|
|
1136
|
-
const zapprovals = {}
|
|
1137
|
-
zapprovals.title = body.approval_title
|
|
1138
|
-
zapprovals.type = body.type
|
|
1139
|
-
zapprovals.approvers = body.approvers
|
|
1140
|
-
zapprovals.knowings = body.knowings
|
|
1141
|
-
zapprovals.content = body.template
|
|
1269
|
+
const zapprovals = {};
|
|
1270
|
+
zapprovals.title = body.approval_title;
|
|
1271
|
+
zapprovals.type = body.type;
|
|
1272
|
+
zapprovals.approvers = body.approvers;
|
|
1273
|
+
zapprovals.knowings = body.knowings;
|
|
1274
|
+
zapprovals.content = body.template;
|
|
1142
1275
|
var data = {
|
|
1143
1276
|
index_ejs: body.index_ejs,
|
|
1144
1277
|
indexcss_ejs: body.indexcss_ejs,
|
|
@@ -1154,185 +1287,190 @@ var saveToZFields = async (body) => {
|
|
|
1154
1287
|
is_approval: is_approval || 0,
|
|
1155
1288
|
approval_json: JSON.stringify(zapprovals),
|
|
1156
1289
|
others: body.others,
|
|
1157
|
-
}
|
|
1290
|
+
};
|
|
1158
1291
|
|
|
1159
|
-
let joinsTables = {}
|
|
1160
|
-
let bodyJoins = body.joins || []
|
|
1161
|
-
let bodyJoinsObj = {}
|
|
1292
|
+
let joinsTables = {};
|
|
1293
|
+
let bodyJoins = body.joins || [];
|
|
1294
|
+
let bodyJoinsObj = {};
|
|
1162
1295
|
if (bodyJoins.length > 0) {
|
|
1163
1296
|
bodyJoins.map((item) => {
|
|
1164
|
-
bodyJoinsObj[item.name] = item.value
|
|
1165
|
-
})
|
|
1297
|
+
bodyJoinsObj[item.name] = item.value;
|
|
1298
|
+
});
|
|
1166
1299
|
}
|
|
1167
1300
|
for (let key in bodyJoinsObj) {
|
|
1168
|
-
if (key.includes(
|
|
1169
|
-
let tablekeys = key.split(
|
|
1170
|
-
let keyName = key.replace(
|
|
1301
|
+
if (key.includes("joins_checks___")) {
|
|
1302
|
+
let tablekeys = key.split("___");
|
|
1303
|
+
let keyName = key.replace("joins_checks___", "");
|
|
1171
1304
|
if (!joinsTables[tablekeys[1]]) {
|
|
1172
|
-
joinsTables[tablekeys[1]] = []
|
|
1305
|
+
joinsTables[tablekeys[1]] = [];
|
|
1173
1306
|
}
|
|
1174
|
-
joinsTables[tablekeys[1]].push({
|
|
1307
|
+
joinsTables[tablekeys[1]].push({
|
|
1308
|
+
key: keyName,
|
|
1309
|
+
value: bodyJoinsObj[`joins_labels___${keyName}`],
|
|
1310
|
+
});
|
|
1175
1311
|
}
|
|
1176
1312
|
}
|
|
1177
1313
|
//console.log(joinsTables);
|
|
1178
1314
|
if (Object.keys(joinsTables).length > 0) {
|
|
1179
|
-
data.joins = JSON.stringify(joinsTables)
|
|
1315
|
+
data.joins = JSON.stringify(joinsTables);
|
|
1180
1316
|
}
|
|
1181
1317
|
|
|
1182
1318
|
let result = await connection.update({
|
|
1183
|
-
table:
|
|
1319
|
+
table: "zfields",
|
|
1184
1320
|
data: data,
|
|
1185
1321
|
where: {
|
|
1186
1322
|
table: table,
|
|
1187
1323
|
},
|
|
1188
|
-
})
|
|
1324
|
+
});
|
|
1189
1325
|
|
|
1190
|
-
return result
|
|
1191
|
-
}
|
|
1326
|
+
return result;
|
|
1327
|
+
};
|
|
1192
1328
|
|
|
1193
1329
|
//drop down chains effect
|
|
1194
1330
|
const chainings = async (table, arr) => {
|
|
1195
|
-
const results = await connection.query(connection.showFields(table))
|
|
1196
|
-
const obj = {}
|
|
1197
|
-
const tableObj = {}
|
|
1331
|
+
const results = await connection.query(connection.showFields(table));
|
|
1332
|
+
const obj = {};
|
|
1333
|
+
const tableObj = {};
|
|
1198
1334
|
for (let i = 0; i < results.length; i++) {
|
|
1199
|
-
const result = results[i]
|
|
1335
|
+
const result = results[i];
|
|
1200
1336
|
if (Util.in_array(result.COLUMN_NAME, arr)) {
|
|
1201
1337
|
obj[result.COLUMN_NAME] = {
|
|
1202
1338
|
table: result.REFERENCED_TABLE_NAME,
|
|
1203
|
-
}
|
|
1204
|
-
tableObj[result.REFERENCED_TABLE_NAME] = result.COLUMN_NAME
|
|
1339
|
+
};
|
|
1340
|
+
tableObj[result.REFERENCED_TABLE_NAME] = result.COLUMN_NAME;
|
|
1205
1341
|
}
|
|
1206
1342
|
}
|
|
1207
1343
|
|
|
1208
1344
|
for (const key in obj) {
|
|
1209
|
-
const rows = await connection.query(connection.showFields(obj[key].table))
|
|
1345
|
+
const rows = await connection.query(connection.showFields(obj[key].table));
|
|
1210
1346
|
rows.forEach(function (row) {
|
|
1211
1347
|
for (var q in obj) {
|
|
1212
1348
|
if (row.REFERENCED_TABLE_NAME == obj[q].table) {
|
|
1213
1349
|
if (!obj[q].chains) {
|
|
1214
|
-
obj[q].chains = {}
|
|
1350
|
+
obj[q].chains = {};
|
|
1215
1351
|
}
|
|
1216
1352
|
obj[q].chains[key] = {
|
|
1217
1353
|
column: row.COLUMN_NAME,
|
|
1218
1354
|
table: row.TABLE_NAME,
|
|
1219
|
-
}
|
|
1355
|
+
};
|
|
1220
1356
|
}
|
|
1221
1357
|
}
|
|
1222
|
-
})
|
|
1358
|
+
});
|
|
1223
1359
|
}
|
|
1224
|
-
return obj
|
|
1225
|
-
}
|
|
1360
|
+
return obj;
|
|
1361
|
+
};
|
|
1226
1362
|
|
|
1227
|
-
router.post(
|
|
1228
|
-
let sorting = req.body.sorting
|
|
1229
|
-
let table = req.body.table
|
|
1363
|
+
router.post("/sorting", async (req, res) => {
|
|
1364
|
+
let sorting = req.body.sorting;
|
|
1365
|
+
let table = req.body.table;
|
|
1230
1366
|
await connection.update({
|
|
1231
|
-
table:
|
|
1367
|
+
table: "zfields",
|
|
1232
1368
|
data: {
|
|
1233
1369
|
sorting: sorting,
|
|
1234
1370
|
},
|
|
1235
1371
|
where: { table: table },
|
|
1236
|
-
})
|
|
1237
|
-
res.send(
|
|
1238
|
-
})
|
|
1372
|
+
});
|
|
1373
|
+
res.send("OK");
|
|
1374
|
+
});
|
|
1239
1375
|
|
|
1240
|
-
router.post(
|
|
1241
|
-
const table = req.body.table
|
|
1242
|
-
let json = Util.jsonSuccess(
|
|
1376
|
+
router.post("/export", async (req, res) => {
|
|
1377
|
+
const table = req.body.table;
|
|
1378
|
+
let json = Util.jsonSuccess("file Complete");
|
|
1243
1379
|
if (table) {
|
|
1244
1380
|
const result = await connection.result({
|
|
1245
|
-
table:
|
|
1381
|
+
table: "zfields",
|
|
1246
1382
|
where: {
|
|
1247
1383
|
table: table,
|
|
1248
1384
|
},
|
|
1249
|
-
})
|
|
1250
|
-
const dir = `${dirRoot}/public/uploads/zgenerator
|
|
1385
|
+
});
|
|
1386
|
+
const dir = `${dirRoot}/public/uploads/zgenerator`;
|
|
1251
1387
|
if (!fs.existsSync(dir)) {
|
|
1252
|
-
fs.mkdirSync(dir)
|
|
1388
|
+
fs.mkdirSync(dir);
|
|
1253
1389
|
}
|
|
1254
|
-
const text = JSON.stringify(result)
|
|
1255
|
-
fs.writeFileSync(`${dir}/${table}.json`, text)
|
|
1390
|
+
const text = JSON.stringify(result);
|
|
1391
|
+
fs.writeFileSync(`${dir}/${table}.json`, text);
|
|
1256
1392
|
} else {
|
|
1257
|
-
json = Util.flashError(
|
|
1393
|
+
json = Util.flashError("no table selected");
|
|
1258
1394
|
}
|
|
1259
|
-
res.json(json)
|
|
1260
|
-
})
|
|
1395
|
+
res.json(json);
|
|
1396
|
+
});
|
|
1261
1397
|
|
|
1262
|
-
router.post(
|
|
1263
|
-
let json = Util.flashError(
|
|
1264
|
-
let table =
|
|
1398
|
+
router.post("/import", async (req, res) => {
|
|
1399
|
+
let json = Util.flashError("Error File");
|
|
1400
|
+
let table = "";
|
|
1265
1401
|
try {
|
|
1266
|
-
let isEmptyFiles = Util.isEmptyObject(req.files)
|
|
1402
|
+
let isEmptyFiles = Util.isEmptyObject(req.files);
|
|
1267
1403
|
if (!isEmptyFiles) {
|
|
1268
|
-
const file = req.files[
|
|
1269
|
-
const name = file.name
|
|
1270
|
-
const mime = file.mimetype
|
|
1271
|
-
if (mime ==
|
|
1272
|
-
const string = file.data.toString(
|
|
1273
|
-
const jsonFile = JSON.parse(string)
|
|
1274
|
-
table = jsonFile.table
|
|
1275
|
-
delete jsonFile.id
|
|
1276
|
-
const data = {}
|
|
1404
|
+
const file = req.files["import-file"];
|
|
1405
|
+
const name = file.name;
|
|
1406
|
+
const mime = file.mimetype;
|
|
1407
|
+
if (mime == "application/json") {
|
|
1408
|
+
const string = file.data.toString("utf8");
|
|
1409
|
+
const jsonFile = JSON.parse(string);
|
|
1410
|
+
table = jsonFile.table;
|
|
1411
|
+
delete jsonFile.id;
|
|
1412
|
+
const data = {};
|
|
1277
1413
|
for (const key in jsonFile) {
|
|
1278
|
-
let value = jsonFile[key]
|
|
1279
|
-
if (typeof value ==
|
|
1280
|
-
value = JSON.stringify(value)
|
|
1414
|
+
let value = jsonFile[key];
|
|
1415
|
+
if (typeof value == "object") {
|
|
1416
|
+
value = JSON.stringify(value);
|
|
1281
1417
|
}
|
|
1282
|
-
data[key] = value
|
|
1418
|
+
data[key] = value;
|
|
1283
1419
|
}
|
|
1284
1420
|
if (!data.tabs) {
|
|
1285
|
-
data.tabs =
|
|
1421
|
+
data.tabs = "[]";
|
|
1286
1422
|
}
|
|
1287
|
-
data.company_id = 1
|
|
1288
|
-
data.created_by = res.locals.userId || 1
|
|
1289
|
-
data.updated_by = res.locals.userId || 1
|
|
1290
|
-
data.created_at = Util.now()
|
|
1291
|
-
data.updated_at = Util.now()
|
|
1292
|
-
data.is_approval = data.is_approval ==
|
|
1293
|
-
delete data.approval_history
|
|
1423
|
+
data.company_id = 1;
|
|
1424
|
+
data.created_by = res.locals.userId || 1;
|
|
1425
|
+
data.updated_by = res.locals.userId || 1;
|
|
1426
|
+
data.created_at = Util.now();
|
|
1427
|
+
data.updated_at = Util.now();
|
|
1428
|
+
data.is_approval = data.is_approval == "null" ? 0 : data.is_approval;
|
|
1429
|
+
delete data.approval_history;
|
|
1294
1430
|
const results = await connection.results({
|
|
1295
|
-
table:
|
|
1431
|
+
table: "zfields",
|
|
1296
1432
|
where: {
|
|
1297
1433
|
table: table,
|
|
1298
1434
|
},
|
|
1299
|
-
})
|
|
1435
|
+
});
|
|
1300
1436
|
if (results.length) {
|
|
1301
1437
|
await connection.update({
|
|
1302
|
-
table:
|
|
1438
|
+
table: "zfields",
|
|
1303
1439
|
data: data,
|
|
1304
1440
|
where: {
|
|
1305
1441
|
table: table,
|
|
1306
1442
|
},
|
|
1307
|
-
})
|
|
1443
|
+
});
|
|
1308
1444
|
} else {
|
|
1309
1445
|
await connection.insert({
|
|
1310
|
-
table:
|
|
1446
|
+
table: "zfields",
|
|
1311
1447
|
data: data,
|
|
1312
|
-
})
|
|
1448
|
+
});
|
|
1313
1449
|
}
|
|
1314
|
-
json = Util.jsonSuccess(
|
|
1450
|
+
json = Util.jsonSuccess(
|
|
1451
|
+
"Success to add module, please generate tax to make a file"
|
|
1452
|
+
);
|
|
1315
1453
|
}
|
|
1316
1454
|
}
|
|
1317
1455
|
} catch (e) {
|
|
1318
|
-
console.log(e)
|
|
1319
|
-
json.message = e.toString()
|
|
1456
|
+
console.log(e);
|
|
1457
|
+
json.message = e.toString();
|
|
1320
1458
|
}
|
|
1321
|
-
json.table = table
|
|
1322
|
-
res.json(json)
|
|
1323
|
-
})
|
|
1459
|
+
json.table = table;
|
|
1460
|
+
res.json(json);
|
|
1461
|
+
});
|
|
1324
1462
|
|
|
1325
1463
|
/*
|
|
1326
1464
|
Scanning relation id to name
|
|
1327
1465
|
*/
|
|
1328
1466
|
const scanning = async (MYMODEL, result) => {
|
|
1329
1467
|
//let MYMODEL = require(`${dirRoot}/models/${table}`);
|
|
1330
|
-
let table = MYMODEL.table
|
|
1331
|
-
const widgets = MYMODEL.widgets
|
|
1332
|
-
const not_scanning = [
|
|
1333
|
-
const concat_bracket = /(?<=\().+?(?=\))/g
|
|
1334
|
-
let changeWidgets = {}
|
|
1335
|
-
let changes = []
|
|
1468
|
+
let table = MYMODEL.table;
|
|
1469
|
+
const widgets = MYMODEL.widgets;
|
|
1470
|
+
const not_scanning = ["created_by", "updated_by"];
|
|
1471
|
+
const concat_bracket = /(?<=\().+?(?=\))/g;
|
|
1472
|
+
let changeWidgets = {};
|
|
1473
|
+
let changes = [];
|
|
1336
1474
|
/*for (let key in widgets) {
|
|
1337
1475
|
if (widgets[key].name == 'relation' || widgets[key].name == 'dropdown_multi' || widgets[key].name == 'typeahead') {
|
|
1338
1476
|
if (!Util.in_array(key, not_scanning)) {
|
|
@@ -1372,257 +1510,259 @@ const scanning = async (MYMODEL, result) => {
|
|
|
1372
1510
|
|
|
1373
1511
|
if (changes.length) {
|
|
1374
1512
|
//rewrite model
|
|
1375
|
-
MYMODEL.widgets = widgets
|
|
1376
|
-
let newModel = `module.exports = { ${Util.newLine}
|
|
1377
|
-
newModel += zRoute.buildFileModel(MYMODEL)
|
|
1378
|
-
newModel += `${Util.newLine}
|
|
1379
|
-
newModel += `}
|
|
1380
|
-
fs.writeFileSync(`${dirRoot}/models/${MYMODEL.table}.js`, newModel)
|
|
1513
|
+
MYMODEL.widgets = widgets;
|
|
1514
|
+
let newModel = `module.exports = { ${Util.newLine}`;
|
|
1515
|
+
newModel += zRoute.buildFileModel(MYMODEL);
|
|
1516
|
+
newModel += `${Util.newLine}`;
|
|
1517
|
+
newModel += `}`;
|
|
1518
|
+
fs.writeFileSync(`${dirRoot}/models/${MYMODEL.table}.js`, newModel);
|
|
1381
1519
|
}
|
|
1382
1520
|
|
|
1383
1521
|
//check if has join
|
|
1384
1522
|
//build join
|
|
1385
1523
|
//console.log(JSON.stringify(MYMODEL))
|
|
1386
|
-
MYMODEL = await buildJoin(MYMODEL, result)
|
|
1524
|
+
MYMODEL = await buildJoin(MYMODEL, result);
|
|
1387
1525
|
//console.log(JSON.stringify(MYMODEL))
|
|
1388
1526
|
|
|
1389
|
-
let newModel = `module.exports = { ${Util.newLine}
|
|
1390
|
-
newModel += zRoute.buildFileModel(MYMODEL)
|
|
1391
|
-
newModel += `${Util.newLine}
|
|
1392
|
-
newModel += `}
|
|
1393
|
-
fs.writeFileSync(`${dirRoot}/models/${MYMODEL.table}.js`, newModel)
|
|
1527
|
+
let newModel = `module.exports = { ${Util.newLine}`;
|
|
1528
|
+
newModel += zRoute.buildFileModel(MYMODEL);
|
|
1529
|
+
newModel += `${Util.newLine}`;
|
|
1530
|
+
newModel += `}`;
|
|
1531
|
+
fs.writeFileSync(`${dirRoot}/models/${MYMODEL.table}.js`, newModel);
|
|
1394
1532
|
//return MYMODEL;
|
|
1395
|
-
}
|
|
1533
|
+
};
|
|
1396
1534
|
|
|
1397
1535
|
//generate all css/js assets
|
|
1398
|
-
router.post(
|
|
1399
|
-
let notifObj = Util.jsonSuccess(
|
|
1536
|
+
router.post("/generate-assets", async (req, res) => {
|
|
1537
|
+
let notifObj = Util.jsonSuccess("Success, generate all assets");
|
|
1400
1538
|
try {
|
|
1401
|
-
let MYMODELS = zRoute.MYMODELS()
|
|
1402
|
-
delete MYMODELS.zrole
|
|
1539
|
+
let MYMODELS = zRoute.MYMODELS();
|
|
1540
|
+
delete MYMODELS.zrole;
|
|
1403
1541
|
for (let key in MYMODELS) {
|
|
1404
|
-
let MYMODEL = MYMODELS[key]
|
|
1405
|
-
let table = MYMODEL.table
|
|
1406
|
-
let path_script = `${dirRoot}/public/runtime/script/${table}
|
|
1407
|
-
Util.dirExist(path_script, true)
|
|
1408
|
-
let path_head = `${dirRoot}/public/runtime/head/${table}
|
|
1409
|
-
Util.dirExist(path_head, true)
|
|
1410
|
-
let path_end = `${dirRoot}/public/runtime/end/${table}
|
|
1411
|
-
Util.dirExist(path_end, true)
|
|
1542
|
+
let MYMODEL = MYMODELS[key];
|
|
1543
|
+
let table = MYMODEL.table;
|
|
1544
|
+
let path_script = `${dirRoot}/public/runtime/script/${table}`;
|
|
1545
|
+
Util.dirExist(path_script, true);
|
|
1546
|
+
let path_head = `${dirRoot}/public/runtime/head/${table}`;
|
|
1547
|
+
Util.dirExist(path_head, true);
|
|
1548
|
+
let path_end = `${dirRoot}/public/runtime/end/${table}`;
|
|
1549
|
+
Util.dirExist(path_end, true);
|
|
1412
1550
|
//we need to generate javascript code into runtime
|
|
1413
|
-
let relations = await zRoute.relations(req, res, MYMODEL.table)
|
|
1551
|
+
let relations = await zRoute.relations(req, res, MYMODEL.table);
|
|
1414
1552
|
//add script
|
|
1415
|
-
let jsObj = zRoute.generateJS(req, res, MYMODEL, relations)
|
|
1416
|
-
let time = new Date().getTime()
|
|
1417
|
-
Util.deleteAllFiles(path_head)
|
|
1418
|
-
Util.deleteAllFiles(path_end)
|
|
1419
|
-
Util.deleteAllFiles(path_script)
|
|
1553
|
+
let jsObj = zRoute.generateJS(req, res, MYMODEL, relations);
|
|
1554
|
+
let time = new Date().getTime();
|
|
1555
|
+
Util.deleteAllFiles(path_head);
|
|
1556
|
+
Util.deleteAllFiles(path_end);
|
|
1557
|
+
Util.deleteAllFiles(path_script);
|
|
1420
1558
|
|
|
1421
1559
|
//minify script
|
|
1422
|
-
let script = uglifyJS.minify(jsObj.script)
|
|
1423
|
-
if (script.error) throw script.error
|
|
1424
|
-
Util.writeFile(`${path_script}/${time}.js`, script.code)
|
|
1425
|
-
Util.writeFile(`${path_head}/head.txt`, jsObj.head)
|
|
1426
|
-
Util.writeFile(`${path_end}/end.txt`, jsObj.end)
|
|
1560
|
+
let script = uglifyJS.minify(jsObj.script);
|
|
1561
|
+
if (script.error) throw script.error;
|
|
1562
|
+
Util.writeFile(`${path_script}/${time}.js`, script.code);
|
|
1563
|
+
Util.writeFile(`${path_head}/head.txt`, jsObj.head);
|
|
1564
|
+
Util.writeFile(`${path_end}/end.txt`, jsObj.end);
|
|
1427
1565
|
}
|
|
1428
1566
|
} catch (e) {
|
|
1429
|
-
notifObj = Util.flashError(e.toString())
|
|
1567
|
+
notifObj = Util.flashError(e.toString());
|
|
1430
1568
|
}
|
|
1431
|
-
res.json(notifObj)
|
|
1432
|
-
})
|
|
1569
|
+
res.json(notifObj);
|
|
1570
|
+
});
|
|
1433
1571
|
|
|
1434
|
-
router.post(
|
|
1435
|
-
let notifyObj = Util.jsonSuccess(
|
|
1572
|
+
router.post("/minify", async (req, res) => {
|
|
1573
|
+
let notifyObj = Util.jsonSuccess("Success to cached");
|
|
1436
1574
|
try {
|
|
1437
|
-
notifyObj = await viewsMini()
|
|
1575
|
+
notifyObj = await viewsMini();
|
|
1438
1576
|
} catch (e) {
|
|
1439
|
-
console.log(e)
|
|
1440
|
-
notifyObj = Util.flashError(e.toString())
|
|
1577
|
+
console.log(e);
|
|
1578
|
+
notifyObj = Util.flashError(e.toString());
|
|
1441
1579
|
}
|
|
1442
|
-
res.json(notifyObj)
|
|
1443
|
-
})
|
|
1580
|
+
res.json(notifyObj);
|
|
1581
|
+
});
|
|
1444
1582
|
|
|
1445
1583
|
const viewsMini = async () => {
|
|
1446
|
-
let notifyObj = Util.jsonSuccess(
|
|
1447
|
-
const viewDir = `${dirRoot}/views
|
|
1448
|
-
const layoutsDir = `${dirRoot}/views/layouts
|
|
1449
|
-
let item
|
|
1584
|
+
let notifyObj = Util.jsonSuccess("Success");
|
|
1585
|
+
const viewDir = `${dirRoot}/views`;
|
|
1586
|
+
const layoutsDir = `${dirRoot}/views/layouts`;
|
|
1587
|
+
let item;
|
|
1450
1588
|
try {
|
|
1451
|
-
let views = Util.getAllFiles(viewDir)
|
|
1589
|
+
let views = Util.getAllFiles(viewDir);
|
|
1452
1590
|
let options = {
|
|
1453
1591
|
minifyJS: true,
|
|
1454
1592
|
minifyCSS: true,
|
|
1455
1593
|
collapseWhitespace: true,
|
|
1456
|
-
}
|
|
1457
|
-
const notViewDir = [
|
|
1594
|
+
};
|
|
1595
|
+
const notViewDir = ["zgenerator", "layouts", "index", "zindex"];
|
|
1458
1596
|
for (const dir of views) {
|
|
1459
|
-
console.log(
|
|
1597
|
+
console.log("dir :", dir);
|
|
1460
1598
|
if (!Util.in_array(dir, notViewDir)) {
|
|
1461
|
-
let files = Util.getAllFiles(`${viewDir}/${dir}`)
|
|
1599
|
+
let files = Util.getAllFiles(`${viewDir}/${dir}`);
|
|
1462
1600
|
//console.log(files);
|
|
1463
1601
|
for (item of files) {
|
|
1464
|
-
if (item.includes(
|
|
1465
|
-
let filename = `${viewDir}/${dir}/${item}
|
|
1466
|
-
let content = Util.readFile(filename)
|
|
1467
|
-
let contentChange = await minify(content, options)
|
|
1468
|
-
fs.writeFileSync(filename, contentChange)
|
|
1602
|
+
if (item.includes(".ejs")) {
|
|
1603
|
+
let filename = `${viewDir}/${dir}/${item}`;
|
|
1604
|
+
let content = Util.readFile(filename);
|
|
1605
|
+
let contentChange = await minify(content, options);
|
|
1606
|
+
fs.writeFileSync(filename, contentChange);
|
|
1469
1607
|
}
|
|
1470
1608
|
}
|
|
1471
1609
|
}
|
|
1472
1610
|
}
|
|
1473
1611
|
|
|
1474
|
-
let layouts = Util.getAllFiles(layoutsDir)
|
|
1612
|
+
let layouts = Util.getAllFiles(layoutsDir);
|
|
1475
1613
|
for (item of layouts) {
|
|
1476
|
-
if (item.includes(
|
|
1477
|
-
let filename = `${layoutsDir}//${item}
|
|
1478
|
-
let content = Util.readFile(filename)
|
|
1479
|
-
let contentChange = await minify(content, options)
|
|
1480
|
-
fs.writeFileSync(filename, contentChange)
|
|
1614
|
+
if (item.includes(".ejs")) {
|
|
1615
|
+
let filename = `${layoutsDir}//${item}`;
|
|
1616
|
+
let content = Util.readFile(filename);
|
|
1617
|
+
let contentChange = await minify(content, options);
|
|
1618
|
+
fs.writeFileSync(filename, contentChange);
|
|
1481
1619
|
}
|
|
1482
1620
|
}
|
|
1483
1621
|
} catch (e) {
|
|
1484
|
-
notifyObj = Util.flashError(
|
|
1622
|
+
notifyObj = Util.flashError(
|
|
1623
|
+
`error in item : ${item} , error str :${e.toString()}`
|
|
1624
|
+
);
|
|
1485
1625
|
}
|
|
1486
|
-
return notifyObj
|
|
1487
|
-
}
|
|
1626
|
+
return notifyObj;
|
|
1627
|
+
};
|
|
1488
1628
|
|
|
1489
|
-
router.post(
|
|
1490
|
-
let jsonNotif = Util.jsonSuccess(
|
|
1491
|
-
let html =
|
|
1629
|
+
router.post("/load-joins", async (req, res) => {
|
|
1630
|
+
let jsonNotif = Util.jsonSuccess("Success");
|
|
1631
|
+
let html = "";
|
|
1492
1632
|
try {
|
|
1493
|
-
let body = req.body
|
|
1494
|
-
console.log(body)
|
|
1495
|
-
let table = body.table
|
|
1496
|
-
html = await loadJoins(table)
|
|
1497
|
-
jsonNotif.html = html
|
|
1633
|
+
let body = req.body;
|
|
1634
|
+
console.log(body);
|
|
1635
|
+
let table = body.table;
|
|
1636
|
+
html = await loadJoins(table);
|
|
1637
|
+
jsonNotif.html = html;
|
|
1498
1638
|
} catch (e) {
|
|
1499
|
-
console.log(e)
|
|
1639
|
+
console.log(e);
|
|
1500
1640
|
}
|
|
1501
|
-
res.json(jsonNotif)
|
|
1502
|
-
})
|
|
1641
|
+
res.json(jsonNotif);
|
|
1642
|
+
});
|
|
1503
1643
|
|
|
1504
|
-
router.post(
|
|
1505
|
-
let jsonNotif = Util.jsonSuccess(
|
|
1506
|
-
let html =
|
|
1644
|
+
router.post("/remove-joins", async (req, res) => {
|
|
1645
|
+
let jsonNotif = Util.jsonSuccess("Success");
|
|
1646
|
+
let html = "";
|
|
1507
1647
|
try {
|
|
1508
|
-
let body = req.body
|
|
1509
|
-
console.log(body)
|
|
1510
|
-
let table = body.table
|
|
1511
|
-
let table_join = body.table_join
|
|
1648
|
+
let body = req.body;
|
|
1649
|
+
console.log(body);
|
|
1650
|
+
let table = body.table;
|
|
1651
|
+
let table_join = body.table_join;
|
|
1512
1652
|
let result = await connection.result({
|
|
1513
|
-
table:
|
|
1653
|
+
table: "zfields",
|
|
1514
1654
|
where: {
|
|
1515
1655
|
table: table,
|
|
1516
1656
|
},
|
|
1517
|
-
})
|
|
1518
|
-
let resultJoins = result.joins || {}
|
|
1519
|
-
delete resultJoins[table_join]
|
|
1657
|
+
});
|
|
1658
|
+
let resultJoins = result.joins || {};
|
|
1659
|
+
delete resultJoins[table_join];
|
|
1520
1660
|
await connection.update({
|
|
1521
|
-
table:
|
|
1661
|
+
table: "zfields",
|
|
1522
1662
|
data: {
|
|
1523
1663
|
joins: JSON.stringify(resultJoins),
|
|
1524
1664
|
},
|
|
1525
1665
|
where: {
|
|
1526
1666
|
table: table,
|
|
1527
1667
|
},
|
|
1528
|
-
})
|
|
1668
|
+
});
|
|
1529
1669
|
} catch (e) {
|
|
1530
|
-
console.log(e)
|
|
1670
|
+
console.log(e);
|
|
1531
1671
|
}
|
|
1532
|
-
res.json(jsonNotif)
|
|
1533
|
-
})
|
|
1672
|
+
res.json(jsonNotif);
|
|
1673
|
+
});
|
|
1534
1674
|
|
|
1535
|
-
router.post(
|
|
1536
|
-
let jsonNotif = Util.jsonSuccess(
|
|
1537
|
-
let html =
|
|
1675
|
+
router.post("/joins", async (req, res) => {
|
|
1676
|
+
let jsonNotif = Util.jsonSuccess("Success");
|
|
1677
|
+
let html = "";
|
|
1538
1678
|
try {
|
|
1539
|
-
let body = req.body
|
|
1540
|
-
console.log(body)
|
|
1541
|
-
let table = body.table
|
|
1542
|
-
let table_join = body.table_join
|
|
1679
|
+
let body = req.body;
|
|
1680
|
+
console.log(body);
|
|
1681
|
+
let table = body.table;
|
|
1682
|
+
let table_join = body.table_join;
|
|
1543
1683
|
let result = await connection.result({
|
|
1544
|
-
table:
|
|
1684
|
+
table: "zfields",
|
|
1545
1685
|
where: {
|
|
1546
1686
|
table: table,
|
|
1547
1687
|
},
|
|
1548
|
-
})
|
|
1549
|
-
let resultJoins = result.joins || {}
|
|
1550
|
-
resultJoins[table_join] = []
|
|
1688
|
+
});
|
|
1689
|
+
let resultJoins = result.joins || {};
|
|
1690
|
+
resultJoins[table_join] = [];
|
|
1551
1691
|
let myzfield = await connection.update({
|
|
1552
|
-
table:
|
|
1692
|
+
table: "zfields",
|
|
1553
1693
|
data: {
|
|
1554
1694
|
joins: JSON.stringify(resultJoins),
|
|
1555
1695
|
},
|
|
1556
1696
|
where: {
|
|
1557
1697
|
table: table,
|
|
1558
1698
|
},
|
|
1559
|
-
})
|
|
1560
|
-
jsonNotif.json = resultJoins
|
|
1561
|
-
html = await joinHTML(resultJoins, myzfield)
|
|
1699
|
+
});
|
|
1700
|
+
jsonNotif.json = resultJoins;
|
|
1701
|
+
html = await joinHTML(resultJoins, myzfield);
|
|
1562
1702
|
} catch (e) {
|
|
1563
|
-
console.log(e)
|
|
1564
|
-
jsonNotif = Util.flashError(e +
|
|
1703
|
+
console.log(e);
|
|
1704
|
+
jsonNotif = Util.flashError(e + "");
|
|
1565
1705
|
}
|
|
1566
|
-
jsonNotif.html = html
|
|
1567
|
-
res.json(jsonNotif)
|
|
1568
|
-
})
|
|
1706
|
+
jsonNotif.html = html;
|
|
1707
|
+
res.json(jsonNotif);
|
|
1708
|
+
});
|
|
1569
1709
|
|
|
1570
1710
|
async function loadJoins(table) {
|
|
1571
|
-
let html =
|
|
1711
|
+
let html = "";
|
|
1572
1712
|
try {
|
|
1573
1713
|
/* let sql = `select ${table}.* from ${table} `;
|
|
1574
1714
|
console.log(sql)*/
|
|
1575
1715
|
let result = await connection.result({
|
|
1576
|
-
table:
|
|
1716
|
+
table: "zfields",
|
|
1577
1717
|
where: {
|
|
1578
1718
|
table: table,
|
|
1579
1719
|
},
|
|
1580
|
-
})
|
|
1581
|
-
let joinsObj = result.joins || {}
|
|
1582
|
-
html = await joinHTML(joinsObj, result)
|
|
1720
|
+
});
|
|
1721
|
+
let joinsObj = result.joins || {};
|
|
1722
|
+
html = await joinHTML(joinsObj, result);
|
|
1583
1723
|
} catch (e) {
|
|
1584
|
-
console.log(e)
|
|
1585
|
-
html = e +
|
|
1724
|
+
console.log(e);
|
|
1725
|
+
html = e + "";
|
|
1586
1726
|
}
|
|
1587
|
-
return html
|
|
1727
|
+
return html;
|
|
1588
1728
|
}
|
|
1589
1729
|
|
|
1590
1730
|
async function joinHTML(json, myzfield) {
|
|
1591
|
-
let html =
|
|
1731
|
+
let html = ``;
|
|
1592
1732
|
try {
|
|
1593
1733
|
let zfields = await connection.results({
|
|
1594
|
-
table:
|
|
1595
|
-
})
|
|
1596
|
-
let zfieldsObj = Util.arrayToObject(zfields,
|
|
1734
|
+
table: "zfields",
|
|
1735
|
+
});
|
|
1736
|
+
let zfieldsObj = Util.arrayToObject(zfields, "table");
|
|
1597
1737
|
for (let key in json) {
|
|
1598
|
-
html += `<div class="joinContainer col-md-4"
|
|
1599
|
-
html += `<h5>${zfieldsObj[key].name} <button type="button" class="btn btn-danger" onclick="removeJoins('${key}')"><i class="fa fa-trash "></i> </button></h5
|
|
1600
|
-
html += fieldHTML(zfieldsObj[key], myzfield)
|
|
1601
|
-
html += `</div
|
|
1738
|
+
html += `<div class="joinContainer col-md-4">`;
|
|
1739
|
+
html += `<h5>${zfieldsObj[key].name} <button type="button" class="btn btn-danger" onclick="removeJoins('${key}')"><i class="fa fa-trash "></i> </button></h5>`;
|
|
1740
|
+
html += fieldHTML(zfieldsObj[key], myzfield);
|
|
1741
|
+
html += `</div>`;
|
|
1602
1742
|
}
|
|
1603
1743
|
} catch (e) {
|
|
1604
|
-
html = e +
|
|
1744
|
+
html = e + "";
|
|
1605
1745
|
}
|
|
1606
|
-
return html
|
|
1746
|
+
return html;
|
|
1607
1747
|
}
|
|
1608
1748
|
|
|
1609
1749
|
function fieldHTML(obj, myzfield) {
|
|
1610
|
-
let html =
|
|
1750
|
+
let html = ``;
|
|
1611
1751
|
try {
|
|
1612
|
-
let labels = obj.labels
|
|
1613
|
-
let table = obj.table
|
|
1614
|
-
let myzfieldJoins = myzfield.joins || {}
|
|
1752
|
+
let labels = obj.labels;
|
|
1753
|
+
let table = obj.table;
|
|
1754
|
+
let myzfieldJoins = myzfield.joins || {};
|
|
1615
1755
|
for (let key in labels) {
|
|
1616
|
-
let mylabel = labels[key]
|
|
1617
|
-
let selected =
|
|
1756
|
+
let mylabel = labels[key];
|
|
1757
|
+
let selected = "";
|
|
1618
1758
|
if (myzfieldJoins[table]) {
|
|
1619
|
-
let arr = myzfieldJoins[table] || []
|
|
1759
|
+
let arr = myzfieldJoins[table] || [];
|
|
1620
1760
|
arr.map((item) => {
|
|
1621
1761
|
if (item.key == `${table}___${key}`) {
|
|
1622
|
-
selected = ` checked
|
|
1623
|
-
mylabel = item.value
|
|
1762
|
+
selected = ` checked `;
|
|
1763
|
+
mylabel = item.value;
|
|
1624
1764
|
}
|
|
1625
|
-
})
|
|
1765
|
+
});
|
|
1626
1766
|
}
|
|
1627
1767
|
html += `<div class="input-group">
|
|
1628
1768
|
<div class="input-group-prepend">
|
|
@@ -1630,12 +1770,12 @@ function fieldHTML(obj, myzfield) {
|
|
|
1630
1770
|
<span class="input-group-text">${key}</span>
|
|
1631
1771
|
<input type="text" class="form-control" name="joins_labels___${table}___${key}" id="joins___${table}___${key}" value="${mylabel}">
|
|
1632
1772
|
</div>
|
|
1633
|
-
</div
|
|
1773
|
+
</div>`;
|
|
1634
1774
|
}
|
|
1635
1775
|
} catch (e) {
|
|
1636
|
-
html = e +
|
|
1776
|
+
html = e + "";
|
|
1637
1777
|
}
|
|
1638
|
-
return html
|
|
1778
|
+
return html;
|
|
1639
1779
|
}
|
|
1640
1780
|
|
|
1641
|
-
module.exports = router
|
|
1781
|
+
module.exports = router;
|