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
25)
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
)
24 (<ucw
:textarea
:accessor
(presentation-slot-value slot instance
)
27 (if (escape-html-p slot
)
28 (<:as-html
(presentation-slot-value slot instance
))
29 (<:as-is
(presentation-slot-value slot instance
)))))
32 (defcomponent mewa-slot-presentation
()
33 ((slot-name :accessor slot-name
36 "The name of the slot being accessed")
37 (fill-gaps-only-p :accessor fill-gaps-only-p
38 :initarg
:fill-gaps-only-p
41 "When nil, the instance is syncronised with the database.
42 When T, only the default value for primary keys and the joins are updated.")
43 (show-label-p :accessor show-label-p
:initarg
:show-label-p
:initform t
))
44 (:documentation
"The superclass of all Mewa slot presentations"))
46 ;;;; this has to be in the eval when i would think
47 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
48 (defun generate-slot-presentation-definition-for-type (type)
49 (let* ((u-name (intern (format nil
"~A-SLOT-PRESENTATION" type
)))
50 (sp-name (intern (format nil
"MEWA-~A" u-name
)))
51 (t-name (intern (format nil
"MEWA-~A" type
))))
52 `(defslot-presentation ,sp-name
(,u-name mewa-slot-presentation
)
54 (:type-name
,t-name
)))))
56 (defmacro define-base-mewa-presentations
(&body types
)
57 "Define the mewa-slot-presentations by subclassing the base UCW ones"
58 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
61 ;;;then actually define the base presentations :
62 (define-base-mewa-presentations
69 (defslot-presentation clsql-wall-time-slot-presentation
(mewa-relation-slot-presentation)
70 ((input-id :accessor input-id
:initform
(arnesi:random-string
10 arnesi
:+ascii-alphabet
+))
71 (trigger-id :accessor trigger-id
:initform
(arnesi:random-string
10 arnesi
:+ascii-alphabet
+)))
72 (:type-name clsql-sys
:wall-time
))
74 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation
) instance
)
75 (let ((date (call-next-method)))
76 (when date
(multiple-value-bind (y m d
) (clsql:time-ymd date
)
77 (format nil
"~a/~a/~a" m d y
)))))
79 (defmethod (setf presentation-slot-value
) ((value string
) (slot clsql-wall-time-slot-presentation
) instance
)
80 (let ((new-time (clsql:parse-date-time
(remove #\Space value
)))
81 (old-time (when (slot-boundp instance
(slot-name slot
))
82 (slot-value instance
(slot-name slot
)))))
83 (unless (or (eql old-time new-time
)
84 (when (and new-time old-time
)
85 (equal :equal
(clsql:time-compare new-time old-time
))))
86 (setf (presentation-slot-value slot instance
) new-time
))))
88 (defmethod label :around
((slot clsql-wall-time-slot-presentation
))
89 (concatenate 'string
(call-next-method) " (m/d/y)"))
91 (defmethod present-slot ((slot clsql-wall-time-slot-presentation
) instance
)
92 (let ((date (presentation-slot-value slot instance
)))
93 (if (and date
(not (editablep slot
)))
95 (when (editablep slot
)
96 (<ucw
:input
:accessor
(presentation-slot-value slot instance
) :id
(input-id slot
) :style
"display:inline")
97 (<:button
:id
(trigger-id slot
) (<:as-html
"[...]"))
98 (<:script
:type
"text/javascript"
99 (<:as-is
(format nil
"
104 ifFormat : \"%m/%d/%Y\" });" (input-id slot
) (trigger-id slot
)))))))
106 (defslot-presentation mewa-relation-slot-presentation
(mewa-slot-presentation slot-presentation
)
107 ((foreign-instance :accessor foreign-instance
)
108 (linkedp :accessor linkedp
:initarg
:linkedp
:initform t
)
109 (creator :accessor creator
:initarg
:creator
:initform
:editor
)
110 (new-instance :accessor new-instance
:initform nil
))
111 (:type-name relation
))
113 (defaction search-records
((slot mewa-relation-slot-presentation
) instance
)
114 (multiple-value-bindf (finstance foreign-slot-name
)
115 (meta-model:explode-foreign-key instance
(slot-name slot
))
116 (let ((new-instance (new-instance self
)))
118 (setf (new-instance self
)
121 (make-instance (or (cadr (mewa:find-attribute finstance
:presentation-search
))
122 'mewa
::mewa-presentation-search
)
124 (mewa:make-presentation finstance
125 :type
:search-presentation
)
127 (mewa:make-presentation finstance
129 (sync-foreign-instance slot new-instance
))))
131 (defmethod sync-foreign-instance ((slot mewa-relation-slot-presentation
) foreign-instance
)
132 (let ((instance (instance (ucw::parent slot
))))
133 (multiple-value-bind (foo f-slot-name
)
134 (meta-model:explode-foreign-key instance
(slot-name slot
))
135 (setf (slot-value instance
(slot-name slot
)) (slot-value foreign-instance f-slot-name
))
136 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p slot
)))))
139 (defaction create-record-on-foreign-key
((slot mewa-relation-slot-presentation
) instance
)
140 (multiple-value-bindf (finstance foreign-slot-name
)
141 (meta-model:explode-foreign-key instance
(slot-name slot
))
145 (mewa:make-presentation finstance
:type
(creator self
)))))
147 ;;;; TODO: this next bit is due to a bad design decision.
148 ;;;; Components should always have (ok) return self, but somewhere
149 ;;;; i've made in return (instance self) sometimes, and this
150 ;;;; bahaviour is totatlly fucked.
152 (when (typep new-instance
'mewa
::mewa
)
153 (setf new-instance
(instance new-instance
)))
155 ;;;; sorry about that, and now back t our regular program.
157 (meta-model:sync-instance new-instance
)
158 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
159 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
162 (defmethod present-relation ((slot mewa-relation-slot-presentation
) instance
)
163 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
164 (let* ((i (foreign-instance slot
))
165 (pres (mewa::make-presentation
170 (list :editablep nil
:linkedp nil
)))))
171 (when (and (ucw::parent slot
) (slot-boundp slot
'ucw
::place
))
172 (setf (component.place pres
) (component.place
(ucw::parent slot
))))
173 (when i
(<ucw
:render-component
:component pres
))))
175 (defmethod present-slot ((slot mewa-relation-slot-presentation
) instance
)
176 (present-relation slot instance
))
178 (defslot-presentation foreign-key-slot-presentation
(mewa-relation-slot-presentation)
180 (:type-name foreign-key
)
183 (defaction view-instance
((self component
) instance
&rest initargs
)
184 (call-component (ucw::parent self
) (apply #'mewa
:make-presentation instance initargs
))
185 ;; the viewed instance could have been changed/deleted, so we sync this instance
186 (meta-model:sync-instance
(instance (ucw::parent self
))))
189 (defmethod present-slot :around
((slot foreign-key-slot-presentation
) instance
)
190 (setf (foreign-instance slot
)
191 (when (presentation-slot-value slot instance
)
192 (meta-model:explode-foreign-key instance
(slot-name slot
))))
193 (flet ((render () (when (foreign-instance slot
)(call-next-method))))
194 (if (slot-boundp slot
'ucw
::place
)
197 (<ucw
:submit
:action
(search-records slot instance
) :value
"Search" :style
"display:inline")
198 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline"))
200 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
204 ;; presentation is used only for rendering
211 (defslot-presentation has-many-slot-presentation
(mewa-relation-slot-presentation)
212 ((add-new-label :accessor add-new-label
:initarg
:add-new-label
:initform
"Add New"))
213 (:type-name has-many
))
215 (defaction add-to-has-many
((slot has-many-slot-presentation
) instance
)
216 ;; if the instance is not stored we must make sure to mark it stored now!
217 (unless (mewa::instance-is-stored-p instance
)
218 (setf (mewa::modifiedp
(ucw::parent self
)) t
))
219 ;; sync up the instance
220 ;;(mewa:ensure-instance-sync (parent slot))
221 (meta-model:sync-instance
(instance (ucw::parent slot
)))
223 (multiple-value-bindf (class home foreign
)
224 (meta-model:explode-has-many instance
(slot-name slot
))
225 (let ((new (make-instance class
)))
226 (setf (slot-value new foreign
) (slot-value instance home
))
227 (meta-model:sync-instance new
:fill-gaps-only-p
(fill-gaps-only-p self
))
228 (call-component (ucw::parent slot
) (mewa:make-presentation new
:type
(creator slot
)))
229 (meta-model:sync-instance instance
))))
231 (defmethod present-slot ((slot has-many-slot-presentation
) instance
)
232 (when (slot-boundp slot
'ucw
::place
)
233 (<ucw
:submit
:action
(add-to-has-many slot instance
) :value
(add-new-label slot
)))
234 (let* ((i (get-foreign-instances slot instance
))
235 (presentation (and i
(make-presentation (car i
) :type
:one-line
))))
237 (flet ((linker (i string
)
239 :action
(view-instance slot i
241 `(:global-properties
,
245 (<:as-html string
))))
248 (<:th
) ;empty col for (view) link
249 (dolist (s (slots presentation
))
250 (<:th
(<:as-html
(label s
)))))
253 (setf (foreign-instance slot
) s
)
254 (when (slot-boundp slot
'ucw
::place
)
256 (<:td
(linker s
" (view) "))
257 (dolist (p (slots (make-presentation s
:type
:one-line
263 (present-slot p s
))))))))))))
266 (defmethod get-foreign-instances ((slot has-many-slot-presentation
) instance
)
267 (slot-value instance
(slot-name slot
)))
269 (defmethod presentation-slot-value ((slot has-many-slot-presentation
) instance
)
270 (get-foreign-instances slot instance
))
272 (defslot-presentation has-very-many-slot-presentation
(has-many-slot-presentation)
273 ((number-to-display :accessor number-to-display
:initarg
:number-to-display
:initform
10)
274 (current :accessor current
:initform
0)
276 (instances :accessor instances
))
277 (:type-name has-very-many
))
279 (defmethod list-next ((slot has-very-many-slot-presentation
))
280 (setf (current slot
) (incf (current slot
) (number-to-display slot
)))
281 (when (< (len slot
) (current slot
))
282 (setf (current slot
) (- (number-to-display slot
) (len slot
)))))
284 (defmethod list-prev ((slot has-very-many-slot-presentation
))
285 (setf (current slot
) (decf (current slot
) (number-to-display slot
)))
286 (when (> 0 (current slot
))
287 ;;what to do here is open to debate
288 (setf (current slot
) (- (len slot
)(number-to-display slot
) ))))
291 (defmethod present-slot ((slot has-very-many-slot-presentation
) instance
)
292 ;;(<:as-html "isance: " instance)
293 (if (slot-boundp slot
'ucw
::place
)
295 (<ucw
:a
:action
(list-prev slot
) (<:as-html
"<<"))
296 (let ((self (ucw::parent slot
)))
297 (<ucw
:a
:action
(call-component self
(mewa:make-presentation
(car (slot-value instance
(slot-name slot
))) :type
:listing
:initargs
(list :instances
(instances slot
))))
298 (<:as-html
(label slot
) (format nil
" ~a-~a " (current slot
) (+ (current slot
) (number-to-display slot
))))))
299 (<ucw
:a
:action
(list-next slot
) (<:as-html
">>"))
301 (<:as-html
"total :" (len slot
)))
304 (defmethod get-foreign-instances :around
((slot has-very-many-slot-presentation
) instance
)
305 (let ((f (call-next-method)))
306 (setf (len slot
) (length f
))
307 (setf (instances slot
) f
)
308 (loop for cons on
(nthcdr (current slot
) f
)
309 for i from
0 upto
(number-to-display slot
)
310 collect
(car cons
))))
312 (defslot-presentation has-a-slot-presentation
(one-of-presentation)
313 ((key :initarg
:key
:accessor key
))
316 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation
) (object t
) (slot-name t
))
317 (slot-value object slot-name
))
319 (defmethod present-slot ((slot has-a-slot-presentation
) instance
)
320 (<:as-html
(presentation-slot-value slot instance
))
322 (<ucw
:select
:accessor
(presentation-slot-value slot instance
) :test
#'equalp
323 (when (allow-nil-p slot
)
324 (<ucw
:option
:value nil
(<:as-html
(none-label slot
))))
325 (dolist (option (get-foreign-instances (presentation slot
) instance
))
326 (setf (instance (presentation slot
)) option
)
327 (<ucw
:option
:value
(get-foreign-slot-value slot option
(key slot
)) (present (presentation slot
)))))
328 (if (presentation-slot-value slot instance
)
330 (setf (instance (presentation slot
)) (presentation-slot-value slot instance
))
331 (present (presentation slot
)))