sync instance when adding many to many ..
[clinton/lisp-on-lines.git] / src / slot-presentations.lisp
1 (in-package :lisp-on-lines)
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 5)
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
25 :accessor (presentation-slot-value slot instance)
26 :reader (or (presentation-slot-value slot instance)
27 "")
28 :rows (rows slot)
29 :cols (columns slot))
30 (if (escape-html-p slot)
31 (<:as-html (presentation-slot-value slot instance))
32 (<:as-is (presentation-slot-value slot instance)))))
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 (show-label-p :accessor show-label-p :initarg :show-label-p :initform t))
47 (:documentation "The superclass of all Mewa slot presentations"))
48
49 ;;;; this has to be in the eval when i would think
50 (eval-when (:compile-toplevel :load-toplevel :execute)
51 (defun generate-slot-presentation-definition-for-type (type)
52 (let* ((u-name (intern (format nil "~A-SLOT-PRESENTATION" type)))
53 (sp-name (intern (format nil "MEWA-~A" u-name)))
54 (t-name (intern (format nil "MEWA-~A" type))))
55 `(defslot-presentation ,sp-name (,u-name mewa-slot-presentation)
56 ()
57 (:type-name ,t-name)))))
58
59 (defmacro define-base-mewa-presentations (&body types)
60 "Define the mewa-slot-presentations by subclassing the base UCW ones"
61 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
62 types)))
63
64 ;;;then actually define the base presentations :
65 (define-base-mewa-presentations
66 boolean
67 string
68 number
69 integer
70 currency)
71
72 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
73 ((input-id :accessor input-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+))
74 (trigger-id :accessor trigger-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+)))
75 (:type-name clsql-sys:wall-time))
76
77 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
78 (let ((date (call-next-method)))
79 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
80 (format nil "~a/~a/~a" m d y)))))
81
82 (defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
83 (let ((new-time (clsql:parse-date-time (remove #\Space value)))
84 (old-time (when (slot-boundp instance (slot-name slot))
85 (slot-value instance (slot-name slot)))))
86 (unless (or (eql old-time new-time)
87 (when (and new-time old-time)
88 (equal :equal (clsql:time-compare new-time old-time))))
89 (setf (presentation-slot-value slot instance) new-time ))))
90
91 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
92 (concatenate 'string (call-next-method) " (m/d/y)"))
93
94 (defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
95 (let ((date (presentation-slot-value slot instance)))
96 (if (and date (not (editablep slot)))
97 (<:as-html date))
98 (when (editablep slot)
99 (<ucw:input :accessor (presentation-slot-value slot instance) :id (input-id slot) :style "display:inline")
100 (<:button :id (trigger-id slot) (<:as-html "[...]"))
101 (<:script :type "text/javascript"
102 (<:as-is (format nil "
103
104 Calendar.setup({
105 inputField : \"~a\",
106 button : \"~a\",
107 ifFormat : \"%m/%d/%Y\" });" (input-id slot) (trigger-id slot)))))))
108
109 (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
110 ((foreign-instance :accessor foreign-instance)
111 (linkedp :accessor linkedp :initarg :linkedp :initform t)
112 (creator :accessor creator :initarg :creator :initform :editor)
113 (new-instance :accessor new-instance :initform nil))
114 (:type-name relation))
115
116 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
117 (multiple-value-bindf (finstance foreign-slot-name)
118 (meta-model:explode-foreign-key instance (slot-name slot))
119 (let ((new-instance (new-instance self)))
120 (unless new-instance
121 (setf (new-instance self)
122 (call-component
123 (ucw::parent slot)
124 (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search))
125 'mewa::mewa-presentation-search)
126 :search-presentation
127 (mewa:make-presentation finstance
128 :type :search-presentation)
129 :list-presentation
130 (mewa:make-presentation finstance
131 :type :listing)))))
132 (sync-foreign-instance slot new-instance))))
133
134 (defmethod sync-foreign-instance ((slot mewa-relation-slot-presentation) foreign-instance)
135 (let ((instance (instance (ucw::parent slot))))
136 (multiple-value-bind (foo f-slot-name)
137 (meta-model:explode-foreign-key instance (slot-name slot))
138 (setf (slot-value instance (slot-name slot)) (slot-value foreign-instance f-slot-name))
139 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p slot)))))
140
141
142 (defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance)
143 (multiple-value-bindf (finstance foreign-slot-name)
144 (meta-model:explode-foreign-key instance (slot-name slot))
145 (let ((new-instance
146 (call-component
147 (ucw::parent slot)
148 (mewa:make-presentation finstance :type (creator self)))))
149
150 ;;;; TODO: this next bit is due to a bad design decision.
151 ;;;; Components should always have (ok) return self, but somewhere
152 ;;;; i've made in return (instance self) sometimes, and this
153 ;;;; bahaviour is totatlly fucked.
154
155 (when (typep new-instance 'mewa::mewa)
156 (setf new-instance (instance new-instance)))
157
158 ;;;; sorry about that, and now back t our regular program.
159
160 (meta-model:sync-instance new-instance)
161 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
162 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
163
164
165 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
166 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
167 (let* ((i (foreign-instance slot))
168 (pres (mewa::make-presentation
169 i
170 :type :one-line
171 :initargs (list
172 :global-properties
173 (list :editablep nil :linkedp nil)))))
174 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
175 (setf (component.place pres) (component.place (ucw::parent slot))))
176 (when i (<ucw:render-component :component pres))))
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 (render)
201 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
202 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
203 ((linkedp slot)
204 (<ucw:a :action (view-instance slot (foreign-instance slot))
205 (render)))
206 (t
207 (render)))
208 ;; presentation is used only for rendering
209 (render))))
210
211
212 (defmethod find-foreign-instances ((slot foreign-key-slot-presentation))
213 (clsql:select (class-name (class-of (meta-model:explode-foreign-key (instance slot) (slot-name slot))))))
214
215
216
217 ;;;; HAS MANY
218 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
219 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
220 (:type-name has-many))
221
222 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
223 ;; if the instance is not stored we must make sure to mark it stored now!
224 (unless (meta-model::persistentp instance)
225 (setf (mewa::modifiedp (ucw::parent self)) t))
226 ;; sync up the instance
227 ;;(mewa:ensure-instance-sync (parent slot))
228 (meta-model:sync-instance (instance (ucw::parent slot)))
229
230 (multiple-value-bindf (class home foreign)
231 (meta-model:explode-has-many instance (slot-name slot))
232 (let ((new (make-instance class)))
233 (setf (slot-value new foreign) (slot-value instance home))
234 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
235 (call-component (ucw::parent slot) (mewa:make-presentation new :type (creator slot)))
236 (meta-model:sync-instance instance))))
237
238 (defmethod present-slot ((slot has-many-slot-presentation) instance)
239 (when (slot-boundp slot 'ucw::place)
240 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
241 (let* ((i (get-foreign-instances slot instance))
242 (presentation (and i (make-presentation (car i) :type :one-line))))
243 (when i
244 (flet ((linker (i string)
245 (<ucw:a
246 :action (view-instance slot i
247 :initargs
248 `(:global-properties ,
249 (list
250 :linkedp t
251 :editablep nil)))
252 (<:as-html string))))
253 (<:table
254 (<:tr
255 (<:th) ;empty col for (view) link
256 (dolist (s (slots presentation))
257 (<:th (<:as-html (label s)))))
258 (dolist (s i)
259 (let ((s s))
260 (setf (foreign-instance slot) s)
261 (when (slot-boundp slot 'ucw::place)
262 (<:tr
263 (<:td (linker s " (view) "))
264 (dolist (p (slots (make-presentation s :type :one-line
265 :initargs
266 '(:global-properties
267 (:editablep nil)))))
268 (<:td
269
270 (present-slot p s))))))))))))
271
272
273 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
274 (slot-value instance (slot-name slot)))
275
276 (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
277 (get-foreign-instances slot instance))
278
279 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
280 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
281 (current :accessor current :initform 0)
282 (len :accessor len )
283 (instances :accessor instances))
284 (:type-name has-very-many))
285
286 (defmethod list-next ((slot has-very-many-slot-presentation))
287 (setf (current slot) (incf (current slot) (number-to-display slot)))
288 (when (< (len slot) (current slot))
289 (setf (current slot) (- (number-to-display slot) (len slot)))))
290
291 (defmethod list-prev ((slot has-very-many-slot-presentation))
292 (setf (current slot) (decf (current slot) (number-to-display slot)))
293 (when (> 0 (current slot))
294 ;;what to do here is open to debate
295 (setf (current slot) (- (len slot)(number-to-display slot) ))))
296
297
298 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
299 ;;(<:as-html "isance: " instance)
300 (if (slot-boundp slot 'ucw::place)
301 (progn
302 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
303 (let ((self (ucw::parent slot)))
304 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
305 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
306 (<ucw:a :action (list-next slot) (<:as-html ">>"))
307 (call-next-method)
308 (<:as-html "total :" (len slot)))
309 (call-next-method)))
310
311 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
312 (let ((f (call-next-method)))
313 (setf (len slot) (length f))
314 (setf (instances slot) f)
315 (loop for cons on (nthcdr (current slot) f)
316 for i from 0 upto (number-to-display slot)
317 collect (car cons))))
318
319
320 ;;;; * Has-a
321 (defslot-presentation has-a-slot-presentation (mewa-relation-slot-presentation)
322 ((allow-nil-p :accessor allow-nil-p :initarg :allow-nil-p :initform t)
323 (attributes :accessor attributes :initarg :attributes :initform nil))
324 (:type-name has-a))
325
326 (defmethod find-foreign-slot-value ((slot has-a-slot-presentation) (object t))
327 (multiple-value-bind (c s)
328 (meta-model:explode-foreign-key (instance (ucw::parent slot)) (slot-name slot))
329 (slot-value object s)))
330
331 (defmethod get-foreign-instances ((slot mewa-relation-slot-presentation) instance)
332 (clsql:select (class-name (class-of
333 (meta-model:explode-foreign-key instance (slot-name slot))))
334 :flatp t))
335
336 (defmethod present-slot ((slot has-a-slot-presentation) instance)
337 ; (<:as-html (presentation-slot-value slot instance))
338 (if (editablep slot)
339 (progn (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
340 (when (allow-nil-p slot)
341 (<ucw:option :value nil (<:as-html "none")))
342 (dolist (option (get-foreign-instances slot instance))
343 (<ucw:option :value (find-foreign-slot-value slot option)
344 (lol:present
345 (lol:make-presentation option
346 :type :as-string
347 :initargs
348 `(:attributes ,(attributes slot)))
349 ))))
350 (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline"))
351 (if (presentation-slot-value slot instance)
352 (progn
353 (lol:present
354 (lol:make-presentation (meta-model:explode-foreign-key instance (slot-name slot))
355 :type :one-line
356 :initargs
357 `(:attributes ,(attributes slot)))
358 ))
359 (<:as-html "--"))))
360
361 (defslot-presentation many-to-many-slot-presentation (mewa-relation-slot-presentation)
362 ((list-view :accessor list-view :initarg :list-view :initform :one-line)
363 (action-view :accessor action-view :initarg :action-view :initform :viewer)
364 (create-view :initform :creator)
365 (select-view :initform :as-string :accessor select-view))
366 (:type-name many-to-many)
367 (:default-initargs :label "many to many"))
368
369 (defun %delete-item (item)
370 (clsql:with-default-database (clsql:*default-database*)
371 (ignore-errors
372 (clsql:delete-instance-records item))))
373
374 (defaction delete-item ((self component) instance)
375 (multiple-value-bind (res err) (%delete-item instance)
376 (if (not err)
377 (call 'info-message :message "Removed Instance")
378 (call 'info-message :message (format nil "Could not remove item. Try removing associated items first. ~A" instance)))))
379
380 (defaction delete-relationship ((slot many-to-many-slot-presentation) rel instance)
381 (delete-item (ucw::parent self) rel)
382 (sync-instance instance)
383 (answer-component (ucw::parent self) t))
384
385
386 (defun find-many-to-many-join-class (slot instance)
387 (let* ((imd (getf (meta-model::find-slot-metadata instance (slot-name slot))
388 :db-info))
389 (jc (make-instance (getf imd :join-class)))
390 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
391 :db-info)))
392 (getf jcmd :join-class)))
393
394 (defmethod find-all-instances ((slot many-to-many-slot-presentation) instance)
395 (clsql:select (find-many-to-many-join-class slot instance) :flatp t))
396
397 (defmethod present-slot ((slot many-to-many-slot-presentation) instance)
398 (let ((instances (slot-value instance (slot-name slot)))
399 new-instance)
400 (<:ul
401 (<:li (<ucw:a :action (add-to-many-to-many slot instance)
402 (<:as-html "Add New")))
403 (<:li (<ucw:button :action (add-to-many-to-many slot instance new-instance)
404 (<:as-html "Add:"))
405 (<ucw:select :accessor new-instance
406 (arnesi:dolist* (i (find-all-instances slot instance))
407 (<ucw:option
408 :value i
409 (lol:present-view (i (select-view slot) slot))))))
410 (dolist* (i instances)
411 (<:li
412 (<ucw:a :action (call-view ((car i) (action-view slot) (ucw::parent slot)))
413 (<:as-html "(view) "))
414 (<ucw:a :action (delete-relationship slot (second i) instance)
415 (<:as-html "(remove) "))
416 (present-view ((car i) (list-view slot) (ucw::parent slot)))) ))))
417
418 (defaction add-to-many-to-many ((slot many-to-many-slot-presentation) instance &optional foreign-instance)
419 ;;;; First things first, we sync.
420 (sync-instance instance)
421 (let* (new
422 (imd (getf (meta-model::find-slot-metadata instance (slot-name slot))
423 :db-info))
424 (jc (make-instance (getf imd :join-class)))
425 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
426 :db-info))
427 (fc (make-instance (getf jcmd :join-class)))
428 (c (if
429 foreign-instance
430 foreign-instance
431 (call-view (fc :creator (ucw::parent slot))))))
432 (when c
433 (sync-instance c)
434 ; (error "~A ~A ~A" (getf imd :foreign-key) (getf jcmd :foreign-key) (getf imd :home-key))
435 (setf (slot-value jc (getf imd :foreign-key))
436 (slot-value instance (getf imd :home-key)))
437 (setf (slot-value jc (getf jcmd :home-key))
438 (slot-value c (getf jcmd :foreign-key)))
439 (sync-instance jc)
440
441 (sync-instance instance)
442 c)))
443
444
445
446
447
448
449
450