zet-lib 1.0.72 → 1.0.77
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/Mail.js +57 -58
- package/lib/Util.js +925 -945
- package/lib/zRoute.js +59 -45
- package/package.json +1 -1
package/lib/Util.js
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
const moment = require('moment')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
const randomstring = require(
|
|
4
|
-
const fs = require('fs-extra')
|
|
5
|
-
const { v4: uuidv4 } = require('uuid')
|
|
6
|
-
const sha256 = require('js-sha256')
|
|
1
|
+
const moment = require('moment')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const randomstring = require('randomstring')
|
|
4
|
+
const fs = require('fs-extra')
|
|
5
|
+
const { v4: uuidv4 } = require('uuid')
|
|
6
|
+
const sha256 = require('js-sha256')
|
|
7
7
|
|
|
8
8
|
const Util = {}
|
|
9
9
|
|
|
10
|
-
Util.tab = '\t'
|
|
10
|
+
Util.tab = '\t'
|
|
11
11
|
Util.tabs = (n) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
12
|
+
let ret = ''
|
|
13
|
+
for (var i = 0; i < n; i++) {
|
|
14
|
+
ret += Util.tab
|
|
15
|
+
}
|
|
16
|
+
return ret
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
Util.NEW_LINE = '\n'
|
|
20
|
-
Util.newLine = '\r\n'
|
|
19
|
+
Util.NEW_LINE = '\n'
|
|
20
|
+
Util.newLine = '\r\n'
|
|
21
21
|
Util.newLines = (n) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
22
|
+
let ret = ''
|
|
23
|
+
for (var i = 0; i < n; i++) {
|
|
24
|
+
ret += Util.newLine
|
|
25
|
+
}
|
|
26
|
+
return ret
|
|
27
|
+
}
|
|
28
28
|
|
|
29
29
|
//sha256
|
|
30
30
|
Util.hash = (string) => {
|
|
31
|
-
|
|
32
|
-
}
|
|
31
|
+
return sha256(string)
|
|
32
|
+
}
|
|
33
33
|
|
|
34
34
|
Util.hashCompare = (myPlaintextPassword, hash) => {
|
|
35
|
-
|
|
36
|
-
}
|
|
35
|
+
return Util.hash(myPlaintextPassword) == hash
|
|
36
|
+
}
|
|
37
37
|
|
|
38
38
|
Util.excelSequence = function () {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
char++;
|
|
39
|
+
let abjads = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
|
|
40
|
+
let arr = abjads
|
|
41
|
+
let char = 0
|
|
42
|
+
let num = 26
|
|
43
|
+
for (let x = 2; x < 15; x++) {
|
|
44
|
+
let idx = 0
|
|
45
|
+
for (let i = 1; i <= 26; i++) {
|
|
46
|
+
arr[num] = abjads[char] + abjads[idx]
|
|
47
|
+
idx++
|
|
48
|
+
num++
|
|
51
49
|
}
|
|
50
|
+
char++
|
|
51
|
+
}
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
}
|
|
53
|
+
return arr
|
|
54
|
+
}
|
|
55
55
|
|
|
56
56
|
Util.now = function () {
|
|
57
|
-
|
|
58
|
-
}
|
|
57
|
+
return moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
|
|
58
|
+
}
|
|
59
59
|
|
|
60
60
|
Util.nowShort = function () {
|
|
61
|
-
|
|
62
|
-
}
|
|
61
|
+
return moment(new Date()).format('YYYY-MM-DD')
|
|
62
|
+
}
|
|
63
63
|
|
|
64
64
|
Util.ago = (data) => {
|
|
65
|
-
|
|
66
|
-
}
|
|
65
|
+
return moment(data).fromNow()
|
|
66
|
+
}
|
|
67
67
|
/*
|
|
68
68
|
moment get one month ago from current
|
|
69
69
|
var monthago = moment().subtract(1, 'months').format('YYYY-MM-DD');
|
|
@@ -71,451 +71,444 @@ Util.ago = (data) => {
|
|
|
71
71
|
*/
|
|
72
72
|
|
|
73
73
|
Util.dateSql = function (date, format) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return '';
|
|
79
|
-
};
|
|
74
|
+
format = format || 'YYYY-MM-DD'
|
|
75
|
+
if (date && date != '0000-00-00') return moment(date).format(format)
|
|
76
|
+
else return ''
|
|
77
|
+
}
|
|
80
78
|
|
|
81
79
|
Util.dateFormat = function (date, format) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return '';
|
|
87
|
-
};
|
|
80
|
+
format = format || 'YYYY-MM-DD'
|
|
81
|
+
if (date && date != '0000-00-00') return moment(date).format(format)
|
|
82
|
+
else return ''
|
|
83
|
+
}
|
|
88
84
|
|
|
89
85
|
Util.timePublic = function (date) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return '';
|
|
94
|
-
};
|
|
86
|
+
if (date) return moment(date).format('DD MMM YYYY')
|
|
87
|
+
else return ''
|
|
88
|
+
}
|
|
95
89
|
|
|
96
90
|
Util.timeSql = function (date, format) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return '';
|
|
103
|
-
};
|
|
91
|
+
if (date && date != '0000-00-00 00:00:00') {
|
|
92
|
+
format = format || 'YYYY-MM-DD HH:mm:ss'
|
|
93
|
+
return moment(date).format(format)
|
|
94
|
+
} else return ''
|
|
95
|
+
}
|
|
104
96
|
|
|
105
97
|
Util.getDate = function (date) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
98
|
+
date = date + '' || ''
|
|
99
|
+
if (date != '') {
|
|
100
|
+
let explode = date.split('-')
|
|
101
|
+
return {
|
|
102
|
+
year: parseInt(explode[0]),
|
|
103
|
+
month: parseInt(explode[1]),
|
|
104
|
+
date: parseInt(explode[2]),
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
return {
|
|
108
|
+
year: 0,
|
|
109
|
+
month: 0,
|
|
110
|
+
date: 0,
|
|
120
111
|
}
|
|
121
|
-
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
122
114
|
|
|
123
115
|
Util.dateIsBetween = (compare, start, end) => {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
116
|
+
if (compare == '' || compare == '0000-00-00') {
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
compare = moment(compare).format('YYYY-MM-DD')
|
|
120
|
+
start = moment(start).format('YYYY-MM-DD')
|
|
121
|
+
end = moment(end).format('YYYY-MM-DD')
|
|
122
|
+
let today = moment(compare)
|
|
123
|
+
let startDate = moment(start)
|
|
124
|
+
let endDate = moment(end)
|
|
125
|
+
|
|
126
|
+
if (compare == start) {
|
|
127
|
+
return true
|
|
128
|
+
} else if (compare == end) {
|
|
129
|
+
return true
|
|
130
|
+
} else {
|
|
131
|
+
return today.isBetween(startDate, endDate)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
142
134
|
|
|
143
135
|
Util.getMonth = function (date) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
136
|
+
if (date.length > 5) {
|
|
137
|
+
let n = new Date(date)
|
|
138
|
+
let m = n.getMonth()
|
|
139
|
+
return parseInt(m) + 1
|
|
140
|
+
}
|
|
141
|
+
return 0
|
|
142
|
+
}
|
|
151
143
|
|
|
152
144
|
Util.getYear = function (date) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
145
|
+
date = Util.dateSql(date) || ''
|
|
146
|
+
return date.slice(0, 4)
|
|
147
|
+
}
|
|
156
148
|
|
|
157
149
|
//first is smaller than second
|
|
158
150
|
Util.calculateDay = function (from, to, holidayInWeek = 0) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
151
|
+
holidayInWeek = parseInt(holidayInWeek) || 0
|
|
152
|
+
let count = 0
|
|
153
|
+
if (holidayInWeek == 1) {
|
|
154
|
+
let days = Util.enumerateDaysBetweenDates(moment(from).format('YYYY-MM-DD'), moment(to).format('YYYY-MM-DD'))
|
|
155
|
+
let countdays = days.filter((item) => parseInt(moment(item).format('d')) != 0)
|
|
156
|
+
count = countdays.length
|
|
157
|
+
} else if (holidayInWeek == 2) {
|
|
158
|
+
let days = Util.enumerateDaysBetweenDates(moment(from).format('YYYY-MM-DD'), moment(to).format('YYYY-MM-DD'))
|
|
159
|
+
let countdays = days.filter((item) => parseInt(moment(item).format('d')) != 0 && parseInt(moment(item).format('d')) != 6)
|
|
160
|
+
count = countdays.length
|
|
161
|
+
} else {
|
|
162
|
+
let a = moment(from)
|
|
163
|
+
let b = moment(to)
|
|
164
|
+
count = b.diff(a, 'days') + 1
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return count
|
|
168
|
+
}
|
|
177
169
|
|
|
178
170
|
var getDaysBetweenDates = function (startDate, endDate) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
171
|
+
let now = startDate.clone(),
|
|
172
|
+
dates = []
|
|
173
|
+
while (now.isSameOrBefore(endDate)) {
|
|
174
|
+
dates.push(now.format('MM/DD/YYYY'))
|
|
175
|
+
now.add(1, 'days')
|
|
176
|
+
}
|
|
177
|
+
return dates
|
|
178
|
+
}
|
|
186
179
|
|
|
187
180
|
//itterate days in array
|
|
188
181
|
Util.enumerateDaysBetweenDates = function (startDate, endDate) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
182
|
+
let now = moment(startDate).clone(),
|
|
183
|
+
dates = []
|
|
184
|
+
while (now.isSameOrBefore(endDate)) {
|
|
185
|
+
dates.push(now.format('MM/DD/YYYY'))
|
|
186
|
+
now.add(1, 'days')
|
|
187
|
+
}
|
|
188
|
+
return dates
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
Util.tableArray = function (arr = []) {
|
|
192
|
+
let r = []
|
|
193
|
+
let tables = arr[0]
|
|
194
|
+
for (let i = 0; i < tables.length; i++) {
|
|
195
|
+
for (let obj in tables[i]) {
|
|
196
|
+
r.push(tables[i][obj])
|
|
204
197
|
}
|
|
205
|
-
|
|
206
|
-
|
|
198
|
+
}
|
|
199
|
+
return r
|
|
200
|
+
}
|
|
207
201
|
|
|
208
202
|
/*
|
|
209
203
|
table array in sql to arr table name
|
|
210
204
|
only for generator
|
|
211
205
|
*/
|
|
212
|
-
Util.tableArrayToObj = (arr) => {
|
|
213
|
-
|
|
214
|
-
}
|
|
206
|
+
Util.tableArrayToObj = (arr = []) => {
|
|
207
|
+
return arr.map((m) => Object.values(m)[0])
|
|
208
|
+
}
|
|
215
209
|
|
|
216
|
-
Util.escapeRegExp = function (str) {
|
|
217
|
-
|
|
218
|
-
}
|
|
210
|
+
Util.escapeRegExp = function (str || "") {
|
|
211
|
+
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1')
|
|
212
|
+
}
|
|
219
213
|
|
|
220
214
|
Util.validateEmail = function (email) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
215
|
+
let re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
216
|
+
return re.test(email)
|
|
217
|
+
}
|
|
224
218
|
|
|
225
219
|
/*
|
|
226
220
|
Replace All like str_replace in PHP
|
|
227
221
|
example : Util.replaceAll("abd","a","")
|
|
228
222
|
*/
|
|
229
223
|
|
|
230
|
-
Util.replaceAll = function (str, find, replace) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
} else {
|
|
240
|
-
if(typeof str == "string") {
|
|
241
|
-
t = str.replace(new RegExp(Util.escapeRegExp(find), 'g'), replace);
|
|
242
|
-
}
|
|
224
|
+
Util.replaceAll = function (str = "", find, replace) {
|
|
225
|
+
let t = ''
|
|
226
|
+
if (Array.isArray(find)) {
|
|
227
|
+
t = str
|
|
228
|
+
for (let i = 0; i < find.length; i++) {
|
|
229
|
+
if (str.indexOf(find[i]) > -1) {
|
|
230
|
+
t = str.replace(new RegExp(Util.escapeRegExp(find[i]), 'g'), replace)
|
|
231
|
+
}
|
|
243
232
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
Util.phoneFixed = function (str) {
|
|
248
|
-
let ret = ''
|
|
249
|
-
str = Util.replaceAll(str, ' ', '')
|
|
250
|
-
let phone = str.trim()
|
|
251
|
-
phone = Util.replaceAll(phone, '-', '')
|
|
252
|
-
let first = phone.charAt(0)
|
|
253
|
-
if (first == '') {
|
|
254
|
-
ret = '';
|
|
255
|
-
} else if (first == '+') {
|
|
256
|
-
ret = phone;
|
|
257
|
-
} else if (first == '0') {
|
|
258
|
-
ret = '+62' + phone.replace('0', '');
|
|
259
|
-
} else {
|
|
260
|
-
ret = '+' + phone
|
|
233
|
+
} else {
|
|
234
|
+
if (typeof str == 'string') {
|
|
235
|
+
t = str.replace(new RegExp(Util.escapeRegExp(find), 'g'), replace)
|
|
261
236
|
}
|
|
237
|
+
}
|
|
238
|
+
return t
|
|
239
|
+
}
|
|
262
240
|
|
|
263
|
-
|
|
264
|
-
|
|
241
|
+
Util.phoneFixed = function (str = '') {
|
|
242
|
+
let ret = ''
|
|
243
|
+
str = Util.replaceAll(str, ' ', '')
|
|
244
|
+
let phone = str.trim()
|
|
245
|
+
phone = Util.replaceAll(phone, '-', '')
|
|
246
|
+
let first = phone.charAt(0)
|
|
247
|
+
if (first == '') {
|
|
248
|
+
ret = ''
|
|
249
|
+
} else if (first == '+') {
|
|
250
|
+
ret = phone
|
|
251
|
+
} else if (first == '0') {
|
|
252
|
+
ret = '+62' + phone.replace('0', '')
|
|
253
|
+
} else {
|
|
254
|
+
ret = '+' + phone
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return ret
|
|
258
|
+
}
|
|
265
259
|
|
|
266
|
-
Util.jsonSuccess = function (message) {
|
|
267
|
-
|
|
268
|
-
|
|
260
|
+
Util.jsonSuccess = function (message = '') {
|
|
261
|
+
message = message || LANGUAGE.data_saved
|
|
262
|
+
let json = { type: 'success', status: 1, title: LANGUAGE.success, message: message }
|
|
269
263
|
|
|
270
|
-
|
|
271
|
-
}
|
|
264
|
+
return json
|
|
265
|
+
}
|
|
272
266
|
|
|
273
|
-
Util.flashError = function (message, errors) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
let json = {type: 'error', status: 0, title: 'Error', message: message, errors: errors}
|
|
267
|
+
Util.flashError = function (message = '', errors = []) {
|
|
268
|
+
message = message || LANGUAGE.data_not_found
|
|
269
|
+
let json = { type: 'error', status: 0, title: 'Error', message: message, errors: errors }
|
|
277
270
|
|
|
278
|
-
|
|
279
|
-
}
|
|
271
|
+
return json
|
|
272
|
+
}
|
|
280
273
|
|
|
281
274
|
Util.jsonError = function (path, message) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
Util.arrayToObject = (array, keyField, isInteger = false) => {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
275
|
+
let json = {}
|
|
276
|
+
json.errorLog = 1
|
|
277
|
+
json.type = 'error'
|
|
278
|
+
json.status = 0
|
|
279
|
+
json.title = path + ' Error!'
|
|
280
|
+
json.message = message
|
|
281
|
+
json.errors = [{ path: path, message: message }]
|
|
282
|
+
|
|
283
|
+
return json
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
Util.arrayToObject = (array = [], keyField, isInteger = false) => {
|
|
287
|
+
let obj = {}
|
|
288
|
+
if (array.length) {
|
|
289
|
+
array.forEach(function (item) {
|
|
290
|
+
var name = item[keyField] == null ? 'xxxxxx' : item[keyField] == 'null' ? 'xxxxxx' : isInteger ? item[keyField] : item[keyField] + ''
|
|
291
|
+
obj[name] = item
|
|
292
|
+
})
|
|
293
|
+
}
|
|
294
|
+
return obj
|
|
295
|
+
}
|
|
303
296
|
|
|
304
297
|
//chase id:1,name:'test' t0 {1:'test'}
|
|
305
|
-
Util.modulesSwitch = (arr) => {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
Util.
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
};
|
|
298
|
+
Util.modulesSwitch = (arr =[]) => {
|
|
299
|
+
let stores = []
|
|
300
|
+
stores.push({ id: '', name: '' })
|
|
301
|
+
arr.forEach((ar, index) => {
|
|
302
|
+
stores.push({ id: index, name: ar })
|
|
303
|
+
})
|
|
304
|
+
return stores
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
Util.buildArrayObjectPrefix = function (arr = []) {
|
|
308
|
+
return Util.modulesSwitch(arr)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
Util.arrayWithObject = (array = [], key, field) => {
|
|
312
|
+
if (array.length) {
|
|
313
|
+
return array.reduce((obj, item) => {
|
|
314
|
+
obj[item[key]] = item[field]
|
|
315
|
+
return obj
|
|
316
|
+
}, {})
|
|
317
|
+
}
|
|
318
|
+
}
|
|
327
319
|
|
|
328
320
|
/*
|
|
329
321
|
for movedupload file using single file
|
|
330
322
|
*/
|
|
331
323
|
Util.moveFile = function (buffer, filename) {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}
|
|
324
|
+
return new Promise(function (resolve, reject) {
|
|
325
|
+
buffer.mv(filename, function (err) {
|
|
326
|
+
if (err) {
|
|
327
|
+
reject(err)
|
|
328
|
+
} else {
|
|
329
|
+
resolve(filename)
|
|
330
|
+
}
|
|
331
|
+
})
|
|
332
|
+
})
|
|
333
|
+
}
|
|
342
334
|
|
|
343
335
|
Util.generateUnique = function (length, charset) {
|
|
344
|
-
|
|
345
|
-
|
|
336
|
+
let random = Util.generate(length, charset)
|
|
337
|
+
let uid = new Date().valueOf().toString(36)
|
|
346
338
|
|
|
347
|
-
|
|
348
|
-
}
|
|
339
|
+
return uid + random
|
|
340
|
+
}
|
|
349
341
|
|
|
350
342
|
Util.generate = function (length, charset) {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
359
|
-
|
|
343
|
+
length = length || 50
|
|
344
|
+
//alphanumeric - [0-9 a-z A-Z] alphabetic - [a-z A-Z] numeric - [0-9] hex - [0-9 a-f] custom - any given characters
|
|
345
|
+
charset = charset || 'alphanumeric'
|
|
346
|
+
return randomstring.generate({
|
|
347
|
+
length: length,
|
|
348
|
+
charset: charset,
|
|
349
|
+
})
|
|
350
|
+
}
|
|
360
351
|
|
|
361
352
|
Util.uuid = () => {
|
|
362
|
-
|
|
363
|
-
}
|
|
353
|
+
return uuidv4()
|
|
354
|
+
}
|
|
364
355
|
/*
|
|
365
356
|
generate random string 8
|
|
366
357
|
*/
|
|
367
358
|
Util.random = function (length) {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
359
|
+
length = length || 5
|
|
360
|
+
let text = ''
|
|
361
|
+
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
362
|
+
for (let i = 0; i < length; i++) text += possible.charAt(Math.floor(Math.random() * possible.length))
|
|
373
363
|
|
|
374
|
-
|
|
375
|
-
}
|
|
364
|
+
return text
|
|
365
|
+
}
|
|
376
366
|
|
|
377
367
|
Util.whitelist = function () {
|
|
378
|
-
|
|
379
|
-
}
|
|
368
|
+
return ['www', 'app', 'my', 'sintret', 'https', 'https']
|
|
369
|
+
}
|
|
380
370
|
|
|
381
371
|
Util.convertDate = function (d) {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
372
|
+
d = d.trim()
|
|
373
|
+
let myarr = d.split(' ')
|
|
374
|
+
return myarr[2] + '-' + Util.monthConvert(myarr[1]) + '-' + myarr[0]
|
|
375
|
+
}
|
|
386
376
|
|
|
387
377
|
// get string start from to
|
|
388
378
|
Util.cut = function (text, start, end) {
|
|
389
|
-
|
|
390
|
-
}
|
|
379
|
+
return text.substr(start, end)
|
|
380
|
+
}
|
|
391
381
|
|
|
392
382
|
Util.getFormattedDate = function (date) {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
}
|
|
383
|
+
let year = date.getFullYear()
|
|
384
|
+
let month = (1 + date.getMonth()).toString()
|
|
385
|
+
month = month.length > 1 ? month : '0' + month
|
|
386
|
+
let day = date.getDate().toString()
|
|
387
|
+
day = day.length > 1 ? day : '0' + day
|
|
388
|
+
let time = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
|
|
389
|
+
return year + '-' + month + '-' + day + ' ' + time
|
|
390
|
+
}
|
|
401
391
|
|
|
402
392
|
Util.uniqueId = function () {
|
|
403
|
-
|
|
404
|
-
}
|
|
393
|
+
return (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase()
|
|
394
|
+
}
|
|
405
395
|
|
|
406
396
|
Util.typePaperSize = [
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
397
|
+
{ title: 'F4', description: 'FOLIO', width: 215, height: 330 },
|
|
398
|
+
{ title: 'LEGAL', description: 'LEGAL', width: 216, height: 356 },
|
|
399
|
+
{ title: 'LETTER', description: 'LETTER', width: 216, height: 279 },
|
|
400
|
+
{ title: 'A3', description: 'A3', width: 297, height: 420 },
|
|
401
|
+
{ title: 'A4', description: 'A4', width: 210, height: 297 },
|
|
402
|
+
{ title: 'A5', description: 'A5', width: 148, height: 210 },
|
|
403
|
+
{ title: 'A6', description: 'A6', width: 105, height: 148 },
|
|
404
|
+
{ title: 'A7', description: 'A7', width: 74, height: 105 },
|
|
405
|
+
{ title: 'A8', description: 'A8', width: 52, height: 74 },
|
|
406
|
+
{ title: 'A9', description: 'A9', width: 37, height: 52 },
|
|
407
|
+
{ title: 'CUSTOM', description: 'CUSTOM', width: 105, height: 148 },
|
|
418
408
|
]
|
|
419
409
|
|
|
420
410
|
Util.typeFont = [
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
411
|
+
'Verdana, Geneva, sans-serif',
|
|
412
|
+
'"Times New Roman", Times, serif',
|
|
413
|
+
'Georgia, serif',
|
|
414
|
+
'"Palatino Linotype", "Book Antiqua", Palatino, serif',
|
|
415
|
+
'Arial, Helvetica, sans-serif',
|
|
416
|
+
'"Arial Black", Gadget, sans-serif',
|
|
417
|
+
'"Comic Sans MS", cursive, sans-serif',
|
|
418
|
+
'Impact, Charcoal, sans-serif',
|
|
419
|
+
'"Lucida Sans Unicode", "Lucida Grande", sans-serif',
|
|
420
|
+
'Tahoma, Geneva, sans-serif',
|
|
421
|
+
'"Trebuchet MS", Helvetica, sans-serif',
|
|
422
|
+
'"Courier New", Courier, monospace',
|
|
423
|
+
'"Lucida Console", Monaco, monospace',
|
|
434
424
|
]
|
|
435
425
|
|
|
436
|
-
Util.objectToGridFormat = function (obj, isInteger) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
}
|
|
426
|
+
Util.objectToGridFormat = function (obj = {}, isInteger = false) {
|
|
427
|
+
isInteger = isInteger || false
|
|
428
|
+
let arr = []
|
|
429
|
+
arr.push({ id: '', name: '' })
|
|
430
|
+
for (let keys in obj) {
|
|
431
|
+
if (isInteger) {
|
|
432
|
+
arr.push({ id: parseInt(keys), name: obj[keys] })
|
|
433
|
+
} else {
|
|
434
|
+
arr.push({ id: keys, name: obj[keys] })
|
|
446
435
|
}
|
|
447
|
-
|
|
436
|
+
}
|
|
437
|
+
return arr
|
|
448
438
|
}
|
|
449
439
|
|
|
450
|
-
Util.random = function (length) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
for (let i = 0; i < length; i++)
|
|
455
|
-
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
440
|
+
Util.random = function (length = 5) {
|
|
441
|
+
let text = ''
|
|
442
|
+
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
443
|
+
for (let i = 0; i < length; i++) text += possible.charAt(Math.floor(Math.random() * possible.length))
|
|
456
444
|
|
|
457
|
-
|
|
445
|
+
return text
|
|
458
446
|
}
|
|
459
447
|
|
|
460
448
|
Util.typePrint = {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
}
|
|
449
|
+
register: '{"paper_size":"F4","paper_size_width":"215","paper_size_height":"330","padding_top":"8","padding_right":"8","padding_bottom":"8","padding_left":"8","border":"1","font":"0","font_size":"10","header":"SURAT PERINTAH KERJA","header_font":"0","header_font_size":"26"}',
|
|
450
|
+
estimation: '{"paper_size":"F4","paper_size_width":"215","paper_size_height":"330","padding_top":"8","padding_right":"8","padding_bottom":"8","padding_left":"8","border":"1","font":"0","font_size":"12","header":"ESTIMASI BIAYA PERBAIKAN","header_font":"0","header_font_size":"18"}',
|
|
451
|
+
invoice: '{"paper_size":"A5","paper_size_width":"148","paper_size_height":"210","padding_top":"8","padding_right":"8","padding_bottom":"8","padding_left":"8","border":"1","font":"0","font_size":"12","header":"INVOICE","header_font":"0","header_font_size":"18"}',
|
|
452
|
+
currency: '{"symbol":"Rp","name":"Rupiah","thousand":"."}',
|
|
453
|
+
}
|
|
466
454
|
|
|
467
455
|
Util.isJson = function (text) {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
456
|
+
if (text) {
|
|
457
|
+
if (
|
|
458
|
+
/^[\],:{}\s]*$/.test(
|
|
459
|
+
text
|
|
460
|
+
.replace(/\\["\\\/bfnrtu]/g, '@')
|
|
461
|
+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
|
|
462
|
+
.replace(/(?:^|:|,)(?:\s*\[)+/g, '')
|
|
463
|
+
)
|
|
464
|
+
) {
|
|
465
|
+
//the json is ok
|
|
466
|
+
return true
|
|
467
|
+
} else {
|
|
468
|
+
return false
|
|
469
|
+
//the json is not ok
|
|
476
470
|
}
|
|
477
|
-
|
|
478
|
-
|
|
471
|
+
}
|
|
472
|
+
return false
|
|
473
|
+
}
|
|
479
474
|
|
|
480
|
-
Util.isEmptyObject = function (obj) {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
}
|
|
475
|
+
Util.isEmptyObject = function (obj = {}) {
|
|
476
|
+
for (let prop in obj) {
|
|
477
|
+
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
|
|
478
|
+
return false
|
|
485
479
|
}
|
|
480
|
+
}
|
|
486
481
|
|
|
487
|
-
|
|
488
|
-
}
|
|
482
|
+
return JSON.stringify(obj) === JSON.stringify({})
|
|
483
|
+
}
|
|
489
484
|
|
|
490
|
-
Util.serializeTable = function (table) {
|
|
491
|
-
|
|
492
|
-
}
|
|
485
|
+
Util.serializeTable = function (table = '') {
|
|
486
|
+
return '`' + table + '`'
|
|
487
|
+
}
|
|
493
488
|
|
|
494
|
-
Util.getKey = function (obj, field) {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
489
|
+
Util.getKey = function (obj = {}, field = '') {
|
|
490
|
+
let t = ''
|
|
491
|
+
for (let item in obj) {
|
|
492
|
+
if (obj[item] == field) {
|
|
493
|
+
return item
|
|
500
494
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
495
|
+
}
|
|
496
|
+
return t
|
|
497
|
+
}
|
|
504
498
|
|
|
505
|
-
Util.camelize = function (text) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
499
|
+
Util.camelize = function (text = '') {
|
|
500
|
+
return text.replace(/^([A-Z])|[\s-_]+(\w)/g, function (match, p1, p2, offset) {
|
|
501
|
+
if (p2) return p2.toUpperCase()
|
|
502
|
+
return p1.toLowerCase()
|
|
503
|
+
})
|
|
510
504
|
}
|
|
511
505
|
|
|
512
|
-
Util.decamelize = function (str, separator) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
.toLowerCase();
|
|
506
|
+
Util.decamelize = function (str = '', separator = '_') {
|
|
507
|
+
return str
|
|
508
|
+
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
|
|
509
|
+
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
|
|
510
|
+
.replace('_', separator)
|
|
511
|
+
.toLowerCase()
|
|
519
512
|
}
|
|
520
513
|
|
|
521
514
|
/*
|
|
@@ -523,679 +516,666 @@ Util.decamelize = function (str, separator) {
|
|
|
523
516
|
to : order_tep
|
|
524
517
|
*/
|
|
525
518
|
|
|
526
|
-
Util.toName = function (str, separator) {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
str = "a"+str;
|
|
533
|
-
}
|
|
534
|
-
let string = str.replace(/\s+/g, separator).toLowerCase();
|
|
535
|
-
string = string.replace("/","");
|
|
536
|
-
string = string.replace("/\/","");
|
|
537
|
-
string = string.replace("__","_");
|
|
538
|
-
return string.replace(/[^A-Za-z0-9/_]/g, "")
|
|
519
|
+
Util.toName = function (str = '', separator = '_') {
|
|
520
|
+
if (str && str.length) {
|
|
521
|
+
str = str.trim()
|
|
522
|
+
//add if first character is number with string character
|
|
523
|
+
if (Util.isInteger(str.charAt(0))) {
|
|
524
|
+
str = 'a' + str
|
|
539
525
|
}
|
|
526
|
+
let string = str.replace(/\s+/g, separator).toLowerCase()
|
|
527
|
+
string = string.replace('/', '')
|
|
528
|
+
string = string.replace('//', '')
|
|
529
|
+
string = string.replace('__', '_')
|
|
530
|
+
return string.replace(/[^A-Za-z0-9/_]/g, '')
|
|
531
|
+
}
|
|
540
532
|
}
|
|
541
533
|
|
|
542
534
|
/*
|
|
543
535
|
change : orderStep
|
|
544
536
|
to : Order Step
|
|
545
537
|
*/
|
|
546
|
-
Util.fieldToName = function (str) {
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
}
|
|
538
|
+
Util.fieldToName = function (str = '') {
|
|
539
|
+
let title = Util.capitalizeFirstLetter(Util.decamelize(str))
|
|
540
|
+
title = Util.replaceAll(title, '_', ' ')
|
|
541
|
+
title = Util.replaceAll(title, ' id', '')
|
|
542
|
+
title = Util.capitalizeAfterSpace(title)
|
|
543
|
+
|
|
544
|
+
const lastWords = Util.lastWord(title)
|
|
545
|
+
if (title.length > 4 && lastWords == 'Id') {
|
|
546
|
+
title = title.replace('Id', '')
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return title
|
|
550
|
+
}
|
|
559
551
|
|
|
560
|
-
Util.capitalizeWord = (string) => {
|
|
561
|
-
|
|
562
|
-
|
|
552
|
+
Util.capitalizeWord = (string = '') => {
|
|
553
|
+
return string
|
|
554
|
+
.split(' ')
|
|
555
|
+
.map((m) => m[0].toUpperCase() + m.substr(1))
|
|
556
|
+
.join(' ')
|
|
557
|
+
}
|
|
563
558
|
|
|
564
|
-
Util.capitalizeFirstLetter = (string) => {
|
|
565
|
-
|
|
566
|
-
}
|
|
559
|
+
Util.capitalizeFirstLetter = (string = '') => {
|
|
560
|
+
return string.charAt(0).toUpperCase() + string.slice(1)
|
|
561
|
+
}
|
|
567
562
|
|
|
568
563
|
Util.asyncWrap = (fn) => {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
Util.capitalizeAfterSpace = function (str) {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
haystack = haystack || [];
|
|
608
|
-
if (haystack.length && needle) {
|
|
609
|
-
return haystack.includes(needle);
|
|
610
|
-
} else {
|
|
611
|
-
return false;
|
|
612
|
-
}
|
|
613
|
-
};
|
|
564
|
+
return (req, res, next) => {
|
|
565
|
+
fn(req, res, next).catch(next)
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
Util.capitalizeAfterSpace = function (str = '') {
|
|
570
|
+
str = Util.replaceAll(str, '_', ' ')
|
|
571
|
+
return str.replace(/\w\S*/g, function (txt) {
|
|
572
|
+
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
|
|
573
|
+
})
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
Util.capitalizeAfterSpaceTitle = function (str = '') {
|
|
577
|
+
str = Util.replaceAll(str, ' ', '_')
|
|
578
|
+
return str.replace(/\w\S*/g, function (txt) {
|
|
579
|
+
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
|
|
580
|
+
})
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
Util.lastWord = function (words = '') {
|
|
584
|
+
let n = words.split(' ')
|
|
585
|
+
return n[n.length - 1]
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
Util.arrayUnShift = function (arr = [], val = '') {
|
|
589
|
+
let obj = {}
|
|
590
|
+
obj[arr[0]] = ''
|
|
591
|
+
obj[arr[1]] = ''
|
|
592
|
+
return obj
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
Util.in_array = function (needle = '', haystack = []) {
|
|
596
|
+
if (haystack.length && needle) {
|
|
597
|
+
return haystack.includes(needle)
|
|
598
|
+
} else {
|
|
599
|
+
return false
|
|
600
|
+
}
|
|
601
|
+
}
|
|
614
602
|
|
|
615
603
|
Util.gridSearch = function (visibles, relations, name, value) {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
}
|
|
604
|
+
let index = 0
|
|
605
|
+
let elm = 'input'
|
|
606
|
+
for (let i = 0; i < visibles.length; i++) {
|
|
607
|
+
if (name == visibles[i]) {
|
|
608
|
+
index = i
|
|
622
609
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
610
|
+
}
|
|
611
|
+
if (!Util.isEmptyObject(relations)) {
|
|
612
|
+
let arr = Object.keys(relations)
|
|
613
|
+
for (let i = 0; i < arr.length; i++) {
|
|
614
|
+
if (name == arr[i]) {
|
|
615
|
+
elm = 'select'
|
|
616
|
+
}
|
|
630
617
|
}
|
|
631
|
-
|
|
632
|
-
|
|
618
|
+
}
|
|
619
|
+
return 'searchValue.eq(' + index + ').find("' + elm + '").val("' + value + '");'
|
|
620
|
+
}
|
|
633
621
|
|
|
634
622
|
Util.toNumber = function (num) {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
Util.formatNumber = function (num, thousandSeparator='.') {
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
Util.fileAttribute = function (filename) {
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
Util.fileImage = ['jpg', 'jpeg', 'png', 'webp', 'bmp', 'tif', 'gif', 'png','svg']
|
|
665
|
-
|
|
666
|
-
Util.fileExtension = (filename) => {
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
Util.fileView = function (dir, file, attributes={}) {
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
623
|
+
num = num + ''
|
|
624
|
+
return parseFloat(Util.replaceAll(num, '.', ''))
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
Util.formatNumber = function (num, thousandSeparator = '.') {
|
|
628
|
+
num = num || ''
|
|
629
|
+
let sep = '$1' + thousandSeparator
|
|
630
|
+
let numString = num.toString()
|
|
631
|
+
if (numString.indexOf('.') > -1) {
|
|
632
|
+
let explode = numString.split('.')
|
|
633
|
+
return explode[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, sep) + ',' + explode[1]
|
|
634
|
+
} else {
|
|
635
|
+
return numString.replace(/(\d)(?=(\d{3})+(?!\d))/g, sep)
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
Util.fileAttribute = function (filename = '') {
|
|
640
|
+
filename = filename.toLowerCase() || ''
|
|
641
|
+
let ext = filename.split('.').pop()
|
|
642
|
+
let obj = {}
|
|
643
|
+
obj.ext = ext
|
|
644
|
+
if (Util.in_array(ext, Util.fileImage)) {
|
|
645
|
+
obj.type = 'image'
|
|
646
|
+
} else {
|
|
647
|
+
obj.type = 'file'
|
|
648
|
+
}
|
|
649
|
+
return obj
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
Util.fileImage = ['jpg', 'jpeg', 'png', 'webp', 'bmp', 'tif', 'gif', 'png', 'svg']
|
|
653
|
+
|
|
654
|
+
Util.fileExtension = (filename = '') => {
|
|
655
|
+
filename = filename.toLowerCase() || ''
|
|
656
|
+
let obj = {}
|
|
657
|
+
let ext = filename.split('.').pop()
|
|
658
|
+
obj.ext = ext
|
|
659
|
+
if (Util.in_array(ext, Util.fileImage)) {
|
|
660
|
+
obj.type = 'image'
|
|
661
|
+
} else {
|
|
662
|
+
obj.type = 'file'
|
|
663
|
+
}
|
|
664
|
+
let file = 'file.png'
|
|
665
|
+
if (ext == 'docx' || ext == 'doc') {
|
|
666
|
+
file = 'word.png'
|
|
667
|
+
} else if (ext == 'xls' || ext == 'xlsx') {
|
|
668
|
+
file = 'excel.png'
|
|
669
|
+
} else if (ext == 'pdf') {
|
|
670
|
+
file = 'pdf.png'
|
|
671
|
+
} else if (ext == 'ppt' || ext == 'pptx') {
|
|
672
|
+
file = 'ppt.png'
|
|
673
|
+
} else if (ext == 'txt') {
|
|
674
|
+
file = 'txt.png'
|
|
675
|
+
} else if (ext == 'zip') {
|
|
676
|
+
file = 'zip.jpg'
|
|
677
|
+
} else if (ext == 'rar') {
|
|
678
|
+
file = 'rar.jpg'
|
|
679
|
+
} else if (ext == 'rar') {
|
|
680
|
+
file = 'file.png'
|
|
681
|
+
}
|
|
682
|
+
obj.file = file
|
|
683
|
+
return obj
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
Util.fileView = function (dir = '', file = '', attributes = {}) {
|
|
687
|
+
let filename = dir + file
|
|
688
|
+
let html = ''
|
|
689
|
+
let width = attributes.hasOwnProperty('width') ? attributes.width : '300'
|
|
690
|
+
let withIcon = attributes.hasOwnProperty('withIcon') ? true : false
|
|
691
|
+
let obj = Util.fileExtension(filename)
|
|
692
|
+
let className = attributes.hasOwnProperty('class') ? ` class="${attributes.class}" ` : ''
|
|
693
|
+
if (filename.includes('https')) {
|
|
694
|
+
html = `<img src="${file}" ${className} class="img-responsive">`
|
|
695
|
+
} else {
|
|
696
|
+
if (obj.type == 'image') {
|
|
697
|
+
html = `<img src="${filename}" ${className} width="${width}px">`
|
|
707
698
|
} else {
|
|
708
|
-
|
|
709
|
-
|
|
699
|
+
if (file) {
|
|
700
|
+
if (withIcon) {
|
|
701
|
+
html = `<img class="mb-3 boxy-small" src="/img/${obj.file}" height="45px" width="45px"><a class="text-success" target="_blank" href="${filename}"> ${file.substring(13)}</a>`
|
|
710
702
|
} else {
|
|
711
|
-
|
|
712
|
-
if(withIcon) {
|
|
713
|
-
html = `<img class="mb-3 boxy-small" src="/img/${obj.file}" height="45px" width="45px"><a class="text-success" target="_blank" href="${filename}"> ${file.substring(13)}</a>`;
|
|
714
|
-
} else {
|
|
715
|
-
html = `<a class="text-success" target="_blank" href="${filename}"> ${file.substring(13)}</a>`;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
703
|
+
html = `<a class="text-success" target="_blank" href="${filename}"> ${file.substring(13)}</a>`
|
|
718
704
|
}
|
|
705
|
+
}
|
|
719
706
|
}
|
|
707
|
+
}
|
|
720
708
|
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
};
|
|
709
|
+
return html
|
|
710
|
+
}
|
|
724
711
|
/// end files
|
|
725
712
|
|
|
726
713
|
Util.arrayDelete = function (arr, value) {
|
|
727
|
-
|
|
728
|
-
}
|
|
714
|
+
return arr.filter((item) => item != value)
|
|
715
|
+
}
|
|
729
716
|
|
|
730
|
-
Util.arrayDeletes = function (arr, array) {
|
|
731
|
-
|
|
732
|
-
}
|
|
717
|
+
Util.arrayDeletes = function (arr = [], array = []) {
|
|
718
|
+
return arr.filter((item) => !Util.in_array(item, array))
|
|
719
|
+
}
|
|
733
720
|
|
|
734
721
|
Util.arrayToList = function (arr, array, delimiter, isCount) {
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
}
|
|
742
|
-
html = html.slice(0, (delimiter.length * -1))
|
|
722
|
+
delimiter = delimiter || '<br>'
|
|
723
|
+
isCount = isCount || 1
|
|
724
|
+
let html = ''
|
|
725
|
+
if (arr) {
|
|
726
|
+
for (var i = 0; i < arr.length; i++) {
|
|
727
|
+
html += isCount == 1 ? i + 1 + '. ' + array[arr[i]] + delimiter : ' ' + array[arr[i]] + delimiter
|
|
743
728
|
}
|
|
729
|
+
html = html.slice(0, delimiter.length * -1)
|
|
730
|
+
}
|
|
744
731
|
|
|
745
|
-
|
|
746
|
-
}
|
|
732
|
+
return html
|
|
733
|
+
}
|
|
747
734
|
|
|
748
735
|
Util.menuAccess = function (menu, params) {
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
}
|
|
759
|
-
};
|
|
736
|
+
const roles = require('./role')
|
|
737
|
+
const routes = roles.routes
|
|
738
|
+
if (Util.in_array(menu, routes)) {
|
|
739
|
+
if (Util.in_array(menu, params)) return true
|
|
740
|
+
else return false
|
|
741
|
+
} else {
|
|
742
|
+
return true
|
|
743
|
+
}
|
|
744
|
+
}
|
|
760
745
|
|
|
761
746
|
Util.dropdownHelper = function (data, field, model) {
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
}
|
|
747
|
+
let fieldsx = field + '[]'
|
|
748
|
+
let name = field + '[]'
|
|
749
|
+
let myvalue = typeof data[fieldsx] == undefined ? ' ' : typeof data[fieldsx] == 'string' ? '["' + data[fieldsx] + '"]' : JSON.stringify(data[name])
|
|
750
|
+
if (myvalue) {
|
|
751
|
+
let unique = myvalue.indexOf('[') > -1 ? myvalue : !myvalue ? '' : '[' + myvalue + ']'
|
|
752
|
+
unique = JSON.parse(unique)
|
|
753
|
+
data[field] = JSON.stringify(unique.filter(Util.arrayUnique))
|
|
754
|
+
delete data[name]
|
|
755
|
+
}
|
|
756
|
+
if (model.fields[field].required) {
|
|
757
|
+
if (!data[field]) {
|
|
758
|
+
return false
|
|
775
759
|
}
|
|
776
|
-
|
|
777
|
-
|
|
760
|
+
}
|
|
761
|
+
return data
|
|
762
|
+
}
|
|
778
763
|
|
|
779
764
|
Util.dropdownAdd = function (data, field, model, datas) {
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
}
|
|
765
|
+
let name = field + '[]'
|
|
766
|
+
let myvalue = typeof datas == undefined ? ' ' : typeof datas == 'string' ? '["' + datas + '"]' : JSON.stringify(datas)
|
|
767
|
+
if (myvalue) {
|
|
768
|
+
let unique = myvalue.indexOf('[') > -1 ? myvalue : !myvalue ? '' : '[' + myvalue + ']'
|
|
769
|
+
unique = JSON.parse(unique)
|
|
770
|
+
myvalue = JSON.stringify(unique.filter(Util.arrayUnique))
|
|
771
|
+
delete data[name]
|
|
772
|
+
|
|
773
|
+
data[field] = myvalue
|
|
774
|
+
}
|
|
775
|
+
if (model.fields[field].required) {
|
|
776
|
+
if (!myvalue) {
|
|
777
|
+
return false
|
|
794
778
|
}
|
|
795
|
-
|
|
796
|
-
|
|
779
|
+
}
|
|
780
|
+
return data
|
|
781
|
+
}
|
|
797
782
|
//array unique
|
|
798
783
|
// array.filter(Util.arrayUnique);
|
|
799
784
|
Util.arrayUnique = (value, index, self) => {
|
|
800
|
-
|
|
801
|
-
}
|
|
785
|
+
return self.indexOf(value) == index
|
|
786
|
+
}
|
|
802
787
|
|
|
803
788
|
Util.virtualHelper = function (obj) {
|
|
804
|
-
|
|
805
|
-
|
|
789
|
+
if (Util.isEmptyObject(obj)) return
|
|
790
|
+
if (obj == undefined) return
|
|
806
791
|
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
792
|
+
var str = ''
|
|
793
|
+
for (var key in obj) {
|
|
794
|
+
str += ', `' + obj[key] + '` AS ' + key
|
|
795
|
+
}
|
|
811
796
|
|
|
812
|
-
|
|
813
|
-
}
|
|
797
|
+
return str
|
|
798
|
+
}
|
|
814
799
|
|
|
815
|
-
Util.nots = ['id', 'createdAt', 'updatedAt', 'createdBy', 'updatedBy', 'companyId', 'created_at', 'updated_at','updated_by', 'created_by', 'modified_by', 'company_id', 'token', 'version', 'signin_method', 'lastLogin', 'last_login', 'forgotPassword', 'forgot_password','no', 'actionColumn']
|
|
800
|
+
Util.nots = ['id', 'createdAt', 'updatedAt', 'createdBy', 'updatedBy', 'companyId', 'created_at', 'updated_at', 'updated_by', 'created_by', 'modified_by', 'company_id', 'token', 'version', 'signin_method', 'lastLogin', 'last_login', 'forgotPassword', 'forgot_password', 'no', 'actionColumn']
|
|
816
801
|
|
|
817
|
-
Util.requiredFields = function (obj) {
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
return arr;
|
|
828
|
-
};
|
|
829
|
-
|
|
830
|
-
Util.extractDetails = function (obj) {
|
|
831
|
-
let arr = [];
|
|
832
|
-
for (let key in obj) {
|
|
833
|
-
if (obj[key].length > 0) {
|
|
834
|
-
for (var i = 0; i < obj[key].length; i++) {
|
|
835
|
-
arr.push(obj[key][i])
|
|
836
|
-
}
|
|
837
|
-
}
|
|
802
|
+
Util.requiredFields = function (obj = {}) {
|
|
803
|
+
var nots = Util.nots
|
|
804
|
+
var arr = []
|
|
805
|
+
for (var key in obj) {
|
|
806
|
+
if (!Util.in_array(key, nots)) {
|
|
807
|
+
if (obj[key].required == true) {
|
|
808
|
+
arr.push(key)
|
|
809
|
+
}
|
|
838
810
|
}
|
|
839
|
-
|
|
840
|
-
|
|
811
|
+
}
|
|
812
|
+
return arr
|
|
813
|
+
}
|
|
841
814
|
|
|
842
|
-
Util.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
815
|
+
Util.extractDetails = function (obj = {}) {
|
|
816
|
+
let arr = []
|
|
817
|
+
for (let key in obj) {
|
|
818
|
+
if (obj[key].length > 0) {
|
|
819
|
+
for (var i = 0; i < obj[key].length; i++) {
|
|
820
|
+
arr.push(obj[key][i])
|
|
821
|
+
}
|
|
846
822
|
}
|
|
847
|
-
|
|
848
|
-
|
|
823
|
+
}
|
|
824
|
+
return arr
|
|
825
|
+
}
|
|
849
826
|
|
|
850
|
-
|
|
851
|
-
|
|
827
|
+
Util.arrayToConcat = function (arr = []) {
|
|
828
|
+
let str = 'CONCAT('
|
|
829
|
+
for (let i = 0; i < arr.length; i++) {
|
|
830
|
+
str += arr[i] + '," - ",'
|
|
831
|
+
}
|
|
832
|
+
str = str.slice(0, -9)
|
|
833
|
+
str += ')'
|
|
834
|
+
|
|
835
|
+
return str
|
|
836
|
+
}
|
|
852
837
|
|
|
853
838
|
//sequence all fields based on drag drop generator
|
|
854
|
-
Util.arraySequence = function (arr, left, right) {
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
temp.push(arr[i])
|
|
863
|
-
}
|
|
839
|
+
Util.arraySequence = function (arr = [], left = [], right = []) {
|
|
840
|
+
let obj = Util.arrayToObject(arr, 'Field')
|
|
841
|
+
let temp = [],
|
|
842
|
+
stores = []
|
|
843
|
+
for (let i = 0; i < arr.length; i++) {
|
|
844
|
+
if (arr[i].Field == 'id') {
|
|
845
|
+
stores.push(arr[i].Field)
|
|
846
|
+
temp.push(arr[i])
|
|
864
847
|
}
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
848
|
+
}
|
|
849
|
+
if (Array.isArray(left)) {
|
|
850
|
+
for (let i = 0; i < left.length; i++) {
|
|
851
|
+
if (i % 2 == 0) {
|
|
852
|
+
temp.push(obj[left[i]])
|
|
853
|
+
stores.push(left[i])
|
|
854
|
+
}
|
|
872
855
|
}
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
856
|
+
}
|
|
857
|
+
if (Array.isArray(right)) {
|
|
858
|
+
for (let i = 0; i < right.length; i++) {
|
|
859
|
+
if (i % 2 == 0) {
|
|
860
|
+
temp.push(obj[right[i]])
|
|
861
|
+
stores.push(right[i])
|
|
862
|
+
}
|
|
880
863
|
}
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
864
|
+
}
|
|
865
|
+
if (Array.isArray(left)) {
|
|
866
|
+
for (let i = 0; i < left.length; i++) {
|
|
867
|
+
if (i % 2 == 1) {
|
|
868
|
+
temp.push(obj[left[i]])
|
|
869
|
+
stores.push(left[i])
|
|
870
|
+
}
|
|
888
871
|
}
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
872
|
+
}
|
|
873
|
+
if (Array.isArray(right)) {
|
|
874
|
+
for (let i = 0; i < right.length; i++) {
|
|
875
|
+
if (i % 2 == 1) {
|
|
876
|
+
temp.push(obj[right[i]])
|
|
877
|
+
stores.push(right[i])
|
|
878
|
+
}
|
|
896
879
|
}
|
|
880
|
+
}
|
|
897
881
|
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
}
|
|
882
|
+
for (let i = 0; i < arr.length; i++) {
|
|
883
|
+
const field = arr[i].Field
|
|
884
|
+
if (!Util.in_array(field, stores)) {
|
|
885
|
+
temp.push(arr[i])
|
|
903
886
|
}
|
|
887
|
+
}
|
|
904
888
|
|
|
905
|
-
|
|
906
|
-
}
|
|
889
|
+
return temp
|
|
890
|
+
}
|
|
907
891
|
|
|
908
892
|
Util.isInteger = (value) => {
|
|
909
|
-
|
|
910
|
-
|
|
893
|
+
return (
|
|
894
|
+
!isNaN(value) &&
|
|
895
|
+
(function (x) {
|
|
896
|
+
return (x | 0) === x
|
|
911
897
|
})(parseFloat(value))
|
|
912
|
-
|
|
898
|
+
)
|
|
899
|
+
}
|
|
913
900
|
|
|
914
901
|
Util.isNumeric = function (value) {
|
|
915
|
-
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
Util.tags = function (tags) {
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
html += `<a href="#" rel="tag">${explode[i]}</a>`
|
|
925
|
-
}
|
|
926
|
-
} else {
|
|
927
|
-
html += `<a href="#" rel="tag">${tags}</a>`;
|
|
902
|
+
return /^-?\d+$/.test(value)
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
Util.tags = function (tags = '') {
|
|
906
|
+
let html = ''
|
|
907
|
+
if (tags.indexOf(',') > -1) {
|
|
908
|
+
let explode = tags.split(',')
|
|
909
|
+
for (var i = 0; i < explode.length; i++) {
|
|
910
|
+
html += `<a href="#" rel="tag">${explode[i]}</a>`
|
|
928
911
|
}
|
|
912
|
+
} else {
|
|
913
|
+
html += `<a href="#" rel="tag">${tags}</a>`
|
|
914
|
+
}
|
|
929
915
|
|
|
930
|
-
|
|
931
|
-
}
|
|
916
|
+
return html
|
|
917
|
+
}
|
|
932
918
|
|
|
933
919
|
/*
|
|
934
920
|
get extension of filename
|
|
935
921
|
*/
|
|
936
922
|
|
|
937
|
-
Util.getExtensionFile = (str) => {
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
}
|
|
923
|
+
Util.getExtensionFile = (str = '') => {
|
|
924
|
+
str = str || ''
|
|
925
|
+
let extension = str.split('.').pop()
|
|
926
|
+
extension = extension.toLowerCase()
|
|
927
|
+
let ret = extension
|
|
928
|
+
if (extension == 'jpg') {
|
|
929
|
+
ret = 'jpeg'
|
|
930
|
+
}
|
|
931
|
+
return ret
|
|
932
|
+
}
|
|
947
933
|
|
|
948
|
-
Util.badgeError = (msg) => {
|
|
949
|
-
|
|
950
|
-
}
|
|
934
|
+
Util.badgeError = (msg = '') => {
|
|
935
|
+
return `<span class="badge badge-danger">${msg}</span>`
|
|
936
|
+
}
|
|
951
937
|
|
|
952
|
-
Util.badgeSuccess = (msg) => {
|
|
953
|
-
|
|
954
|
-
}
|
|
938
|
+
Util.badgeSuccess = (msg = '') => {
|
|
939
|
+
return `<span class="badge badge-success">${msg}</span>`
|
|
940
|
+
}
|
|
955
941
|
|
|
956
|
-
Util.alertError = function (msg) {
|
|
957
|
-
|
|
958
|
-
}
|
|
942
|
+
Util.alertError = function (msg = '') {
|
|
943
|
+
return `<div class="alert alert-danger" role="alert">${msg}</div>`
|
|
944
|
+
}
|
|
959
945
|
|
|
960
|
-
Util.alertSuccess = function (msg) {
|
|
961
|
-
|
|
962
|
-
}
|
|
946
|
+
Util.alertSuccess = function (msg = '') {
|
|
947
|
+
return `<div class="alert alert-success" role="alert">${msg}</div>`
|
|
948
|
+
}
|
|
963
949
|
|
|
964
|
-
Util.alertInfo = function (msg) {
|
|
965
|
-
|
|
966
|
-
}
|
|
950
|
+
Util.alertInfo = function (msg = '') {
|
|
951
|
+
return `<div class="alert alert-info" role="alert">${msg}</div>`
|
|
952
|
+
}
|
|
967
953
|
|
|
968
954
|
Util.regexPassword = (lengthMin, lengthMax) => {
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
955
|
+
//minimum length
|
|
956
|
+
lengthMin = lengthMin || 6
|
|
957
|
+
lengthMax = lengthMax || 20
|
|
972
958
|
|
|
973
|
-
|
|
974
|
-
}
|
|
959
|
+
return new RegExp('^[a-zA-Z0-9_-]{' + lengthMin + ',' + lengthMax + '}$', 'i')
|
|
960
|
+
}
|
|
975
961
|
|
|
976
962
|
//huruf dan angka saja
|
|
977
963
|
Util.regexCode = (lengthMin, lengthMax) => {
|
|
978
|
-
|
|
979
|
-
|
|
964
|
+
lengthMin = lengthMin || 2
|
|
965
|
+
lengthMax = lengthMax || 10
|
|
980
966
|
|
|
981
|
-
|
|
982
|
-
}
|
|
967
|
+
return new RegExp('^[A-Z0-9]{' + lengthMin + ',' + lengthMax + '}$', 'i')
|
|
968
|
+
}
|
|
983
969
|
|
|
984
|
-
Util.imageProfile = function (image) {
|
|
985
|
-
|
|
986
|
-
}
|
|
970
|
+
Util.imageProfile = function (image = '') {
|
|
971
|
+
return image ? (image.indexOf('http') > -1 ? image : '/uploads/user/' + image) : '/img/user.png'
|
|
972
|
+
}
|
|
987
973
|
|
|
988
974
|
/*
|
|
989
975
|
Files
|
|
990
976
|
*/
|
|
991
|
-
Util.readFile = function (filename) {
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
}
|
|
997
|
-
} catch (err) {
|
|
977
|
+
Util.readFile = function (filename = '') {
|
|
978
|
+
try {
|
|
979
|
+
if (Util.fileExist(filename)) {
|
|
980
|
+
const data = fs.readFileSync(filename, 'utf8')
|
|
981
|
+
return data
|
|
998
982
|
}
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
}
|
|
983
|
+
} catch (err) {}
|
|
984
|
+
return ''
|
|
985
|
+
}
|
|
1002
986
|
//check directory if not exist create or not return true/false
|
|
1003
|
-
Util.dirExist = (dir, create = false) =>{
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
});
|
|
1008
|
-
}
|
|
1009
|
-
// check if directory exists
|
|
1010
|
-
if (fs.existsSync(dir)) {
|
|
1011
|
-
return true;
|
|
1012
|
-
}
|
|
1013
|
-
} catch (e) {
|
|
1014
|
-
}
|
|
1015
|
-
return false;
|
|
1016
|
-
};
|
|
1017
|
-
|
|
1018
|
-
Util.fileExist = (filename) => {
|
|
1019
|
-
if (fs.existsSync(filename)) {
|
|
1020
|
-
return true;
|
|
1021
|
-
}
|
|
1022
|
-
return false;
|
|
1023
|
-
};
|
|
1024
|
-
|
|
1025
|
-
Util.getAllFiles = (dir) => {
|
|
1026
|
-
let files = [];
|
|
1027
|
-
try {
|
|
1028
|
-
if(Util.dirExist(dir, true)) {
|
|
1029
|
-
files = fs.readdirSync(dir);
|
|
1030
|
-
}
|
|
1031
|
-
} catch (e) {
|
|
1032
|
-
return [];
|
|
987
|
+
Util.dirExist = (dir = '', create = false) => {
|
|
988
|
+
try {
|
|
989
|
+
if (create) {
|
|
990
|
+
fs.ensureDir(dir, (err) => {})
|
|
1033
991
|
}
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
Util.writeFile = (filename, content) => {
|
|
1038
|
-
try {
|
|
1039
|
-
let dir = require('path').dirname(filename);
|
|
1040
|
-
Util.dirExist(dir,true);
|
|
1041
|
-
fs.writeFileSync(filename, content);
|
|
1042
|
-
return true
|
|
1043
|
-
} catch (e) {
|
|
1044
|
-
return false
|
|
992
|
+
// check if directory exists
|
|
993
|
+
if (fs.existsSync(dir)) {
|
|
994
|
+
return true
|
|
1045
995
|
}
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
996
|
+
} catch (e) {}
|
|
997
|
+
return false
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
Util.fileExist = (filename = '') => {
|
|
1001
|
+
if (fs.existsSync(filename)) {
|
|
1002
|
+
return true
|
|
1003
|
+
}
|
|
1004
|
+
return false
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
Util.getAllFiles = (dir = '') => {
|
|
1008
|
+
let files = []
|
|
1009
|
+
try {
|
|
1010
|
+
if (Util.dirExist(dir, true)) {
|
|
1011
|
+
files = fs.readdirSync(dir)
|
|
1054
1012
|
}
|
|
1055
|
-
}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
let
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1013
|
+
} catch (e) {
|
|
1014
|
+
return []
|
|
1015
|
+
}
|
|
1016
|
+
return files
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
Util.writeFile = (filename = '', content = '') => {
|
|
1020
|
+
try {
|
|
1021
|
+
let dir = require('path').dirname(filename)
|
|
1022
|
+
Util.dirExist(dir, true)
|
|
1023
|
+
fs.writeFileSync(filename, content)
|
|
1024
|
+
return true
|
|
1025
|
+
} catch (e) {
|
|
1026
|
+
return false
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
Util.deleteAllFiles = (dir = '') => {
|
|
1031
|
+
try {
|
|
1032
|
+
fs.emptyDirSync(dir)
|
|
1033
|
+
return true
|
|
1034
|
+
} catch (e) {
|
|
1035
|
+
return false
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
Util.findFilesName = (arr = [], filename = '') => {
|
|
1040
|
+
return arr.filter((item) => item.includes(filename))
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
Util.getFiles = function (dir, token = '') {
|
|
1044
|
+
let arr = fs.readdirSync(dir)
|
|
1045
|
+
let folders = ''
|
|
1046
|
+
let files = ''
|
|
1047
|
+
arr.forEach(function (item) {
|
|
1048
|
+
if (item.indexOf('.') > -1) {
|
|
1049
|
+
var explode = dir.split('public/')
|
|
1050
|
+
var path = explode[1]
|
|
1051
|
+
var url = '/' + path + '/' + item
|
|
1052
|
+
var extension = item.split('.').pop()
|
|
1053
|
+
files += ` <div class="folder data-file ui-draggable ui-draggable-handle ui-selectee" data-toggle="tooltip" data-type="file" data-url="${url}" data-extension="${extension}" data-name="${item}" filename="${item}" data-original-title="${item}">
|
|
1073
1054
|
<img src="/assets/images/formats/file.png" class="img-responsive ui-selectee">
|
|
1074
1055
|
<p class="text-ellipsis ui-selectee">${item}</p>
|
|
1075
|
-
</div
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1056
|
+
</div>`
|
|
1057
|
+
} else {
|
|
1058
|
+
let explode = dir.split(token)
|
|
1059
|
+
let path = explode[1] || ''
|
|
1060
|
+
let state = ''
|
|
1061
|
+
if (path == '') {
|
|
1062
|
+
state = '/' + item
|
|
1063
|
+
} else {
|
|
1064
|
+
state = path.replace(item, '') + '/' + item
|
|
1065
|
+
}
|
|
1066
|
+
folders += `<div class="folder data-folder ui-draggable ui-draggable-handle ui-droppable ui-selectee" data-toggle="tooltip" data-type="folder" data-state="${state}" data-name="${item}" data-original-title="${item}">
|
|
1086
1067
|
<img src="/assets/images/folder.png" class="img-responsive ui-selectee">
|
|
1087
1068
|
<p class="text-ellipsis ui-selectee">${item}</p>
|
|
1088
|
-
</div
|
|
1089
|
-
|
|
1090
|
-
|
|
1069
|
+
</div>`
|
|
1070
|
+
}
|
|
1071
|
+
})
|
|
1091
1072
|
|
|
1092
|
-
|
|
1093
|
-
}
|
|
1073
|
+
return folders + files
|
|
1074
|
+
}
|
|
1094
1075
|
|
|
1095
|
-
Util.ejsOpen =
|
|
1096
|
-
Util.ejsStart =
|
|
1097
|
-
Util.ejsClose =
|
|
1076
|
+
Util.ejsOpen = '<%- '
|
|
1077
|
+
Util.ejsStart = '<% '
|
|
1078
|
+
Util.ejsClose = ' %>'
|
|
1098
1079
|
Util.ejsFunction = (yourCode, isStatement = false) => {
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
}
|
|
1102
|
-
|
|
1103
|
-
Util.attributeOptions = (obj, defaultObj = {}) => {
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
}
|
|
1112
|
-
html += ` ${key}="${value}" `;
|
|
1113
|
-
}
|
|
1114
|
-
if(arr.length){
|
|
1115
|
-
arr.map(item => html += ` ${item}="${defaultObj[item]}" `)
|
|
1080
|
+
const open = isStatement ? Util.ejsStart : Util.ejsOpen
|
|
1081
|
+
return open + yourCode + Util.ejsClose
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
Util.attributeOptions = (obj = {}, defaultObj = {}) => {
|
|
1085
|
+
let html = ''
|
|
1086
|
+
let arr = Object.keys(defaultObj) || []
|
|
1087
|
+
for (const key in obj) {
|
|
1088
|
+
let value = obj[key]
|
|
1089
|
+
if (defaultObj.hasOwnProperty(key)) {
|
|
1090
|
+
value = defaultObj[key] + obj[key]
|
|
1091
|
+
Util.arrayDelete(arr, key)
|
|
1116
1092
|
}
|
|
1117
|
-
|
|
1118
|
-
}
|
|
1093
|
+
html += ` ${key}="${value}" `
|
|
1094
|
+
}
|
|
1095
|
+
if (arr.length) {
|
|
1096
|
+
arr.map((item) => (html += ` ${item}="${defaultObj[item]}" `))
|
|
1097
|
+
}
|
|
1098
|
+
return html
|
|
1099
|
+
}
|
|
1119
1100
|
|
|
1120
|
-
Util.userAvatar = (img) => {
|
|
1121
|
-
|
|
1122
|
-
}
|
|
1101
|
+
Util.userAvatar = (img = '') => {
|
|
1102
|
+
return img ? (img.includes('http') ? img : `/uploads/user/${img}`) : `/img/user.png`
|
|
1103
|
+
}
|
|
1123
1104
|
|
|
1124
1105
|
/*
|
|
1125
1106
|
MYSQL HELPER
|
|
1126
1107
|
*/
|
|
1127
|
-
Util.selectParser = (fields, MYMODEL) => {
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
}
|
|
1108
|
+
Util.selectParser = (fields = [], MYMODEL = {}) => {
|
|
1109
|
+
fields = fields.filter((e) => e !== 'actionColumn')
|
|
1110
|
+
let virtuals = []
|
|
1111
|
+
for (var key in MYMODEL.widgets) {
|
|
1112
|
+
if (MYMODEL.widgets[key].name === 'virtual') {
|
|
1113
|
+
virtuals.push(key)
|
|
1134
1114
|
}
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
})
|
|
1145
|
-
|
|
1146
|
-
let select = `"${arr.join('","')}"`;
|
|
1147
|
-
return select;
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
Util.selectMysql = (fields,relations={}) => {
|
|
1151
|
-
let obj = {}
|
|
1152
|
-
let arr = [];
|
|
1153
|
-
let virtuals = {};
|
|
1154
|
-
let virtualArray = [];
|
|
1155
|
-
if(relations.hasOwnProperty("zvirtuals")){
|
|
1156
|
-
virtuals = relations.zvirtuals;
|
|
1157
|
-
delete relations.zvirtuals;
|
|
1158
|
-
virtualArray = Object.keys(virtuals);
|
|
1159
|
-
virtualArray.forEach(function (item) {
|
|
1160
|
-
fields = Util.arrayDelete(fields,item);
|
|
1161
|
-
});
|
|
1115
|
+
}
|
|
1116
|
+
let arr = []
|
|
1117
|
+
fields.forEach((item) => {
|
|
1118
|
+
if (virtuals.includes(item)) {
|
|
1119
|
+
arr.push(MYMODEL.widgets[item].fields)
|
|
1120
|
+
} else if (item === 'no') {
|
|
1121
|
+
arr.push('id')
|
|
1122
|
+
} else {
|
|
1123
|
+
arr.push(item)
|
|
1162
1124
|
}
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1125
|
+
})
|
|
1126
|
+
|
|
1127
|
+
let select = `"${arr.join('","')}"`
|
|
1128
|
+
return select
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
Util.selectMysql = (fields = [], relations = {}) => {
|
|
1132
|
+
let obj = {}
|
|
1133
|
+
let arr = []
|
|
1134
|
+
let virtuals = {}
|
|
1135
|
+
let virtualArray = []
|
|
1136
|
+
if (relations.hasOwnProperty('zvirtuals')) {
|
|
1137
|
+
virtuals = relations.zvirtuals
|
|
1138
|
+
delete relations.zvirtuals
|
|
1139
|
+
virtualArray = Object.keys(virtuals)
|
|
1140
|
+
virtualArray.forEach(function (item) {
|
|
1141
|
+
fields = Util.arrayDelete(fields, item)
|
|
1173
1142
|
})
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
if(
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1143
|
+
}
|
|
1144
|
+
let selects = []
|
|
1145
|
+
fields.forEach(function (item) {
|
|
1146
|
+
if (item != 'actionColumn') {
|
|
1147
|
+
if (item == 'no') {
|
|
1148
|
+
selects.push('id')
|
|
1149
|
+
} else {
|
|
1150
|
+
selects.push(item)
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
})
|
|
1154
|
+
//make sure id
|
|
1155
|
+
selects.push('id')
|
|
1156
|
+
let select = `"${selects.join('","')}"`
|
|
1157
|
+
if (virtualArray.length) {
|
|
1158
|
+
for (let key in virtuals) {
|
|
1159
|
+
select += `, ${virtuals[key]} `
|
|
1181
1160
|
}
|
|
1182
|
-
|
|
1183
|
-
|
|
1161
|
+
}
|
|
1162
|
+
return select
|
|
1163
|
+
}
|
|
1184
1164
|
|
|
1185
1165
|
/*
|
|
1186
1166
|
Sorting array object with key name
|
|
1187
1167
|
*/
|
|
1188
|
-
Util.sortArray = (arr,key) => {
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
}
|
|
1196
|
-
return 0;
|
|
1168
|
+
Util.sortArray = (arr, key) => {
|
|
1169
|
+
function compare(a, b) {
|
|
1170
|
+
if (a[key] < b[key]) {
|
|
1171
|
+
return -1
|
|
1172
|
+
}
|
|
1173
|
+
if (a[key] > b[key]) {
|
|
1174
|
+
return 1
|
|
1197
1175
|
}
|
|
1198
|
-
return
|
|
1199
|
-
}
|
|
1176
|
+
return 0
|
|
1177
|
+
}
|
|
1178
|
+
return arr.sort(compare)
|
|
1179
|
+
}
|
|
1200
1180
|
|
|
1201
|
-
module.exports = Util
|
|
1181
|
+
module.exports = Util
|