2232ec179f56f06ef3ea05b58d99c1eade76cea2
[clinton/lisp-on-lines.git] / src / mewa / slot-presentations.lisp
1 (in-package :it.bese.ucw)
2
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)))
6
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))
11 ,@body))
12
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
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.
45 When 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)
70
71 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
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)
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 ))))
88
89 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
90 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
91
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
106 (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
107 ((foreign-instance :accessor foreign-instance)
108 (linkedp :accessor linkedp :initarg :linkedp :initform t)
109 (creator :accessor creator :initarg :creator :initform :editor))
110 (:type-name relation))
111
112 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
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
117 (parent slot)
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))
127 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
128
129 (defaction create-record-on-foreign-key ((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
133 (call-component
134 (parent slot)
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)
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)))))
150
151
152 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
153 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
154 (let* ((i (foreign-instance slot))
155 (pres (mewa::make-presentation
156 i
157 :type :one-line
158 :initargs (list
159 :global-properties
160 (list :editablep nil :linkedp nil)))))
161 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
162 (setf (component.place pres) (component.place (ucw::parent slot))))
163 (when i (<ucw:render-component :component pres))))
164
165
166
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)
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))))
179
180
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)
187 (setf (foreign-instance slot)
188 (when (presentation-slot-value slot instance)
189 (meta-model:explode-foreign-key instance (slot-name slot))))
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")
196 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
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))))
204
205 ;;;; HAS MANY
206 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
207 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
208 (:type-name has-many))
209
210 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
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
215 (mewa:ensure-instance-sync (parent slot))
216
217 (multiple-value-bindf (class home foreign)
218 (meta-model:explode-has-many instance (slot-name slot))
219 (let ((new (make-instance class)))
220 (setf (slot-value new foreign) (slot-value instance home))
221 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
222 (call-component (parent slot) (mewa:make-presentation new :type (creator slot)))
223 (meta-model:sync-instance instance))))
224
225 (defmethod present-slot ((slot has-many-slot-presentation) instance)
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
230 (<:ul
231 (dolist (s i)
232 (let ((s s))
233 (setf (foreign-instance slot) s)
234 (when (slot-boundp slot 'place)
235 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
236 (<:li (setf (linkedp slot) nil)
237 (present-relation slot instance)))))))))
238
239
240 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
241 (slot-value instance (slot-name slot)))
242
243 (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
244 (get-foreign-instances slot instance))
245
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)
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)))
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)))
307 (<:as-html "--"))))
308
309
310
311
312
313
314