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
))
33 (defcomponent mewa-slot-presentation
()
34 ((slot-name :accessor slot-name
37 "The name of the slot being accessed")
38 (fill-gaps-only-p :accessor fill-gaps-only-p
39 :initarg
:fill-gaps-only-p
42 "When nil, the instance is syncronised with the database.
43 When T, only the default value for primary keys and the joins are updated.")
44 (show-label-p :accessor show-label-p
:initarg
:show-label-p
:initform t
))
45 (:documentation
"The superclass of all Mewa slot presentations"))
47 ;;;; this has to be in the eval when i would think
48 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
49 (defun generate-slot-presentation-definition-for-type (type)
50 (let* ((u-name (intern (format nil
"~A-SLOT-PRESENTATION" type
)))
51 (sp-name (intern (format nil
"MEWA-~A" u-name
)))
52 (t-name (intern (format nil
"MEWA-~A" type
))))
53 `(defslot-presentation ,sp-name
(,u-name mewa-slot-presentation
)
55 (:type-name
,t-name
)))))
57 (defmacro define-base-mewa-presentations
(&body types
)
58 "Define the mewa-slot-presentations by subclassing the base UCW ones"
59 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
62 ;;;then actually define the base presentations :
63 (define-base-mewa-presentations
70 (defslot-presentation clsql-wall-time-slot-presentation
(mewa-relation-slot-presentation)
71 ((input-id :accessor input-id
:initform
(arnesi:random-string
10 arnesi
:+ascii-alphabet
+))
72 (trigger-id :accessor trigger-id
:initform
(arnesi:random-string
10 arnesi
:+ascii-alphabet
+)))
73 (:type-name clsql-sys
:wall-time
))
75 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation
) instance
)
76 (let ((date (call-next-method)))
77 (when date
(multiple-value-bind (y m d
) (clsql:time-ymd date
)
78 (format nil
"~a/~a/~a" m d y
)))))
80 (defmethod (setf presentation-slot-value
) ((value string
) (slot clsql-wall-time-slot-presentation
) instance
)
81 (let ((new-time (clsql:parse-date-time
(remove #\Space value
)))
82 (old-time (when (slot-boundp instance
(slot-name slot
))
83 (slot-value instance
(slot-name slot
)))))
84 (unless (or (eql old-time new-time
)
85 (when (and new-time old-time
)
86 (equal :equal
(clsql:time-compare new-time old-time
))))
87 (setf (presentation-slot-value slot instance
) new-time
))))
89 (defmethod label :around
((slot clsql-wall-time-slot-presentation
))
90 (concatenate 'string
(call-next-method) " (m/d/y)"))
92 (defmethod present-slot ((slot clsql-wall-time-slot-presentation
) instance
)
93 (let ((date (presentation-slot-value slot instance
)))
94 (if (and date
(not (editablep slot
)))
96 (when (editablep slot
)
97 (<ucw
:input
:accessor
(presentation-slot-value slot instance
) :id
(input-id slot
) :style
"display:inline")
98 (<:button
:id
(trigger-id slot
) (<:as-html
"[...]"))
99 (<:script
:type
"text/javascript"
100 (<:as-is
(format nil
"
105 ifFormat : \"%m/%d/%Y\" });" (input-id slot
) (trigger-id slot
)))))))
107 (defslot-presentation mewa-relation-slot-presentation
(mewa-slot-presentation slot-presentation
)
108 ((foreign-instance :accessor foreign-instance
)
109 (linkedp :accessor linkedp
:initarg
:linkedp
:initform t
)
110 (creator :accessor creator
:initarg
:creator
:initform
:editor
)
111 (new-instance :accessor new-instance
:initform nil
))
112 (:type-name relation
))
114 (defaction search-records
((slot mewa-relation-slot-presentation
) instance
)
115 (multiple-value-bindf (finstance foreign-slot-name
)
116 (meta-model:explode-foreign-key instance
(slot-name slot
))
117 (let ((new-instance (new-instance self
)))
119 (setf (new-instance self
)
122 (make-instance (or (cadr (mewa:find-attribute finstance
:presentation-search
))
123 'mewa
::mewa-presentation-search
)
125 (mewa:make-presentation finstance
126 :type
:search-presentation
)
128 (mewa:make-presentation finstance
130 (sync-foreign-instance slot new-instance
))))
132 (defmethod sync-foreign-instance ((slot mewa-relation-slot-presentation
) foreign-instance
)
133 (let ((instance (instance (ucw::parent slot
))))
134 (multiple-value-bind (foo f-slot-name
)
135 (meta-model:explode-foreign-key instance
(slot-name slot
))
136 (setf (slot-value instance
(slot-name slot
)) (slot-value foreign-instance f-slot-name
))
137 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p slot
)))))
140 (defaction create-record-on-foreign-key
((slot mewa-relation-slot-presentation
) instance
)
141 (multiple-value-bindf (finstance foreign-slot-name
)
142 (meta-model:explode-foreign-key instance
(slot-name slot
))
146 (mewa:make-presentation finstance
:type
(creator self
)))))
148 ;;;; TODO: this next bit is due to a bad design decision.
149 ;;;; Components should always have (ok) return self, but somewhere
150 ;;;; i've made in return (instance self) sometimes, and this
151 ;;;; bahaviour is totatlly fucked.
153 (when (typep new-instance
'mewa
::mewa
)
154 (setf new-instance
(instance new-instance
)))
156 ;;;; sorry about that, and now back t our regular program.
158 (meta-model:sync-instance new-instance
)
159 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
160 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
163 (defmethod present-relation ((slot mewa-relation-slot-presentation
) instance
)
164 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
165 (let* ((i (foreign-instance slot
))
166 (pres (mewa::make-presentation
171 (list :editablep nil
:linkedp nil
)))))
172 (when (and (ucw::parent slot
) (slot-boundp slot
'ucw
::place
))
173 (setf (component.place pres
) (component.place
(ucw::parent slot
))))
174 (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
)
200 (<ucw
:submit
:action
(search-records slot instance
) :value
"Search" :style
"display:inline")
201 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline"))
203 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
207 ;; presentation is used only for rendering
214 (defslot-presentation has-many-slot-presentation
(mewa-relation-slot-presentation)
215 ((add-new-label :accessor add-new-label
:initarg
:add-new-label
:initform
"Add New"))
216 (:type-name has-many
))
218 (defaction add-to-has-many
((slot has-many-slot-presentation
) instance
)
219 ;; if the instance is not stored we must make sure to mark it stored now!
220 (unless (mewa::instance-is-stored-p instance
)
221 (setf (mewa::modifiedp
(ucw::parent self
)) t
))
222 ;; sync up the instance
223 ;;(mewa:ensure-instance-sync (parent slot))
224 (meta-model:sync-instance
(instance (ucw::parent slot
)))
226 (multiple-value-bindf (class home foreign
)
227 (meta-model:explode-has-many instance
(slot-name slot
))
228 (let ((new (make-instance class
)))
229 (setf (slot-value new foreign
) (slot-value instance home
))
230 (meta-model:sync-instance new
:fill-gaps-only-p
(fill-gaps-only-p self
))
231 (call-component (ucw::parent slot
) (mewa:make-presentation new
:type
(creator slot
)))
232 (meta-model:sync-instance instance
))))
234 (defmethod present-slot ((slot has-many-slot-presentation
) instance
)
235 (when (slot-boundp slot
'ucw
::place
)
236 (<ucw
:submit
:action
(add-to-has-many slot instance
) :value
(add-new-label slot
)))
237 (let* ((i (get-foreign-instances slot instance
))
238 (presentation (and i
(make-presentation (car i
) :type
:one-line
))))
240 (flet ((linker (i string
)
242 :action
(view-instance slot i
244 `(:global-properties
,
248 (<:as-html string
))))
251 (<:th
) ;empty col for (view) link
252 (dolist (s (slots presentation
))
253 (<:th
(<:as-html
(label s
)))))
256 (setf (foreign-instance slot
) s
)
257 (when (slot-boundp slot
'ucw
::place
)
259 (<:td
(linker s
" (view) "))
260 (dolist (p (slots (make-presentation s
:type
:one-line
266 (present-slot p s
))))))))))))
269 (defmethod get-foreign-instances ((slot has-many-slot-presentation
) instance
)
270 (slot-value instance
(slot-name slot
)))
272 (defmethod presentation-slot-value ((slot has-many-slot-presentation
) instance
)
273 (get-foreign-instances slot instance
))
275 (defslot-presentation has-very-many-slot-presentation
(has-many-slot-presentation)
276 ((number-to-display :accessor number-to-display
:initarg
:number-to-display
:initform
10)
277 (current :accessor current
:initform
0)
279 (instances :accessor instances
))
280 (:type-name has-very-many
))
282 (defmethod list-next ((slot has-very-many-slot-presentation
))
283 (setf (current slot
) (incf (current slot
) (number-to-display slot
)))
284 (when (< (len slot
) (current slot
))
285 (setf (current slot
) (- (number-to-display slot
) (len slot
)))))
287 (defmethod list-prev ((slot has-very-many-slot-presentation
))
288 (setf (current slot
) (decf (current slot
) (number-to-display slot
)))
289 (when (> 0 (current slot
))
290 ;;what to do here is open to debate
291 (setf (current slot
) (- (len slot
)(number-to-display slot
) ))))
294 (defmethod present-slot ((slot has-very-many-slot-presentation
) instance
)
295 ;;(<:as-html "isance: " instance)
296 (if (slot-boundp slot
'ucw
::place
)
298 (<ucw
:a
:action
(list-prev slot
) (<:as-html
"<<"))
299 (let ((self (ucw::parent slot
)))
300 (<ucw
:a
:action
(call-component self
(mewa:make-presentation
(car (slot-value instance
(slot-name slot
))) :type
:listing
:initargs
(list :instances
(instances slot
))))
301 (<:as-html
(label slot
) (format nil
" ~a-~a " (current slot
) (+ (current slot
) (number-to-display slot
))))))
302 (<ucw
:a
:action
(list-next slot
) (<:as-html
">>"))
304 (<:as-html
"total :" (len slot
)))
307 (defmethod get-foreign-instances :around
((slot has-very-many-slot-presentation
) instance
)
308 (let ((f (call-next-method)))
309 (setf (len slot
) (length f
))
310 (setf (instances slot
) f
)
311 (loop for cons on
(nthcdr (current slot
) f
)
312 for i from
0 upto
(number-to-display slot
)
313 collect
(car cons
))))
315 (defslot-presentation has-a-slot-presentation
(one-of-presentation)
316 ((key :initarg
:key
:accessor key
))
319 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation
) (object t
) (slot-name t
))
320 (slot-value object slot-name
))
322 (defmethod present-slot ((slot has-a-slot-presentation
) instance
)
323 (<:as-html
(presentation-slot-value slot instance
))
325 (<ucw
:select
:accessor
(presentation-slot-value slot instance
) :test
#'equalp
326 (when (allow-nil-p slot
)
327 (<ucw
:option
:value nil
(<:as-html
(none-label slot
))))
328 (dolist (option (get-foreign-instances (presentation slot
) instance
))
329 (setf (instance (presentation slot
)) option
)
330 (<ucw
:option
:value
(get-foreign-slot-value slot option
(key slot
)) (present (presentation slot
)))))
331 (if (presentation-slot-value slot instance
)
333 (setf (instance (presentation slot
)) (presentation-slot-value slot instance
))
334 (present (presentation slot
)))