A whole host of changes rescued from the alcoholic laptop.
[clinton/lisp-on-lines.git] / src / mewa / slot-presentations.lisp
1 (in-package :mewa)
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
16 (defslot-presentation text-slot-presentation ()
17 ((rows :initarg :rows :accessor rows :initform 25)
18 (columns :initarg :columns :accessor columns :initform 40)
19 (escape-html-p :initarg :escape-html-p :accessor escape-html-p :initform nil))
20 (:type-name text))
21
22 (defmethod present-slot ((slot text-slot-presentation) instance)
23 (if (editablep slot)
24 (<ucw:textarea :accessor (presentation-slot-value slot instance)
25 :rows (rows slot)
26 :cols (columns slot))
27 (if (escape-html-p slot)
28 (<:as-html (presentation-slot-value slot instance))
29 (<:as-is (presentation-slot-value slot instance))
30 )))
31
32
33 (defcomponent mewa-slot-presentation ()
34 ((slot-name :accessor slot-name
35 :initarg :slot-name
36 :documentation
37 "The name of the slot being accessed")
38 (fill-gaps-only-p :accessor fill-gaps-only-p
39 :initarg :fill-gaps-only-p
40 :initform nil
41 :documentation
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"))
46
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)
54 ()
55 (:type-name ,t-name)))))
56
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
60 types)))
61
62 ;;;then actually define the base presentations :
63 (define-base-mewa-presentations
64 boolean
65 string
66 number
67 integer
68 currency)
69
70 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
71 ((input-id :accessor input-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+))
72 (trigger-id :accessor trigger-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+)))
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) " (m/d/y)"))
91
92 (defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
93 (let ((date (presentation-slot-value slot instance)))
94 (if (and date (not (editablep slot)))
95 (<:as-html date))
96 (when (editablep slot)
97 (<ucw:input :accessor (presentation-slot-value slot instance) :id (input-id slot) :style "display:inline")
98 (<:button :id (trigger-id slot) (<:as-html "[...]"))
99 (<:script :type "text/javascript"
100 (<:as-is (format nil "
101
102 Calendar.setup({
103 inputField : \"~a\",
104 button : \"~a\",
105 ifFormat : \"%m/%d/%Y\" });" (input-id slot) (trigger-id slot)))))))
106
107 (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
108 ((foreign-instance :accessor foreign-instance)
109 (linkedp :accessor linkedp :initarg :linkedp :initform t)
110 (creator :accessor creator :initarg :creator :initform :editor)
111 (new-instance :accessor new-instance :initform nil))
112 (:type-name relation))
113
114 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
115 (multiple-value-bindf (finstance foreign-slot-name)
116 (meta-model:explode-foreign-key instance (slot-name slot))
117 (let ((new-instance (new-instance self)))
118 (unless new-instance
119 (setf (new-instance self)
120 (call-component
121 (ucw::parent slot)
122 (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search))
123 'mewa::mewa-presentation-search)
124 :search-presentation
125 (mewa:make-presentation finstance
126 :type :search-presentation)
127 :list-presentation
128 (mewa:make-presentation finstance
129 :type :listing)))))
130 (sync-foreign-instance slot new-instance))))
131
132 (defmethod sync-foreign-instance ((slot mewa-relation-slot-presentation) foreign-instance)
133 (let ((instance (instance (ucw::parent slot))))
134 (multiple-value-bind (foo f-slot-name)
135 (meta-model:explode-foreign-key instance (slot-name slot))
136 (setf (slot-value instance (slot-name slot)) (slot-value foreign-instance f-slot-name))
137 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p slot)))))
138
139
140 (defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
141 (multiple-value-bindf (finstance foreign-slot-name)
142 (meta-model:explode-foreign-key instance (slot-name slot))
143 (let ((new-instance
144 (call-component
145 (ucw::parent slot)
146 (mewa:make-presentation finstance :type (creator self)))))
147
148 ;;;; TODO: this next bit is due to a bad design decision.
149 ;;;; Components should always have (ok) return self, but somewhere
150 ;;;; i've made in return (instance self) sometimes, and this
151 ;;;; bahaviour is totatlly fucked.
152
153 (when (typep new-instance 'mewa::mewa)
154 (setf new-instance (instance new-instance)))
155
156 ;;;; sorry about that, and now back t our regular program.
157
158 (meta-model:sync-instance new-instance)
159 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
160 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
161
162
163 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
164 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
165 (let* ((i (foreign-instance slot))
166 (pres (mewa::make-presentation
167 i
168 :type :one-line
169 :initargs (list
170 :global-properties
171 (list :editablep nil :linkedp nil)))))
172 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
173 (setf (component.place pres) (component.place (ucw::parent slot))))
174 (when i (<ucw:render-component :component pres))))
175
176
177
178 (defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
179 (present-relation slot instance))
180
181 (defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
182 ()
183 (:type-name foreign-key)
184 (:default-initargs))
185
186 (defaction view-instance ((self component) instance &rest initargs)
187 (call-component (ucw::parent self) (apply #'mewa:make-presentation instance initargs))
188 ;; the viewed instance could have been changed/deleted, so we sync this instance
189 (meta-model:sync-instance (instance (ucw::parent self))))
190
191
192 (defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
193 (setf (foreign-instance slot)
194 (when (presentation-slot-value slot instance)
195 (meta-model:explode-foreign-key instance (slot-name slot))))
196 (flet ((render () (when (foreign-instance slot)(call-next-method))))
197 (if (slot-boundp slot 'ucw::place)
198 (cond
199 ((editablep slot)
200 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
201 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
202 ((linkedp slot)
203 (<ucw:a :action (view-instance slot (foreign-instance slot))
204 (render)))
205 (t
206 (render)))
207 ;; presentation is used only for rendering
208 (render))))
209
210
211
212
213 ;;;; HAS MANY
214 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
215 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
216 (:type-name has-many))
217
218 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
219 ;; if the instance is not stored we must make sure to mark it stored now!
220 (unless (mewa::instance-is-stored-p instance)
221 (setf (mewa::modifiedp (ucw::parent self)) t))
222 ;; sync up the instance
223 ;;(mewa:ensure-instance-sync (parent slot))
224 (meta-model:sync-instance (instance (ucw::parent slot)))
225
226 (multiple-value-bindf (class home foreign)
227 (meta-model:explode-has-many instance (slot-name slot))
228 (let ((new (make-instance class)))
229 (setf (slot-value new foreign) (slot-value instance home))
230 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
231 (call-component (ucw::parent slot) (mewa:make-presentation new :type (creator slot)))
232 (meta-model:sync-instance instance))))
233
234 (defmethod present-slot ((slot has-many-slot-presentation) instance)
235 (when (slot-boundp slot 'ucw::place)
236 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
237 (let* ((i (get-foreign-instances slot instance))
238 (presentation (and i (make-presentation (car i) :type :one-line))))
239 (when i
240 (flet ((linker (i string)
241 (<ucw:a
242 :action (view-instance slot i
243 :initargs
244 `(:global-properties ,
245 (list
246 :linkedp t
247 :editablep nil)))
248 (<:as-html string))))
249 (<:table
250 (<:tr
251 (<:th) ;empty col for (view) link
252 (dolist (s (slots presentation))
253 (<:th (<:as-html (label s)))))
254 (dolist (s i)
255 (let ((s s))
256 (setf (foreign-instance slot) s)
257 (when (slot-boundp slot 'ucw::place)
258 (<:tr
259 (<:td (linker s " (view) "))
260 (dolist (p (slots (make-presentation s :type :one-line
261 :initargs
262 '(:global-properties
263 (:editablep nil)))))
264 (<:td
265
266 (present-slot p s))))))))))))
267
268
269 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
270 (slot-value instance (slot-name slot)))
271
272 (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
273 (get-foreign-instances slot instance))
274
275 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
276 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
277 (current :accessor current :initform 0)
278 (len :accessor len )
279 (instances :accessor instances))
280 (:type-name has-very-many))
281
282 (defmethod list-next ((slot has-very-many-slot-presentation))
283 (setf (current slot) (incf (current slot) (number-to-display slot)))
284 (when (< (len slot) (current slot))
285 (setf (current slot) (- (number-to-display slot) (len slot)))))
286
287 (defmethod list-prev ((slot has-very-many-slot-presentation))
288 (setf (current slot) (decf (current slot) (number-to-display slot)))
289 (when (> 0 (current slot))
290 ;;what to do here is open to debate
291 (setf (current slot) (- (len slot)(number-to-display slot) ))))
292
293
294 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
295 ;;(<:as-html "isance: " instance)
296 (if (slot-boundp slot 'ucw::place)
297 (progn
298 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
299 (let ((self (ucw::parent slot)))
300 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
301 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
302 (<ucw:a :action (list-next slot) (<:as-html ">>"))
303 (call-next-method)
304 (<:as-html "total :" (len slot)))
305 (call-next-method)))
306
307 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
308 (let ((f (call-next-method)))
309 (setf (len slot) (length f))
310 (setf (instances slot) f)
311 (loop for cons on (nthcdr (current slot) f)
312 for i from 0 upto (number-to-display slot)
313 collect (car cons))))
314
315 (defslot-presentation has-a-slot-presentation (one-of-presentation)
316 ((key :initarg :key :accessor key))
317 (:type-name has-a))
318
319 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
320 (slot-value object slot-name))
321
322 (defmethod present-slot ((slot has-a-slot-presentation) instance)
323 (<:as-html (presentation-slot-value slot instance))
324 (if (editablep slot)
325 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
326 (when (allow-nil-p slot)
327 (<ucw:option :value nil (<:as-html (none-label slot))))
328 (dolist (option (get-foreign-instances (presentation slot) instance))
329 (setf (instance (presentation slot)) option)
330 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
331 (if (presentation-slot-value slot instance)
332 (progn
333 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
334 (present (presentation slot)))
335 (<:as-html "--"))))
336
337
338
339
340
341
342