Commit | Line | Data |
---|---|---|
579597e3 | 1 | (in-package :it.bese.ucw) |
2 | ||
19a82bd5 | 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)) | |
579597e3 | 10 | |
e9454185 DC |
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) | |
9d6c69fb | 46 | |
9d6c69fb | 47 | |
f4170204 | 48 | (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation) |
579597e3 | 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) | |
f4170204 | 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) | |
ec4f5786 | 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 )))) | |
579597e3 | 65 | |
19531fbd | 66 | (defmethod label :around ((slot clsql-wall-time-slot-presentation)) |
67 | (concatenate 'string (call-next-method) " (mm/dd/yyyy)")) | |
68 | ||
579597e3 | 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 | ||
e9454185 DC |
83 | (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation) |
84 | ((foreign-instance :accessor foreign-instance) | |
579597e3 | 85 | (linkedp :accessor linkedp :initarg :linkedp :initform t)) |
86 | (:type-name relation)) | |
87 | ||
7d87c1d2 | 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) | |
e9454185 DC |
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)) | |
f1ce8b6e | 109 | (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self))))) |
e9454185 DC |
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 | ||
579597e3 | 121 | (defmethod present-relation ((slot mewa-relation-slot-presentation) instance) |
122 | ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance ) | |
19531fbd | 123 | (let* ((i (foreign-instance slot)) |
579597e3 | 124 | (pres (mewa::make-presentation |
125 | i | |
126 | :type :one-line | |
127 | :initargs (list | |
128 | :global-properties | |
19531fbd | 129 | (list :editablep nil :linkedp nil))))) |
9d6c69fb | 130 | (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place)) |
498061f6 | 131 | (setf (component.place pres) (component.place (ucw::parent slot)))) |
132 | (when i (<ucw:render-component :component pres)))) | |
579597e3 | 133 | |
7d87c1d2 | 134 | |
135 | ||
579597e3 | 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) | |
12dcf3d4 | 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)))) | |
579597e3 | 148 | |
498061f6 | 149 | |
150 | (defmethod present-slot :around ((slot foreign-key-slot-presentation) instance) | |
f1ce8b6e DC |
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)))) | |
579597e3 | 169 | |
170 | ;;;; HAS MANY | |
171 | (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation) | |
12dcf3d4 | 172 | ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New")) |
579597e3 | 173 | (:type-name has-many)) |
174 | ||
498061f6 | 175 | |
498061f6 | 176 | (defaction add-to-has-many ((slot has-many-slot-presentation) instance) |
85281029 DC |
177 | (mewa:ensure-instance-sync (parent slot)) |
178 | (multiple-value-bindf (class home foreign) | |
179 | (meta-model:explode-has-many instance (slot-name slot)) | |
498061f6 | 180 | (let ((new (make-instance class))) |
181 | (setf (slot-value new foreign) (slot-value instance home)) | |
e9454185 | 182 | (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self)) |
ec4f5786 | 183 | (call-component (parent slot) (mewa:make-presentation new :type :editor)) |
e9454185 | 184 | (meta-model:sync-instance instance)))) |
498061f6 | 185 | |
579597e3 | 186 | (defmethod present-slot ((slot has-many-slot-presentation) instance) |
f1ce8b6e DC |
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 | ||
498061f6 | 191 | (<:ul |
192 | (dolist (s i) | |
193 | (let ((s s)) | |
194 | (setf (foreign-instance slot) s) | |
e9454185 DC |
195 | (when (slot-boundp slot 'place) |
196 | (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil))) | |
498061f6 | 197 | (<:li (setf (linkedp slot) nil) |
e9454185 | 198 | (present-relation slot instance))))))))) |
579597e3 | 199 | |
200 | ||
201 | (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance) | |
202 | (slot-value instance (slot-name slot))) | |
203 | ||
ec4f5786 | 204 | (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance) |
205 | (get-foreign-instances slot instance)) | |
206 | ||
579597e3 | 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) | |
f1ce8b6e DC |
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))) | |
579597e3 | 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))) | |
85281029 | 268 | (<:as-html "--")))) |