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