1 (in-package :lisp-on-lines
)
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
)))
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
))
14 ;;;; ** Textarea Slot Presentation
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
))
22 (defmethod present-slot ((slot text-slot-presentation
) instance
)
25 :accessor
(presentation-slot-value slot instance
)
26 :reader
(or (presentation-slot-value slot instance
)
30 (if (escape-html-p slot
)
31 (<:as-html
(presentation-slot-value slot instance
))
32 (<:as-is
(presentation-slot-value slot instance
)))))
35 (defcomponent mewa-slot-presentation
()
36 ((slot-name :accessor slot-name
39 "The name of the slot being accessed")
40 (fill-gaps-only-p :accessor fill-gaps-only-p
41 :initarg
:fill-gaps-only-p
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"))
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
)
57 (:type-name
,t-name
)))))
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
64 ;;;then actually define the base presentations :
65 (define-base-mewa-presentations
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
))
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
)))))
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
))))
91 (defmethod label :around
((slot clsql-wall-time-slot-presentation
))
92 (concatenate 'string
(call-next-method) " (m/d/y)"))
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
)))
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
"
107 ifFormat : \"%m/%d/%Y\" });" (input-id slot
) (trigger-id slot
)))))))
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
))
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
)))
121 (setf (new-instance self
)
124 (make-instance (or (cadr (mewa:find-attribute finstance
:presentation-search
))
125 'mewa
::mewa-presentation-search
)
127 (mewa:make-presentation finstance
128 :type
:search-presentation
)
130 (mewa:make-presentation finstance
132 (sync-foreign-instance slot new-instance
))))
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
)))))
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
))
148 (mewa:make-presentation finstance
:type
(creator self
)))))
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.
155 (when (typep new-instance
'mewa
::mewa
)
156 (setf new-instance
(instance new-instance
)))
158 ;;;; sorry about that, and now back t our regular program.
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
)))))
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
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
))))
178 (defmethod present-slot ((slot mewa-relation-slot-presentation
) instance
)
179 (present-relation slot instance
))
181 (defslot-presentation foreign-key-slot-presentation
(mewa-relation-slot-presentation)
183 (:type-name foreign-key
)
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
))))
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
)
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"))
204 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
208 ;; presentation is used only for rendering
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
))))))
218 (defslot-presentation has-many-slot-presentation
(mewa-relation-slot-presentation)
219 ((add-new-label :accessor add-new-label
:initarg
:add-new-label
:initform
"Add New"))
220 (:type-name has-many
))
222 (defaction add-to-has-many
((slot has-many-slot-presentation
) instance
)
223 ;; if the instance is not stored we must make sure to mark it stored now!
224 (unless (meta-model::persistentp instance
)
225 (setf (mewa::modifiedp
(ucw::parent self
)) t
))
226 ;; sync up the instance
227 ;;(mewa:ensure-instance-sync (parent slot))
228 (meta-model:sync-instance
(instance (ucw::parent slot
)))
230 (multiple-value-bindf (class home foreign
)
231 (meta-model:explode-has-many instance
(slot-name slot
))
232 (let ((new (make-instance class
)))
233 (setf (slot-value new foreign
) (slot-value instance home
))
234 (meta-model:sync-instance new
:fill-gaps-only-p
(fill-gaps-only-p self
))
235 (call-component (ucw::parent slot
) (mewa:make-presentation new
:type
(creator slot
)))
236 (meta-model:sync-instance instance
))))
238 (defmethod present-slot ((slot has-many-slot-presentation
) instance
)
239 (when (slot-boundp slot
'ucw
::place
)
240 (<ucw
:submit
:action
(add-to-has-many slot instance
) :value
(add-new-label slot
)))
241 (let* ((i (get-foreign-instances slot instance
))
242 (presentation (and i
(make-presentation (car i
) :type
:one-line
))))
244 (flet ((linker (i string
)
246 :action
(view-instance slot i
248 `(:global-properties
,
252 (<:as-html string
))))
255 (<:th
) ;empty col for (view) link
256 (dolist (s (slots presentation
))
257 (<:th
(<:as-html
(label s
)))))
260 (setf (foreign-instance slot
) s
)
261 (when (slot-boundp slot
'ucw
::place
)
263 (<:td
(linker s
" (view) "))
264 (dolist (p (slots (make-presentation s
:type
:one-line
270 (present-slot p s
))))))))))))
273 (defmethod get-foreign-instances ((slot has-many-slot-presentation
) instance
)
274 (slot-value instance
(slot-name slot
)))
276 (defmethod presentation-slot-value ((slot has-many-slot-presentation
) instance
)
277 (get-foreign-instances slot instance
))
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)
283 (instances :accessor instances
))
284 (:type-name has-very-many
))
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
)))))
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
) ))))
298 (defmethod present-slot ((slot has-very-many-slot-presentation
) instance
)
299 ;;(<:as-html "isance: " instance)
300 (if (slot-boundp slot
'ucw
::place
)
302 (<ucw
:a
:action
(list-prev slot
) (<:as-html
"<<"))
303 (let ((self (ucw::parent slot
)))
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
">>"))
308 (<:as-html
"total :" (len slot
)))
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
))))
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
))
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
)))
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
))))
336 (defmethod present-slot ((slot has-a-slot-presentation
) instance
)
337 ; (<:as-html (presentation-slot-value slot instance))
339 (progn (<ucw
:select
:accessor
(presentation-slot-value slot instance
) :test
#'equalp
340 (when (allow-nil-p slot
)
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
)
345 (lol:make-presentation option
348 `(:attributes
,(attributes slot
)))
350 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline"))
351 (if (presentation-slot-value slot instance
)
354 (lol:make-presentation
(meta-model:explode-foreign-key instance
(slot-name slot
))
357 `(:attributes
,(attributes slot
)))
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
)
364 (create-view :initform
:creator
)
365 (select-view :initform
:as-string
:accessor select-view
))
366 (:type-name many-to-many
)
367 (:default-initargs
:label
"many to many"))
369 (defun %delete-item
(item)
370 (clsql:with-default-database
(clsql:*default-database
*)
372 (clsql:delete-instance-records item
))))
374 (defaction delete-item
((self component
) instance
)
375 (multiple-value-bind (res err
) (%delete-item instance
)
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
)))))
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
))
386 (defun find-many-to-many-join-class (slot instance
)
387 (let* ((imd (getf (meta-model::find-slot-metadata instance
(slot-name slot
))
389 (jc (make-instance (getf imd
:join-class
)))
390 (jcmd (getf (meta-model::find-slot-metadata jc
(getf imd
:target-slot
))
392 (getf jcmd
:join-class
)))
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
))
397 (defmethod present-slot ((slot many-to-many-slot-presentation
) instance
)
398 (let ((instances (slot-value instance
(slot-name slot
)))
401 (<:li
(<ucw
:a
:action
(add-on-many-to-many slot instance
)
402 (<:as-html
"Add New")))
403 (<:li
(<ucw
:button
:action
(add-to-many-to-many slot instance new-instance
)
405 (<ucw
:select
:accessor new-instance
406 (arnesi:dolist
* (i (find-all-instances slot instance
))
409 (lol:present-view
(i (select-view slot
) slot
))))))
410 (dolist* (i instances
)
412 (<ucw
:a
:action
(call-view ((car i
) (action-view slot
) (ucw::parent slot
)))
413 (<:as-html
"(view) "))
414 (<ucw
:a
:action
(delete-relationship slot
(second i
) instance
)
415 (<:as-html
"(remove) "))
416 (present-view ((car i
) (list-view slot
) (ucw::parent slot
)))) ))))
418 (defaction add-to-many-to-many
((slot many-to-many-slot-presentation
) instance
&optional foreign-instance
)
420 (imd (getf (meta-model::find-slot-metadata instance
(slot-name slot
))
422 (jc (make-instance (getf imd
:join-class
)))
423 (jcmd (getf (meta-model::find-slot-metadata jc
(getf imd
:target-slot
))
425 (fc (make-instance (getf jcmd
:join-class
)))
429 (call-view (fc :creator
(ucw::parent slot
))))))
432 ; (error "~A ~A ~A" (getf imd :foreign-key) (getf jcmd :foreign-key) (getf imd :home-key))
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
)))
439 (sync-instance instance
)