fixes, enhancements, and more update-to-latest-ucw stuff. minor, all of it.
[clinton/lisp-on-lines.git] / src / slot-presentations.lisp
1 ;; i know this is horrible, but it works wonders.
2 (declaim (optimize (speed 0) (space 3) (safety 0)))
3
4
5 (in-package :lisp-on-lines)
6
7
8 ;;;; I dont think i'm using these anymore.
9 (defun multiple-value-funcall->list (function &rest args)
10 "The function to be called by m-v-bf"
11 (multiple-value-call #'list (apply function args)))
12
13 (defmacro multiple-value-bindf (vars form &body body)
14 "Like M-V-B, only it works in actions. form must be a function call"
15 `(destructuring-bind ,vars
16 (multiple-value-funcall->list #',(car form) ,@(cdr form))
17 ,@body))
18
19
20 ;;;; ** Textarea Slot Presentation
21
22 (defslot-presentation text-slot-presentation ()
23 ((rows :initarg :rows :accessor rows :initform 5)
24 (columns :initarg :columns :accessor columns :initform 40)
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))
27 (:type-name text))
28
29 (defmethod present-slot ((slot text-slot-presentation) instance)
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
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))
50 (when (presentation-slot-value slot instance)
51 (maybe-convert-newline-and-escape-html-then-print)))))
52
53
54 (defcomponent mewa-slot-presentation ()
55 ((validate-functions :accessor validate-functions :initform (list (constantly t)))
56 (slot-name :accessor slot-name
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.
65 When T, only the default value for primary keys and the joins are updated.")
66 (show-label-p :accessor show-label-p :initarg :show-label-p :initform t)
67 (creatablep :accessor creatablep :initarg :creatablep :initform t))
68 (:documentation "The superclass of all Mewa slot presentations"))
69
70
71
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)
94
95 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
96 ((input-id :accessor input-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+))
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))
99 (:type-name clsql-sys:wall-time))
100
101 (defmethod lol::presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
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
106 (defmethod (setf lol::presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
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)
111 (when (and new-time old-time)
112 (equal :equal (clsql:time-compare new-time old-time))))
113 (setf (presentation-slot-value slot instance) new-time))))
114
115 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
116 (concatenate 'string (call-next-method) " (m/d/y)"))
117
118 (defmethod lol::present-slot ((slot clsql-wall-time-slot-presentation) instance)
119 (let ((date (lol::presentation-slot-value slot instance)))
120 ;; Default values
121 (when (and (not date) (default-to-now-p slot))
122 (setf (lol::presentation-slot-value slot instance) (clsql:get-time)))
123 ;;simple viewer
124 (if (and date (not (editablep slot)))
125 (<:as-html date))
126 ;; editor
127 (when (editablep slot)
128 (<ucw:input :accessor (lol::presentation-slot-value slot instance) :id (input-id slot) :style "display:inline")
129 (<:button :id (trigger-id slot) (<:as-html "[...]"))
130 (<:script :type "text/javascript"
131 (<:as-is (format nil "
132
133 Calendar.setup({
134 inputField : \"~a\",
135 button : \"~a\",
136 ifFormat : \"%m/%d/%Y\" });" (input-id slot) (trigger-id slot)))))))
137
138 (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
139 ((foreign-instance :accessor foreign-instance)
140 (linkedp :accessor linkedp :initarg :linkedp :initform t)
141 (creator :accessor creator :initarg :creator :initform :editor)
142 (new-instance :accessor new-instance :initform nil))
143 (:type-name relation))
144
145 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
146 (multiple-value-bindf (finstance foreign-slot-name)
147 (meta-model:explode-foreign-key instance (slot-name slot))
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
171 (defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
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
176 (ucw::parent slot)
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)
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)))))
192
193
194 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
195 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
196 (let* ((i (foreign-instance slot))
197 (pres (mewa::make-presentation
198 i
199 :type :one-line
200 :initargs (list
201 :global-properties
202 (list :editablep nil :linkedp nil)))))
203 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
204 (setf (component.place pres) (component.place (ucw::parent slot))))
205 (when i (<ucw:render-component :component pres))))
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)
216 (call-component (ucw::parent self) (apply #'mewa:make-presentation instance initargs))
217 ;; the viewed instance could have been changed/deleted, so we sync this instance
218 (meta-model:sync-instance (instance (ucw::parent self))))
219
220
221 (defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
222 (setf (foreign-instance slot)
223 (when (lol::presentation-slot-value slot instance)
224 (meta-model:explode-foreign-key instance (slot-name slot))))
225 (flet ((render () (when (foreign-instance slot)(call-next-method))))
226 (if (slot-boundp slot 'ucw::place)
227 (cond
228 ((editablep slot)
229 (render)
230 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
231 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
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))))
239
240
241 (defmethod find-foreign-instances ((slot foreign-key-slot-presentation))
242 (clsql:select (class-name (class-of (meta-model:explode-foreign-key (instance slot) (slot-name slot))))))
243
244
245
246 ;;;; HAS MANY
247 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
248 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
249 (:type-name has-many))
250
251 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
252 ;; if the instance is not stored we must make sure to mark it stored now!
253 (unless (meta-model::persistentp instance)
254 (setf (mewa::modifiedp (ucw::parent self)) t))
255 ;; sync up the instance
256 ;;(mewa:ensure-instance-sync (parent slot))
257 (meta-model:sync-instance (instance (ucw::parent slot)))
258
259 (multiple-value-bindf (class home foreign)
260 (meta-model:explode-has-many instance (slot-name slot))
261 (let ((new (make-instance class)))
262 (setf (slot-value new foreign) (slot-value instance home))
263 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
264 (call-component (ucw::parent slot) (mewa:make-presentation new :type (creator slot)))
265 (meta-model:sync-instance instance))))
266
267 (defmethod present-slot ((slot has-many-slot-presentation) instance)
268 (when (slot-boundp slot 'ucw::place)
269 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
270 (let* ((i (get-foreign-instances slot instance))
271 (presentation (and i (make-presentation (car i) :type :one-line))))
272 (when i
273 (flet ((linker (i string)
274 (<ucw:a
275 :action (view-instance slot i
276 :initargs
277 `(:global-properties ,
278 (list
279 :linkedp t
280 :editablep nil)))
281 (<:as-html string))))
282 (<:table :cellpadding 10
283 (<:tr
284 (<:th) ;empty col for (view) link
285 (dolist (s (slots presentation))
286 (<:th (<:as-html (label s)))))
287 (dolist (s i)
288 (let ((s s))
289 (setf (foreign-instance slot) s)
290 (when (slot-boundp slot 'ucw::place)
291 (<:tr
292 (<:td (linker s " (view) "))
293 (dolist (p (slots (make-presentation s :type :one-line
294 :initargs
295 '(:global-properties
296 (:editablep nil)))))
297 (<:td
298
299 (present-slot p s))))))))))))
300
301
302 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
303 (sort (slot-value instance (slot-name slot)) #'<
304 :key #'(lambda (x) (funcall (car (list-keys instance)) x))))
305
306 (defmethod lol::presentation-slot-value ((slot has-many-slot-presentation) instance)
307 (get-foreign-instances slot instance))
308
309 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
310 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
311 (current :accessor current :initform 0)
312 (len :accessor len )
313 (instances :accessor instances))
314 (:type-name has-very-many))
315
316 (defmethod list-next ((slot has-very-many-slot-presentation))
317 (setf (current slot) (incf (current slot) (number-to-display slot)))
318 (when (< (len slot) (current slot))
319 (setf (current slot) (- (number-to-display slot) (len slot)))))
320
321 (defmethod list-prev ((slot has-very-many-slot-presentation))
322 (setf (current slot) (decf (current slot) (number-to-display slot)))
323 (when (> 0 (current slot))
324 ;;what to do here is open to debate
325 (setf (current slot) (- (len slot)(number-to-display slot) ))))
326
327
328 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
329 ;;(<:as-html "isance: " instance)
330 (if (slot-boundp slot 'ucw::place)
331 (progn
332 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
333 (let ((self (ucw::parent slot)))
334 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
335 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
336 (<ucw:a :action (list-next slot) (<:as-html ">>"))
337 (call-next-method)
338 (<:as-html "total :" (len slot)))
339 (call-next-method)))
340
341 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
342 (let ((f (call-next-method)))
343 (setf (len slot) (length f))
344 (setf (instances slot) f)
345 (loop for cons on (nthcdr (current slot) f)
346 for i from 0 upto (number-to-display slot)
347 collect (car cons))))
348
349
350 ;;;; * Has-a
351 (defslot-presentation has-a-slot-presentation (mewa-relation-slot-presentation)
352 ((allow-nil-p :accessor allow-nil-p :initarg :allow-nil-p :initform t)
353 (attributes :accessor attributes :initarg :attributes :initform nil))
354 (:type-name has-a))
355
356 (defmethod find-foreign-slot-value ((slot has-a-slot-presentation) (object t))
357 (multiple-value-bind (c s)
358 (meta-model:explode-foreign-key (instance (ucw::parent slot)) (slot-name slot))
359 (slot-value object s)))
360
361 (defmethod get-foreign-instances ((slot mewa-relation-slot-presentation) instance)
362 (clsql:select (class-name (class-of
363 (meta-model:explode-foreign-key instance (slot-name slot))))
364 :flatp t))
365
366 (defmethod present-slot ((slot has-a-slot-presentation) instance)
367 ; (<:as-html (lol::presentation-slot-value slot instance))
368 (if (editablep slot)
369 (progn (<ucw:select :accessor (lol::presentation-slot-value slot instance) :test #'equalp
370 (when (allow-nil-p slot)
371 (<ucw:option :value nil (<:as-html "none")))
372 (dolist (option (get-foreign-instances slot instance))
373 (<ucw:option :value (find-foreign-slot-value slot option)
374 (lol::present
375 (lol::make-presentation option
376 :type :as-string
377 :initargs
378 `(:attributes ,(attributes slot)))
379 ))))
380 (when (creatablep slot)
381 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline")))
382 (if (lol::presentation-slot-value slot instance)
383 (progn
384 (lol::present
385 (lol:make-presentation (meta-model:explode-foreign-key instance (slot-name slot))
386 :type :one-line
387 :initargs
388 `(:attributes ,(attributes slot)))
389 ))
390 (<:as-html "--"))))
391
392 (defslot-presentation many-to-many-slot-presentation (mewa-relation-slot-presentation)
393 ((list-view :accessor list-view :initarg :list-view :initform :one-line)
394 (action-view :accessor action-view :initarg :action-view :initform :viewer)
395 (create-view :initform :creator)
396 (select-view :initform :as-string :accessor select-view))
397 (:type-name many-to-many)
398 (:default-initargs :label "many to many"))
399
400 (defun %delete-item (item)
401 (clsql:with-default-database (clsql:*default-database*)
402 (ignore-errors
403 (clsql:delete-instance-records item))))
404
405 (defaction delete-item ((self component) instance)
406 (multiple-value-bind (res err) (%delete-item instance)
407 (if (not err)
408 (call 'info-message :message "Removed Instance")
409 (call 'info-message :message (format nil "Could not remove item. Try removing associated items first. ~A" instance)))))
410
411 (defaction delete-relationship ((slot many-to-many-slot-presentation) rel instance)
412 (delete-item (ucw::parent self) rel)
413 (sync-instance instance)
414 (answer-component (ucw::parent self) t))
415
416
417 (defun find-many-to-many-join-class (slot instance)
418 (let* ((imd (getf (meta-model::find-slot-metadata instance (slot-name slot))
419 :db-info))
420 (jc (make-instance (getf imd :join-class)))
421 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
422 :db-info)))
423 (getf jcmd :join-class)))
424
425 (defmethod find-all-instances ((slot many-to-many-slot-presentation) instance)
426 (clsql:select (find-many-to-many-join-class slot instance) :flatp t))
427
428 (defmethod present-slot ((slot many-to-many-slot-presentation) instance)
429 (let ((instances (slot-value instance (slot-name slot)))
430 new-instance)
431 (<:ul
432 (<:li (<ucw:button :action (add-to-many-to-many slot instance)
433 (<:as-html "Add New")))
434 (<:li (<ucw:button :action (add-to-many-to-many slot instance new-instance)
435 (<:as-html "Add:"))
436 (<ucw:select :accessor new-instance
437 (arnesi:dolist* (i (find-all-instances slot instance))
438 (<ucw:option
439 :value i
440 (lol:present-view (i (select-view slot) slot))))))
441 (dolist* (i instances)
442 (<:li
443 (<ucw:a :action (call-view ((car i) (action-view slot) (ucw::parent slot)))
444 (<:as-html "(view) "))
445 (<ucw:a :action (delete-relationship slot (second i) instance)
446 (<:as-html "(remove) "))
447 (present-view ((car i) (list-view slot) (ucw::parent slot)))) ))))
448
449
450 (defaction add-to-many-to-many ((slot many-to-many-slot-presentation) instance &optional foreign-instance)
451 ;;;; First things first, we sync.
452 (sync-instance instance)
453 (let* (new
454 (imd (getf (meta-model::find-slot-metadata instance (slot-name slot))
455 :db-info))
456 (jc (make-instance (getf imd :join-class)))
457 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
458 :db-info))
459 (fc (make-instance (getf jcmd :join-class)))
460 (c (if
461 foreign-instance
462 foreign-instance
463 (call-view (fc :creator (ucw::parent slot))))))
464 (when c
465 (sync-instance c)
466 ; (error "~A ~A ~A" (getf imd :foreign-key) (getf jcmd :foreign-key) (getf imd :home-key))
467 (setf (slot-value jc (getf imd :foreign-key))
468 (slot-value instance (getf imd :home-key)))
469 (setf (slot-value jc (getf jcmd :home-key))
470 (slot-value c (getf jcmd :foreign-key)))
471 (sync-instance jc)
472
473 (sync-instance instance)
474 c)))