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