Newer
Older
Earl Miles
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
// $Id$
/**
* @file view.inc
* Provides the view object type and associated methods.
*/
class view {
var $vid = 0;
var $name = '';
var $description = '';
var $display = array();
var $argument = array();
var $field = array();
var $sort = array();
var $filter = array();
var $base_table = 'node';
var $view_args_php = '';
// State variables
var $is_built = FALSE;
var $args = array();
var $query = NULL;
var $count_query = NULL;
function set_args($args) {
$this->args = $args;
}
function set_page_size($page_size) {
$this->page_size = $page_size;
}
function set_use_pager($use_pager) {
$this->use_pager = $use_pager;
}
function set_offset($offset) {
$this->offset = $offset;
}
function set_filter_input($filters) {
$this->filter_input = $filters;
}
function set_display($display_id) {
$this->display = $display_id;
}
function build($display_id = NULL) { }
function render() { }
function get_title($context) { }
function get_url() { }
function is_cacheable() { }
/**
* Load a view from the database based upon either vid or name.
*/
function load($arg) {
$where = (is_numeric($arg) ? "vid = %d" : "name = '%s'");
$data = db_fetch_object(db_query("SELECT * FROM {views_view} WHERE $where", $arg));
_views_unpack_schema($this, 'views_view', $data);
$this->_load_row('display');
$this->_load_row('argument');
$this->_load_row('field');
$this->_load_row('sort');
$this->_load_row('filter');
}
/**
* Load one of our sub tables.
*/
function _load_row($key) {
$object_name = "views_$key";
$table = $object_name . 's';
$result = db_query("SELECT * FROM {$table} WHERE vid = %d ORDER BY position", $this->vid);
while ($data = db_fetch_object($result)) {
$object = new $object_name;
_views_unpack_schema($object, $table, $data);
array_push($this->$key, $object);
}
}
/**
* Save the view to the database. If the view does not already exist,
* A vid will be assigned to the view and also returned from this function.
*/
function save() {
if (!empty($this->vid)) {
// remove existing table entries
db_query("DELETE from {views_displays} WHERE vid = %d", $this->vid);
db_query("DELETE from {views_arguments} WHERE vid = %d", $this->vid);
db_query("DELETE from {views_fields} WHERE vid = %d", $this->vid);
db_query("DELETE from {views_sorts} WHERE vid = %d", $this->vid);
db_query("DELETE from {views_filters} WHERE vid = %d", $this->vid);
}
_views_save_query('views_view', $this, !empty($this->vid) ? 'vid' : FALSE);
$this->_save_rows('display');
$this->_save_rows('argument');
$this->_save_rows('field');
$this->_save_rows('sort');
$this->_save_rows('filter');
cache_clear_all('views_urls', 'cache_views');
cache_clear_all(); // clear the page cache as well.
}
function _save_rows($key) {
foreach ($this->$key as $position => $object) {
$object->position = $position;
$object->vid = $this->vid;
_views_save_query("views_" . $key . "s", $object);
}
}
function delete() {
if (empty($view->vid)) {
return;
}
db_query("DELETE FROM {views_view} where vid = %d", $view->vid);
db_query("DELETE FROM {views_displays} where vid = %d", $view->vid);
db_query("DELETE FROM {views_arguments} where vid = %d", $view->vid);
db_query("DELETE FROM {views_fields} where vid = %d", $view->vid);
db_query("DELETE FROM {views_sorts} where vid = %d", $view->vid);
db_query("DELETE FROM {views_filters} where vid = %d", $view->vid);
cache_clear_all('views_query:' . $view->name, 'cache_views');
cache_clear_all(); // In Drupal 5.0 and later this clears the page cache only.
}
function export() {
require_once drupal_get_path('module', 'views') . '/includes/export.inc';
views_export_view($this);
}
}
class views_argument {
var $type = '';
var $default_action = '';
var $title = '';
var $wildcard = '';
var $wildcard_text = '';
var $summary_format;
var $options = array();
var $position = 0;
}
class views_field {
var $table = '';
var $field = '';
var $label = '';
var $handler = '';
var $position = 0;
// Options contains things like: Sortable, default sort, column, etc.
// Based upon the needs of the output type.
var $options = array();
}
class views_sort {
var $table = '';
var $field = '';
var $order = '';
var $options = array();
var $position = 0;
}
class views_filter {
var $table = '';
var $field = '';
var $group = '';
var $operator = '';
var $value = '';
var $options = '';
var $exposed = FALSE;
var $exposed_settings = array();
var $position = 0;
}
class views_display {
var $type = '';
var $output_type = '';
var $access = '';
var $title = '';
var $header = '';
var $header_format = '';
var $header_hide_if_empty = '';
var $footer = '';
var $footer_format = '';
var $footer_hide_if_empty = '';
var $empty = '';
var $empty_format = '';
var $use_pager = '';
var $page_size = '';
var $url = '';
var $display_options = array();
var $output_options = array();
var $position = 0;
var $filters_type = ''; // 'table', 'list'
var $filters_location = ''; // 'view', 'block'
}
// build an insert/update query based upon schema info.
function _views_save_query($table, &$object, $update = NULL) {
$schema = drupal_get_schema($table);
$fields = $defs = $values = $serials = array();
// Go through our schema and build correlations.
foreach ($schema['fields'] as $field => $info) {
if (!isset($object->$field)) {
if (isset($info['default'])) {
$object->$field = $info['default'];
}
else {
$object->$field = '';
}
}
// special case -- skip serial types if we are updating.
if ($info['type'] == 'serial' && $update) {
continue;
}
$fields[] = $field;
switch ($info['type']) {
case 'serial':
$defs[] = '%s';
$object->$field = 'NULL';
$serials[] = $field;
break;
case 'int':
$defs[] = '%d';
break;
case 'float':
case 'numeric':
$defs[] = '%f';
break;
default:
$defs[] = "'%s'";
}
if (empty($info['serialize'])) {
$values[] = $object->$field;
}
else {
$values[] = serialize($object->$field);
}
}
$query = '';
if (!$update) {
$query = "INSERT INTO {$table} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $defs) . ')';
}
else {
$query = '';
foreach ($fields as $id => $field) {
if ($query) {
$query .= ', ';
}
$query .= $field . ' = ' . $defs[$id];
}
$query = "UPDATE {$table} SET " . $query . " WHERE $update = " . $object->$update;
}
db_query($query, $values);
if ($serials) {
// get last insert ids and fill them in.
foreach ($serials as $field) {
$object->$field = db_last_insert_id($table, $field);
}
}
}
function _views_unpack_schema(&$object, $table, $data) {
$schema = drupal_get_schema($table);
// Go through our schema and build correlations.
foreach ($schema['fields'] as $field => $info) {
$object->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field);
}
}