slot-presentation-cleanups
[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
e9454185
DC
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.
23When 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)
9d6c69fb 48
f4170204 49(defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
579597e3 50 ()
51 (:type-name clsql-sys:wall-time))
52
53(defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
54 (let ((date (call-next-method)))
55 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
56 (format nil "~a/~a/~a" m d y)))))
57
58(defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
f4170204 59 (let ((new-time (clsql:parse-date-time (remove #\Space value)))
60 (old-time (when (slot-boundp instance (slot-name slot))
61 (slot-value instance (slot-name slot)))))
62 (unless (or (eql old-time new-time)
ec4f5786 63 (when (and new-time old-time)
64 (equal :equal (clsql:time-compare new-time old-time))))
65 (setf (presentation-slot-value slot instance) new-time ))))
579597e3 66
19531fbd 67(defmethod label :around ((slot clsql-wall-time-slot-presentation))
68 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
69
579597e3 70(defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
71 (let ((date (presentation-slot-value slot instance))
72 (input-id (string (gensym))))
73 (if (and date (not (editablep slot)))
74 (<:span (<:as-html date)))
75 (when (editablep slot)
76 (<ucw:input :accessor (presentation-slot-value slot instance) :id input-id)
77 (<:script :type "text/javascript"
78 (<:as-is (format nil "
79 Calendar.setup({
80 inputField : \"~a\",
81 ifFormat : \"%m/%d/%Y\",
82 });" input-id))))))
83
e9454185
DC
84(defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
85 ((foreign-instance :accessor foreign-instance)
569ad9e6
DC
86 (linkedp :accessor linkedp :initarg :linkedp :initform t)
87 (creator :accessor creator :initarg :creator :initform :editor))
579597e3 88 (:type-name relation))
89
7d87c1d2 90(defaction search-records ((slot mewa-relation-slot-presentation) instance)
e9454185
DC
91 (multiple-value-bindf (finstance foreign-slot-name)
92 (meta-model:explode-foreign-key instance (slot-name slot))
93 (let ((new-instance
94 (call-component
5f0b37e7 95 (parent slot)
e9454185
DC
96 (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search))
97 'mewa::mewa-presentation-search)
98 :search-presentation
99 (mewa:make-presentation finstance
100 :type :search-presentation)
101 :list-presentation
102 (mewa:make-presentation finstance
103 :type :listing)))))
104 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
f1ce8b6e 105 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
e9454185 106
569ad9e6 107(defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
e9454185
DC
108 (multiple-value-bindf (finstance foreign-slot-name)
109 (meta-model:explode-foreign-key instance (slot-name slot))
110 (let ((new-instance
111 (call-component
112 (parent slot)
569ad9e6
DC
113 (mewa:make-presentation finstance :type (creator self)))))
114
115 ;;;; TODO: this next bit is due to a bad design decision.
116 ;;;; Components should always have (ok) return self, but somewhere
117 ;;;; i've made in return (instance self) sometimes, and this
118 ;;;; bahaviour is totatlly fucked.
119
120 (when (typep new-instance 'mewa::mewa)
121 (setf new-instance (instance new-instance)))
122
123 ;;;; sorry about that, and now back t our regular program.
124
125 (meta-model:sync-instance new-instance)
e9454185
DC
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)))))
569ad9e6 128
e9454185 129
579597e3 130(defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
131 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
19531fbd 132 (let* ((i (foreign-instance slot))
579597e3 133 (pres (mewa::make-presentation
134 i
135 :type :one-line
136 :initargs (list
137 :global-properties
19531fbd 138 (list :editablep nil :linkedp nil)))))
9d6c69fb 139 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
498061f6 140 (setf (component.place pres) (component.place (ucw::parent slot))))
141 (when i (<ucw:render-component :component pres))))
579597e3 142
7d87c1d2 143
144
579597e3 145(defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
146 (present-relation slot instance))
147
148(defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
149 ()
150 (:type-name foreign-key)
151 (:default-initargs))
152
153(defaction view-instance ((self component) instance &rest initargs)
12dcf3d4 154 (call-component (parent self) (apply #'mewa:make-presentation instance initargs))
155 ;; the viewed instance could have been changed/deleted, so we sync this instance
156 (meta-model:sync-instance (instance (parent self))))
579597e3 157
498061f6 158
569ad9e6
DC
159
160(defmethod present-slot :before ((slot foreign-key-slot-presentation) instance)
161 ())
162
163
164(defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
f1ce8b6e
DC
165 (setf (foreign-instance slot)
166 (when (presentation-slot-value slot instance)
167 (meta-model:explode-foreign-key instance (slot-name slot))))
f1ce8b6e
DC
168 (flet ((render () (when (foreign-instance slot)(call-next-method))))
169 (if (slot-boundp slot 'place)
170 (cond
171 ((editablep slot)
172 (render)
173 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
569ad9e6 174 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
f1ce8b6e
DC
175 ((linkedp slot)
176 (<ucw:a :action (view-instance slot (foreign-instance slot))
177 (render)))
178 (t
179 (render)))
180 ;; presentation is used only for rendering
181 (render))))
579597e3 182
183;;;; HAS MANY
184(defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
12dcf3d4 185 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
579597e3 186 (:type-name has-many))
187
498061f6 188(defaction add-to-has-many ((slot has-many-slot-presentation) instance)
8e6e6b56
DC
189 ;; if the instance is not stored we must make sure to mark it stored now!
190 (unless (mewa::instance-is-stored-p instance)
191 (setf (mewa::modifiedp (parent self)) t))
192 ;; sync up the instance
85281029 193 (mewa:ensure-instance-sync (parent slot))
8e6e6b56 194
85281029
DC
195 (multiple-value-bindf (class home foreign)
196 (meta-model:explode-has-many instance (slot-name slot))
498061f6 197 (let ((new (make-instance class)))
198 (setf (slot-value new foreign) (slot-value instance home))
e9454185 199 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
569ad9e6 200 (call-component (parent slot) (mewa:make-presentation new :type (creator slot)))
e9454185 201 (meta-model:sync-instance instance))))
498061f6 202
579597e3 203(defmethod present-slot ((slot has-many-slot-presentation) instance)
f1ce8b6e
DC
204 (when (slot-boundp slot 'place)
205 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
206 (let ((i (get-foreign-instances slot instance)))
207
498061f6 208 (<:ul
209 (dolist (s i)
210 (let ((s s))
211 (setf (foreign-instance slot) s)
e9454185
DC
212 (when (slot-boundp slot 'place)
213 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
498061f6 214 (<:li (setf (linkedp slot) nil)
e9454185 215 (present-relation slot instance)))))))))
579597e3 216
217
218(defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
219 (slot-value instance (slot-name slot)))
220
ec4f5786 221(defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
222 (get-foreign-instances slot instance))
223
579597e3 224(defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
225 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
226 (current :accessor current :initform 0)
227 (len :accessor len )
228 (instances :accessor instances))
229
230 (:type-name has-very-many))
231
232(defmethod list-next ((slot has-very-many-slot-presentation))
233 (setf (current slot) (incf (current slot) (number-to-display slot)))
234 (when (< (len slot) (current slot))
235 (setf (current slot) (- (number-to-display slot) (len slot)))))
236
237(defmethod list-prev ((slot has-very-many-slot-presentation))
238 (setf (current slot) (decf (current slot) (number-to-display slot)))
239 (when (> 0 (current slot))
240 ;;what to do here is open to debate
241 (setf (current slot) (- (len slot)(number-to-display slot) ))))
242
243
244(defmethod present-slot ((slot has-very-many-slot-presentation) instance)
245 ;;(<:as-html "isance: " instance)
f1ce8b6e
DC
246 (if (slot-boundp slot 'place)
247 (progn
248 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
249 (let ((self (parent slot)))
250 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
251 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
252 (<ucw:a :action (list-next slot) (<:as-html ">>"))
253 (call-next-method)
254 (<:as-html "total :" (len slot)))
255 (call-next-method)))
579597e3 256
257(defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
258 (let ((f (call-next-method)))
259 (setf (len slot) (length f))
260 (setf (instances slot) f)
261 (loop for cons on (nthcdr (current slot) f)
262 for i from 0 upto (number-to-display slot)
263 collect (car cons))))
264
265(defslot-presentation has-a-slot-presentation (one-of-presentation)
266 ((key :initarg :key :accessor key))
267 (:type-name has-a))
268
269(defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
270 (slot-value object slot-name))
271
272(defmethod present-slot ((slot has-a-slot-presentation) instance)
273 (<:as-html (presentation-slot-value slot instance))
274 (if (editablep slot)
275 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
276 (when (allow-nil-p slot)
277 (<ucw:option :value nil (<:as-html (none-label slot))))
278 (dolist (option (get-foreign-instances (presentation slot) instance))
279 (setf (instance (presentation slot)) option)
280 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
281 (if (presentation-slot-value slot instance)
282 (progn
283 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
284 (present (presentation slot)))
85281029 285 (<:as-html "--"))))
5f0b37e7 286
5f0b37e7 287
5f0b37e7 288
2cb4247d
DC
289
290
291
292