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