1 (in-package :it.bese.ucw
)
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
15 ;;;; This should really be in UCW.
17 (defslot-presentation text-slot-presentation
()
18 ((rows :initarg
:rows
:accessor rows
:initform nil
)
19 (columns :initarg
:columns
:accessor columns
:initform nil
)
20 (html-contentp :initarg
:escape-html-p
:accessor escape-html-p
:initform nil
))
23 (defmethod present-slot ((slot text-slot-presentation
) instance
)
25 (<ucw
:textarea
:accessor
(presentation-slot-value slot instance
)
28 (if (escape-html-p slot
)
29 (<:as-is
(presentation-slot-value slot instance
))
30 (<:as-html
(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)
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 (input-id (string (gensym))))
94 (if (and date
(not (editablep slot
)))
95 (<:span
(<:as-html date
)))
96 (when (editablep slot
)
97 (<ucw
:input
:accessor
(presentation-slot-value slot instance
) :id input-id
)
98 (<:script
:type
"text/javascript"
99 (<:as-is
(format nil
"
102 ifFormat : \"%m/%d/%Y\",
105 (defslot-presentation mewa-relation-slot-presentation
(mewa-slot-presentation slot-presentation
)
106 ((foreign-instance :accessor foreign-instance
)
107 (linkedp :accessor linkedp
:initarg
:linkedp
:initform t
)
108 (creator :accessor creator
:initarg
:creator
:initform
:editor
))
109 (:type-name relation
))
111 (defaction search-records
((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 (make-instance (or (cadr (mewa:find-attribute finstance
:presentation-search
))
118 'mewa
::mewa-presentation-search
)
120 (mewa:make-presentation finstance
121 :type
:search-presentation
)
123 (mewa:make-presentation finstance
125 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
126 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
128 (defaction create-record-on-foreign-key
((slot mewa-relation-slot-presentation
) instance
)
129 (multiple-value-bindf (finstance foreign-slot-name
)
130 (meta-model:explode-foreign-key instance
(slot-name slot
))
134 (mewa:make-presentation finstance
:type
(creator self
)))))
136 ;;;; TODO: this next bit is due to a bad design decision.
137 ;;;; Components should always have (ok) return self, but somewhere
138 ;;;; i've made in return (instance self) sometimes, and this
139 ;;;; bahaviour is totatlly fucked.
141 (when (typep new-instance
'mewa
::mewa
)
142 (setf new-instance
(instance new-instance
)))
144 ;;;; sorry about that, and now back t our regular program.
146 (meta-model:sync-instance new-instance
)
147 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
148 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
151 (defmethod present-relation ((slot mewa-relation-slot-presentation
) instance
)
152 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
153 (let* ((i (foreign-instance slot
))
154 (pres (mewa::make-presentation
159 (list :editablep nil
:linkedp nil
)))))
160 (when (and (ucw::parent slot
) (slot-boundp slot
'ucw
::place
))
161 (setf (component.place pres
) (component.place
(ucw::parent slot
))))
162 (when i
(<ucw
:render-component
:component pres
))))
166 (defmethod present-slot ((slot mewa-relation-slot-presentation
) instance
)
167 (present-relation slot instance
))
169 (defslot-presentation foreign-key-slot-presentation
(mewa-relation-slot-presentation)
171 (:type-name foreign-key
)
174 (defaction view-instance
((self component
) instance
&rest initargs
)
175 (call-component (parent self
) (apply #'mewa
:make-presentation instance initargs
))
176 ;; the viewed instance could have been changed/deleted, so we sync this instance
177 (meta-model:sync-instance
(instance (parent self
))))
180 (defmethod present-slot :around
((slot foreign-key-slot-presentation
) instance
)
181 (setf (foreign-instance slot
)
182 (when (presentation-slot-value slot instance
)
183 (meta-model:explode-foreign-key instance
(slot-name slot
))))
184 (flet ((render () (when (foreign-instance slot
)(call-next-method))))
185 (if (slot-boundp slot
'place
)
188 (<ucw
:submit
:action
(search-records slot instance
) :value
"Search" :style
"display:inline")
189 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline"))
191 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
195 ;; presentation is used only for rendering
201 ;;;; TODO: This search stuff should probably me refactored elsewhere
203 (defmethod find-slots-of-type (model &key
(type 'string
)
204 (types '((string)) types-supplied-p
))
205 "returns a list of slots matching TYPE, or matching any of TYPES"
209 (setf ty
(list type
)))
210 (remove nil
(mapcar #'(lambda (st) (when (member (second st
) ty
)
212 (lisp-on-lines::list-slot-types model
)))))
214 (defslot-presentation ajax-foreign-key-slot-presentation
(foreign-key-slot-presentation)
215 ((search-slots :accessor search-slots
:initarg
:search-slots
:initform nil
)
217 :accessor live-search
218 :component
(lisp-on-lines:auto-complete
221 (when (< 0 (length value
))
222 (limited-word-search 'person
'(first-name last-name company-name
) (list value
))))
225 (<:as-html
(if (> (length (last-name x
)) 0)
226 (strcat (last-name x
) ", ")
228 (first-name x
)" " (company-name x
)))
229 :as-value
(lambda (x) x
)
230 :submit-on-click-p nil
)))
231 (:type-name ajax-foreign-key
))
234 (defmethod shared-initialize :after
((slot ajax-foreign-key-slot-presentation
) slots
&rest args
)
235 ;; If no search-slots than use the any slots of type string
236 (unless (search-slots slot
)
237 (setf (search-slots slot
) t
)
238 (let ((l (live-search slot
)))
239 (setf (lisp-on-lines::values-generator l
) t
))))
242 (defmethod present-slot :around
((slot ajax-foreign-key-slot-presentation
) instance
)
243 (setf (foreign-instance slot
)
244 (when (presentation-slot-value slot instance
)
245 (meta-model:explode-foreign-key instance
(slot-name slot
))))
246 (flet ((render () (when (foreign-instance slot
)(call-next-method))))
247 (if (slot-boundp slot
'place
)
251 (<ucw
:submit
:action
(search-records slot instance
) :value
"find" :style
"display:inline"))
253 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
257 ;; presentation is used only for rendering
261 (defslot-presentation has-many-slot-presentation
(mewa-relation-slot-presentation)
262 ((add-new-label :accessor add-new-label
:initarg
:add-new-label
:initform
"Add New"))
263 (:type-name has-many
))
265 (defaction add-to-has-many
((slot has-many-slot-presentation
) instance
)
266 ;; if the instance is not stored we must make sure to mark it stored now!
267 (unless (mewa::instance-is-stored-p instance
)
268 (setf (mewa::modifiedp
(parent self
)) t
))
269 ;; sync up the instance
270 ;;(mewa:ensure-instance-sync (parent slot))
271 (meta-model:sync-instance
(instance (parent slot
)))
273 (multiple-value-bindf (class home foreign
)
274 (meta-model:explode-has-many instance
(slot-name slot
))
275 (let ((new (make-instance class
)))
276 (setf (slot-value new foreign
) (slot-value instance home
))
277 (meta-model:sync-instance new
:fill-gaps-only-p
(fill-gaps-only-p self
))
278 (call-component (parent slot
) (mewa:make-presentation new
:type
(creator slot
)))
279 (meta-model:sync-instance instance
))))
281 (defmethod present-slot ((slot has-many-slot-presentation
) instance
)
282 (when (slot-boundp slot
'place
)
283 (<ucw
:submit
:action
(add-to-has-many slot instance
) :value
(add-new-label slot
)))
284 (let ((i (get-foreign-instances slot instance
)))
289 (setf (foreign-instance slot
) s
)
290 (when (slot-boundp slot
'place
)
291 (<ucw
:a
:action
(view-instance slot s
:initargs
`(:global-properties
,(list :linkedp t
:editablep nil
)))
292 (<:li
(setf (linkedp slot
) nil
)
293 (present-relation slot instance
)))))))))
296 (defmethod get-foreign-instances ((slot has-many-slot-presentation
) instance
)
297 (slot-value instance
(slot-name slot
)))
299 (defmethod presentation-slot-value ((slot has-many-slot-presentation
) instance
)
300 (get-foreign-instances slot instance
))
302 (defslot-presentation has-very-many-slot-presentation
(has-many-slot-presentation)
303 ((number-to-display :accessor number-to-display
:initarg
:number-to-display
:initform
10)
304 (current :accessor current
:initform
0)
306 (instances :accessor instances
))
308 (:type-name has-very-many
))
310 (defmethod list-next ((slot has-very-many-slot-presentation
))
311 (setf (current slot
) (incf (current slot
) (number-to-display slot
)))
312 (when (< (len slot
) (current slot
))
313 (setf (current slot
) (- (number-to-display slot
) (len slot
)))))
315 (defmethod list-prev ((slot has-very-many-slot-presentation
))
316 (setf (current slot
) (decf (current slot
) (number-to-display slot
)))
317 (when (> 0 (current slot
))
318 ;;what to do here is open to debate
319 (setf (current slot
) (- (len slot
)(number-to-display slot
) ))))
322 (defmethod present-slot ((slot has-very-many-slot-presentation
) instance
)
323 ;;(<:as-html "isance: " instance)
324 (if (slot-boundp slot
'place
)
326 (<ucw
:a
:action
(list-prev slot
) (<:as-html
"<<"))
327 (let ((self (parent slot
)))
328 (<ucw
:a
:action
(call-component self
(mewa:make-presentation
(car (slot-value instance
(slot-name slot
))) :type
:listing
:initargs
(list :instances
(instances slot
))))
329 (<:as-html
(label slot
) (format nil
" ~a-~a " (current slot
) (+ (current slot
) (number-to-display slot
))))))
330 (<ucw
:a
:action
(list-next slot
) (<:as-html
">>"))
332 (<:as-html
"total :" (len slot
)))
335 (defmethod get-foreign-instances :around
((slot has-very-many-slot-presentation
) instance
)
336 (let ((f (call-next-method)))
337 (setf (len slot
) (length f
))
338 (setf (instances slot
) f
)
339 (loop for cons on
(nthcdr (current slot
) f
)
340 for i from
0 upto
(number-to-display slot
)
341 collect
(car cons
))))
343 (defslot-presentation has-a-slot-presentation
(one-of-presentation)
344 ((key :initarg
:key
:accessor key
))
347 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation
) (object t
) (slot-name t
))
348 (slot-value object slot-name
))
350 (defmethod present-slot ((slot has-a-slot-presentation
) instance
)
351 (<:as-html
(presentation-slot-value slot instance
))
353 (<ucw
:select
:accessor
(presentation-slot-value slot instance
) :test
#'equalp
354 (when (allow-nil-p slot
)
355 (<ucw
:option
:value nil
(<:as-html
(none-label slot
))))
356 (dolist (option (get-foreign-instances (presentation slot
) instance
))
357 (setf (instance (presentation slot
)) option
)
358 (<ucw
:option
:value
(get-foreign-slot-value slot option
(key slot
)) (present (presentation slot
)))))
359 (if (presentation-slot-value slot instance
)
361 (setf (instance (presentation slot
)) (presentation-slot-value slot instance
))
362 (present (presentation slot
)))