Added standard-wrappers.lisp for 'wrapper' displays
[clinton/lisp-on-lines.git] / src / lisp-on-lines.lisp
CommitLineData
da26d003
DC
1(in-package :lisp-on-lines)
2
3;;;; *LoL Entry points
4;;;;
5
6;;;; This file contains the high level functions and macros
7;;;; that are part of LoL proper, that is to say, not Mewa
8;;;; or Meta-Model.
9
10;;;; ** Initialisation
6f63d3a4
DC
11(defmethod find-default-attributes ((object t))
12 "return the default attributes for a given object using the meta-model's meta-data"
13 (append (mapcar #'(lambda (s)
14 (cons (car s)
15 (gen-pslot
16 (if (meta-model:foreign-key-p object (car s))
17 'foreign-key
18 (cadr s))
19 (string (car s)) (car s))))
20 (meta-model:list-slot-types object))
21 (mapcar #'(lambda (s)
22 (cons s (append (gen-pslot 'has-many (string s) s)
23 `(:presentation
24 (make-presentation
25 ,object
26 :type :one-line)))))
27 (meta-model:list-has-many object))
28 (find-default-presentation-attribute-definitions)))
29
30(defmethod set-default-attributes ((object t))
31 "Set the default attributes for MODEL"
32 (clear-attributes object)
33 (mapcar #'(lambda (x)
34 (setf (find-attribute object (car x)) (cdr x)))
35 (find-default-attributes object)))
36
37;;;; This automagically initialises any meta model
38
39(eval-when (:compile-toplevel :load-toplevel :execute)
40 (defmethod meta-model::generate-base-class-expander :after (meta-model name args)
41 (set-default-attributes name)))
42
da26d003 43;;;; The following macros are used to initialise a set of database tables as LoL objects.
b8c89851 44
6f63d3a4
DC
45
46
da26d003 47(eval-when (:compile-toplevel :load-toplevel :execute)
d5e996b3 48 (defun generate-define-view-for-table (table)
da26d003
DC
49 "
50Generates a form that, when evaluated, initialises the given table as an lol object.
51This involves creating a meta-model, a clsql view-class, and the setting up the default attributes for a mewa presentation"
52
53 `(progn
5a4eea11 54 (def-view-class-from-table ,table)
da26d003
DC
55 (set-default-attributes (quote ,(meta-model::sql->sym table))))))
56
d5e996b3 57(defmacro define-view-for-table (&rest tables)
da26d003
DC
58 " expand to a form which initialises TABLES for use with LOL"
59 `(progn
d5e996b3 60 ,@(loop for tbl in tables collect (generate-define-view-for-table tbl))
da26d003
DC
61 (values)))
62
d5e996b3 63(defmacro define-views-for-database ()
da26d003 64 "expands to init-i-f-t using the listing of tables provided by meta-model"
d5e996b3
DC
65 `(define-view-for-table ,@(meta-model::list-tables)))
66
2259a106
DC
67
68
69;;;; These are some macros over the old presentation system.
70;;;; Considered depreciated, they will eventually be implemented in terms of the new
71;;;; display system, and delegated to backwards-compat-0.2.lisp
72
d5e996b3 73(eval-when (:compile-toplevel :load-toplevel :execute)
b890d449
DC
74 (defun %make-view (object type attributes args)
75
2259a106
DC
76 (when attributes
77 (setf args
78 (cons `(:attributes ,attributes) args)))
79 `(mewa::make-presentation
80 ,object
81 :type ,type
82 ,@(when args
83 `(:initargs
84 '(,@ (mapcan #'identity args)))))))
d5e996b3 85
598f1fa8 86(defmethod make-view (object &rest args &key (type :viewer)
d5e996b3 87 &allow-other-keys )
598f1fa8
DC
88 (remf args :type)
89 ;(warn "~A ~A" args `(:type ,type :initargs ,@args))
90 (apply #'make-presentation object `(:type ,type ,@ (when args
91 `(:initargs ,args)))))
d5e996b3 92
b890d449 93(defmacro present-view ((object &optional (type :viewer) (parent 'self))
d5e996b3 94 &body attributes-and-args)
b890d449 95 (arnesi:with-unique-names (view)
b8c89851 96 `(let ((,view (lol::make-view ,object
b890d449 97 :type ,type
598f1fa8
DC
98 ,@(when (car attributes-and-args)
99 `(:attributes ',(car attributes-and-args)))
b890d449
DC
100 ,@ (cdr attributes-and-args))))
101 (setf (ucw::parent ,view) ,parent)
b8c89851 102 (lol::present ,view))))
d5e996b3
DC
103
104
105(defmacro call-view ((object &optional (type :viewer) (component 'self component-supplied-p))
b890d449 106 &body attributes-and-args)
d5e996b3
DC
107 `(ucw:call-component
108 ,component
b890d449 109 ,(%make-view object type (car attributes-and-args) (cdr attributes-and-args))))
d5e996b3
DC
110
111(defmethod slot-view ((self mewa) slot-name)
112 (mewa::find-attribute-slot self slot-name))
113
114(defmethod present-slot-view ((self mewa) slot-name &optional (instance (instance self)))
14a7e1bc
DC
115 (let ((v (slot-view self slot-name)))
116
117 (if v
118 (present-slot v instance)
119 (<:as-html slot-name))))
d5e996b3 120
38a016c7 121
38a016c7
DC
122(defmethod find-slots-of-type (model &key (type 'string)
123 (types '((string)) types-supplied-p))
124 "returns a list of slots matching TYPE, or matching any of TYPES"
125 (let (ty)
126 (if types-supplied-p
127 (setf ty types)
128 (setf ty (list type)))
129 (remove nil (mapcar #'(lambda (st) (when (member (second st) ty)
130 (first st)))
131 (lisp-on-lines::list-slot-types model)))))
132
133
b890d449 134
b890d449 135
e8f6e086 136
b890d449 137
38a016c7
DC
138(defmethod word-search (class-name slots search-terms
139 &key (limit 10) (where (sql-and t)))
140 (select class-name
141 :where (sql-and
142 where
143 (word-search-where class-name slots search-terms :format-string "~a%"))
144 :flatp t
145 :limit limit))
146
147
148(defmethod word-search (class-name slots (s string) &rest args)
487243db 149 (apply #'word-search class-name slots (list s) args))
38a016c7
DC
150
151(defmethod word-search-where (class-name slots search-terms &key (format-string "%~a%"))
152 (sql-or
153 (mapcar #'(lambda (term)
154 (apply #'sql-or
155 (mapcar #'(lambda (slot)
156 (sql-uplike
157 (sql-slot-value class-name slot)
158 (format nil format-string term)))
159 slots)))
160 search-terms)))
161
b890d449
DC
162
163