removed errant paren
[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 (: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)
71
72 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
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+)))
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)
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)
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 ))))
90
91 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
92 (concatenate 'string (call-next-method) " (m/d/y)"))
93
94 (defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
95 (let ((date (presentation-slot-value slot instance)))
96 (if (and date (not (editablep slot)))
97 (<:as-html date))
98 (when (editablep slot)
99 (<ucw:input :accessor (presentation-slot-value slot instance) :id (input-id slot) :style "display:inline")
100 (<:button :id (trigger-id slot) (<:as-html "[...]"))
101 (<:script :type "text/javascript"
102 (<:as-is (format nil "
103
104 Calendar.setup({
105 inputField : \"~a\",
106 button : \"~a\",
107 ifFormat : \"%m/%d/%Y\" });" (input-id slot) (trigger-id slot)))))))
108
109 (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
110 ((foreign-instance :accessor foreign-instance)
111 (linkedp :accessor linkedp :initarg :linkedp :initform t)
112 (creator :accessor creator :initarg :creator :initform :editor)
113 (new-instance :accessor new-instance :initform nil))
114 (:type-name relation))
115
116 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
117 (multiple-value-bindf (finstance foreign-slot-name)
118 (meta-model:explode-foreign-key instance (slot-name slot))
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
142 (defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
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
147 (ucw::parent slot)
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)
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)))))
163
164
165 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
166 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
167 (let* ((i (foreign-instance slot))
168 (pres (mewa::make-presentation
169 i
170 :type :one-line
171 :initargs (list
172 :global-properties
173 (list :editablep nil :linkedp nil)))))
174 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
175 (setf (component.place pres) (component.place (ucw::parent slot))))
176 (when i (<ucw:render-component :component pres))))
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)
187 (call-component (ucw::parent self) (apply #'mewa:make-presentation instance initargs))
188 ;; the viewed instance could have been changed/deleted, so we sync this instance
189 (meta-model:sync-instance (instance (ucw::parent self))))
190
191
192 (defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
193 (setf (foreign-instance slot)
194 (when (presentation-slot-value slot instance)
195 (meta-model:explode-foreign-key instance (slot-name slot))))
196 (flet ((render () (when (foreign-instance slot)(call-next-method))))
197 (if (slot-boundp slot 'ucw::place)
198 (cond
199 ((editablep slot)
200 (render)
201 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
202 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
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))))
210
211
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))))))
214
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)
221
222 ;;;; HAS MANY
223 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
224 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
225 (:type-name has-many))
226
227 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
228 ;; if the instance is not stored we must make sure to mark it stored now!
229 (unless (meta-model::persistentp instance)
230 (setf (mewa::modifiedp (ucw::parent self)) t))
231 ;; sync up the instance
232 ;;(mewa:ensure-instance-sync (parent slot))
233 (meta-model:sync-instance (instance (ucw::parent slot)))
234
235 (multiple-value-bindf (class home foreign)
236 (meta-model:explode-has-many instance (slot-name slot))
237 (let ((new (make-instance class)))
238 (setf (slot-value new foreign) (slot-value instance home))
239 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
240 (call-component (ucw::parent slot) (mewa:make-presentation new :type (creator slot)))
241 (meta-model:sync-instance instance))))
242
243 (defmethod present-slot ((slot has-many-slot-presentation) instance)
244 (when (slot-boundp slot 'ucw::place)
245 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
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
277
278 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
279 (slot-value instance (slot-name slot)))
280
281 (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
282 (get-foreign-instances slot instance))
283
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))
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)
305 (if (slot-boundp slot 'ucw::place)
306 (progn
307 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
308 (let ((self (ucw::parent slot)))
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)))
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)))
344 (<:as-html "--"))))
345
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)
349 (create-view :initform :creator)
350 (select-view :initform :as-string :accessor select-view))
351 (:type-name many-to-many)
352 (:default-initargs :label "many to many"))
353
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)
387 (<:ul
388 (<:li (<ucw:a :action (add-on-many-to-many slot instance)
389 (<:as-html "Add New")))
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))))))
397 (dolist* (i instances)
398 (<:li
399 (<ucw:a :action (call-view ((car i) (action-view slot) (ucw::parent slot)))
400 (<:as-html "(view) "))
401 (<ucw:a :action (delete-relationship slot (second i) instance)
402 (<:as-html "(remove) "))
403 (present-view ((car i) (list-view slot) (ucw::parent slot)))) ))))
404
405 (defaction add-to-many-to-many ((slot many-to-many-slot-presentation) instance &optional foreign-instance)
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)))
413 (c (if
414 foreign-instance
415 foreign-instance
416 (call-view (fc :creator (ucw::parent slot))))))
417 (when c
418 (sync-instance c)
419 ; (error "~A ~A ~A" (getf imd :foreign-key) (getf jcmd :foreign-key) (getf imd :home-key))
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)
425
426 (sync-instance instance)
427 c)))
428
429
430
431
432
433
434
435