Fixed search presentations by adding a PREPARE somthing method. too tired to explain...
[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 (defcomponent mewa-slot-presentation ()
12 ((slot-name :accessor slot-name
13 :initarg :slot-name
14 :documentation
15 "The name of the slot being accessed")
16 (fill-gaps-only-p :accessor fill-gaps-only-p
17 :initarg :fill-gaps-only-p
18 :initform nil
19 :documentation
20 "When nil, the instance is syncronised with the database.
21 When T, only the default value for primary keys and the joins are updated."))
22 (:documentation "The superclass of all Mewa slot presentations"))
23
24 ;;;; this has to be in the eval when i would think
25 (eval-when (:compile-toplevel :load-toplevel :execute)
26 (defun generate-slot-presentation-definition-for-type (type)
27 (let* ((u-name (intern (format nil "~A-SLOT-PRESENTATION" type)))
28 (sp-name (intern (format nil "MEWA-~A" u-name)))
29 (t-name (intern (format nil "MEWA-~A" type))))
30 `(defslot-presentation ,sp-name (,u-name mewa-slot-presentation)
31 ()
32 (:type-name ,t-name)))))
33
34 (defmacro define-base-mewa-presentations (&body types)
35 "Define the mewa-slot-presentations by subclassing the base UCW ones"
36 `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type
37 types)))
38
39 ;;;then actually define the base presentations :
40 (define-base-mewa-presentations
41 boolean
42 string
43 number
44 integer
45 currency)
46
47
48 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
49 ()
50 (:type-name clsql-sys:wall-time))
51
52 (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
53 (let ((date (call-next-method)))
54 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
55 (format nil "~a/~a/~a" m d y)))))
56
57 (defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
58 (let ((new-time (clsql:parse-date-time (remove #\Space value)))
59 (old-time (when (slot-boundp instance (slot-name slot))
60 (slot-value instance (slot-name slot)))))
61 (unless (or (eql old-time new-time)
62 (when (and new-time old-time)
63 (equal :equal (clsql:time-compare new-time old-time))))
64 (setf (presentation-slot-value slot instance) new-time ))))
65
66 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
67 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
68
69 (defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
70 (let ((date (presentation-slot-value slot instance))
71 (input-id (string (gensym))))
72 (if (and date (not (editablep slot)))
73 (<:span (<:as-html date)))
74 (when (editablep slot)
75 (<ucw:input :accessor (presentation-slot-value slot instance) :id input-id)
76 (<:script :type "text/javascript"
77 (<:as-is (format nil "
78 Calendar.setup({
79 inputField : \"~a\",
80 ifFormat : \"%m/%d/%Y\",
81 });" input-id))))))
82
83 (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation)
84 ((foreign-instance :accessor foreign-instance)
85 (linkedp :accessor linkedp :initarg :linkedp :initform t))
86 (:type-name relation))
87
88 (defun get-fkey-data (instance slot-name)
89 "ugly workaround b/c UCW does not like M-V-B"
90 (multiple-value-bind (finstance foreign-slot-name)
91 (meta-model:explode-foreign-key instance slot-name)
92 (cons finstance foreign-slot-name)))
93
94 (defaction search-records ((slot mewa-relation-slot-presentation) instance)
95 (multiple-value-bindf (finstance foreign-slot-name)
96 (meta-model:explode-foreign-key instance (slot-name slot))
97 (let ((new-instance
98 (call-component
99 (parent slot)
100 (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search))
101 'mewa::mewa-presentation-search)
102 :search-presentation
103 (mewa:make-presentation finstance
104 :type :search-presentation)
105 :list-presentation
106 (mewa:make-presentation finstance
107 :type :listing)))))
108 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
109 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
110
111 (defaction create-record ((slot mewa-relation-slot-presentation) instance)
112 (multiple-value-bindf (finstance foreign-slot-name)
113 (meta-model:explode-foreign-key instance (slot-name slot))
114 (let ((new-instance
115 (call-component
116 (parent slot)
117 (mewa:make-presentation finstance :type :editor))))
118 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
119 (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self)))))
120
121 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
122 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
123 (let* ((i (foreign-instance slot))
124 (pres (mewa::make-presentation
125 i
126 :type :one-line
127 :initargs (list
128 :global-properties
129 (list :editablep nil :linkedp nil)))))
130 (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place))
131 (setf (component.place pres) (component.place (ucw::parent slot))))
132 (when i (<ucw:render-component :component pres))))
133
134
135
136 (defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
137 (present-relation slot instance))
138
139 (defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
140 ()
141 (:type-name foreign-key)
142 (:default-initargs))
143
144 (defaction view-instance ((self component) instance &rest initargs)
145 (call-component (parent self) (apply #'mewa:make-presentation instance initargs))
146 ;; the viewed instance could have been changed/deleted, so we sync this instance
147 (meta-model:sync-instance (instance (parent self))))
148
149
150 (defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
151 (setf (foreign-instance slot)
152 (when (presentation-slot-value slot instance)
153 (meta-model:explode-foreign-key instance (slot-name slot))))
154
155 (flet ((render () (when (foreign-instance slot)(call-next-method))))
156 (if (slot-boundp slot 'place)
157 (cond
158 ((editablep slot)
159 (render)
160 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
161 (<ucw:submit :action (create-record slot instance) :value "Add New" :style "display:inline"))
162 ((linkedp slot)
163 (<ucw:a :action (view-instance slot (foreign-instance slot))
164 (render)))
165 (t
166 (render)))
167 ;; presentation is used only for rendering
168 (render))))
169
170 ;;;; HAS MANY
171 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
172 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
173 (:type-name has-many))
174
175
176 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
177 (mewa:ensure-instance-sync (parent slot))
178 (multiple-value-bindf (class home foreign)
179 (meta-model:explode-has-many instance (slot-name slot))
180 (let ((new (make-instance class)))
181 (setf (slot-value new foreign) (slot-value instance home))
182 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
183 (call-component (parent slot) (mewa:make-presentation new :type :editor))
184 (meta-model:sync-instance instance))))
185
186 (defmethod present-slot ((slot has-many-slot-presentation) instance)
187 (when (slot-boundp slot 'place)
188 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot)))
189 (let ((i (get-foreign-instances slot instance)))
190
191 (<:ul
192 (dolist (s i)
193 (let ((s s))
194 (setf (foreign-instance slot) s)
195 (when (slot-boundp slot 'place)
196 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
197 (<:li (setf (linkedp slot) nil)
198 (present-relation slot instance)))))))))
199
200
201 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
202 (slot-value instance (slot-name slot)))
203
204 (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
205 (get-foreign-instances slot instance))
206
207 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
208 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
209 (current :accessor current :initform 0)
210 (len :accessor len )
211 (instances :accessor instances))
212
213 (:type-name has-very-many))
214
215 (defmethod list-next ((slot has-very-many-slot-presentation))
216 (setf (current slot) (incf (current slot) (number-to-display slot)))
217 (when (< (len slot) (current slot))
218 (setf (current slot) (- (number-to-display slot) (len slot)))))
219
220 (defmethod list-prev ((slot has-very-many-slot-presentation))
221 (setf (current slot) (decf (current slot) (number-to-display slot)))
222 (when (> 0 (current slot))
223 ;;what to do here is open to debate
224 (setf (current slot) (- (len slot)(number-to-display slot) ))))
225
226
227 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
228 ;;(<:as-html "isance: " instance)
229 (if (slot-boundp slot 'place)
230 (progn
231 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
232 (let ((self (parent slot)))
233 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
234 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
235 (<ucw:a :action (list-next slot) (<:as-html ">>"))
236 (call-next-method)
237 (<:as-html "total :" (len slot)))
238 (call-next-method)))
239
240 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
241 (let ((f (call-next-method)))
242 (setf (len slot) (length f))
243 (setf (instances slot) f)
244 (loop for cons on (nthcdr (current slot) f)
245 for i from 0 upto (number-to-display slot)
246 collect (car cons))))
247
248 (defslot-presentation has-a-slot-presentation (one-of-presentation)
249 ((key :initarg :key :accessor key))
250 (:type-name has-a))
251
252 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
253 (slot-value object slot-name))
254
255 (defmethod present-slot ((slot has-a-slot-presentation) instance)
256 (<:as-html (presentation-slot-value slot instance))
257 (if (editablep slot)
258 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
259 (when (allow-nil-p slot)
260 (<ucw:option :value nil (<:as-html (none-label slot))))
261 (dolist (option (get-foreign-instances (presentation slot) instance))
262 (setf (instance (presentation slot)) option)
263 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
264 (if (presentation-slot-value slot instance)
265 (progn
266 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
267 (present (presentation slot)))
268 (<:as-html "--"))))