enhanced has-many presentations to include adding new items
[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 (multiple-value-call #'list (apply function args)))
5
6 (defmacro multiple-value-bindf (vars form &body body)
7 `(destructuring-bind ,vars
8 (multiple-value-funcall->list #',(car form) ,@(cdr form))
9 ,@body))
10
11 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
12 ()
13 (:type-name clsql-sys:wall-time))
14
15 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
16 (let ((date (call-next-method)))
17 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
18 (format nil "~a/~a/~a" m d y)))))
19
20 (defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
21 (let ((new-time (clsql:parse-date-time (remove #\Space value)))
22 (old-time (when (slot-boundp instance (slot-name slot))
23 (slot-value instance (slot-name slot)))))
24 (unless (or (eql old-time new-time)
25 (and (null old-time) new-time)
26 (equal :equal (clsql:time-compare new-time old-time)))
27 (setf (presentation-slot-value slot instance) new-time ))))
28
29 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
30 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
31
32 (defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
33 (let ((date (presentation-slot-value slot instance))
34 (input-id (string (gensym))))
35 (if (and date (not (editablep slot)))
36 (<:span (<:as-html date)))
37 (when (editablep slot)
38 (<ucw:input :accessor (presentation-slot-value slot instance) :id input-id)
39 (<:script :type "text/javascript"
40 (<:as-is (format nil "
41 Calendar.setup({
42 inputField : \"~a\",
43 ifFormat : \"%m/%d/%Y\",
44 });" input-id))))))
45
46 (defslot-presentation mewa-relation-slot-presentation ()
47 ((slot-name :accessor slot-name :initarg :slot-name)
48 (foreign-instance :accessor foreign-instance)
49 (linkedp :accessor linkedp :initarg :linkedp :initform t))
50 (:type-name relation))
51
52 (defun get-fkey-data (instance slot-name)
53 "ugly workaround b/c UCW does not like M-V-B"
54 (multiple-value-bind (finstance foreign-slot-name)
55 (meta-model:explode-foreign-key instance slot-name)
56 (cons finstance foreign-slot-name)))
57
58 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
59 (let* ((d (get-fkey-data instance (slot-name slot)))
60 (finstance (car d))
61 (foreign-slot-name (cdr d))
62 (new-instance
63 (call-component
64 (parent slot)
65 (make-instance 'mewa::mewa-presentation-search
66 :search-presentation
67 (mewa:make-presentation finstance
68 :type :search-presentation)
69 :list-presentation
70 (mewa:make-presentation finstance
71 :type :listing)))))
72 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
73 (meta-model:sync-instance instance)
74 (clsql:update-objects-joins (list instance))))
75
76 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
77 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
78 (let* ((i (foreign-instance slot))
79 (pres (mewa::make-presentation
80 i
81 :type :one-line
82 :initargs (list
83 :global-properties
84 (list :editablep nil :linkedp nil)))))
85 (when (ucw::parent slot)
86 (setf (component.place pres) (component.place (ucw::parent slot))))
87 (when i (<ucw:render-component :component pres))))
88 ))
89
90
91
92 (defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
93 (present-relation slot instance))
94
95 (defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
96 ()
97 (:type-name foreign-key)
98 (:default-initargs))
99
100 (defaction view-instance ((self component) instance &rest initargs)
101 (call-component (parent self) (apply #'mewa:make-presentation instance initargs)))
102
103
104 (defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
105 (setf (foreign-instance slot) (when (presentation-slot-value slot instance) (meta-model:explode-foreign-key instance (slot-name slot))))
106 (flet ((render () (call-next-method)))
107 (cond
108 ((editablep slot)
109 (render)
110 (<ucw:a :action (search-records slot instance) (<:as-html " (search)"))
111 (<ucw:a :action (create-record slot instance) (<:as-html " (new)")))
112 ((linkedp slot)
113 (<ucw:a :action (view-instance slot (foreign-instance slot))
114 (render)))
115 (t
116 (render)))))
117
118 ;;;; HAS MANY
119 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
120 ()
121 (:type-name has-many))
122
123
124 (defun get-join-class-info (slot instance)
125 "hack around m-v-b"
126 (multiple-value-bind (s h f) (meta-model:explode-has-many instance (slot-name slot))
127 (list s h f)))
128 ((jci (get-join-class-info slot instance))
129 (class (first jci))
130 (home (second jci))
131 (foreign (third jci)))
132
133 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
134 (destructuring-bind (class home foreign)
135 (maxwell-web-gui::multiple-value-funcall #'meta-model:explode-has-many instance (slot-name slot))
136 (let ((new (make-instance class)))
137 (setf (slot-value new foreign) (slot-value instance home))
138 (meta-model:sync-instance new)
139 (call-component (parent slot) (mewa:make-presentation new :type :editor)))))
140
141 (defmethod present-slot ((slot has-many-slot-presentation) instance)
142 (<ucw:a :action (add-to-has-many slot instance)
143 (<:as-html "(add new)"))
144 (let ((i (get-foreign-instances slot instance))
145 (linkedp (linkedp slot)))
146 (<:ul
147 (dolist (s i)
148 (let ((s s))
149 (setf (foreign-instance slot) s)
150 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
151 (<:li (setf (linkedp slot) nil)
152 (present-relation slot instance))))))))
153
154
155 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
156 (slot-value instance (slot-name slot)))
157
158 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
159 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
160 (current :accessor current :initform 0)
161 (len :accessor len )
162 (instances :accessor instances))
163
164 (:type-name has-very-many))
165
166 (defmethod list-next ((slot has-very-many-slot-presentation))
167 (setf (current slot) (incf (current slot) (number-to-display slot)))
168 (when (< (len slot) (current slot))
169 (setf (current slot) (- (number-to-display slot) (len slot)))))
170
171 (defmethod list-prev ((slot has-very-many-slot-presentation))
172 (setf (current slot) (decf (current slot) (number-to-display slot)))
173 (when (> 0 (current slot))
174 ;;what to do here is open to debate
175 (setf (current slot) (- (len slot)(number-to-display slot) ))))
176
177
178 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
179 ;;(<:as-html "isance: " instance)
180 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
181 (let ((self (parent slot)))
182 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
183 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
184 (<ucw:a :action (list-next slot) (<:as-html ">>"))
185 (call-next-method)
186 (<:as-html "total :" (len slot)))
187
188 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
189 (let ((f (call-next-method)))
190 (setf (len slot) (length f))
191 (setf (instances slot) f)
192 (loop for cons on (nthcdr (current slot) f)
193 for i from 0 upto (number-to-display slot)
194 collect (car cons))))
195
196 (defslot-presentation has-a-slot-presentation (one-of-presentation)
197 ((key :initarg :key :accessor key))
198 (:type-name has-a))
199
200 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
201 (slot-value object slot-name))
202
203 (defmethod present-slot ((slot has-a-slot-presentation) instance)
204 (<:as-html (presentation-slot-value slot instance))
205 (if (editablep slot)
206 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
207 (when (allow-nil-p slot)
208 (<ucw:option :value nil (<:as-html (none-label slot))))
209 (dolist (option (get-foreign-instances (presentation slot) instance))
210 (setf (instance (presentation slot)) option)
211 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
212 (if (presentation-slot-value slot instance)
213 (progn
214 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
215 (present (presentation slot)))
216 (<:as-html "--"))))