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