aboutsummaryrefslogtreecommitdiff
path: root/cgi-bin/nea.cgi.in
blob: ca609abef7fb936fbbf8cb4689892c2b69b18874 (plain)
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
#! =GUILE_BINDIR=/guile -s
=AUTOGENERATED=
!#
;;;; News page for Ellinika
;;;; Copyright (C) 2004, 2005 Sergey Poznyakoff
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;;;

;;; Tailor this statement to your needs if necessary.
;=GUILE_COMMENT=;(set! %load-path (cons "=GUILE_SITE=" %load-path))

(use-modules (www cgi)
	     (gamma sql)
	     (gamma gettext)
	     (xmltools dict)
	     (ellinika xlat)
	     (ellinika cgi))
(cgi:init)

(define tmpl (if (and monima-nea-template-file-name
		      (cgi:value "timestamp"))
		 monima-nea-template-file-name
		 nea-template-file-name))

(ellinika-cgi-init tmpl)

(define conn #f)
(define article #f)

(define (permalink tag timestamp)
  (display (string-append "<" tag " class=\"permalink\">"))
  (display "<a href=\"")
  (display (make-cgi-name nea-cgi-path "timestamp" timestamp))
  (display "\">[permanent link]</a>")
  (display (string-append "</" tag ">")))

(define (sql-error-handler err descr)
  (format #t "<h1 class=\"error\">~A</h1>\n"
	  (_ "ΣΦΆΛΜΑ: σύνδεση με την βάση δεδομένων απέτυχε."))
  (with-output-to-port
      (current-error-port)
    (lambda ()
      (display err)
      (display ": ")
      (display descr))))  

(defmacro catch-sql (expr)
  `(catch 'gsql-error
	  (lambda () ,expr)
	  (lambda (key err descr)
	    (sql-error-handler err descr))))

(defmacro assert-article (expr)
  `(if article
       (if (null? article)
	   (format #t "<h1 class=\"error\">~A</h1>\n"
		   (_ "Κάμια καταχώρηση"))
	   ,expr)))
	   
(define (summary)
  (catch-sql
   (let ((result (sql-query
		  conn "SELECT date,header,ident FROM news ORDER BY date")))
     (cond
      ((null? result)
       (display "<div align=\"center\">")
       (display (_ "Κανένα νέα"))
       (display "</div>"))
      (else
       (display "<table class=\"news-summary\">\n")
       (let ((ctr 0))
	 (for-each
	  (lambda (entry)
	    (display "<tr class=\"")
	    (display (if (= (modulo ctr 2) 0) "even" "odd"))
	    (display "\">\n")
	    (set! ctr (1+ ctr))
	    (display "<td class=\"date\">")
	    (display (list-ref entry 0))
	    (display "</td>")
	    (display "<td class=\"subject\"><a href=\"")
	    (display (make-cgi-name nea-cgi-path "id" (list-ref entry 2)))
	    (display "\">")
	    (display (list-ref entry 1))
	    (display "</a></td>")
	    (display "\n</tr>\n"))
	  result))
       (display "</table>"))))))

(define (display-article-header item)
  (format #t "<span class=\"itemdate\">~A</span>\n" (car item))
  (display "<span class=\"itemsubject\">\n")
  (display (list-ref item 2))
  (display "</span> "))

(define (display-article-text item . rest)
  (let ((class (and (not (null? rest)) (car rest))))
    (cond
     (class
      (display "\n<div class=\"")
      (display class)
      (display "\">\n")
      (display (list-ref item 3))
      (display "</div>\n"))
     (else
      (display (list-ref item 3))))))

(define (main)
  (catch-sql
   (assert-article
    (for-each
     (lambda (item)
       (display-article-header item)
       
       (if (not (cgi:value "timestamp"))
	   (permalink "span" (list-ref item 1)))
       
       (display-article-text item "itemtext"))
     article))))

(define (title)
  (if article
      (display (if (null? article)
		   (string-append
		    "<h1 class=\"error\">"
		    (_ "Κάμια καταχώρηση")
		    "</h1>")
		   (list-ref (car article) 2)))))
      

(define (nea-html)
  (let ((explist (list (cons "@@main@@"     main)
		       (cons "@@summary@@"  summary)
		       (cons "@@title@@"    title)
		       (cons "@@article-text@@"
			     (lambda ()
			       (catch-sql
				(assert-article
				 (display-article-text (car article))))))
		       (cons "@@article-date@@"
			     (lambda ()
			       (catch-sql
				(assert-article
				 (display (caar article))))))
		       (cons "@@article-header@@"
			     (lambda ()
			       (catch-sql
				(assert-article
				 (display (list-ref (car article) 2))))))
		       (cons "@@full-header@@"
			     (lambda ()
			       (catch-sql
				(assert-article
				 (display-article-header
				  (car article)))))))))
  
    (do ((line (read-line) (read-line)))
	((eof-object? line) #f)
      (expand-template explist line)
      (newline))))

;;; Main
(display "Content-type: text/html; charset=utf-8\r\n\r\n")

(catch 'gsql-error
       (lambda ()
	 (set! conn (sql-connect
		     sql-iface sql-host sql-port sql-database
		     sql-username sql-password))

	 (if (or (cgi:value "timestamp") (cgi:value "id"))
	     (set! article (sql-query conn
				      (cond
				       ((cgi:value "timestamp")
					(string-append
					 "SELECT date,unix_timestamp(date),header,text FROM news WHERE unix_timestamp(date)="
					 (cgi:value "timestamp")))
				       ((cgi:value "id")
					(string-append
					 "SELECT date,unix_timestamp(date),header,text FROM news WHERE ident="
					 (cgi:value "id")))))))
	 
	 (with-input-from-file
	     (template-file target-language tmpl)
	   nea-html)

	 (sql-connect-close conn))
       
       (lambda (key err descr)
	 (with-input-from-file
	     (template-file target-language tmpl)
	   (lambda ()
	     (let ((explist
		    (list (cons "@@main@@"
				(lambda ()
				  (sql-error-handler err descr)))
			  (cons "@@article-text@@"
				(lambda ()
				  (sql-error-handler err descr)))
			  (cons "@@summary@@"  (lambda () #f))
			  (cons "@@title@@"    (lambda () #f))
			  (cons "@@article-date@@" (lambda () #f))
			  (cons "@@article-header@@" (lambda () #f))
			  (cons "@@full-header@@" (lambda () #f)))))
	       (do ((line (read-line) (read-line)))
		   ((eof-object? line) #f)	       
		 (expand-template explist line)
		 (newline)))))))
	 
;;;; Local variables:
;;;; mode: Scheme
;;;; buffer-file-coding-system: utf-8
;;;; End:

Return to:

Send suggestions and report system problems to the System administrator.