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