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
)
20 (convert-newlines-p :initarg
:convert-newlines-p
:accessor convert-newlines-p
:initform nil
))
23 (defmethod present-slot ((slot text-slot-presentation
) instance
)
24 (flet ((maybe-convert-newline-and-escape-html-then-print ()
25 (let ((string (if (convert-newlines-p slot
)
26 (with-output-to-string (new-string)
27 (with-input-from-string
28 (s (presentation-slot-value slot instance
))
29 (loop for line
= (read-line s nil
)
31 do
(format new-string
"~A~A" line
"<br/>"))))
32 (presentation-slot-value slot instance
))))
33 (if (escape-html-p slot
)
39 :accessor
(presentation-slot-value slot instance
)
40 :reader
(or (presentation-slot-value slot instance
)
44 (maybe-convert-newline-and-escape-html-then-print))))
47 (defcomponent mewa-slot-presentation
()
48 ((slot-name :accessor slot-name
51 "The name of the slot being accessed")
52 (fill-gaps-only-p :accessor fill-gaps-only-p
53 :initarg
:fill-gaps-only-p
56 "When nil, the instance is syncronised with the database.
57 When T, only the default value for primary keys and the joins are updated.")
58 (show-label-p :accessor show-label-p
:initarg
:show-label-p
:initform t
)
59 (creatablep :accessor creatablep
:initarg
:creatablep
:initform t
))
60 (:documentation
"The superclass of all Mewa slot presentations"))
62 ;;;; this has to be in the eval when i would think
63 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
64 (defun generate-slot-presentation-definition-for-type (type)
65 (let* ((u-name (intern (format nil
"~A-SLOT-PRESENTATION" type
)))
66 (sp-name (intern (format nil
"MEWA-~A" u-name
)))
67 (t-name (intern (format nil
"MEWA-~A" type
))))
68 `(defslot-presentation ,sp-name
(,u-name mewa-slot-presentation
)
70 (:type-name
,t-name
)))))
72 (defmacro define-base-mewa-presentations
(&body types
)
73 "Define the mewa-slot-presentations by subclassing the base UCW ones"
74 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
77 ;;;then actually define the base presentations :
78 (define-base-mewa-presentations
85 (defslot-presentation clsql-wall-time-slot-presentation
(mewa-relation-slot-presentation)
86 ((input-id :accessor input-id
:initform
(arnesi:random-string
10 arnesi
:+ascii-alphabet
+))
87 (trigger-id :accessor trigger-id
:initform
(arnesi:random-string
10 arnesi
:+ascii-alphabet
+)))
88 (:type-name clsql-sys
:wall-time
))
90 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation
) instance
)
91 (let ((date (call-next-method)))
92 (when date
(multiple-value-bind (y m d
) (clsql:time-ymd date
)
93 (format nil
"~a/~a/~a" m d y
)))))
95 (defmethod (setf presentation-slot-value
) ((value string
) (slot clsql-wall-time-slot-presentation
) instance
)
96 (let ((new-time (clsql:parse-date-time
(remove #\Space value
)))
97 (old-time (when (slot-boundp instance
(slot-name slot
))
98 (slot-value instance
(slot-name slot
)))))
99 (unless (or (eql old-time new-time
)
100 (when (and new-time old-time
)
101 (equal :equal
(clsql:time-compare new-time old-time
))))
102 (setf (presentation-slot-value slot instance
) new-time
))))
104 (defmethod label :around
((slot clsql-wall-time-slot-presentation
))
105 (concatenate 'string
(call-next-method) " (m/d/y)"))
107 (defmethod present-slot ((slot clsql-wall-time-slot-presentation
) instance
)
108 (let ((date (presentation-slot-value slot instance
)))
109 (if (and date
(not (editablep slot
)))
111 (when (editablep slot
)
112 (<ucw
:input
:accessor
(presentation-slot-value slot instance
) :id
(input-id slot
) :style
"display:inline")
113 (<:button
:id
(trigger-id slot
) (<:as-html
"[...]"))
114 (<:script
:type
"text/javascript"
115 (<:as-is
(format nil
"
120 ifFormat : \"%m/%d/%Y\" });" (input-id slot
) (trigger-id slot
)))))))
122 (defslot-presentation mewa-relation-slot-presentation
(mewa-slot-presentation slot-presentation
)
123 ((foreign-instance :accessor foreign-instance
)
124 (linkedp :accessor linkedp
:initarg
:linkedp
:initform t
)
125 (creator :accessor creator
:initarg
:creator
:initform
:editor
)
126 (new-instance :accessor new-instance
:initform nil
))
127 (:type-name relation
))
129 (defaction search-records
((slot mewa-relation-slot-presentation
) instance
)
130 (multiple-value-bindf (finstance foreign-slot-name
)
131 (meta-model:explode-foreign-key instance
(slot-name slot
))
132 (let ((new-instance (new-instance self
)))
134 (setf (new-instance self
)
137 (make-instance (or (cadr (mewa:find-attribute finstance
:presentation-search
))
138 'mewa
::mewa-presentation-search
)
140 (mewa:make-presentation finstance
141 :type
:search-presentation
)
143 (mewa:make-presentation finstance
145 (sync-foreign-instance slot new-instance
))))
147 (defmethod sync-foreign-instance ((slot mewa-relation-slot-presentation
) foreign-instance
)
148 (let ((instance (instance (ucw::parent slot
))))
149 (multiple-value-bind (foo f-slot-name
)
150 (meta-model:explode-foreign-key instance
(slot-name slot
))
151 (setf (slot-value instance
(slot-name slot
)) (slot-value foreign-instance f-slot-name
))
152 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p slot
)))))
155 (defaction create-record-on-foreign-key
((slot mewa-relation-slot-presentation
) instance
)
156 (multiple-value-bindf (finstance foreign-slot-name
)
157 (meta-model:explode-foreign-key instance
(slot-name slot
))
161 (mewa:make-presentation finstance
:type
(creator self
)))))
163 ;;;; TODO: this next bit is due to a bad design decision.
164 ;;;; Components should always have (ok) return self, but somewhere
165 ;;;; i've made in return (instance self) sometimes, and this
166 ;;;; bahaviour is totatlly fucked.
168 (when (typep new-instance
'mewa
::mewa
)
169 (setf new-instance
(instance new-instance
)))
171 ;;;; sorry about that, and now back t our regular program.
173 (meta-model:sync-instance new-instance
)
174 (setf (slot-value instance
(slot-name slot
)) (slot-value new-instance foreign-slot-name
))
175 (meta-model:sync-instance instance
:fill-gaps-only-p
(fill-gaps-only-p self
)))))
178 (defmethod present-relation ((slot mewa-relation-slot-presentation
) instance
)
179 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
180 (let* ((i (foreign-instance slot
))
181 (pres (mewa::make-presentation
186 (list :editablep nil
:linkedp nil
)))))
187 (when (and (ucw::parent slot
) (slot-boundp slot
'ucw
::place
))
188 (setf (component.place pres
) (component.place
(ucw::parent slot
))))
189 (when i
(<ucw
:render-component
:component pres
))))
191 (defmethod present-slot ((slot mewa-relation-slot-presentation
) instance
)
192 (present-relation slot instance
))
194 (defslot-presentation foreign-key-slot-presentation
(mewa-relation-slot-presentation)
196 (:type-name foreign-key
)
199 (defaction view-instance
((self component
) instance
&rest initargs
)
200 (call-component (ucw::parent self
) (apply #'mewa
:make-presentation instance initargs
))
201 ;; the viewed instance could have been changed/deleted, so we sync this instance
202 (meta-model:sync-instance
(instance (ucw::parent self
))))
205 (defmethod present-slot :around
((slot foreign-key-slot-presentation
) instance
)
206 (setf (foreign-instance slot
)
207 (when (presentation-slot-value slot instance
)
208 (meta-model:explode-foreign-key instance
(slot-name slot
))))
209 (flet ((render () (when (foreign-instance slot
)(call-next-method))))
210 (if (slot-boundp slot
'ucw
::place
)
214 (<ucw
:submit
:action
(search-records slot instance
) :value
"Search" :style
"display:inline")
215 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline"))
217 (<ucw
:a
:action
(view-instance slot
(foreign-instance slot
))
221 ;; presentation is used only for rendering
225 (defmethod find-foreign-instances ((slot foreign-key-slot-presentation
))
226 (clsql:select
(class-name (class-of (meta-model:explode-foreign-key
(instance slot
) (slot-name slot
))))))
231 (defslot-presentation has-many-slot-presentation
(mewa-relation-slot-presentation)
232 ((add-new-label :accessor add-new-label
:initarg
:add-new-label
:initform
"Add New"))
233 (:type-name has-many
))
235 (defaction add-to-has-many
((slot has-many-slot-presentation
) instance
)
236 ;; if the instance is not stored we must make sure to mark it stored now!
237 (unless (meta-model::persistentp instance
)
238 (setf (mewa::modifiedp
(ucw::parent self
)) t
))
239 ;; sync up the instance
240 ;;(mewa:ensure-instance-sync (parent slot))
241 (meta-model:sync-instance
(instance (ucw::parent slot
)))
243 (multiple-value-bindf (class home foreign
)
244 (meta-model:explode-has-many instance
(slot-name slot
))
245 (let ((new (make-instance class
)))
246 (setf (slot-value new foreign
) (slot-value instance home
))
247 (meta-model:sync-instance new
:fill-gaps-only-p
(fill-gaps-only-p self
))
248 (call-component (ucw::parent slot
) (mewa:make-presentation new
:type
(creator slot
)))
249 (meta-model:sync-instance instance
))))
251 (defmethod present-slot ((slot has-many-slot-presentation
) instance
)
252 (when (slot-boundp slot
'ucw
::place
)
253 (<ucw
:submit
:action
(add-to-has-many slot instance
) :value
(add-new-label slot
)))
254 (let* ((i (get-foreign-instances slot instance
))
255 (presentation (and i
(make-presentation (car i
) :type
:one-line
))))
257 (flet ((linker (i string
)
259 :action
(view-instance slot i
261 `(:global-properties
,
265 (<:as-html string
))))
266 (<:table
:cellpadding
10
268 (<:th
) ;empty col for (view) link
269 (dolist (s (slots presentation
))
270 (<:th
(<:as-html
(label s
)))))
273 (setf (foreign-instance slot
) s
)
274 (when (slot-boundp slot
'ucw
::place
)
276 (<:td
(linker s
" (view) "))
277 (dolist (p (slots (make-presentation s
:type
:one-line
283 (present-slot p s
))))))))))))
286 (defmethod get-foreign-instances ((slot has-many-slot-presentation
) instance
)
287 (slot-value instance
(slot-name slot
)))
289 (defmethod presentation-slot-value ((slot has-many-slot-presentation
) instance
)
290 (get-foreign-instances slot instance
))
292 (defslot-presentation has-very-many-slot-presentation
(has-many-slot-presentation)
293 ((number-to-display :accessor number-to-display
:initarg
:number-to-display
:initform
10)
294 (current :accessor current
:initform
0)
296 (instances :accessor instances
))
297 (:type-name has-very-many
))
299 (defmethod list-next ((slot has-very-many-slot-presentation
))
300 (setf (current slot
) (incf (current slot
) (number-to-display slot
)))
301 (when (< (len slot
) (current slot
))
302 (setf (current slot
) (- (number-to-display slot
) (len slot
)))))
304 (defmethod list-prev ((slot has-very-many-slot-presentation
))
305 (setf (current slot
) (decf (current slot
) (number-to-display slot
)))
306 (when (> 0 (current slot
))
307 ;;what to do here is open to debate
308 (setf (current slot
) (- (len slot
)(number-to-display slot
) ))))
311 (defmethod present-slot ((slot has-very-many-slot-presentation
) instance
)
312 ;;(<:as-html "isance: " instance)
313 (if (slot-boundp slot
'ucw
::place
)
315 (<ucw
:a
:action
(list-prev slot
) (<:as-html
"<<"))
316 (let ((self (ucw::parent slot
)))
317 (<ucw
:a
:action
(call-component self
(mewa:make-presentation
(car (slot-value instance
(slot-name slot
))) :type
:listing
:initargs
(list :instances
(instances slot
))))
318 (<:as-html
(label slot
) (format nil
" ~a-~a " (current slot
) (+ (current slot
) (number-to-display slot
))))))
319 (<ucw
:a
:action
(list-next slot
) (<:as-html
">>"))
321 (<:as-html
"total :" (len slot
)))
324 (defmethod get-foreign-instances :around
((slot has-very-many-slot-presentation
) instance
)
325 (let ((f (call-next-method)))
326 (setf (len slot
) (length f
))
327 (setf (instances slot
) f
)
328 (loop for cons on
(nthcdr (current slot
) f
)
329 for i from
0 upto
(number-to-display slot
)
330 collect
(car cons
))))
334 (defslot-presentation has-a-slot-presentation
(mewa-relation-slot-presentation)
335 ((allow-nil-p :accessor allow-nil-p
:initarg
:allow-nil-p
:initform t
)
336 (attributes :accessor attributes
:initarg
:attributes
:initform nil
))
339 (defmethod find-foreign-slot-value ((slot has-a-slot-presentation
) (object t
))
340 (multiple-value-bind (c s
)
341 (meta-model:explode-foreign-key
(instance (ucw::parent slot
)) (slot-name slot
))
342 (slot-value object s
)))
344 (defmethod get-foreign-instances ((slot mewa-relation-slot-presentation
) instance
)
345 (clsql:select
(class-name (class-of
346 (meta-model:explode-foreign-key instance
(slot-name slot
))))
349 (defmethod present-slot ((slot has-a-slot-presentation
) instance
)
350 ; (<:as-html (presentation-slot-value slot instance))
352 (progn (<ucw
:select
:accessor
(presentation-slot-value slot instance
) :test
#'equalp
353 (when (allow-nil-p slot
)
354 (<ucw
:option
:value nil
(<:as-html
"none")))
355 (dolist (option (get-foreign-instances slot instance
))
356 (<ucw
:option
:value
(find-foreign-slot-value slot option
)
358 (lol:make-presentation option
361 `(:attributes
,(attributes slot
)))
363 (when (creatablep slot
)
364 (<ucw
:submit
:action
(create-record-on-foreign-key slot instance
) :value
"Add New" :style
"display:inline")))
365 (if (presentation-slot-value slot instance
)
368 (lol:make-presentation
(meta-model:explode-foreign-key instance
(slot-name slot
))
371 `(:attributes
,(attributes slot
)))
375 (defslot-presentation many-to-many-slot-presentation
(mewa-relation-slot-presentation)
376 ((list-view :accessor list-view
:initarg
:list-view
:initform
:one-line
)
377 (action-view :accessor action-view
:initarg
:action-view
:initform
:viewer
)
378 (create-view :initform
:creator
)
379 (select-view :initform
:as-string
:accessor select-view
))
380 (:type-name many-to-many
)
381 (:default-initargs
:label
"many to many"))
383 (defun %delete-item
(item)
384 (clsql:with-default-database
(clsql:*default-database
*)
386 (clsql:delete-instance-records item
))))
388 (defaction delete-item
((self component
) instance
)
389 (multiple-value-bind (res err
) (%delete-item instance
)
391 (call 'info-message
:message
"Removed Instance")
392 (call 'info-message
:message
(format nil
"Could not remove item. Try removing associated items first. ~A" instance
)))))
394 (defaction delete-relationship
((slot many-to-many-slot-presentation
) rel instance
)
395 (delete-item (ucw::parent self
) rel
)
396 (sync-instance instance
)
397 (answer-component (ucw::parent self
) t
))
400 (defun find-many-to-many-join-class (slot instance
)
401 (let* ((imd (getf (meta-model::find-slot-metadata instance
(slot-name slot
))
403 (jc (make-instance (getf imd
:join-class
)))
404 (jcmd (getf (meta-model::find-slot-metadata jc
(getf imd
:target-slot
))
406 (getf jcmd
:join-class
)))
408 (defmethod find-all-instances ((slot many-to-many-slot-presentation
) instance
)
409 (clsql:select
(find-many-to-many-join-class slot instance
) :flatp t
))
411 (defmethod present-slot ((slot many-to-many-slot-presentation
) instance
)
412 (let ((instances (slot-value instance
(slot-name slot
)))
415 (<:li
(<ucw
:button
:action
(add-to-many-to-many slot instance
)
416 (<:as-html
"Add New")))
417 (<:li
(<ucw
:button
:action
(add-to-many-to-many slot instance new-instance
)
419 (<ucw
:select
:accessor new-instance
420 (arnesi:dolist
* (i (find-all-instances slot instance
))
423 (lol:present-view
(i (select-view slot
) slot
))))))
424 (dolist* (i instances
)
426 (<ucw
:a
:action
(call-view ((car i
) (action-view slot
) (ucw::parent slot
)))
427 (<:as-html
"(view) "))
428 (<ucw
:a
:action
(delete-relationship slot
(second i
) instance
)
429 (<:as-html
"(remove) "))
430 (present-view ((car i
) (list-view slot
) (ucw::parent slot
)))) ))))
433 (defaction add-to-many-to-many
((slot many-to-many-slot-presentation
) instance
&optional foreign-instance
)
434 ;;;; First things first, we sync.
435 (sync-instance instance
)
437 (imd (getf (meta-model::find-slot-metadata instance
(slot-name slot
))
439 (jc (make-instance (getf imd
:join-class
)))
440 (jcmd (getf (meta-model::find-slot-metadata jc
(getf imd
:target-slot
))
442 (fc (make-instance (getf jcmd
:join-class
)))
446 (call-view (fc :creator
(ucw::parent slot
))))))
449 ; (error "~A ~A ~A" (getf imd :foreign-key) (getf jcmd :foreign-key) (getf imd :home-key))
450 (setf (slot-value jc
(getf imd
:foreign-key
))
451 (slot-value instance
(getf imd
:home-key
)))
452 (setf (slot-value jc
(getf jcmd
:home-key
))
453 (slot-value c
(getf jcmd
:foreign-key
)))
456 (sync-instance instance
)