whole bunch of cleanups so we build
[clinton/lisp-on-lines.git] / src / mewa / slot-presentations.lisp
CommitLineData
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
f4170204 11(defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
579597e3 12 ()
13 (:type-name clsql-sys:wall-time))
14
15(defmethod presentation-slot-value ((slot clsql-wall-time-slot-presentation) instance)
16 (let ((date (call-next-method)))
17 (when date (multiple-value-bind (y m d) (clsql:time-ymd date)
18 (format nil "~a/~a/~a" m d y)))))
19
20(defmethod (setf presentation-slot-value) ((value string) (slot clsql-wall-time-slot-presentation) instance)
f4170204 21 (let ((new-time (clsql:parse-date-time (remove #\Space value)))
22 (old-time (when (slot-boundp instance (slot-name slot))
23 (slot-value instance (slot-name slot)))))
24 (unless (or (eql old-time new-time)
25 (and (null old-time) new-time)
26 (equal :equal (clsql:time-compare new-time old-time)))
27 (setf (presentation-slot-value slot instance) new-time ))))
579597e3 28
19531fbd 29(defmethod label :around ((slot clsql-wall-time-slot-presentation))
30 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
31
579597e3 32(defmethod present-slot ((slot clsql-wall-time-slot-presentation) instance)
33 (let ((date (presentation-slot-value slot instance))
34 (input-id (string (gensym))))
35 (if (and date (not (editablep slot)))
36 (<:span (<:as-html date)))
37 (when (editablep slot)
38 (<ucw:input :accessor (presentation-slot-value slot instance) :id input-id)
39 (<:script :type "text/javascript"
40 (<:as-is (format nil "
41 Calendar.setup({
42 inputField : \"~a\",
43 ifFormat : \"%m/%d/%Y\",
44 });" input-id))))))
45
46(defslot-presentation mewa-relation-slot-presentation ()
47 ((slot-name :accessor slot-name :initarg :slot-name)
48 (foreign-instance :accessor foreign-instance)
49 (linkedp :accessor linkedp :initarg :linkedp :initform t))
50 (:type-name relation))
51
7d87c1d2 52(defun get-fkey-data (instance slot-name)
53 "ugly workaround b/c UCW does not like M-V-B"
54 (multiple-value-bind (finstance foreign-slot-name)
55 (meta-model:explode-foreign-key instance slot-name)
56 (cons finstance foreign-slot-name)))
57
58(defaction search-records ((slot mewa-relation-slot-presentation) instance)
59 (let* ((d (get-fkey-data instance (slot-name slot)))
60 (finstance (car d))
61 (foreign-slot-name (cdr d))
62 (new-instance
63 (call-component
64 (parent slot)
65 (make-instance 'mewa::mewa-presentation-search
66 :search-presentation
67 (mewa:make-presentation finstance
68 :type :search-presentation)
69 :list-presentation
70 (mewa:make-presentation finstance
71 :type :listing)))))
72 (setf (slot-value instance (slot-name slot)) (slot-value new-instance foreign-slot-name))
73 (meta-model:sync-instance instance)
74 (clsql:update-objects-joins (list instance))))
75
579597e3 76(defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
77 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
19531fbd 78 (let* ((i (foreign-instance slot))
579597e3 79 (pres (mewa::make-presentation
80 i
81 :type :one-line
82 :initargs (list
83 :global-properties
19531fbd 84 (list :editablep nil :linkedp nil)))))
498061f6 85 (when (ucw::parent slot)
86 (setf (component.place pres) (component.place (ucw::parent slot))))
87 (when i (<ucw:render-component :component pres))))
579597e3 88
7d87c1d2 89
90
579597e3 91(defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
92 (present-relation slot instance))
93
94(defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
95 ()
96 (:type-name foreign-key)
97 (:default-initargs))
98
99(defaction view-instance ((self component) instance &rest initargs)
100 (call-component (parent self) (apply #'mewa:make-presentation instance initargs)))
101
498061f6 102
103(defmethod present-slot :around ((slot foreign-key-slot-presentation) instance)
104 (setf (foreign-instance slot) (when (presentation-slot-value slot instance) (meta-model:explode-foreign-key instance (slot-name slot))))
105 (flet ((render () (call-next-method)))
106 (cond
107 ((editablep slot)
108 (render)
109 (<ucw:a :action (search-records slot instance) (<:as-html " (search)"))
110 (<ucw:a :action (create-record slot instance) (<:as-html " (new)")))
111 ((linkedp slot)
112 (<ucw:a :action (view-instance slot (foreign-instance slot))
113 (render)))
114 (t
115 (render)))))
579597e3 116
117;;;; HAS MANY
118(defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
119 ()
120 (:type-name has-many))
121
498061f6 122
123(defun get-join-class-info (slot instance)
124 "hack around m-v-b"
125 (multiple-value-bind (s h f) (meta-model:explode-has-many instance (slot-name slot))
126 (list s h f)))
498061f6 127
128(defaction add-to-has-many ((slot has-many-slot-presentation) instance)
129 (destructuring-bind (class home foreign)
88dedfa8 130 (multiple-value-funcall #'meta-model:explode-has-many instance (slot-name slot))
498061f6 131 (let ((new (make-instance class)))
132 (setf (slot-value new foreign) (slot-value instance home))
133 (meta-model:sync-instance new)
134 (call-component (parent slot) (mewa:make-presentation new :type :editor)))))
135
579597e3 136(defmethod present-slot ((slot has-many-slot-presentation) instance)
498061f6 137 (<ucw:a :action (add-to-has-many slot instance)
138 (<:as-html "(add new)"))
579597e3 139 (let ((i (get-foreign-instances slot instance))
140 (linkedp (linkedp slot)))
498061f6 141 (<:ul
142 (dolist (s i)
143 (let ((s s))
144 (setf (foreign-instance slot) s)
145 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
146 (<:li (setf (linkedp slot) nil)
147 (present-relation slot instance))))))))
579597e3 148
149
150(defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
151 (slot-value instance (slot-name slot)))
152
153(defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
154 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
155 (current :accessor current :initform 0)
156 (len :accessor len )
157 (instances :accessor instances))
158
159 (:type-name has-very-many))
160
161(defmethod list-next ((slot has-very-many-slot-presentation))
162 (setf (current slot) (incf (current slot) (number-to-display slot)))
163 (when (< (len slot) (current slot))
164 (setf (current slot) (- (number-to-display slot) (len slot)))))
165
166(defmethod list-prev ((slot has-very-many-slot-presentation))
167 (setf (current slot) (decf (current slot) (number-to-display slot)))
168 (when (> 0 (current slot))
169 ;;what to do here is open to debate
170 (setf (current slot) (- (len slot)(number-to-display slot) ))))
171
172
173(defmethod present-slot ((slot has-very-many-slot-presentation) instance)
174 ;;(<:as-html "isance: " instance)
175 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
176 (let ((self (parent slot)))
177 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
178 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
179 (<ucw:a :action (list-next slot) (<:as-html ">>"))
180 (call-next-method)
181 (<:as-html "total :" (len slot)))
182
183(defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
184 (let ((f (call-next-method)))
185 (setf (len slot) (length f))
186 (setf (instances slot) f)
187 (loop for cons on (nthcdr (current slot) f)
188 for i from 0 upto (number-to-display slot)
189 collect (car cons))))
190
191(defslot-presentation has-a-slot-presentation (one-of-presentation)
192 ((key :initarg :key :accessor key))
193 (:type-name has-a))
194
195(defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
196 (slot-value object slot-name))
197
198(defmethod present-slot ((slot has-a-slot-presentation) instance)
199 (<:as-html (presentation-slot-value slot instance))
200 (if (editablep slot)
201 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
202 (when (allow-nil-p slot)
203 (<ucw:option :value nil (<:as-html (none-label slot))))
204 (dolist (option (get-foreign-instances (presentation slot) instance))
205 (setf (instance (presentation slot)) option)
206 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
207 (if (presentation-slot-value slot instance)
208 (progn
209 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
210 (present (presentation slot)))
211 (<:as-html "--"))))