adding text-slot-presentation from rob
[clinton/lisp-on-lines.git] / src / mewa / slot-presentations.lisp
CommitLineData
579597e3 1(in-package :it.bese.ucw)
2
19a82bd5 3(defun multiple-value-funcall->list (function &rest args)
4e2ecf69 4 "The function to be called by m-v-bf"
19a82bd5 5 (multiple-value-call #'list (apply function args)))
6
7(defmacro multiple-value-bindf (vars form &body body)
4e2ecf69 8 "Like M-V-B, only it works in actions. form must be a function call"
19a82bd5 9 `(destructuring-bind ,vars
10 (multiple-value-funcall->list #',(car form) ,@(cdr form))
11 ,@body))
579597e3 12
44108fd6
DC
13
14;;;; ** Textarea Slot Presentation
15;;;; This should really be in UCW.
16
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))
21 (:type-name text))
22
23(defmethod present-slot ((slot text-slot-presentation) instance)
24 (if (editablep slot)
25 (<ucw:textarea :accessor (presentation-slot-value slot instance)
26 :rows (rows slot)
27 :cols (columns slot))
28 (if (escape-html-p slot)
29 (<:as-is (presentation-slot-value slot instance))
30 (<:as-html (presentation-slot-value slot instance)))))
31
32
33
34
e9454185
DC
35(defcomponent mewa-slot-presentation ()
36 ((slot-name :accessor slot-name
37 :initarg :slot-name
38 :documentation
39 "The name of the slot being accessed")
40 (fill-gaps-only-p :accessor fill-gaps-only-p
41 :initarg :fill-gaps-only-p
42 :initform nil
43 :documentation
44 "When nil, the instance is syncronised with the database.
45When T, only the default value for primary keys and the joins are updated."))
46 (:documentation "The superclass of all Mewa slot presentations"))
47
48;;;; this has to be in the eval when i would think
49(eval-when (:compile-toplevel :load-toplevel :execute)
50 (defun generate-slot-presentation-definition-for-type (type)
51 (let* ((u-name (intern (format nil "~A-SLOT-PRESENTATION" type)))
52 (sp-name (intern (format nil "MEWA-~A" u-name)))
53 (t-name (intern (format nil "MEWA-~A" type))))
54 `(defslot-presentation ,sp-name (,u-name mewa-slot-presentation)
55 ()
56 (:type-name ,t-name)))))
57
58(defmacro define-base-mewa-presentations (&body types)
59 "Define the mewa-slot-presentations by subclassing the base UCW ones"
60 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
61 types)))
62
63;;;then actually define the base presentations :
64(define-base-mewa-presentations
65 boolean
66 string
67 number
68 integer
69 currency)
9d6c69fb 70
f4170204 71(defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
579597e3 72 ()
73 (:type-name clsql-sys:wall-time))
74
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)))))
79
80(defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
f4170204 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)
ec4f5786 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 ))))
579597e3 88
19531fbd 89(defmethod label :around ((slot clsql-wall-time-slot-presentation))
90 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
91
579597e3 92(defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
93 (let ((date (presentation-slot-value slot instance))
94 (input-id (string (gensym))))
95 (if (and date (not (editablep slot)))
96 (<:span (<:as-html date)))
97 (when (editablep slot)
98 (<ucw:input :accessor (presentation-slot-value slot instance) :id input-id)
99 (<:script :type "text/javascript"
100 (<:as-is (format nil "
101 Calendar.setup({
102 inputField : \"~a\",
103 ifFormat : \"%m/%d/%Y\",
104 });" input-id))))))
105
e9454185
DC
106(defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
107 ((foreign-instance :accessor foreign-instance)
569ad9e6
DC
108 (linkedp :accessor linkedp :initarg :linkedp :initform t)
109 (creator :accessor creator :initarg :creator :initform :editor))
579597e3 110 (:type-name relation))
111
7d87c1d2 112(defaction search-records ((slot mewa-relation-slot-presentation) instance)
e9454185
DC
113 (multiple-value-bindf (finstance foreign-slot-name)
114 (meta-model:explode-foreign-key instance (slot-name slot))
115 (let ((new-instance
116 (call-component
5f0b37e7 117 (parent slot)
e9454185
DC
118 (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search))
119 'mewa::mewa-presentation-search)
120 :search-presentation
121 (mewa:make-presentation finstance
122 :type :search-presentation)
123 :list-presentation
124 (mewa:make-presentation finstance
125 :type :listing)))))
126 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
f1ce8b6e 127 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
e9454185 128
569ad9e6 129(defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
e9454185
DC
130 (multiple-value-bindf (finstance foreign-slot-name)
131 (meta-model:explode-foreign-key instance (slot-name slot))
132 (let ((new-instance
133 (call-component
134 (parent slot)
569ad9e6
DC
135 (mewa:make-presentation finstance :type (creator self)))))
136
137 ;;;; TODO: this next bit is due to a bad design decision.
138 ;;;; Components should always have (ok) return self, but somewhere
139 ;;;; i've made in return (instance self) sometimes, and this
140 ;;;; bahaviour is totatlly fucked.
141
142 (when (typep new-instance 'mewa::mewa)
143 (setf new-instance (instance new-instance)))
144
145 ;;;; sorry about that, and now back t our regular program.
146
147 (meta-model:sync-instance new-instance)
e9454185
DC
148 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
149 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
569ad9e6 150
e9454185 151
579597e3 152(defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
153 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
19531fbd 154 (let* ((i (foreign-instance slot))
579597e3 155 (pres (mewa::make-presentation
156 i
157 :type :one-line
158 :initargs (list
159 :global-properties
19531fbd 160 (list :editablep nil :linkedp nil)))))
9d6c69fb 161 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
498061f6 162 (setf (component.place pres) (component.place (ucw::parent slot))))
163 (when i (<ucw:render-component :component pres))))
579597e3 164
7d87c1d2 165
166
579597e3 167(defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
168 (present-relation slot instance))
169
170(defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
171 ()
172 (:type-name foreign-key)
173 (:default-initargs))
174
175(defaction view-instance ((self component) instance &rest initargs)
12dcf3d4 176 (call-component (parent self) (apply #'mewa:make-presentation instance initargs))
177 ;; the viewed instance could have been changed/deleted, so we sync this instance
178 (meta-model:sync-instance (instance (parent self))))
579597e3 179
498061f6 180
569ad9e6
DC
181
182(defmethod present-slot :before ((slot foreign-key-slot-presentation) instance)
183 ())
184
185
186(defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
f1ce8b6e
DC
187 (setf (foreign-instance slot)
188 (when (presentation-slot-value slot instance)
189 (meta-model:explode-foreign-key instance (slot-name slot))))
f1ce8b6e
DC
190 (flet ((render () (when (foreign-instance slot)(call-next-method))))
191 (if (slot-boundp slot 'place)
192 (cond
193 ((editablep slot)
194 (render)
195 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
569ad9e6 196 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
f1ce8b6e
DC
197 ((linkedp slot)
198 (<ucw:a :action (view-instance slot (foreign-instance slot))
199 (render)))
200 (t
201 (render)))
202 ;; presentation is used only for rendering
203 (render))))
579597e3 204
205;;;; HAS MANY
206(defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
12dcf3d4 207 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
579597e3 208 (:type-name has-many))
209
498061f6 210(defaction add-to-has-many ((slot has-many-slot-presentation) instance)
8e6e6b56
DC
211 ;; if the instance is not stored we must make sure to mark it stored now!
212 (unless (mewa::instance-is-stored-p instance)
213 (setf (mewa::modifiedp (parent self)) t))
214 ;; sync up the instance
85281029 215 (mewa:ensure-instance-sync (parent slot))
8e6e6b56 216
85281029
DC
217 (multiple-value-bindf (class home foreign)
218 (meta-model:explode-has-many instance (slot-name slot))
498061f6 219 (let ((new (make-instance class)))
220 (setf (slot-value new foreign) (slot-value instance home))
e9454185 221 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
569ad9e6 222 (call-component (parent slot) (mewa:make-presentation new :type (creator slot)))
e9454185 223 (meta-model:sync-instance instance))))
498061f6 224
579597e3 225(defmethod present-slot ((slot has-many-slot-presentation) instance)
f1ce8b6e
DC
226 (when (slot-boundp slot 'place)
227 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
228 (let ((i (get-foreign-instances slot instance)))
229
498061f6 230 (<:ul
231 (dolist (s i)
232 (let ((s s))
233 (setf (foreign-instance slot) s)
e9454185
DC
234 (when (slot-boundp slot 'place)
235 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
498061f6 236 (<:li (setf (linkedp slot) nil)
e9454185 237 (present-relation slot instance)))))))))
579597e3 238
239
240(defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
241 (slot-value instance (slot-name slot)))
242
ec4f5786 243(defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
244 (get-foreign-instances slot instance))
245
579597e3 246(defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
247 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
248 (current :accessor current :initform 0)
249 (len :accessor len )
250 (instances :accessor instances))
251
252 (:type-name has-very-many))
253
254(defmethod list-next ((slot has-very-many-slot-presentation))
255 (setf (current slot) (incf (current slot) (number-to-display slot)))
256 (when (< (len slot) (current slot))
257 (setf (current slot) (- (number-to-display slot) (len slot)))))
258
259(defmethod list-prev ((slot has-very-many-slot-presentation))
260 (setf (current slot) (decf (current slot) (number-to-display slot)))
261 (when (> 0 (current slot))
262 ;;what to do here is open to debate
263 (setf (current slot) (- (len slot)(number-to-display slot) ))))
264
265
266(defmethod present-slot ((slot has-very-many-slot-presentation) instance)
267 ;;(<:as-html "isance: " instance)
f1ce8b6e
DC
268 (if (slot-boundp slot 'place)
269 (progn
270 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
271 (let ((self (parent slot)))
272 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
273 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
274 (<ucw:a :action (list-next slot) (<:as-html ">>"))
275 (call-next-method)
276 (<:as-html "total :" (len slot)))
277 (call-next-method)))
579597e3 278
279(defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
280 (let ((f (call-next-method)))
281 (setf (len slot) (length f))
282 (setf (instances slot) f)
283 (loop for cons on (nthcdr (current slot) f)
284 for i from 0 upto (number-to-display slot)
285 collect (car cons))))
286
287(defslot-presentation has-a-slot-presentation (one-of-presentation)
288 ((key :initarg :key :accessor key))
289 (:type-name has-a))
290
291(defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
292 (slot-value object slot-name))
293
294(defmethod present-slot ((slot has-a-slot-presentation) instance)
295 (<:as-html (presentation-slot-value slot instance))
296 (if (editablep slot)
297 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
298 (when (allow-nil-p slot)
299 (<ucw:option :value nil (<:as-html (none-label slot))))
300 (dolist (option (get-foreign-instances (presentation slot) instance))
301 (setf (instance (presentation slot)) option)
302 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
303 (if (presentation-slot-value slot instance)
304 (progn
305 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
306 (present (presentation slot)))
85281029 307 (<:as-html "--"))))
5f0b37e7 308
5f0b37e7 309
5f0b37e7 310
2cb4247d
DC
311
312
313
314