Removed most of the old LoL stuff for good.
[clinton/lisp-on-lines.git] / src / attributes / relational-attributes.lisp
... / ...
CommitLineData
1(in-package :lisp-on-lines)
2
3;;;; * Relational Attributes
4
5
6;;;; ** has-a
7;;;; Used for foreign keys, currently only works with clsql.
8
9(defattribute has-a ()
10 ()
11 (:default-properties
12 :has-a nil
13 :test 'meta-model::generic-equal))
14
15;;
16(define-layered-method attribute-value (object (attribute has-a))
17 (multiple-value-bind (obj key class)
18 (meta-model:explode-foreign-key object (slot-name attribute) :nilp t)
19 (if (persistentp object)
20 obj
21 (first (select class
22 :where [= [slot-value class key] (call-next-method)]
23 :flatp t
24 )))))
25
26(define-layered-method (setf attribute-value) ((value standard-object) object (attribute has-a))
27 (let ((val (slot-value value (find-if (curry #'primary-key-p value) (list-keys value)))))
28 (setf (attribute-value object attribute) val)))
29
30
31
32(define-layered-function find-all-foreign-objects (o a))
33
34(define-layered-method find-all-foreign-objects (object (attribute has-a))
35 (select (meta-model:find-join-class object (slot-name attribute)) :flatp t))
36
37(defdisplay ((attribute has-a) object)
38 (let ((args (plist-union (description.properties attribute) (has-a attribute)))
39 (val (attribute-value object attribute)))
40 (when val
41 (setf (getf args :type)
42 'lol::one-line))
43 (apply #'display* val
44 args)))
45
46
47(defdisplay
48 :in-layer editor ((attribute has-a) object)
49 (<ucw:select
50 :accessor (attribute-value object attribute)
51
52 :test (test attribute)
53 (dolist* (obj (find-all-foreign-objects object attribute))
54 (<ucw:option
55 :value obj
56 (display* obj :layers '(+ as-string))))))
57
58
59;;;; ** Has-Many attribute
60
61(defattribute has-many ()
62 ()
63 (:default-properties
64 :add-new-label "Add New"
65 :sort-arguments (list #'< :key #'(lambda (x) (funcall (car (list-keys x)) x))))
66 (:default-initargs
67 :type 'lol::one-line))
68
69(define-layered-method
70 attribute-value (object (has-many has-many))
71 (slot-value object (slot-name has-many)))
72
73(defdisplay ((attribute has-many) object)
74 ;
75 ;(<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label attribute))
76
77 (<:div :style "clear:both;"
78 (let* ((i (apply #'sort (slot-value object (slot-name attribute))
79 (sort-arguments attribute))))
80 (<:ul
81 (dolist* (x i)
82 (<:li (display* x
83 :type 'lol::as-string
84 :layers '(+ wrap-link - label-attributes))))))))
85
86
87(defun find-many-to-many-class (slot-name instance)
88 (let* ((imd (getf (meta-model::find-slot-metadata instance slot-name)
89 :db-info))
90 (jc (make-instance (getf imd :join-class)))
91 (jcmd (getf (meta-model::find-slot-metadata jc (getf imd :target-slot))
92 :db-info)))
93 (getf jcmd :join-class)))
94
95
96(defattribute many-to-many ()
97 ())
98
99
100
101(defdisplay ((attribute many-to-many) object)
102 (<:as-html "ASDASD"))
103
104 #+nil(let ((instances (select-instances object t))
105 new-instance)
106 (<:ul
107 (<:li (<ucw:button :action (add-new-relation component object (.get slot-name))
108 (<:as-html "Add New")))
109 (<:li (<ucw:button :action (add-new-relation component object new-instance)
110 (<:as-html "Add:"))
111 (<ucw:select :accessor new-instance
112 (arnesi:dolist* (i instances)
113 (<ucw:option
114 :value i
115 (display component i :type 'one-line)))))
116 (dolist* (i (attribute-value object attribute))
117 (<:li
118 (<ucw:a :action (call-view ((car i) (action-view slot) (ucw::parent slot)))
119 (<:as-html "(view) "))
120 (<ucw:a :action (delete-relationship slot (second i) instance)
121 (<:as-html "(remove) "))
122 (display component object)))))
123 ;(display component (mapcar #'car (slot-value object (.get :slot-name))))
124