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