-
Notifications
You must be signed in to change notification settings - Fork 44
/
seamap.prg
361 lines (264 loc) · 9.33 KB
/
seamap.prg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#ifdef __PLATFORM__WINDOWS
#include "c:\harbour\include\hbclass.ch"
#else
#ifdef __PLATFORM__UNIX
#include "/usr/include/harbour/hbclass.ch"
#else
#include "/Users/anto/harbour/include/hbclass.ch"
#endif
#endif
//----------------------------------------------------------------------------//
function Main()
CheckTables()
Controller()
return nil
//----------------------------------------------------------------------------//
function Controller()
local cMethod := AP_Method()
local aArgs := hb_aTokens( AP_Args(), ":" )
local oModel := Model():New()
local oView := View():New()
local nRecNo
if Len( aArgs ) > 1
nRecNo = Val( aArgs[ 2 ] )
endif
do case
case cMethod == "GET"
? "We process a GET"
do case
case aArgs[ 1 ] == "add"
? "add is required" + "<br>"
oModel:Add()
? oView:Browse( oModel:Browse() )
case aArgs[ 1 ] == "edit"
? "edit is required" + "<br>"
? oView:Edit( oModel:Edit( nRecNo ) )
case aArgs[ 1 ] == "next"
? "browse next is required" + "<br>"
oModel:BrowseNext()
case aArgs[ 1 ] == "prev"
? "browse prev is required" + "<br>"
oModel:BrowsePrev()
case aArgs[ 1 ] == "del"
? "del is required" + "<br>"
oModel:Delete( nRecNo )
? oView:Browse( oModel:Browse() )
otherwise
? "default browse" + "<br>"
? oView:Browse( oModel:Browse() )
endcase
case cMethod == "POST"
? "We process a POST"
oModel:Save()
? oView:Browse( oModel:Browse() )
endcase
return nil
//----------------------------------------------------------------------------//
function CheckTables()
if ! File( hb_GetEnv( "PRGPATH" ) + "/data/links.dbf" )
DbCreate( hb_GetEnv( "PRGPATH" ) + "/data/links.dbf",;
{ { "LINKDEST", "C", 200, 0 },;
{ "LINKNAME", "C", 200, 0 },;
{ "LONGTEXT", "M", 10, 0 } } )
endif
USE ( hb_GetEnv( "PRGPATH" ) + "/data/links" ) SHARED NEW
if RecCount() == 0
APPEND BLANK
field->linkdest := "www.google.com"
field->linkname := "google"
field->longText := "search engine"
APPEND BLANK
field->linkdest := "www.modharbour.live"
field->linkname := "mod_harbour"
field->longText := "harbour on the web"
GO TOP
endif
return nil
//----------------------------------------------------------------------------//
CLASS Model
METHOD Add()
METHOD Edit()
METHOD Browse( hData )
METHOD BrowseNext() VIRTUAL
METHOD BrowsePrev() VIRTUAL
METHOD Delete( nRecNo )
METHOD Save( nRecNo )
ENDCLASS
//----------------------------------------------------------------------------//
METHOD Add() CLASS Model
APPEND BLANK
field->linkdest := "www.new.com"
field->linkname := "new"
field->longText := "a new link"
GO TOP
return nil
//----------------------------------------------------------------------------//
METHOD Browse() CLASS Model
local hData := {=>}
local n, aHeaders := {}, aRows := {}, aRow := {}
for n = 1 to FCount()
AAdd( aHeaders, FieldName( n ) )
next
hData[ "headers" ] = aHeaders
while ! Eof()
aRow = {}
for n = 1 to FCount()
AAdd( aRow, AllTrim( FieldGet( n ) ) )
next
AAdd( aRows, aRow )
SKIP
end
hData[ "rows" ] = aRows
return hData
//----------------------------------------------------------------------------//
METHOD Delete( nRecNo ) CLASS Model
GOTO nRecNo
if RLock()
DELETE
DbUnLock()
endif
USE
USE ( hb_GetEnv( "PRGPATH" ) + "/data/links" )
PACK
USE
USE ( hb_GetEnv( "PRGPATH" ) + "/data/links" ) SHARED NEW
GO TOP
return nil
//----------------------------------------------------------------------------//
METHOD Edit( nRecNo ) CLASS Model
local hData := {=>}
local n, aFields := {}, aValues := {}
GOTO nRecNo
for n = 1 to FCount()
AAdd( aFields, FieldName( n ) )
AAdd( aValues, FieldGet( n ) )
next
hData[ "fields" ] = aFields
hData[ "values" ] = aValues
hData[ "recno" ] = nRecNo
return hData
//----------------------------------------------------------------------------//
METHOD Save() CLASS Model
local n, hPairs := AP_PostPairs()
GOTO Val( hPairs[ "recno" ] )
if RLock()
for n = 1 to FCount()
if hb_HHasKey( hPairs, FieldName( n ) )
do case
case FieldType( n ) == "D"
FieldPut( n, CToD( hb_UrlDecode( hb_HGet( hPairs, FieldName( n ) ) ) ) )
case FieldType( n ) == "L"
FieldPut( n, "on" $ hb_UrlDecode( hb_HGet( hPairs, FieldName( n ) ) ) )
otherwise
FieldPut( n, hb_UrlDecode( hb_HGet( hPairs, FieldName( n ) ) ) )
endcase
else
if FieldType( n ) == "L"
FieldPut( n, .F. )
endif
endif
next
DbUnLock()
endif
GO TOP
return nil
//----------------------------------------------------------------------------//
CLASS View
DATA cHTML INIT ""
METHOD New()
METHOD Browse( hData )
METHOD Edit( hData )
METHOD End()
ENDCLASS
//----------------------------------------------------------------------------//
METHOD New() CLASS View
local cHeader
TEXT INTO cHeader
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<script>
function add()
{
location.href = "seamap.prg?add";
}
function edit( nRecord )
{
location.href = "seamap.prg?edit:" + nRecord;
}
function del( nRecord )
{
location.href = "seamap.prg?del:" + nRecord;
}
function cancel()
{
location.href = "seamap.prg";
return false;
}
</script>
</head>
<body>
ENDTEXT
::cHTML += cHeader + CRLF
return Self
//----------------------------------------------------------------------------//
METHOD Browse( hData ) CLASS View
local n, nRow
::cHtml += "<button onclick='add()'>Add</button><br><br>" + CRLF
::cHtml += "<table>" + CRLF + "<tr>" + CRLF
for n = 1 to Len( hData[ "headers" ] )
::cHtml += "<th>" + hData[ "headers" ][ n ] + "</th>"
next
::cHtml += "<th>Actions</th>"
for n = 1 to Len( hData[ "rows" ] )
::cHtml += "<tr>" + CRLF
for nRow = 1 to Len( hData[ "rows" ][ n ] )
::cHtml += "<td>" + hData[ "rows" ][ n ][ nRow ] + "</td>" + CRLF
next
::cHtml += "<td>"
::cHtml += '<button type="button" onclick="edit(' + AllTrim( Str( n ) ) + ")" + '" class="btn btn-xs btn-default">'
::cHtml += ' <span class="glyphicon glyphicon-pencil"></span>'
::cHtml += "</button>"
::cHtml += '<button type="button" onclick="del(' + AllTrim( Str( n ) ) + ")" + '" class="remove-news btn btn-xs btn-default" data-toggle="tooltip" data-placement="top" data-original-title="Delete">'
::cHtml += ' <span class="glyphicon glyphicon-trash"></span>'
::cHtml += "</button>"
::cHtml += "</td>"
::cHtml += "</tr>" + CRLF
next
::cHtml += "</table>" + CRLF
::End()
return ::cHtml
//----------------------------------------------------------------------------//
METHOD Edit( hData ) CLASS View
local n
::cHtml += "<form action='seamap.prg' method='post'>" + CRLF
::cHtml += "<table>" + CRLF + "<tr>" + CRLF
::cHtml += "<tr>" + CRLF
::cHtml += "<th>FieldName</th>" + CRLF
::cHtml += "<th>Value</th>" + CRLF
::cHtml += "</tr>" + CRLF
for n = 1 to Len( hData[ "fields" ] )
::cHtml += "<tr>" + CRLF
::cHtml += "<td>" + hData[ "fields" ][ n ] + "</td>" + CRLF
::cHtml += "<td><input name='" + hData[ "fields" ][ n ] + "' type='text' value='" + hData[ "values" ][ n ] + "'></td>" + CRLF
::cHtml += "</tr>" + CRLF
next
::cHtml += "</table><br>" + CRLF
::cHtml += "<input type='text' name='recno' id='recno' value='" + AllTrim( Str( hData[ "recno" ] ) ) + "' hidden>"
::cHtml += "<input type='submit' value='Save'>"
::cHtml += "<input type='button' onclick='cancel()' value='Cancel'>" + CRLF
::cHtml += "</form>"
::End()
return ::cHtml
//----------------------------------------------------------------------------//
METHOD End() CLASS View
::cHtml += "</body>" + CRLF
::cHtml += "</html>"
return nil
//----------------------------------------------------------------------------//