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 (creatablep :accessor creatablep
:initarg
:creatablep
:initform t
))
48 (:documentation
"The superclass of all Mewa slot presentations"))
50 ;;;; this has to be in the eval when i would think
51 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
52 (defun generate-slot-presentation-definition-for-type (type)
53 (let* ((u-name (intern (format nil
"~A-SLOT-PRESENTATION" type
)))
54 (sp-name (intern (format nil
"MEWA-~A" u-name
)))
55 (t-name (intern (format nil
"MEWA-~A" type
))))
56 `(defslot-presentation ,sp-name
(,u-name mewa-slot-presentation
)
58 (:type-name
,t-name
)))))
60 (defmacro define-base-mewa-presentations
(&body types
)
61 "Define the mewa-slot-presentations by subclassing the base UCW ones"
62 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
65 ;;;then actually define the base presentations :
66 (define-base-mewa-presentations
73 (defslot-presentation clsql-wall-time-slot-presentation
(mewa-relation-slot-presentation)
74 ((input-id :accessor input-id
:initform
(arnesi:random-string
10 arnesi
:+ascii-alphabet
+))
75 (trigger-id :accessor trigger-id
:initform
(arnesi:random-string
10 arnesi
:+ascii-alphabet
+)))
76 (:type-name clsql-sys
:wall-time
))
78 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation
) instance
)
79 (let ((date (call-next-method)))
80 (when date
(multiple-value-bind (y m d
) (clsql:time-ymd date
)
81 (format nil
"~a/~a/~a" m d y
)))))
83 (defmethod (setf presentation-slot-value
) ((value string
) (slot clsql-wall-time-slot-presentation
) instance
)
84 (let ((new-time (clsql:parse-date-time
(remove #\Space value
)))
85 (old-time (when (slot-boundp instance
(slot-name slot
))
86 (slot-value instance
(slot-name slot
)))))
87 (unless (or (eql old-time new-time
)
88 (when (and new-time old-time
)
89 (equal :equal
(clsql:time-compare new-time old-time
))))
90 (setf (presentation-slot-value slot instance
) new-time
))))
92 (defmethod label :around
((slot clsql-wall-time-slot-presentation
))
93 (concatenate 'string
(call-next-method) " (m/d/y)"))
95 (defmethod present-slot ((slot clsql-wall-time-slot-presentation
) instance
)
96 (let ((date (presentation-slot-value slot instance
)))
97 (if (and date
(not (editablep slot
)))
99 (when (editablep slot
)
100 (<ucw
:input
:accessor
(presentation-slot-value slot instance
) :id
(input-id slot
) :style
"display:inline")
101 (<:button
:id
(trigger-id slot
) (<:as-html
"[...]"))
102 (<:script
:type
"text/javascript"
103 (<:as-is
(format nil
"
108 ifFormat : \"%m/%d/%Y\" });" (input-id slot
) (trigger-id slot
)))))))
110 (defslot-presentation mewa-relation-slot-presentation
(mewa-slot-presentation slot-presentation
)
111 ((foreign-instance :accessor foreign-instance
)
112 (linkedp :accessor linkedp
:initarg
:linkedp
:initform t
)
113 (creator :accessor creator
:initarg
:creator
:initform
:editor
)
114 (new-instance :accessor new-instance
:initform nil
))
115 (:type-name relation
))
117 (defaction search-records
((slot mewa-relation-slot-presentation
) instance
)
118 (multiple-value-bindf (finstance foreign-slot-name
)
119 (meta-model:explode-foreign-key instance
(slot-name slot
))
120 (let ((new-instance (new-instance self
)))
122 (setf (new-instance self
)
125 (make-instance (or (cadr (mewa:find-attribute finstance
:presentation-search
))
126 'mewa
::mewa-presentation-search
)
128 (mewa:make-presentation finstance
129 :type
:search-presentation
)
131 (mewa:make-presentation finstance
133 (sync-foreign-instance slot new-instance
))))
135 (defmethod sync-foreign-instance ((slot mewa-relation-slot-presentation
) foreign-instance
)
136 (let ((instance (instance (ucw::parent slot
))))
137 (multiple-value-bind (foo f-slot-name
)
138 (meta-model:explode-foreign-key instance
(slot-name slot
))
139 (setf (slot-value instance
(slot-name slot
)) (slot-value foreign-instance f-slot-name
))
140 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p slot
)))))
143 (defaction create-record-on-foreign-key
((slot mewa-relation-slot-presentation
) instance
)
144 (multiple-value-bindf (finstance foreign-slot-name
)
145 (meta-model:explode-foreign-key instance
(slot-name slot
))
149 (mewa:make-presentation finstance
:type
(creator self
)))))
151 ;;;; TODO: this next bit is due to a bad design decision.
152 ;;;; Components should always have (ok) return self, but somewhere
153 ;;;; i've made in return (instance self) sometimes, and this
154 ;;;; bahaviour is totatlly fucked.
156 (when (typep new-instance
'mewa
::mewa
)
157 (setf new-instance
(instance new-instance
)))
159 ;;;; sorry about that, and now back t our regular program.
161 (meta-model:sync-instance new-instance
)
162 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
163 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
166 (defmethod present-relation ((slot mewa-relation-slot-presentation
) instance
)
167 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
168 (let* ((i (foreign-instance slot
))
169 (pres (mewa::make-presentation
174 (list :editablep nil
:linkedp nil
)))))
175 (when (and (ucw::parent slot
) (slot-boundp slot
'ucw
::place
))
176 (setf (component.place pres
) (component.place
(ucw::parent slot
))))
177 (when i
(<ucw
:render-component
:component pres
))))
179 (defmethod present-slot ((slot mewa-relation-slot-presentation
) instance
)
180 (present-relation slot instance
))
182 (defslot-presentation foreign-key-slot-presentation
(mewa-relation-slot-presentation)
184 (:type-name foreign-key
)
187 (defaction view-instance
((self component
) instance
&rest initargs
)
188 (call-component (ucw::parent self
) (apply #'mewa
:make-presentation instance initargs
))
189 ;; the viewed instance could have been changed/deleted, so we sync this instance
190 (meta-model:sync-instance
(instance (ucw::parent self
))))
193 (defmethod present-slot :around
((slot foreign-key-slot-presentation
) instance
)
194 (setf (foreign-instance slot
)
195 (when (presentation-slot-value slot instance
)
196 (meta-model:explode-foreign-key instance
(slot-name slot
))))
197 (flet ((render () (when (foreign-instance slot
)(call-next-method))))
198 (if (slot-boundp slot
'ucw
::place
)
202 (<ucw
:submit
:action
(search-records slot instance
) :value
"Search" :style
"display:inline")
203 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline"))
205 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
209 ;; presentation is used only for rendering
213 (defmethod find-foreign-instances ((slot foreign-key-slot-presentation
))
214 (clsql:select
(class-name (class-of (meta-model:explode-foreign-key
(instance slot
) (slot-name slot
))))))
219 (defslot-presentation has-many-slot-presentation
(mewa-relation-slot-presentation)
220 ((add-new-label :accessor add-new-label
:initarg
:add-new-label
:initform
"Add New"))
221 (:type-name has-many
))
223 (defaction add-to-has-many
((slot has-many-slot-presentation
) instance
)
224 ;; if the instance is not stored we must make sure to mark it stored now!
225 (unless (meta-model::persistentp instance
)
226 (setf (mewa::modifiedp
(ucw::parent self
)) t
))
227 ;; sync up the instance
228 ;;(mewa:ensure-instance-sync (parent slot))
229 (meta-model:sync-instance
(instance (ucw::parent slot
)))
231 (multiple-value-bindf (class home foreign
)
232 (meta-model:explode-has-many instance
(slot-name slot
))
233 (let ((new (make-instance class
)))
234 (setf (slot-value new foreign
) (slot-value instance home
))
235 (meta-model:sync-instance new
:fill-gaps-only-p
(fill-gaps-only-p self
))
236 (call-component (ucw::parent slot
) (mewa:make-presentation new
:type
(creator slot
)))
237 (meta-model:sync-instance instance
))))
239 (defmethod present-slot ((slot has-many-slot-presentation
) instance
)
240 (when (slot-boundp slot
'ucw
::place
)
241 (<ucw
:submit
:action
(add-to-has-many slot instance
) :value
(add-new-label slot
)))
242 (let* ((i (get-foreign-instances slot instance
))
243 (presentation (and i
(make-presentation (car i
) :type
:one-line
))))
245 (flet ((linker (i string
)
247 :action
(view-instance slot i
249 `(:global-properties
,
253 (<:as-html string
))))
254 (<:table
:cellpadding
10
256 (<:th
) ;empty col for (view) link
257 (dolist (s (slots presentation
))
258 (<:th
(<:as-html
(label s
)))))
261 (setf (foreign-instance slot
) s
)
262 (when (slot-boundp slot
'ucw
::place
)
264 (<:td
(linker s
" (view) "))
265 (dolist (p (slots (make-presentation s
:type
:one-line
271 (present-slot p s
))))))))))))
274 (defmethod get-foreign-instances ((slot has-many-slot-presentation
) instance
)
275 (slot-value instance
(slot-name slot
)))
277 (defmethod presentation-slot-value ((slot has-many-slot-presentation
) instance
)
278 (get-foreign-instances slot instance
))
280 (defslot-presentation has-very-many-slot-presentation
(has-many-slot-presentation)
281 ((number-to-display :accessor number-to-display
:initarg
:number-to-display
:initform
10)
282 (current :accessor current
:initform
0)
284 (instances :accessor instances
))
285 (:type-name has-very-many
))
287 (defmethod list-next ((slot has-very-many-slot-presentation
))
288 (setf (current slot
) (incf (current slot
) (number-to-display slot
)))
289 (when (< (len slot
) (current slot
))
290 (setf (current slot
) (- (number-to-display slot
) (len slot
)))))
292 (defmethod list-prev ((slot has-very-many-slot-presentation
))
293 (setf (current slot
) (decf (current slot
) (number-to-display slot
)))
294 (when (> 0 (current slot
))
295 ;;what to do here is open to debate
296 (setf (current slot
) (- (len slot
)(number-to-display slot
) ))))
299 (defmethod present-slot ((slot has-very-many-slot-presentation
) instance
)
300 ;;(<:as-html "isance: " instance)
301 (if (slot-boundp slot
'ucw
::place
)
303 (<ucw
:a
:action
(list-prev slot
) (<:as-html
"<<"))
304 (let ((self (ucw::parent slot
)))
305 (<ucw
:a
:action
(call-component self
(mewa:make-presentation
(car (slot-value instance
(slot-name slot
))) :type
:listing
:initargs
(list :instances
(instances slot
))))
306 (<:as-html
(label slot
) (format nil
" ~a-~a " (current slot
) (+ (current slot
) (number-to-display slot
))))))
307 (<ucw
:a
:action
(list-next slot
) (<:as-html
">>"))
309 (<:as-html
"total :" (len slot
)))
312 (defmethod get-foreign-instances :around
((slot has-very-many-slot-presentation
) instance
)
313 (let ((f (call-next-method)))
314 (setf (len slot
) (length f
))
315 (setf (instances slot
) f
)
316 (loop for cons on
(nthcdr (current slot
) f
)
317 for i from
0 upto
(number-to-display slot
)
318 collect
(car cons
))))
322 (defslot-presentation has-a-slot-presentation
(mewa-relation-slot-presentation)
323 ((allow-nil-p :accessor allow-nil-p
:initarg
:allow-nil-p
:initform t
)
324 (attributes :accessor attributes
:initarg
:attributes
:initform nil
))
327 (defmethod find-foreign-slot-value ((slot has-a-slot-presentation
) (object t
))
328 (multiple-value-bind (c s
)
329 (meta-model:explode-foreign-key
(instance (ucw::parent slot
)) (slot-name slot
))
330 (slot-value object s
)))
332 (defmethod get-foreign-instances ((slot mewa-relation-slot-presentation
) instance
)
333 (clsql:select
(class-name (class-of
334 (meta-model:explode-foreign-key instance
(slot-name slot
))))
337 (defmethod present-slot ((slot has-a-slot-presentation
) instance
)
338 ; (<:as-html (presentation-slot-value slot instance))
340 (progn (<ucw
:select
:accessor
(presentation-slot-value slot instance
) :test
#'equalp
341 (when (allow-nil-p slot
)
342 (<ucw
:option
:value nil
(<:as-html
"none")))
343 (dolist (option (get-foreign-instances slot instance
))
344 (<ucw
:option
:value
(find-foreign-slot-value slot option
)
346 (lol:make-presentation option
349 `(:attributes
,(attributes slot
)))
351 (when (creatablep slot
)
352 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline")))
353 (if (presentation-slot-value slot instance
)
356 (lol:make-presentation
(meta-model:explode-foreign-key instance
(slot-name slot
))
359 `(:attributes
,(attributes slot
)))
363 (defslot-presentation many-to-many-slot-presentation
(mewa-relation-slot-presentation)
364 ((list-view :accessor list-view
:initarg
:list-view
:initform
:one-line
)
365 (action-view :accessor action-view
:initarg
:action-view
:initform
:viewer
)
366 (create-view :initform
:creator
)
367 (select-view :initform
:as-string
:accessor select-view
))
368 (:type-name many-to-many
)
369 (:default-initargs
:label
"many to many"))
371 (defun %delete-item
(item)
372 (clsql:with-default-database
(clsql:*default-database
*)
374 (clsql:delete-instance-records item
))))
376 (defaction delete-item
((self component
) instance
)
377 (multiple-value-bind (res err
) (%delete-item instance
)
379 (call 'info-message
:message
"Removed Instance")
380 (call 'info-message
:message
(format nil
"Could not remove item. Try removing associated items first. ~A" instance
)))))
382 (defaction delete-relationship
((slot many-to-many-slot-presentation
) rel instance
)
383 (delete-item (ucw::parent self
) rel
)
384 (sync-instance instance
)
385 (answer-component (ucw::parent self
) t
))
388 (defun find-many-to-many-join-class (slot instance
)
389 (let* ((imd (getf (meta-model::find-slot-metadata instance
(slot-name slot
))
391 (jc (make-instance (getf imd
:join-class
)))
392 (jcmd (getf (meta-model::find-slot-metadata jc
(getf imd
:target-slot
))
394 (getf jcmd
:join-class
)))
396 (defmethod find-all-instances ((slot many-to-many-slot-presentation
) instance
)
397 (clsql:select
(find-many-to-many-join-class slot instance
) :flatp t
))
399 (defmethod present-slot ((slot many-to-many-slot-presentation
) instance
)
400 (let ((instances (slot-value instance
(slot-name slot
)))
403 (<:li
(<ucw
:button
:action
(add-to-many-to-many slot instance
)
404 (<:as-html
"Add New")))
405 (<:li
(<ucw
:button
:action
(add-to-many-to-many slot instance new-instance
)
407 (<ucw
:select
:accessor new-instance
408 (arnesi:dolist
* (i (find-all-instances slot instance
))
411 (lol:present-view
(i (select-view slot
) slot
))))))
412 (dolist* (i instances
)
414 (<ucw
:a
:action
(call-view ((car i
) (action-view slot
) (ucw::parent slot
)))
415 (<:as-html
"(view) "))
416 (<ucw
:a
:action
(delete-relationship slot
(second i
) instance
)
417 (<:as-html
"(remove) "))
418 (present-view ((car i
) (list-view slot
) (ucw::parent slot
)))) ))))
421 (defaction add-to-many-to-many
((slot many-to-many-slot-presentation
) instance
&optional foreign-instance
)
422 ;;;; First things first, we sync.
423 (sync-instance instance
)
425 (imd (getf (meta-model::find-slot-metadata instance
(slot-name slot
))
427 (jc (make-instance (getf imd
:join-class
)))
428 (jcmd (getf (meta-model::find-slot-metadata jc
(getf imd
:target-slot
))
430 (fc (make-instance (getf jcmd
:join-class
)))
434 (call-view (fc :creator
(ucw::parent slot
))))))
437 ; (error "~A ~A ~A" (getf imd :foreign-key) (getf jcmd :foreign-key) (getf imd :home-key))
438 (setf (slot-value jc
(getf imd
:foreign-key
))
439 (slot-value instance
(getf imd
:home-key
)))
440 (setf (slot-value jc
(getf jcmd
:home-key
))
441 (slot-value c
(getf jcmd
:foreign-key
)))
444 (sync-instance instance
)