added a covert-newlines-p in the text slot presentation.
[clinton/lisp-on-lines.git] / src / slot-presentations.lisp
1 (in-package :lisp-on-lines)
2
3 (defun multiple-value-funcall->list (function &rest args)
4 "The function to be called by m-v-bf"
5 (multiple-value-call #'list (apply function args)))
6
7 (defmacro multiple-value-bindf (vars form &body body)
8 "Like M-V-B, only it works in actions. form must be a function call"
9 `(destructuring-bind ,vars
10 (multiple-value-funcall->list #',(car form) ,@(cdr form))
11 ,@body))
12
13
14 ;;;; ** Textarea Slot Presentation
15
16 (defslot-presentation text-slot-presentation ()
17 ((rows :initarg :rows :accessor rows :initform 5)
18 (columns :initarg :columns :accessor columns :initform 40)
19 (escape-html-p :initarg :escape-html-p :accessor escape-html-p :initform nil)
20 (convert-newlines-p :initarg :convert-newlines-p :accessor convert-newlines-p :initform nil))
21 (:type-name text))
22
23 (defmethod present-slot ((slot text-slot-presentation) instance)
24 (flet ((maybe-convert-newline-and-escape-html-then-print ()
25 (let ((string (if (convert-newlines-p slot)
26 (with-output-to-string (new-string)
27 (with-input-from-string
28 (s (presentation-slot-value slot instance))
29 (loop for line = (read-line s nil)
30 while line
31 do (format new-string "~A~A" line "<br/>"))))
32 (presentation-slot-value slot instance))))
33 (if (escape-html-p slot)
34 (<:as-html string)
35 (<:as-is string)))))
36
37 (if (editablep slot)
38 (<ucw:textarea
39 :accessor (presentation-slot-value slot instance)
40 :reader (or (presentation-slot-value slot instance)
41 "")
42 :rows (rows slot)
43 :cols (columns slot))
44 (maybe-convert-newline-and-escape-html-then-print))))
45
46
47 (defcomponent mewa-slot-presentation ()
48 ((slot-name :accessor slot-name
49 :initarg :slot-name
50 :documentation
51 "The name of the slot being accessed")
52 (fill-gaps-only-p :accessor fill-gaps-only-p
53 :initarg :fill-gaps-only-p
54 :initform nil
55 :documentation
56 "When nil, the instance is syncronised with the database.
57 When T, only the default value for primary keys and the joins are updated.")
58 (show-label-p :accessor show-label-p :initarg :show-label-p :initform t)
59 (creatablep :accessor creatablep :initarg :creatablep :initform t))
60 (:documentation "The superclass of all Mewa slot presentations"))
61
62 ;;;; this has to be in the eval when i would think
63 (eval-when (:compile-toplevel :load-toplevel :execute)
64 (defun generate-slot-presentation-definition-for-type (type)
65 (let* ((u-name (intern (format nil "~A-SLOT-PRESENTATION" type)))
66 (sp-name (intern (format nil "MEWA-~A" u-name)))
67 (t-name (intern (format nil "MEWA-~A" type))))
68 `(defslot-presentation ,sp-name (,u-name mewa-slot-presentation)
69 ()
70 (:type-name ,t-name)))))
71
72 (defmacro define-base-mewa-presentations (&body types)
73 "Define the mewa-slot-presentations by subclassing the base UCW ones"
74 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
75 types)))
76
77 ;;;then actually define the base presentations :
78 (define-base-mewa-presentations
79 boolean
80 string
81 number
82 integer
83 currency)
84
85 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
86 ((input-id :accessor input-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+))
87 (trigger-id :accessor trigger-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+)))
88 (:type-name clsql-sys:wall-time))
89
90 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
91 (let ((date (call-next-method)))
92 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
93 (format nil "~a/~a/~a" m d y)))))
94
95 (defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
96 (let ((new-time (clsql:parse-date-time (remove #\Space value)))
97 (old-time (when (slot-boundp instance (slot-name slot))
98 (slot-value instance (slot-name slot)))))
99 (unless (or (eql old-time new-time)
100 (when (and new-time old-time)
101 (equal :equal (clsql:time-compare new-time old-time))))
102 (setf (presentation-slot-value slot instance) new-time ))))
103
104 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
105 (concatenate 'string (call-next-method) " (m/d/y)"))
106
107 (defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
108 (let ((date (presentation-slot-value slot instance)))
109 (if (and date (not (editablep slot)))
110 (<:as-html date))
111 (when (editablep slot)
112 (<ucw:input :accessor (presentation-slot-value slot instance) :id (input-id slot) :style "display:inline")
113 (<:button :id (trigger-id slot) (<:as-html "[...]"))
114 (<:script :type "text/javascript"
115 (<:as-is (format nil "
116
117 Calendar.setup({
118 inputField : \"~a\",
119 button : \"~a\",
120 ifFormat : \"%m/%d/%Y\" });" (input-id slot) (trigger-id slot)))))))
121
122 (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
123 ((foreign-instance :accessor foreign-instance)
124 (linkedp :accessor linkedp :initarg :linkedp :initform t)
125 (creator :accessor creator :initarg :creator :initform :editor)
126 (new-instance :accessor new-instance :initform nil))
127 (:type-name relation))
128
129 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
130 (multiple-value-bindf (finstance foreign-slot-name)
131 (meta-model:explode-foreign-key instance (slot-name slot))
132 (let ((new-instance (new-instance self)))
133 (unless new-instance
134 (setf (new-instance self)
135 (call-component
136 (ucw::parent slot)
137 (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search))
138 'mewa::mewa-presentation-search)
139 :search-presentation
140 (mewa:make-presentation finstance
141 :type :search-presentation)
142 :list-presentation
143 (mewa:make-presentation finstance
144 :type :listing)))))
145 (sync-foreign-instance slot new-instance))))
146
147 (defmethod sync-foreign-instance ((slot mewa-relation-slot-presentation) foreign-instance)
148 (let ((instance (instance (ucw::parent slot))))
149 (multiple-value-bind (foo f-slot-name)
150 (meta-model:explode-foreign-key instance (slot-name slot))
151 (setf (slot-value instance (slot-name slot)) (slot-value foreign-instance f-slot-name))
152 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p slot)))))
153
154
155 (defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
156 (multiple-value-bindf (finstance foreign-slot-name)
157 (meta-model:explode-foreign-key instance (slot-name slot))
158 (let ((new-instance
159 (call-component
160 (ucw::parent slot)
161 (mewa:make-presentation finstance :type (creator self)))))
162
163 ;;;; TODO: this next bit is due to a bad design decision.
164 ;;;; Components should always have (ok) return self, but somewhere
165 ;;;; i've made in return (instance self) sometimes, and this
166 ;;;; bahaviour is totatlly fucked.
167
168 (when (typep new-instance 'mewa::mewa)
169 (setf new-instance (instance new-instance)))
170
171 ;;;; sorry about that, and now back t our regular program.
172
173 (meta-model:sync-instance new-instance)
174 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
175 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
176
177
178 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
179 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
180 (let* ((i (foreign-instance slot))
181 (pres (mewa::make-presentation
182 i
183 :type :one-line
184 :initargs (list
185 :global-properties
186 (list :editablep nil :linkedp nil)))))
187 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
188 (setf (component.place pres) (component.place (ucw::parent slot))))
189 (when i (<ucw:render-component :component pres))))
190
191 (defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
192 (present-relation slot instance))
193
194 (defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
195 ()
196 (:type-name foreign-key)
197 (:default-initargs))
198
199 (defaction view-instance ((self component) instance &rest initargs)
200 (call-component (ucw::parent self) (apply #'mewa:make-presentation instance initargs))
201 ;; the viewed instance could have been changed/deleted, so we sync this instance
202 (meta-model:sync-instance (instance (ucw::parent self))))
203
204
205 (defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
206 (setf (foreign-instance slot)
207 (when (presentation-slot-value slot instance)
208 (meta-model:explode-foreign-key instance (slot-name slot))))
209 (flet ((render () (when (foreign-instance slot)(call-next-method))))
210 (if (slot-boundp slot 'ucw::place)
211 (cond
212 ((editablep slot)
213 (render)
214 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
215 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
216 ((linkedp slot)
217 (<ucw:a :action (view-instance slot (foreign-instance slot))
218 (render)))
219 (t
220 (render)))
221 ;; presentation is used only for rendering
222 (render))))
223
224
225 (defmethod find-foreign-instances ((slot foreign-key-slot-presentation))
226 (clsql:select (class-name (class-of (meta-model:explode-foreign-key (instance slot) (slot-name slot))))))
227
228
229
230 ;;;; HAS MANY
231 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
232 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
233 (:type-name has-many))
234
235 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
236 ;; if the instance is not stored we must make sure to mark it stored now!
237 (unless (meta-model::persistentp instance)
238 (setf (mewa::modifiedp (ucw::parent self)) t))
239 ;; sync up the instance
240 ;;(mewa:ensure-instance-sync (parent slot))
241 (meta-model:sync-instance (instance (ucw::parent slot)))
242
243 (multiple-value-bindf (class home foreign)
244 (meta-model:explode-has-many instance (slot-name slot))
245 (let ((new (make-instance class)))
246 (setf (slot-value new foreign) (slot-value instance home))
247 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
248 (call-component (ucw::parent slot) (mewa:make-presentation new :type (creator slot)))
249 (meta-model:sync-instance instance))))
250
251 (defmethod present-slot ((slot has-many-slot-presentation) instance)
252 (when (slot-boundp slot 'ucw::place)
253 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
254 (let* ((i (get-foreign-instances slot instance))
255 (presentation (and i (make-presentation (car i) :type :one-line))))
256 (when i
257 (flet ((linker (i string)
258 (<ucw:a
259 :action (view-instance slot i
260 :initargs
261 `(:global-properties ,
262 (list
263 :linkedp t
264 :editablep nil)))
265 (<:as-html string))))
266 (<:table :cellpadding 10
267 (<:tr
268 (<:th) ;empty col for (view) link
269 (dolist (s (slots presentation))
270 (<:th (<:as-html (label s)))))
271 (dolist (s i)
272 (let ((s s))
273 (setf (foreign-instance slot) s)
274 (when (slot-boundp slot 'ucw::place)
275 (<:tr
276 (<:td (linker s " (view) "))
277 (dolist (p (slots (make-presentation s :type :one-line
278 :initargs
279 '(:global-properties
280 (:editablep nil)))))
281 (<:td
282
283 (present-slot p s))))))))))))
284
285
286 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
287 (slot-value instance (slot-name slot)))
288
289 (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
290 (get-foreign-instances slot instance))
291
292 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
293 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
294 (current :accessor current :initform 0)
295 (len :accessor len )
296 (instances :accessor instances))
297 (:type-name has-very-many))
298
299 (defmethod list-next ((slot has-very-many-slot-presentation))
300 (setf (current slot) (incf (current slot) (number-to-display slot)))
301 (when (< (len slot) (current slot))
302 (setf (current slot) (- (number-to-display slot) (len slot)))))
303
304 (defmethod list-prev ((slot has-very-many-slot-presentation))
305 (setf (current slot) (decf (current slot) (number-to-display slot)))
306 (when (> 0 (current slot))
307 ;;what to do here is open to debate
308 (setf (current slot) (- (len slot)(number-to-display slot) ))))
309
310
311 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
312 ;;(<:as-html "isance: " instance)
313 (if (slot-boundp slot 'ucw::place)
314 (progn
315 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
316 (let ((self (ucw::parent slot)))
317 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
318 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
319 (<ucw:a :action (list-next slot) (<:as-html ">>"))
320 (call-next-method)
321 (<:as-html "total :" (len slot)))
322 (call-next-method)))
323
324 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
325 (let ((f (call-next-method)))
326 (setf (len slot) (length f))
327 (setf (instances slot) f)
328 (loop for cons on (nthcdr (current slot) f)
329 for i from 0 upto (number-to-display slot)
330 collect (car cons))))
331
332
333 ;;;; * Has-a
334 (defslot-presentation has-a-slot-presentation (mewa-relation-slot-presentation)
335 ((allow-nil-p :accessor allow-nil-p :initarg :allow-nil-p :initform t)
336 (attributes :accessor attributes :initarg :attributes :initform nil))
337 (:type-name has-a))
338
339 (defmethod find-foreign-slot-value ((slot has-a-slot-presentation) (object t))
340 (multiple-value-bind (c s)
341 (meta-model:explode-foreign-key (instance (ucw::parent slot)) (slot-name slot))
342 (slot-value object s)))
343
344 (defmethod get-foreign-instances ((slot mewa-relation-slot-presentation) instance)
345 (clsql:select (class-name (class-of
346 (meta-model:explode-foreign-key instance (slot-name slot))))
347 :flatp t))
348
349 (defmethod present-slot ((slot has-a-slot-presentation) instance)
350 ; (<:as-html (presentation-slot-value slot instance))
351 (if (editablep slot)
352 (progn (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
353 (when (allow-nil-p slot)
354 (<ucw:option :value nil (<:as-html "none")))
355 (dolist (option (get-foreign-instances slot instance))
356 (<ucw:option :value (find-foreign-slot-value slot option)
357 (lol:present
358 (lol:make-presentation option
359 :type :as-string
360 :initargs
361 `(:attributes ,(attributes slot)))
362 ))))
363 (when (creatablep slot)
364 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline")))
365 (if (presentation-slot-value slot instance)
366 (progn
367 (lol:present
368 (lol:make-presentation (meta-model:explode-foreign-key instance (slot-name slot))
369 :type :one-line
370 :initargs
371 `(:attributes ,(attributes slot)))
372 ))
373 (<:as-html "--"))))
374
375 (defslot-presentation many-to-many-slot-presentation (mewa-relation-slot-presentation)
376 ((list-view :accessor list-view :initarg :list-view :initform :one-line)
377 (action-view :accessor action-view :initarg :action-view :initform :viewer)
378 (create-view :initform :creator)
379 (select-view :initform :as-string :accessor select-view))
380 (:type-name many-to-many)
381 (:default-initargs :label "many to many"))
382
383 (defun %delete-item (item)
384 (clsql:with-default-database (clsql:*default-database*)
385 (ignore-errors
386 (clsql:delete-instance-records item))))
387
388 (defaction delete-item ((self component) instance)
389 (multiple-value-bind (res err) (%delete-item instance)
390 (if (not err)
391 (call 'info-message :message "Removed Instance")
392 (call 'info-message :message (format nil "Could not remove item. Try removing associated items first. ~A" instance)))))
393
394 (defaction delete-relationship ((slot many-to-many-slot-presentation) rel instance)
395 (delete-item (ucw::parent self) rel)
396 (sync-instance instance)
397 (answer-component (ucw::parent self) t))
398
399
400 (defun find-many-to-many-join-class (slot instance)
401 (let* ((imd (getf (meta-model::find-slot-metadata instance (slot-name slot))
402 :db-info))
403 (jc (make-instance (getf imd :join-class)))
404 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
405 :db-info)))
406 (getf jcmd :join-class)))
407
408 (defmethod find-all-instances ((slot many-to-many-slot-presentation) instance)
409 (clsql:select (find-many-to-many-join-class slot instance) :flatp t))
410
411 (defmethod present-slot ((slot many-to-many-slot-presentation) instance)
412 (let ((instances (slot-value instance (slot-name slot)))
413 new-instance)
414 (<:ul
415 (<:li (<ucw:button :action (add-to-many-to-many slot instance)
416 (<:as-html "Add New")))
417 (<:li (<ucw:button :action (add-to-many-to-many slot instance new-instance)
418 (<:as-html "Add:"))
419 (<ucw:select :accessor new-instance
420 (arnesi:dolist* (i (find-all-instances slot instance))
421 (<ucw:option
422 :value i
423 (lol:present-view (i (select-view slot) slot))))))
424 (dolist* (i instances)
425 (<:li
426 (<ucw:a :action (call-view ((car i) (action-view slot) (ucw::parent slot)))
427 (<:as-html "(view) "))
428 (<ucw:a :action (delete-relationship slot (second i) instance)
429 (<:as-html "(remove) "))
430 (present-view ((car i) (list-view slot) (ucw::parent slot)))) ))))
431
432
433 (defaction add-to-many-to-many ((slot many-to-many-slot-presentation) instance &optional foreign-instance)
434 ;;;; First things first, we sync.
435 (sync-instance instance)
436 (let* (new
437 (imd (getf (meta-model::find-slot-metadata instance (slot-name slot))
438 :db-info))
439 (jc (make-instance (getf imd :join-class)))
440 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
441 :db-info))
442 (fc (make-instance (getf jcmd :join-class)))
443 (c (if
444 foreign-instance
445 foreign-instance
446 (call-view (fc :creator (ucw::parent slot))))))
447 (when c
448 (sync-instance c)
449 ; (error "~A ~A ~A" (getf imd :foreign-key) (getf jcmd :foreign-key) (getf imd :home-key))
450 (setf (slot-value jc (getf imd :foreign-key))
451 (slot-value instance (getf imd :home-key)))
452 (setf (slot-value jc (getf jcmd :home-key))
453 (slot-value c (getf jcmd :foreign-key)))
454 (sync-instance jc)
455
456 (sync-instance instance)
457 c)))