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