Commit | Line | Data |
---|---|---|
38a016c7 | 1 | (in-package :mewa) |
579597e3 | 2 | |
19a82bd5 | 3 | (defun multiple-value-funcall->list (function &rest args) |
4e2ecf69 | 4 | "The function to be called by m-v-bf" |
19a82bd5 | 5 | (multiple-value-call #'list (apply function args))) |
6 | ||
7 | (defmacro multiple-value-bindf (vars form &body body) | |
4e2ecf69 | 8 | "Like M-V-B, only it works in actions. form must be a function call" |
19a82bd5 | 9 | `(destructuring-bind ,vars |
10 | (multiple-value-funcall->list #',(car form) ,@(cdr form)) | |
11 | ,@body)) | |
579597e3 | 12 | |
44108fd6 DC |
13 | |
14 | ;;;; ** Textarea Slot Presentation | |
44108fd6 DC |
15 | |
16 | (defslot-presentation text-slot-presentation () | |
d5e996b3 DC |
17 | ((rows :initarg :rows :accessor rows :initform 25) |
18 | (columns :initarg :columns :accessor columns :initform 40) | |
19 | (escape-html-p :initarg :escape-html-p :accessor escape-html-p :initform nil)) | |
44108fd6 DC |
20 | (:type-name text)) |
21 | ||
22 | (defmethod present-slot ((slot text-slot-presentation) instance) | |
23 | (if (editablep slot) | |
24 | (<ucw:textarea :accessor (presentation-slot-value slot instance) | |
25 | :rows (rows slot) | |
26 | :cols (columns slot)) | |
27 | (if (escape-html-p slot) | |
d5e996b3 | 28 | (<:as-html (presentation-slot-value slot instance)) |
44108fd6 | 29 | (<:as-is (presentation-slot-value slot instance)) |
d5e996b3 | 30 | ))) |
44108fd6 DC |
31 | |
32 | ||
e9454185 DC |
33 | (defcomponent mewa-slot-presentation () |
34 | ((slot-name :accessor slot-name | |
35 | :initarg :slot-name | |
36 | :documentation | |
37 | "The name of the slot being accessed") | |
38 | (fill-gaps-only-p :accessor fill-gaps-only-p | |
39 | :initarg :fill-gaps-only-p | |
40 | :initform nil | |
41 | :documentation | |
42 | "When nil, the instance is syncronised with the database. | |
68a53dce DC |
43 | When T, only the default value for primary keys and the joins are updated.") |
44 | (show-label-p :accessor show-label-p :initarg :show-label-p :initform t)) | |
e9454185 DC |
45 | (:documentation "The superclass of all Mewa slot presentations")) |
46 | ||
47 | ;;;; this has to be in the eval when i would think | |
48 | (eval-when (:compile-toplevel :load-toplevel :execute) | |
49 | (defun generate-slot-presentation-definition-for-type (type) | |
50 | (let* ((u-name (intern (format nil "~A-SLOT-PRESENTATION" type))) | |
51 | (sp-name (intern (format nil "MEWA-~A" u-name))) | |
52 | (t-name (intern (format nil "MEWA-~A" type)))) | |
53 | `(defslot-presentation ,sp-name (,u-name mewa-slot-presentation) | |
54 | () | |
55 | (:type-name ,t-name))))) | |
56 | ||
57 | (defmacro define-base-mewa-presentations (&body types) | |
58 | "Define the mewa-slot-presentations by subclassing the base UCW ones" | |
59 | `(progn ,@(mapcar #'generate-slot-presentation-definition-for-type | |
60 | types))) | |
61 | ||
62 | ;;;then actually define the base presentations : | |
63 | (define-base-mewa-presentations | |
64 | boolean | |
65 | string | |
66 | number | |
67 | integer | |
68 | currency) | |
9d6c69fb | 69 | |
f4170204 | 70 | (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation) |
d5e996b3 DC |
71 | ((input-id :accessor input-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+)) |
72 | (trigger-id :accessor trigger-id :initform (arnesi:random-string 10 arnesi:+ascii-alphabet+))) | |
579597e3 | 73 | (:type-name clsql-sys:wall-time)) |
74 | ||
75 | (defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance) | |
76 | (let ((date (call-next-method))) | |
77 | (when date (multiple-value-bind (y m d) (clsql:time-ymd date) | |
78 | (format nil "~a/~a/~a" m d y))))) | |
79 | ||
80 | (defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance) | |
f4170204 | 81 | (let ((new-time (clsql:parse-date-time (remove #\Space value))) |
82 | (old-time (when (slot-boundp instance (slot-name slot)) | |
83 | (slot-value instance (slot-name slot))))) | |
84 | (unless (or (eql old-time new-time) | |
ec4f5786 | 85 | (when (and new-time old-time) |
86 | (equal :equal (clsql:time-compare new-time old-time)))) | |
87 | (setf (presentation-slot-value slot instance) new-time )))) | |
579597e3 | 88 | |
19531fbd | 89 | (defmethod label :around ((slot clsql-wall-time-slot-presentation)) |
ec044146 | 90 | (concatenate 'string (call-next-method) " (m/d/y)")) |
19531fbd | 91 | |
579597e3 | 92 | (defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance) |
d5e996b3 | 93 | (let ((date (presentation-slot-value slot instance))) |
579597e3 | 94 | (if (and date (not (editablep slot))) |
d2dbe50f | 95 | (<:as-html date)) |
579597e3 | 96 | (when (editablep slot) |
d5e996b3 DC |
97 | (<ucw:input :accessor (presentation-slot-value slot instance) :id (input-id slot) :style "display:inline") |
98 | (<:button :id (trigger-id slot) (<:as-html "[...]")) | |
579597e3 | 99 | (<:script :type "text/javascript" |
100 | (<:as-is (format nil " | |
d5e996b3 DC |
101 | |
102 | Calendar.setup({ | |
103 | inputField : \"~a\", | |
104 | button : \"~a\", | |
105 | ifFormat : \"%m/%d/%Y\" });" (input-id slot) (trigger-id slot))))))) | |
579597e3 | 106 | |
e9454185 DC |
107 | (defslot-presentation mewa-relation-slot-presentation (mewa-slot-presentation slot-presentation) |
108 | ((foreign-instance :accessor foreign-instance) | |
569ad9e6 | 109 | (linkedp :accessor linkedp :initarg :linkedp :initform t) |
38a016c7 DC |
110 | (creator :accessor creator :initarg :creator :initform :editor) |
111 | (new-instance :accessor new-instance :initform nil)) | |
579597e3 | 112 | (:type-name relation)) |
113 | ||
7d87c1d2 | 114 | (defaction search-records ((slot mewa-relation-slot-presentation) instance) |
e9454185 DC |
115 | (multiple-value-bindf (finstance foreign-slot-name) |
116 | (meta-model:explode-foreign-key instance (slot-name slot)) | |
38a016c7 DC |
117 | (let ((new-instance (new-instance self))) |
118 | (unless new-instance | |
119 | (setf (new-instance self) | |
120 | (call-component | |
121 | (ucw::parent slot) | |
122 | (make-instance (or (cadr (mewa:find-attribute finstance :presentation-search)) | |
123 | 'mewa::mewa-presentation-search) | |
124 | :search-presentation | |
125 | (mewa:make-presentation finstance | |
126 | :type :search-presentation) | |
127 | :list-presentation | |
128 | (mewa:make-presentation finstance | |
129 | :type :listing))))) | |
130 | (sync-foreign-instance slot new-instance)))) | |
131 | ||
132 | (defmethod sync-foreign-instance ((slot mewa-relation-slot-presentation) foreign-instance) | |
133 | (let ((instance (instance (ucw::parent slot)))) | |
134 | (multiple-value-bind (foo f-slot-name) | |
135 | (meta-model:explode-foreign-key instance (slot-name slot)) | |
136 | (setf (slot-value instance (slot-name slot)) (slot-value foreign-instance f-slot-name)) | |
137 | (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p slot))))) | |
138 | ||
139 | ||
569ad9e6 | 140 | (defaction create-record-on-foreign-key ((slot mewa-relation-slot-presentation) instance) |
e9454185 DC |
141 | (multiple-value-bindf (finstance foreign-slot-name) |
142 | (meta-model:explode-foreign-key instance (slot-name slot)) | |
143 | (let ((new-instance | |
144 | (call-component | |
38a016c7 | 145 | (ucw::parent slot) |
569ad9e6 DC |
146 | (mewa:make-presentation finstance :type (creator self))))) |
147 | ||
148 | ;;;; TODO: this next bit is due to a bad design decision. | |
149 | ;;;; Components should always have (ok) return self, but somewhere | |
150 | ;;;; i've made in return (instance self) sometimes, and this | |
151 | ;;;; bahaviour is totatlly fucked. | |
152 | ||
153 | (when (typep new-instance 'mewa::mewa) | |
154 | (setf new-instance (instance new-instance))) | |
155 | ||
156 | ;;;; sorry about that, and now back t our regular program. | |
157 | ||
158 | (meta-model:sync-instance new-instance) | |
e9454185 DC |
159 | (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name)) |
160 | (meta-model:sync-instance instance :fill-gaps-only-p (fill-gaps-only-p self))))) | |
569ad9e6 | 161 | |
e9454185 | 162 | |
579597e3 | 163 | (defmethod present-relation ((slot mewa-relation-slot-presentation) instance) |
164 | ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance ) | |
19531fbd | 165 | (let* ((i (foreign-instance slot)) |
579597e3 | 166 | (pres (mewa::make-presentation |
167 | i | |
168 | :type :one-line | |
169 | :initargs (list | |
170 | :global-properties | |
19531fbd | 171 | (list :editablep nil :linkedp nil))))) |
9d6c69fb | 172 | (when (and (ucw::parent slot) (slot-boundp slot 'ucw::place)) |
498061f6 | 173 | (setf (component.place pres) (component.place (ucw::parent slot)))) |
174 | (when i (<ucw:render-component :component pres)))) | |
579597e3 | 175 | |
7d87c1d2 | 176 | |
177 | ||
579597e3 | 178 | (defmethod present-slot ((slot mewa-relation-slot-presentation) instance) |
179 | (present-relation slot instance)) | |
180 | ||
181 | (defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation) | |
182 | () | |
183 | (:type-name foreign-key) | |
184 | (:default-initargs)) | |
185 | ||
186 | (defaction view-instance ((self component) instance &rest initargs) | |
38a016c7 | 187 | (call-component (ucw::parent self) (apply #'mewa:make-presentation instance initargs)) |
12dcf3d4 | 188 | ;; the viewed instance could have been changed/deleted, so we sync this instance |
38a016c7 | 189 | (meta-model:sync-instance (instance (ucw::parent self)))) |
579597e3 | 190 | |
498061f6 | 191 | |
569ad9e6 | 192 | (defmethod present-slot :around ((slot foreign-key-slot-presentation) instance) |
f1ce8b6e DC |
193 | (setf (foreign-instance slot) |
194 | (when (presentation-slot-value slot instance) | |
195 | (meta-model:explode-foreign-key instance (slot-name slot)))) | |
f1ce8b6e | 196 | (flet ((render () (when (foreign-instance slot)(call-next-method)))) |
38a016c7 | 197 | (if (slot-boundp slot 'ucw::place) |
f1ce8b6e DC |
198 | (cond |
199 | ((editablep slot) | |
f1ce8b6e | 200 | (<ucw:submit :action (search-records slot instance) :value "Search" :style "display:inline") |
569ad9e6 | 201 | (<ucw:submit :action (create-record-on-foreign-key slot instance) :value "Add New" :style "display:inline")) |
f1ce8b6e DC |
202 | ((linkedp slot) |
203 | (<ucw:a :action (view-instance slot (foreign-instance slot)) | |
204 | (render))) | |
205 | (t | |
206 | (render))) | |
207 | ;; presentation is used only for rendering | |
208 | (render)))) | |
579597e3 | 209 | |
d2512426 | 210 | |
38a016c7 | 211 | |
d2512426 | 212 | |
579597e3 | 213 | ;;;; HAS MANY |
214 | (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation) | |
12dcf3d4 | 215 | ((add-new-label :accessor add-new-label :initarg :add-new-label :initform "Add New")) |
579597e3 | 216 | (:type-name has-many)) |
217 | ||
498061f6 | 218 | (defaction add-to-has-many ((slot has-many-slot-presentation) instance) |
8e6e6b56 DC |
219 | ;; if the instance is not stored we must make sure to mark it stored now! |
220 | (unless (mewa::instance-is-stored-p instance) | |
38a016c7 | 221 | (setf (mewa::modifiedp (ucw::parent self)) t)) |
8e6e6b56 | 222 | ;; sync up the instance |
ec044146 | 223 | ;;(mewa:ensure-instance-sync (parent slot)) |
38a016c7 | 224 | (meta-model:sync-instance (instance (ucw::parent slot))) |
ec044146 | 225 | |
85281029 DC |
226 | (multiple-value-bindf (class home foreign) |
227 | (meta-model:explode-has-many instance (slot-name slot)) | |
498061f6 | 228 | (let ((new (make-instance class))) |
229 | (setf (slot-value new foreign) (slot-value instance home)) | |
e9454185 | 230 | (meta-model:sync-instance new :fill-gaps-only-p (fill-gaps-only-p self)) |
38a016c7 | 231 | (call-component (ucw::parent slot) (mewa:make-presentation new :type (creator slot))) |
e9454185 | 232 | (meta-model:sync-instance instance)))) |
498061f6 | 233 | |
579597e3 | 234 | (defmethod present-slot ((slot has-many-slot-presentation) instance) |
38a016c7 | 235 | (when (slot-boundp slot 'ucw::place) |
f1ce8b6e | 236 | (<ucw:submit :action (add-to-has-many slot instance) :value (add-new-label slot))) |
d5e996b3 DC |
237 | (let* ((i (get-foreign-instances slot instance)) |
238 | (presentation (and i (make-presentation (car i) :type :one-line)))) | |
239 | (when i | |
240 | (flet ((linker (i string) | |
241 | (<ucw:a | |
242 | :action (view-instance slot i | |
243 | :initargs | |
244 | `(:global-properties , | |
245 | (list | |
246 | :linkedp t | |
247 | :editablep nil))) | |
248 | (<:as-html string)))) | |
249 | (<:table | |
250 | (<:tr | |
251 | (<:th) ;empty col for (view) link | |
252 | (dolist (s (slots presentation)) | |
253 | (<:th (<:as-html (label s))))) | |
254 | (dolist (s i) | |
255 | (let ((s s)) | |
256 | (setf (foreign-instance slot) s) | |
257 | (when (slot-boundp slot 'ucw::place) | |
258 | (<:tr | |
259 | (<:td (linker s " (view) ")) | |
260 | (dolist (p (slots (make-presentation s :type :one-line | |
261 | :initargs | |
262 | '(:global-properties | |
263 | (:editablep nil))))) | |
264 | (<:td | |
265 | ||
266 | (present-slot p s)))))))))))) | |
267 | ||
579597e3 | 268 | |
269 | (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance) | |
270 | (slot-value instance (slot-name slot))) | |
271 | ||
ec4f5786 | 272 | (defmethod presentation-slot-value ((slot has-many-slot-presentation) instance) |
273 | (get-foreign-instances slot instance)) | |
274 | ||
579597e3 | 275 | (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation) |
276 | ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10) | |
277 | (current :accessor current :initform 0) | |
278 | (len :accessor len ) | |
279 | (instances :accessor instances)) | |
579597e3 | 280 | (:type-name has-very-many)) |
281 | ||
282 | (defmethod list-next ((slot has-very-many-slot-presentation)) | |
283 | (setf (current slot) (incf (current slot) (number-to-display slot))) | |
284 | (when (< (len slot) (current slot)) | |
285 | (setf (current slot) (- (number-to-display slot) (len slot))))) | |
286 | ||
287 | (defmethod list-prev ((slot has-very-many-slot-presentation)) | |
288 | (setf (current slot) (decf (current slot) (number-to-display slot))) | |
289 | (when (> 0 (current slot)) | |
290 | ;;what to do here is open to debate | |
291 | (setf (current slot) (- (len slot)(number-to-display slot) )))) | |
292 | ||
293 | ||
294 | (defmethod present-slot ((slot has-very-many-slot-presentation) instance) | |
295 | ;;(<:as-html "isance: " instance) | |
38a016c7 | 296 | (if (slot-boundp slot 'ucw::place) |
f1ce8b6e DC |
297 | (progn |
298 | (<ucw:a :action (list-prev slot) (<:as-html "<<")) | |
38a016c7 | 299 | (let ((self (ucw::parent slot))) |
f1ce8b6e DC |
300 | (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot)))) |
301 | (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot)))))) | |
302 | (<ucw:a :action (list-next slot) (<:as-html ">>")) | |
303 | (call-next-method) | |
304 | (<:as-html "total :" (len slot))) | |
305 | (call-next-method))) | |
579597e3 | 306 | |
307 | (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance) | |
308 | (let ((f (call-next-method))) | |
309 | (setf (len slot) (length f)) | |
310 | (setf (instances slot) f) | |
311 | (loop for cons on (nthcdr (current slot) f) | |
312 | for i from 0 upto (number-to-display slot) | |
313 | collect (car cons)))) | |
314 | ||
315 | (defslot-presentation has-a-slot-presentation (one-of-presentation) | |
316 | ((key :initarg :key :accessor key)) | |
317 | (:type-name has-a)) | |
318 | ||
319 | (defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t)) | |
320 | (slot-value object slot-name)) | |
321 | ||
322 | (defmethod present-slot ((slot has-a-slot-presentation) instance) | |
323 | (<:as-html (presentation-slot-value slot instance)) | |
324 | (if (editablep slot) | |
325 | (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp | |
326 | (when (allow-nil-p slot) | |
327 | (<ucw:option :value nil (<:as-html (none-label slot)))) | |
328 | (dolist (option (get-foreign-instances (presentation slot) instance)) | |
329 | (setf (instance (presentation slot)) option) | |
330 | (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot))))) | |
331 | (if (presentation-slot-value slot instance) | |
332 | (progn | |
333 | (setf (instance (presentation slot)) (presentation-slot-value slot instance)) | |
334 | (present (presentation slot))) | |
85281029 | 335 | (<:as-html "--")))) |
5f0b37e7 | 336 | |
5f0b37e7 | 337 | |
5f0b37e7 | 338 | |
2cb4247d DC |
339 | |
340 | ||
341 | ||
342 |