removed errant paren
[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
DC
215(defslot-presentation has-a-slot-presentation (foreign-key-slot-presentation)
216 ()
217 (:type-name has-a))
218
219(defmethod present-slot ((slot has-a-slot-presentation) instance)
220 t)
d2512426 221
579597e3 222;;;; HAS MANY
223(defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
12dcf3d4 224 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
579597e3 225 (:type-name has-many))
226
498061f6 227(defaction add-to-has-many ((slot has-many-slot-presentation) instance)
8e6e6b56 228 ;; if the instance is not stored we must make sure to mark it stored now!
5dea194e 229 (unless (meta-model::persistentp instance)
38a016c7 230 (setf (mewa::modifiedp (ucw::parent self)) t))
8e6e6b56 231 ;; sync up the instance
ec044146 232 ;;(mewa:ensure-instance-sync (parent slot))
38a016c7 233 (meta-model:sync-instance (instance (ucw::parent slot)))
ec044146 234
85281029
DC
235 (multiple-value-bindf (class home foreign)
236 (meta-model:explode-has-many instance (slot-name slot))
498061f6 237 (let ((new (make-instance class)))
238 (setf (slot-value new foreign) (slot-value instance home))
e9454185 239 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
38a016c7 240 (call-component (ucw::parent slot) (mewa:make-presentation new :type (creator slot)))
e9454185 241 (meta-model:sync-instance instance))))
498061f6 242
579597e3 243(defmethod present-slot ((slot has-many-slot-presentation) instance)
38a016c7 244 (when (slot-boundp slot 'ucw::place)
f1ce8b6e 245 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
d5e996b3
DC
246 (let* ((i (get-foreign-instances slot instance))
247 (presentation (and i (make-presentation (car i) :type :one-line))))
248 (when i
249 (flet ((linker (i string)
250 (<ucw:a
251 :action (view-instance slot i
252 :initargs
253 `(:global-properties ,
254 (list
255 :linkedp t
256 :editablep nil)))
257 (<:as-html string))))
258 (<:table
259 (<:tr
260 (<:th) ;empty col for (view) link
261 (dolist (s (slots presentation))
262 (<:th (<:as-html (label s)))))
263 (dolist (s i)
264 (let ((s s))
265 (setf (foreign-instance slot) s)
266 (when (slot-boundp slot 'ucw::place)
267 (<:tr
268 (<:td (linker s " (view) "))
269 (dolist (p (slots (make-presentation s :type :one-line
270 :initargs
271 '(:global-properties
272 (:editablep nil)))))
273 (<:td
274
275 (present-slot p s))))))))))))
276
579597e3 277
278(defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
279 (slot-value instance (slot-name slot)))
280
ec4f5786 281(defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
282 (get-foreign-instances slot instance))
283
579597e3 284(defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
285 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
286 (current :accessor current :initform 0)
287 (len :accessor len )
288 (instances :accessor instances))
579597e3 289 (:type-name has-very-many))
290
291(defmethod list-next ((slot has-very-many-slot-presentation))
292 (setf (current slot) (incf (current slot) (number-to-display slot)))
293 (when (< (len slot) (current slot))
294 (setf (current slot) (- (number-to-display slot) (len slot)))))
295
296(defmethod list-prev ((slot has-very-many-slot-presentation))
297 (setf (current slot) (decf (current slot) (number-to-display slot)))
298 (when (> 0 (current slot))
299 ;;what to do here is open to debate
300 (setf (current slot) (- (len slot)(number-to-display slot) ))))
301
302
303(defmethod present-slot ((slot has-very-many-slot-presentation) instance)
304 ;;(<:as-html "isance: " instance)
38a016c7 305 (if (slot-boundp slot 'ucw::place)
f1ce8b6e
DC
306 (progn
307 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
38a016c7 308 (let ((self (ucw::parent slot)))
f1ce8b6e
DC
309 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
310 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
311 (<ucw:a :action (list-next slot) (<:as-html ">>"))
312 (call-next-method)
313 (<:as-html "total :" (len slot)))
314 (call-next-method)))
579597e3 315
316(defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
317 (let ((f (call-next-method)))
318 (setf (len slot) (length f))
319 (setf (instances slot) f)
320 (loop for cons on (nthcdr (current slot) f)
321 for i from 0 upto (number-to-display slot)
322 collect (car cons))))
323
324(defslot-presentation has-a-slot-presentation (one-of-presentation)
325 ((key :initarg :key :accessor key))
326 (:type-name has-a))
327
328(defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
329 (slot-value object slot-name))
330
331(defmethod present-slot ((slot has-a-slot-presentation) instance)
332 (<:as-html (presentation-slot-value slot instance))
333 (if (editablep slot)
334 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
335 (when (allow-nil-p slot)
336 (<ucw:option :value nil (<:as-html (none-label slot))))
337 (dolist (option (get-foreign-instances (presentation slot) instance))
338 (setf (instance (presentation slot)) option)
339 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
340 (if (presentation-slot-value slot instance)
341 (progn
342 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
343 (present (presentation slot)))
85281029 344 (<:as-html "--"))))
5f0b37e7 345
5dea194e
DC
346(defslot-presentation many-to-many-slot-presentation (mewa-relation-slot-presentation)
347 ((list-view :accessor list-view :initarg :list-view :initform :one-line)
348 (action-view :accessor action-view :initarg :action-view :initform :viewer)
fce59b36
DC
349 (create-view :initform :creator)
350 (select-view :initform :as-string :accessor select-view))
5dea194e
DC
351 (:type-name many-to-many)
352 (:default-initargs :label "many to many"))
5f0b37e7 353
fce59b36
DC
354(defun %delete-item (item)
355 (clsql:with-default-database (clsql:*default-database*)
356 (ignore-errors
357 (clsql:delete-instance-records item))))
358
359(defaction delete-item ((self component) instance)
360 (multiple-value-bind (res err) (%delete-item instance)
361 (if (not err)
362 (call 'info-message :message "Removed Instance")
363 (call 'info-message :message (format nil "Could not remove item. Try removing associated items first. ~A" instance)))))
364
365(defaction delete-relationship ((slot many-to-many-slot-presentation) rel instance)
366 (delete-item (ucw::parent self) rel)
367 (sync-instance instance)
368 (answer-component (ucw::parent self) t))
369
370
371(defun find-many-to-many-join-class (slot instance)
372 (let* ((imd (getf (meta-model::find-slot-metadata instance (slot-name slot))
373 :db-info))
374 (jc (make-instance (getf imd :join-class)))
375 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
376 :db-info)))
377 (getf jcmd :join-class)))
378
379
380
381(defmethod find-all-instances ((slot many-to-many-slot-presentation) instance)
382 (clsql:select (find-many-to-many-join-class slot instance) :flatp t))
383
384(defmethod present-slot ((slot many-to-many-slot-presentation) instance)
385 (let ((instances (slot-value instance (slot-name slot)))
386 new-instance)
5dea194e
DC
387 (<:ul
388 (<:li (<ucw:a :action (add-on-many-to-many slot instance)
389 (<:as-html "Add New")))
fce59b36
DC
390 (<:li (<ucw:button :action (add-to-many-to-many slot instance new-instance)
391 (<:as-html "Add:"))
392 (<ucw:select :accessor new-instance
393 (arnesi:dolist* (i (find-all-instances slot instance))
394 (<ucw:option
395 :value i
396 (lol:present-view (i (select-view slot) slot))))))
5dea194e
DC
397 (dolist* (i instances)
398 (<:li
fce59b36 399 (<ucw:a :action (call-view ((car i) (action-view slot) (ucw::parent slot)))
5dea194e 400 (<:as-html "(view) "))
fce59b36 401 (<ucw:a :action (delete-relationship slot (second i) instance)
5dea194e 402 (<:as-html "(remove) "))
fce59b36 403 (present-view ((car i) (list-view slot) (ucw::parent slot)))) ))))
5dea194e 404
fce59b36 405(defaction add-to-many-to-many ((slot many-to-many-slot-presentation) instance &optional foreign-instance)
5dea194e
DC
406 (let* (new
407 (imd (getf (meta-model::find-slot-metadata instance (slot-name slot))
408 :db-info))
409 (jc (make-instance (getf imd :join-class)))
410 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
411 :db-info))
412 (fc (make-instance (getf jcmd :join-class)))
fce59b36
DC
413 (c (if
414 foreign-instance
415 foreign-instance
416 (call-view (fc :creator (ucw::parent slot))))))
5dea194e
DC
417 (when c
418 (sync-instance c)
fce59b36 419; (error "~A ~A ~A" (getf imd :foreign-key) (getf jcmd :foreign-key) (getf imd :home-key))
5dea194e
DC
420 (setf (slot-value jc (getf imd :foreign-key))
421 (slot-value instance (getf imd :home-key)))
422 (setf (slot-value jc (getf jcmd :home-key))
423 (slot-value c (getf jcmd :foreign-key)))
424 (sync-instance jc)
fce59b36 425
5dea194e
DC
426 (sync-instance instance)
427 c)))
428
429
430
5f0b37e7 431
2cb4247d
DC
432
433
434
435