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