1 (in-package :it.bese.ucw
)
3 (defun multiple-value-funcall->list
(function &rest args
)
4 (multiple-value-call #'list
(apply function args
)))
6 (defmacro multiple-value-bindf
(vars form
&body body
)
7 `(destructuring-bind ,vars
8 (multiple-value-funcall->list
#',(car form
) ,@(cdr form
))
11 (defcomponent mewa-slot-presentation
()
12 ((slot-name :accessor slot-name
15 "The name of the slot being accessed")
16 (fill-gaps-only-p :accessor fill-gaps-only-p
17 :initarg
:fill-gaps-only-p
20 "When nil, the instance is syncronised with the database.
21 When T, only the default value for primary keys and the joins are updated."))
22 (:documentation
"The superclass of all Mewa slot presentations"))
24 ;;;; this has to be in the eval when i would think
25 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
26 (defun generate-slot-presentation-definition-for-type (type)
27 (let* ((u-name (intern (format nil
"~A-SLOT-PRESENTATION" type
)))
28 (sp-name (intern (format nil
"MEWA-~A" u-name
)))
29 (t-name (intern (format nil
"MEWA-~A" type
))))
30 `(defslot-presentation ,sp-name
(,u-name mewa-slot-presentation
)
32 (:type-name
,t-name
)))))
34 (defmacro define-base-mewa-presentations
(&body types
)
35 "Define the mewa-slot-presentations by subclassing the base UCW ones"
36 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
39 ;;;then actually define the base presentations :
40 (define-base-mewa-presentations
48 (defslot-presentation clsql-wall-time-slot-presentation
(mewa-relation-slot-presentation)
50 (:type-name clsql-sys
:wall-time
))
52 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation
) instance
)
53 (let ((date (call-next-method)))
54 (when date
(multiple-value-bind (y m d
) (clsql:time-ymd date
)
55 (format nil
"~a/~a/~a" m d y
)))))
57 (defmethod (setf presentation-slot-value
) ((value string
) (slot clsql-wall-time-slot-presentation
) instance
)
58 (let ((new-time (clsql:parse-date-time
(remove #\Space value
)))
59 (old-time (when (slot-boundp instance
(slot-name slot
))
60 (slot-value instance
(slot-name slot
)))))
61 (unless (or (eql old-time new-time
)
62 (when (and new-time old-time
)
63 (equal :equal
(clsql:time-compare new-time old-time
))))
64 (setf (presentation-slot-value slot instance
) new-time
))))
66 (defmethod label :around
((slot clsql-wall-time-slot-presentation
))
67 (concatenate 'string
(call-next-method) " (mm/dd/yyyy)"))
69 (defmethod present-slot ((slot clsql-wall-time-slot-presentation
) instance
)
70 (let ((date (presentation-slot-value slot instance
))
71 (input-id (string (gensym))))
72 (if (and date
(not (editablep slot
)))
73 (<:span
(<:as-html date
)))
74 (when (editablep slot
)
75 (<ucw
:input
:accessor
(presentation-slot-value slot instance
) :id input-id
)
76 (<:script
:type
"text/javascript"
77 (<:as-is
(format nil
"
80 ifFormat : \"%m/%d/%Y\",
83 (defslot-presentation mewa-relation-slot-presentation
(mewa-slot-presentation slot-presentation
)
84 ((foreign-instance :accessor foreign-instance
)
85 (linkedp :accessor linkedp
:initarg
:linkedp
:initform t
))
86 (:type-name relation
))
88 (defun get-fkey-data (instance slot-name
)
89 "ugly workaround b/c UCW does not like M-V-B"
90 (multiple-value-bind (finstance foreign-slot-name
)
91 (meta-model:explode-foreign-key instance slot-name
)
92 (cons finstance foreign-slot-name
)))
94 (defaction search-records
((slot mewa-relation-slot-presentation
) instance
)
95 (multiple-value-bindf (finstance foreign-slot-name
)
96 (meta-model:explode-foreign-key instance
(slot-name slot
))
100 (make-instance (or (cadr (mewa:find-attribute finstance
:presentation-search
))
101 'mewa
::mewa-presentation-search
)
103 (mewa:make-presentation finstance
104 :type
:search-presentation
)
106 (mewa:make-presentation finstance
108 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
109 (meta-model:sync-instance instance
:fill-gaps-only t
))))
111 (defaction create-record
((slot mewa-relation-slot-presentation
) instance
)
112 (multiple-value-bindf (finstance foreign-slot-name
)
113 (meta-model:explode-foreign-key instance
(slot-name slot
))
117 (mewa:make-presentation finstance
:type
:editor
))))
118 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
119 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
121 (defmethod present-relation ((slot mewa-relation-slot-presentation
) instance
)
122 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
123 (let* ((i (foreign-instance slot
))
124 (pres (mewa::make-presentation
129 (list :editablep nil
:linkedp nil
)))))
130 (when (and (ucw::parent slot
) (slot-boundp slot
'ucw
::place
))
131 (setf (component.place pres
) (component.place
(ucw::parent slot
))))
132 (when i
(<ucw
:render-component
:component pres
))))
136 (defmethod present-slot ((slot mewa-relation-slot-presentation
) instance
)
137 (present-relation slot instance
))
139 (defslot-presentation foreign-key-slot-presentation
(mewa-relation-slot-presentation)
141 (:type-name foreign-key
)
144 (defaction view-instance
((self component
) instance
&rest initargs
)
145 (call-component (parent self
) (apply #'mewa
:make-presentation instance initargs
))
146 ;; the viewed instance could have been changed/deleted, so we sync this instance
147 (meta-model:sync-instance
(instance (parent self
))))
150 (defmethod present-slot :around
((slot foreign-key-slot-presentation
) instance
)
151 (setf (foreign-instance slot
) (when (presentation-slot-value slot instance
) (meta-model:explode-foreign-key instance
(slot-name slot
))))
152 (flet ((render () (call-next-method)))
156 (<ucw
:submit
:action
(search-records slot instance
) :value
"Search" :style
"display:inline")
157 (<ucw
:submit
:action
(create-record slot instance
) :value
"Add New" :style
"display:inline"))
159 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
165 (defslot-presentation has-many-slot-presentation
(mewa-relation-slot-presentation)
166 ((add-new-label :accessor add-new-label
:initarg
:add-new-label
:initform
"Add New"))
167 (:type-name has-many
))
170 (defaction add-to-has-many
((slot has-many-slot-presentation
) instance
)
171 (mewa:ensure-instance-sync
(parent slot
))
172 (multiple-value-bindf (class home foreign
)
173 (meta-model:explode-has-many instance
(slot-name slot
))
174 (let ((new (make-instance class
)))
175 (setf (slot-value new foreign
) (slot-value instance home
))
176 (meta-model:sync-instance new
:fill-gaps-only-p
(fill-gaps-only-p self
))
177 (call-component (parent slot
) (mewa:make-presentation new
:type
:editor
))
178 (meta-model:sync-instance instance
))))
180 (defmethod present-slot ((slot has-many-slot-presentation
) instance
)
181 (<ucw
:submit
:action
(add-to-has-many slot instance
) :value
(add-new-label slot
))
182 (let ((i (get-foreign-instances slot instance
))
183 (linkedp (linkedp slot
)))
187 (setf (foreign-instance slot
) s
)
188 (when (slot-boundp slot
'place
)
189 (<ucw
:a
:action
(view-instance slot s
:initargs
`(:global-properties
,(list :linkedp t
:editablep nil
)))
190 (<:li
(setf (linkedp slot
) nil
)
191 (present-relation slot instance
)))))))))
194 (defmethod get-foreign-instances ((slot has-many-slot-presentation
) instance
)
195 (slot-value instance
(slot-name slot
)))
197 (defmethod presentation-slot-value ((slot has-many-slot-presentation
) instance
)
198 (get-foreign-instances slot instance
))
200 (defslot-presentation has-very-many-slot-presentation
(has-many-slot-presentation)
201 ((number-to-display :accessor number-to-display
:initarg
:number-to-display
:initform
10)
202 (current :accessor current
:initform
0)
204 (instances :accessor instances
))
206 (:type-name has-very-many
))
208 (defmethod list-next ((slot has-very-many-slot-presentation
))
209 (setf (current slot
) (incf (current slot
) (number-to-display slot
)))
210 (when (< (len slot
) (current slot
))
211 (setf (current slot
) (- (number-to-display slot
) (len slot
)))))
213 (defmethod list-prev ((slot has-very-many-slot-presentation
))
214 (setf (current slot
) (decf (current slot
) (number-to-display slot
)))
215 (when (> 0 (current slot
))
216 ;;what to do here is open to debate
217 (setf (current slot
) (- (len slot
)(number-to-display slot
) ))))
220 (defmethod present-slot ((slot has-very-many-slot-presentation
) instance
)
221 ;;(<:as-html "isance: " instance)
222 (<ucw
:a
:action
(list-prev slot
) (<:as-html
"<<"))
223 (let ((self (parent slot
)))
224 (<ucw
:a
:action
(call-component self
(mewa:make-presentation
(car (slot-value instance
(slot-name slot
))) :type
:listing
:initargs
(list :instances
(instances slot
))))
225 (<:as-html
(label slot
) (format nil
" ~a-~a " (current slot
) (+ (current slot
) (number-to-display slot
))))))
226 (<ucw
:a
:action
(list-next slot
) (<:as-html
">>"))
228 (<:as-html
"total :" (len slot
)))
230 (defmethod get-foreign-instances :around
((slot has-very-many-slot-presentation
) instance
)
231 (let ((f (call-next-method)))
232 (setf (len slot
) (length f
))
233 (setf (instances slot
) f
)
234 (loop for cons on
(nthcdr (current slot
) f
)
235 for i from
0 upto
(number-to-display slot
)
236 collect
(car cons
))))
238 (defslot-presentation has-a-slot-presentation
(one-of-presentation)
239 ((key :initarg
:key
:accessor key
))
242 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation
) (object t
) (slot-name t
))
243 (slot-value object slot-name
))
245 (defmethod present-slot ((slot has-a-slot-presentation
) instance
)
246 (<:as-html
(presentation-slot-value slot instance
))
248 (<ucw
:select
:accessor
(presentation-slot-value slot instance
) :test
#'equalp
249 (when (allow-nil-p slot
)
250 (<ucw
:option
:value nil
(<:as-html
(none-label slot
))))
251 (dolist (option (get-foreign-instances (presentation slot
) instance
))
252 (setf (instance (presentation slot
)) option
)
253 (<ucw
:option
:value
(get-foreign-slot-value slot option
(key slot
)) (present (presentation slot
)))))
254 (if (presentation-slot-value slot instance
)
256 (setf (instance (presentation slot
)) (presentation-slot-value slot instance
))
257 (present (presentation slot
)))