File: address-book.euc.l

package info (click to toggle)
euslisp 9.27%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 55,344 kB
  • sloc: ansic: 41,162; lisp: 3,339; makefile: 256; sh: 208; asm: 138; python: 53
file content (471 lines) | stat: -rw-r--r-- 17,335 bytes parent folder | download | duplicates (6)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
;;;; address-book.l
;;;; rsj committee member list management system
;;;; Copyright (c) 2000, Toshihiro Matsui, Electrotechnical Laboratory
;;;; Note that this program should be encoded in sjis.
;;;
;;;  This programs retrieves address book data from the database,
;;;  displays the list, provides search, and allows user's change and insert.
;;;  This program uses postgreSQL as the database server via libpq.so.
;;;  PostgreSQL tables are assumed to have the following fields (columns)
;;; +------------------+-------------+-------+
;;; |      Field       |      Type   | Length|
;;; +------------------+-------------+-------+
;;; | id               | int4        |     4 |
;;; | title            | text        |   var |
;;; | name             | text        |   var |
;;; | kana             | text        |   var |
;;; | office           | text        |   var |
;;; | office_phone     | text        |   var |
;;; | office_phone2    | text        |   var |
;;; | office_fax       | text        |   var |
;;; | email            | text        |   var |
;;; | office_address   | text        |   var |
;;; | home_address     | text        |   var |
;;; | home_phone       | text        |   var |
;;; | update_date      | date        |     4 |
;;; | remarks          | text        |   var |
;;; +------------------+-------------+-------+
;;;  
;;; There several screens:
;;;   list -- displays a list of members and buttons for mail, insert, etc.
;;;   detail- displays detail information of a particular member
;;;   change- changes the detail info
;;;   insert- create a new record
;;;   search- inputs fields for search, results are shown in the list screen
;;;   mail--- sends Email to all/selected members
;;;   download - sends csv file for selected/all members
;;;

(defun euserror (code msg1 form &optional (msg2))
  (format *error-output*
	"eus error ~s ~s ~s ~s~%" code msg1 form msg2)
  (unix::exit 1))

(setq *error-handler* 'euserror)


(load "/usr/local/eus/lib/llib/pgsql.l")
(load "/usr/local/eus/lib/llib/httpcgi.l")
(load "/usr/local/eus/lib/llib/kana_euc")
;; pgsql loads "time" automatically.

(defvar db)

(defun jtable-name (tname)
   (case tname 
	(trustees "理事")
        (techaward "実用化技術賞選考委員会")
	;; add more if you have more address tables
	))

(defun delimit-list (xlist delimiter)
   (let (rlist)
      (while xlist
	 (push (string (pop xlist)) rlist)
	 (if xlist (push delimiter rlist)))
      (apply #'concatenate string (nreverse rlist))))

;; select fields (id name phone ...) where id=1 or id=2 ...
;; if ids=t, all are selected.
(defun rsj-select-by-id (table fields ids)
    (pq:query db nil
	 "select ~a from ~a ~a ~a order by id"
	 (delimit-list fields ",")
	 table
	 (if (consp ids) "where" "")
	 (if (consp ids)
	     (delimit-list 
		 (mapcar #'(lambda (x) (format nil "id=~d"  x))	ids)
		 " or ")
	     "") )
   )

;; collect receiver=1&receiver=2 ... or ids="(1 2)" from the query string
(defun collect-selection (forms)
    (if (assoc 'receiver forms)
	(mapcar #'(lambda (x) (read-from-string (second x)))
		(collect-if #'(lambda (x) (eq (car x) 'receiver)) forms))
	(if (assoc 'ids forms)
	    (read-from-string (qval 'ids forms)))) )

(defun qval (arg query) (sjis2euc (cadr (assoc arg query))))

;;****************************************************************
;; list display
;;****************************************************************

(defun rsj-list (table &optional (ids))
   ;; entry to the member list
   (let ((rsj-list) (delimited-ids ""))
	(if (consp ids)
	    (setq delimited-ids
		(delimit-list
		    (mapcar
			#'(lambda (x) (format nil "id=~a" x)) ids) " or ")))
 	(gen "<h1>RSJ~a名簿 (一覧) </h1><br>~%" (jtable-name table))
	(gen "個人の詳細データを見たり、データを更新・削除するには、各行の
		先頭の数字(id)をクリックして下さい。<br>~%")
	(gen
"<form action=\"address-book.cgi\" method=post>
<table>
<tr><td><input type=submit name=command value=create> 新規レコード作成</td>
<td><input type=submit name=command value=list>全リスト</td></tr>
<tr><td><input type=submit name=command value=mail> 選択した人たちにEmail送信</td>
<td><input type=submit name=command value=mail-all> 全員にEmail送信</td></tr>
</table><br>
<input type=hidden name=table value=~a>~%" table)
	(when ids
	    (setq rsj-list
	       (rsj-select-by-id table
		  '(id title name office email office_phone update_date)
		  ids))
	    (gen "<input type=hidden name=ids value=\"~s\">~%" ids))
	(format *cgi-out* "~s~%" (mapcar #'car rsj-list))
	(setq rsj-list
	   (mapcar #'(lambda (x) (list
	        (format nil 
		  "<input type=checkbox name=receiver value=~d>" (car x))
	        (first x) (second x)  (third x) (fourth x)
	        (format nil "<a href=\"mailto:~a\">~a</a>" (fifth x) (fifth x))
	        (sixth x) (seventh x)))
	      rsj-list))
	(format *cgi-out* "~a<br>~%"
	   (html-table rsj-list :table-option "border=1"
		:href "address-book.cgi"
		:table table
		:command 'detail
		:href-column 2
		:heading '(select id "Title" "Name" "Office" "Email"
				"Office Phone" update)))
	;;(format *cgi-out* "</form>~%")
	;;
	;; <p><form action=\"address-book.cgi\" method=post>
	(format *cgi-out* "<hr>
<input type=hidden name=table value=~a>
<input type=submit name=command value=download>
<input type=submit name=command value=search><br></form>~%" table)    ))


;;****************************************************************
;; detail display
;;****************************************************************
(defun rsj-detail (table n)
   ;; ID button in the entire list brings here.
   (let ((detail (car
	    (pq:query db nil
		"select id, title, name, kana, office, email,
			office_phone, office_fax, office_address,
			home_phone, home_address, update_date, remarks
			from ~a where id=~s" table n))))
      (format *cgi-out*
"<h2>RSJ~aメンバーの詳細情報 </h2><br>
内容を変更するときはchange buttonを押して下さい。
このレコードを削除するにはdelete buttonを押して下さい。<br>
<form action=\"address-book.cgi\" method=post>
<table><tr>
<td><input type=submit name=command value=change></td>
<td><input type=submit name=command value=delete></td>
<td><input type=submit name=command value=list></td></tr></table>
<input type=HIDDEN name=arg value=~a> 
<input type=hidden name=table value=~a> </form>" (jtable-name table) n table)
      ;;
      (format *cgi-out* "~a~%" 
	    (html-table 
		(transpose-list 
			(list '(id title name kana office email
				office_phone office_fax office_address
				home_phone home_address update_date remarks)
			        detail))
		:table table
		:table-option "border=1"
		))
   ) )

;;****************************************************************
;; edit record 
;;****************************************************************

(defun rsj-input-form (table fields initial)
   ;; table form of inputs for fields
   ;; common form for change, create and search
   (gen "<table border=1>~%")
   (let ((detail))
      (setq detail
	   (if initial
	       (car
		  (pq:query db nil
			"select ~a from ~a where id=~s"
			(delimit-list fields ",") table initial))
		(fill (make-list (length fields)) "")    ) )
      (dolist (x (transpose-list (list fields detail)))
         (gen "<tr><td>~a</td>
	<td><input type=TEXT size=60 name=\"~a\" value=\"~a\"></td></tr><br>~%"
		(car x)	(car x) (euc2sjis (cadr x)) ))
        (gen "</table>~%")
	(gen "<input type=HIDDEN name=arg value=~a>~%" initial))
     )

(defun rsj-change (table n)
  ;; provide input forms for n-th record
  ;; [change] button in the detail display brings here.
  ;; 
  (format *cgi-out* "<h2>RSJ~aメンバーレコードの変更</h2><br>
正しいデータを入力し、updateボタンを押して下さい。</h2>~%"
	(jtable-name table))
  (let ((detail (car
	    (pq:query db nil
		"select title, name, kana, office, email,
			office_phone, office_fax, office_address,
			home_phone, home_address, remarks
			from ~a where id=~s" table n))))
     (gen
"<form action=\"address-book.cgi\" method=post>
<input type=hidden name=table value=~a>
<input type=submit name=command value=update>
<input type=reset name=clear  value=\"reset to original\">
<input type=submit name=command value=list>~%<hr>~%" table)
     (rsj-input-form table '("title" "name" "kana" "office" "email"
		"office_phone" "office_fax" "office_address"
		"home_phone" "home_address" "remarks")
	  n)
     (gen "</form>~%")
    ))
      ;;

(defun rsj-update (table id args)
   ;; rsj-change finishes, and the update button brings here.
   ;; encoding should be changed from sjis to euc to put in the database. 
   (pq:query db nil
	(format nil "update ~a set
		title='~a', name='~a',  kana='~a', office='~a', email='~a',
		office_phone='~a', office_fax='~a', office_address='~a',
		home_phone='~a', home_address='~a', remarks='~a',
		update_date='~a' where id='~a'"
		table
		(qval 'title args) (qval 'name args) 
		(qval 'kana args) (qval 'office args) (qval 'email args)
		(qval 'office_phone args) (qval 'office_fax args)
		(qval 'office_address args) (qval 'home_phone args)
		(qval 'home_address args) (qval 'remarks args)
		(send (now) :iso-date-string)
		id ) )
   (rsj-detail table id)	;; display the detail again
   (gen "The member record of Id=~a has been updated.<br>~%" id)
   )


;;****************************************************************
;; create/insert a new record
;;****************************************************************

(defun rsj-create (table)
  ;; provide input forms for n-th record
  ;; create button in the list window brings here.
  ;;
  (gen "<h1>RSJ~a新規メンバー登録</h1><br>
データを入力し、insertボタンを押して下さい。
半角カナは使わないようにお願いします。IDは自動的に割り当てられます。</h2>~%"
	(jtable-name table))
  (let ()
     (gen
"<form action=\"address-book.cgi\" method=post>
<input type=submit name=command value=insert>登録<br>
<input type=reset name=command  value=clear>
<input type=hidden name=table  value=~a>
~%<hr>~%" table)
     (rsj-input-form table  '("title" "name" "kana" "office" "email"
			"office_phone" "office_fax" "office_address"
			"home_phone" "home_address" "remarks")
		nil)
     (gen "</form>~%") 
   )  )

(defun rsj-insert (table args)
  ;; Data for a new record are ready.  Put them in the database.
  (let ((id (caar (pq:query db nil "select max(id) from trustees"))))
     (incf id)
#|     (format *cgi-out* "new id=~a<br>~%" id) |#
     (pq:query db nil
        (format nil "insert into ~a
		(id, title, name, kana, office, email,
		 office_phone, office_fax, office_address,
		 home_phone, home_address, remarks, update_date)
		values ('~a', '~a', '~a', '~a', '~a', '~a',
			 '~a', '~a', '~a', '~a', '~a', '~a', '~a')~%"
		table id
		(qval 'title args) (qval 'name args)
		(qval 'kana args) (qval 'office args) (qval 'email args)
		(qval 'office_phone args) (qval 'office_fax args)
		(qval 'office_address args) (qval 'home_phone args)
		(qval 'home_address args) (qval 'remarks args)
		(send (now) :iso-date-string)
		))
     (gen "<h2>新規memberを登録しました。id=~s</h2>~%" id)
     (rsj-detail table id)
   ))

;;****************************************************************
;; delete
;;****************************************************************

(defun rsj-delete (table id)
   (let ((name (caar
	   (pq:query db nil "select name from ~a where id=~a" table id))))
     (gen
	 "<h1>~aの名簿から~aさん(id=~a)の
レコードを削除してよろしいですか?<br></h1>" (jtable-name table) name id)
     (gen "<form action=\"address-book.cgi\" method=GET>
	<input type=submit name=command value=do-delete>
	<input type=HIDDEN name=arg value=~a> 
	<input type=hidden name=table value=~a> 
	</form><br>~%"  id table)
    ))

(defun rsj-do-delete (table id)
   (let ((name (caar
	   (pq:query db nil "select name from ~a where id=~a" table id))))
     (pq:query db nil "delete from trustees where id='~a'" id )
     (rsj-list table t)
     (format *cgi-out* "~a さんのレコード(id=~a)を削除しました。<br>~%"
	name id))
    )

;;****************************************************************
;; search
;;****************************************************************

(defun rsj-search (table)
   (gen "Input search string for names
	<form action=\"address-book.cgi\" method=GET>~%")
   (rsj-input-form table '("id" "title" "name" "kana" "office" "email") nil)
   (gen "<input type=submit name=command value=\"do-search\"> 
	<input type=reset name=clear value=clear>
	<input type=hidden name=table value=~a>
	</form>~%" table)
 )

(defun rsj-do-search (table args)
   ;; (gen "do-search ~a<br>~%" args)
   (let ((name-to-search (qval 'name args))
	 (title-to-search (qval 'title args))
	 (id-to-search (qval 'id args))
	 (kana-to-search (qval 'kana args))
	 (email-to-search (qval 'email args))
	 (office-to-search (qval 'office args)))
#|      (gen "id=~s~%<br>~%" id-to-search)
      (gen "name=~s~%<br>~%" name-to-search)
      (gen "title=~s~%<br>~%" name-to-search)
      (gen "kana=~s~%<br>~%" kana-to-search)
      (gen "email=~s~%<br>~%" email-to-search)
      (gen "office=~s~%<br>~%" office-to-search) |#
      (if (not (zerop (length id-to-search)))
	  ;; search for the specified ID
	  (progn
	     (setq ids (pq:query db nil
		 "select id from ~a where id=~a" table id-to-search) ))
	  (progn
	     (setq ids (pq:query db nil
		"select id from ~a where name like '%~a%' and
			title like '%~a%' and kana  like '%~a%' and
			email like '%~a%' and office like '%~a%'"
		table name-to-search title-to-search kana-to-search
		email-to-search office-to-search))) )
      ;; (gen "~s~%" ids)
      (rsj-list table (mapcar #'car ids))
      ))

;;****************************************************************
;; download
;;****************************************************************

(defun rsj-download (table ids) 
   (let ((all) (b) (fname (format nil "/home/httpd/html/rsj/rsj-~a.csv" table)))
     (setq all
	(rsj-select-by-id table
		'(id title name kana office email office_phone office_fax
		 office_address home_phone home_address remarks update_date)
		ids))
     (with-open-file (out fname :direction :output)
	(dolist (a all)
	   (while a
	      (setq b (pop a))
	      (format out "~a~a"
		   (cond ((derivedp b interval-time) (send b :iso-date-string))
			 ((stringp b) (euc2sjis b))
			 (t b))
		   (if b "," "")))
	   (terpri out)))
     (unix:system (format nil "tar cf - ~a | gzip >~a.tar.gz" fname fname))
     (gen "<h1>RSJ~a名簿のダウンロード</h1><br>
<a href=\"http://lys.matsui.jp/rsj/rsj-~a.csv.tar.gz\"> <li>download csv.tar.gz</a><br>
<a href=\"http://lys.matsui.jp/rsj/rsj-~a.csv\"> <li>download .csv</a><br>
~%"	(jtable-name table) table table) )
  )

(defun rsj-mail (table receivers)
   ;; (gen "rsj-mail ~s~%" receivers)
   (let ((ids) (email-name) (email-addresses) (names))
      (setq email-name
	    (rsj-select-by-id table '(email name) receivers))
      (setq names (mapcar #'second email-name))
      (setq email-addresses (mapcar #'car email-name))
      (setq email-addresses  (delimit-list email-addresses ", "))
      (gen
	 "<h1>Email送信</h1><br>下の行をクリックすると、~a さんにEmailを送信します。<br>~%"
	 (euc2sjis (delimit-list names ", ")))
      (gen "<a href=\"mailto:~a\"> ~a</a><br>~%"
	 email-addresses email-addresses)))


;;;****************************************************************
;;; main
;;;
(let ((query) (forms) (ids) (command) (table)
	(host (unix:getenv "DATABASE_HOST")))
	(html-header)
	(setq query (get-cgi-query))
	(if (and query (plusp (length query)))
	    (setq forms (parse-http-query query)) )
 	(setq db (instance pq:pgsql :init 
		:host host
		:dbname "t.matsui"
		:user "t.matsui"))
	(setq command (cadr (assoc 'command forms)))
	(if command (setq command (read-from-string command)))
        (setq table (cadr (assoc 'table forms)))
	(setq table (if table (read-from-string table) "trustees"))
	(gen "<head><title>~a</title></head>" table)	
	(gen "<body bgcolor=\"#ffe0c0\">~%")
	;; debug output
	(gen "<!--~%")
	;; (gen "query_string=~s~%" query) 
	(gen "forms=~s~%" forms) 
	(gen "command=~s table=~s -->~%" command table)
	;;
	(if (null command) (setq command 'list))
	(case command
	   ((list) (rsj-list table t))
	   (detail (rsj-detail
		table
		(read-from-string (cadr (assoc 'arg forms)))	 ))
	   (change
		(rsj-change table (read-from-string (cadr (assoc 'arg forms))))	 )
	   (update (rsj-update table (read-from-string (cadr (assoc 'arg forms)))
			 forms)	)
	   (insert (rsj-insert table forms))
	   (create (rsj-create table))
	   (delete (rsj-delete table (read-from-string (cadr (assoc 'arg forms)))))
	   (do-delete (rsj-do-delete table
		 (read-from-string (cadr (assoc 'arg forms)))))
	   (search (rsj-search table))
	   (do-search (rsj-do-search table forms))
	   (download 
		 (rsj-download table (or (collect-selection forms) t)))
	   (mail (rsj-mail table (or (collect-selection forms) t)))
	   (mail-all (rsj-mail table (or (collect-selection forms) t)))
	   (t (format *cgi-out* "no command<br>~%")
		 (gen "~s<br>~%" (unix:environ))
		 (gen "~s<br>~%" forms)
		 (rsj-list "trustees" t)))
	)