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