7a6671e65c7ebaed8ed208acdba69821f78f5cfd
[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 (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))
10
11 (defslot-presentation clsql-wall-time-slot-presentation (mewa-relation-slot-presentation)
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)
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 ))))
28
29 (defmethod label :around ((slot clsql-wall-time-slot-presentation))
30 (concatenate 'string (call-next-method) " (mm/dd/yyyy)"))
31
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
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
76 (defmethod present-relation ((slot mewa-relation-slot-presentation) instance)
77 ;;;;(<:as-html (slot-name slot) "=> " (foreign-instance slot) " from " instance )
78 (let* ((i (foreign-instance slot))
79 (pres (mewa::make-presentation
80 i
81 :type :one-line
82 :initargs (list
83 :global-properties
84 (list :editablep nil :linkedp nil)))))
85 (when (ucw::parent slot) (setf (component.place pres) (component.place (ucw::parent slot))))
86 (flet ((render () (when i (<ucw:render-component :component pres))))
87 (cond
88 ((editablep slot)
89 (render)
90 (<ucw:a :action (search-records slot instance) (<:as-html " (search)"))
91 (<ucw:a :action (create-record slot i) (<:as-html " (new)")))
92 ((linkedp slot)
93 (<ucw:a :action (view-instance slot i)
94 (render)))
95 (t
96 (render))))))
97
98
99
100 (defmethod present-slot ((slot mewa-relation-slot-presentation) instance)
101 (present-relation slot instance))
102
103 (defslot-presentation foreign-key-slot-presentation (mewa-relation-slot-presentation)
104 ()
105 (:type-name foreign-key)
106 (:default-initargs))
107
108 (defaction view-instance ((self component) instance &rest initargs)
109 (call-component (parent self) (apply #'mewa:make-presentation instance initargs)))
110
111 (defmethod present-slot :before ((slot foreign-key-slot-presentation) instance)
112 (setf (foreign-instance slot) (meta-model:explode-foreign-key instance (slot-name slot))))
113
114 ;;;; HAS MANY
115 (defslot-presentation has-many-slot-presentation (mewa-relation-slot-presentation)
116 ()
117 (:type-name has-many))
118
119 (defmethod present-slot ((slot has-many-slot-presentation) instance)
120 (let ((i (get-foreign-instances slot instance))
121 (linkedp (linkedp slot)))
122 (<:ul
123 (dolist (s i)
124 (let ((s s))
125 (setf (foreign-instance slot) s)
126 (<ucw:a :action (view-instance slot s :initargs `(:global-properties ,(list :linkedp t :editablep nil)))
127 (<:li (setf (linkedp slot) nil)
128 (present-relation slot instance))))))))
129
130
131 (defmethod get-foreign-instances ((slot has-many-slot-presentation) instance)
132 (slot-value instance (slot-name slot)))
133
134 (defslot-presentation has-very-many-slot-presentation (has-many-slot-presentation)
135 ((number-to-display :accessor number-to-display :initarg :number-to-display :initform 10)
136 (current :accessor current :initform 0)
137 (len :accessor len )
138 (instances :accessor instances))
139
140 (:type-name has-very-many))
141
142 (defmethod list-next ((slot has-very-many-slot-presentation))
143 (setf (current slot) (incf (current slot) (number-to-display slot)))
144 (when (< (len slot) (current slot))
145 (setf (current slot) (- (number-to-display slot) (len slot)))))
146
147 (defmethod list-prev ((slot has-very-many-slot-presentation))
148 (setf (current slot) (decf (current slot) (number-to-display slot)))
149 (when (> 0 (current slot))
150 ;;what to do here is open to debate
151 (setf (current slot) (- (len slot)(number-to-display slot) ))))
152
153
154 (defmethod present-slot ((slot has-very-many-slot-presentation) instance)
155 ;;(<:as-html "isance: " instance)
156 (<ucw:a :action (list-prev slot) (<:as-html "<<"))
157 (let ((self (parent slot)))
158 (<ucw:a :action (call-component self (mewa:make-presentation (car (slot-value instance (slot-name slot))) :type :listing :initargs (list :instances (instances slot))))
159 (<:as-html (label slot) (format nil " ~a-~a " (current slot) (+ (current slot) (number-to-display slot))))))
160 (<ucw:a :action (list-next slot) (<:as-html ">>"))
161 (call-next-method)
162 (<:as-html "total :" (len slot)))
163
164 (defmethod get-foreign-instances :around ((slot has-very-many-slot-presentation) instance)
165 (let ((f (call-next-method)))
166 (setf (len slot) (length f))
167 (setf (instances slot) f)
168 (loop for cons on (nthcdr (current slot) f)
169 for i from 0 upto (number-to-display slot)
170 collect (car cons))))
171
172 (defslot-presentation has-a-slot-presentation (one-of-presentation)
173 ((key :initarg :key :accessor key))
174 (:type-name has-a))
175
176 (defmethod get-foreign-slot-value ((slot has-a-slot-presentation) (object t) (slot-name t))
177 (slot-value object slot-name))
178
179 (defmethod present-slot ((slot has-a-slot-presentation) instance)
180 (<:as-html (presentation-slot-value slot instance))
181 (if (editablep slot)
182 (<ucw:select :accessor (presentation-slot-value slot instance) :test #'equalp
183 (when (allow-nil-p slot)
184 (<ucw:option :value nil (<:as-html (none-label slot))))
185 (dolist (option (get-foreign-instances (presentation slot) instance))
186 (setf (instance (presentation slot)) option)
187 (<ucw:option :value (get-foreign-slot-value slot option (key slot)) (present (presentation slot)))))
188 (if (presentation-slot-value slot instance)
189 (progn
190 (setf (instance (presentation slot)) (presentation-slot-value slot instance))
191 (present (presentation slot)))
192 (<:as-html "--"))))