Renamed fill-gaps-only to fill-gaps-only-p, and added the :fill-gaps-only-p initarg...
[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 t))))
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) (when (presentation-slot-value slot instance) (meta-model:explode-foreign-key instance (slot-name slot))))
152 (flet ((render () (call-next-method)))
153 (cond
154 ((editablep slot)
155 (render)
156 (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline")
157 (<ucw:submit :action (create-record slot instance) :value "Add New" :style "display:inline"))
158 ((linkedp slot)
159 (<ucw:a :action (view-instance slot (foreign-instance slot))
160 (render)))
161 (t
162 (render)))))
163
164 ;;;; HAS MANY
165 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
166 ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New"))
167 (:type-name has-many))
168
169
170 (defaction add-to-has-many ((slot has-many-slot-presentation) instance)
171 (mewa:ensure-instance-sync (parent slot))
172 (multiple-value-bindf (class home foreign)
173 (meta-model:explode-has-many instance (slot-name slot))
174 (let ((new (make-instance class)))
175 (setf (slot-value new foreign) (slot-value instance home))
176 (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self))
177 (call-component (parent slot) (mewa:make-presentation new :type :editor))
178 (meta-model:sync-instance instance))))
179
180 (defmethod present-slot ((slot has-many-slot-presentation) instance)
181 (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot))
182 (let ((i (get-foreign-instances slot instance))
183 (linkedp (linkedp slot)))
184 (<:ul
185 (dolist (s i)
186 (let ((s s))
187 (setf (foreign-instance slot) s)
188 (when (slot-boundp slot 'place)
189 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
190 (<:li (setf (linkedp slot) nil)
191 (present-relation slot instance)))))))))
192
193
194 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
195 (slot-value instance (slot-name slot)))
196
197 (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance)
198 (get-foreign-instances slot instance))
199
200 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
201 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
202 (current :accessor current :initform 0)
203 (len :accessor len )
204 (instances :accessor instances))
205
206 (:type-name has-very-many))
207
208 (defmethod list-next ((slot has-very-many-slot-presentation))
209 (setf (current slot) (incf (current slot) (number-to-display slot)))
210 (when (< (len slot) (current slot))
211 (setf (current slot) (- (number-to-display slot) (len slot)))))
212
213 (defmethod list-prev ((slot has-very-many-slot-presentation))
214 (setf (current slot) (decf (current slot) (number-to-display slot)))
215 (when (> 0 (current slot))
216 ;;what to do here is open to debate
217 (setf (current slot) (- (len slot)(number-to-display slot) ))))
218
219
220 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
221 ;;(<:as-html "isance: " instance)
222 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
223 (let ((self (parent slot)))
224 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
225 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
226 (<ucw:a :action (list-next slot) (<:as-html ">>"))
227 (call-next-method)
228 (<:as-html "total :" (len slot)))
229
230 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
231 (let ((f (call-next-method)))
232 (setf (len slot) (length f))
233 (setf (instances slot) f)
234 (loop for cons on (nthcdr (current slot) f)
235 for i from 0 upto (number-to-display slot)
236 collect (car cons))))
237
238 (defslot-presentation has-a-slot-presentation (one-of-presentation)
239 ((key :initarg :key :accessor key))
240 (:type-name has-a))
241
242 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
243 (slot-value object slot-name))
244
245 (defmethod present-slot ((slot has-a-slot-presentation) instance)
246 (<:as-html (presentation-slot-value slot instance))
247 (if (editablep slot)
248 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
249 (when (allow-nil-p slot)
250 (<ucw:option :value nil (<:as-html (none-label slot))))
251 (dolist (option (get-foreign-instances (presentation slot) instance))
252 (setf (instance (presentation slot)) option)
253 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
254 (if (presentation-slot-value slot instance)
255 (progn
256 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
257 (present (presentation slot)))
258 (<:as-html "--"))))