Newer
Older
The Great Git Migration
committed
<?php
/**
* @file convert.inc
*
* Contains forms and routines to convert older views to newer views.
*/
/**
* Page callback for the tools - Views 1 convert page
*/
function views_ui_admin_convert() {
if (!db_table_exists('view_view')) {
return t('There are no Views 1 views stored in the database to convert.');
}
$items = array();
$sorts = array();
$header = array(
array('data' => t('View name'), 'field' => 'name', 'sort' => 'asc'),
array('data' => t('Description')),
array('data' => t('Operations')),
);
$current_views = views_get_all_views();
$result = db_query("SELECT v.* FROM {view_view} v");
foreach ($result as $view) {
The Great Git Migration
committed
$ops = array();
if (!isset($current_views[$view->name])) {
$ops[] = l(t('Convert'), "admin/structure/views1/convert/$view->name");
The Great Git Migration
committed
}
else {
$ops[] = t('Converted');
}
$ops[] = l(t('Delete'), "admin/structure/views1/delete/$view->name");
The Great Git Migration
committed
$item = array();
$item[] = check_plain($view->name);
$item[] = check_plain($view->description);
$item[] = implode(' | ', $ops);
$items[] = $item;
$ts = tablesort_init($header);
switch ($ts['sql']) {
case 'name':
default:
$sorts[] = $item[0];
break;
case 'title':
$sorts[] = $item[1];
break;
}
}
if (!empty($ts)) {
if (strtolower($ts['sort']) == 'desc') {
arsort($sorts);
}
else {
asort($sorts);
}
}
$i = array();
foreach ($sorts as $id => $title) {
$i[] = $items[$id];
}
$output = t('The table below lists Views version 1 views that are stored in the database. You can either convert them to work in Views version 2, or delete them. The views are convertible only if there is no Views 2 view with the same name.');
$output .= theme('table', array('header' => $header, 'rows' => $i));
The Great Git Migration
committed
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
$output .= drupal_get_form('views_ui_convert_cleanup_form');
return $output;
}
/**
* Provide form to clean up Views 1 tables.
*/
function views_ui_convert_cleanup_form() {
$form['verify'] = array(
'#type' => 'checkbox',
'#title' => t('Remove all Views 1 tables'),
'#description' => t('Check this box and then click clean up to drop all Views 1 tables. Warning: this operation will not be reversible! Do this only if you are sure you no longer need this data.'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Clean up'),
);
return $form;
}
function views_ui_convert_cleanup_form_submit($form, $form_state) {
if (empty($form_state['values']['verify'])) {
drupal_set_message('Please check the box to verify you want to destroy your Views 1 table data.');
return;
}
$ret = array();
if (db_table_exists('view_view')) {
db_drop_table($ret, 'view_view');
}
if (db_table_exists('view_sort')) {
db_drop_table($ret, 'view_sort');
}
if (db_table_exists('view_argument')) {
db_drop_table($ret, 'view_argument');
}
if (db_table_exists('view_tablefield')) {
db_drop_table($ret, 'view_tablefield');
}
if (db_table_exists('view_filter')) {
db_drop_table($ret, 'view_filter');
}
if (db_table_exists('view_exposed_filter')) {
db_drop_table($ret, 'view_exposed_filter');
}
drupal_set_message(t('All Views 1 tables have been removed.'));
}
/**
* Page callback for the tools - Views 1 convert page
*/
function views_ui_convert1($name) {
$old = views1_load($name);
if (!$old) {
return t('Unable to find view.');
}
$view = views1_import($old);
if ($view) {
views_ui_cache_set($view);
drupal_goto('admin/structure/views/edit/' . $view->name);
The Great Git Migration
committed
}
else {
return t('Unable to convert view.');
}
}
/**
* Page to delete a Views 1 view.
*/
function views_ui_delete1_confirm($form, &$form_state, $vid) {
The Great Git Migration
committed
$form_state['vid'] = $vid;
$form = array();
$cancel = 'admin/structure/views/tools/convert';
The Great Git Migration
committed
if (!empty($_REQUEST['cancel'])) {
$cancel = $_REQUEST['cancel'];
}
return confirm_form($form,
t('Are you sure you want to delete the view %name?', array('%name' => $view->name)),
$cancel,
t('This action cannot be undone.'),
t('Delete'),
t('Cancel'));
}
/**
* Submit handler to delete a view.
*/
function views_ui_delete1_confirm_submit(&$form, &$form_state) {
views1_delete($form_state['vid']);
drupal_set_message(t('The view has been deleted'));
$form_state['redirect'] = 'admin/structure/views/tools/convert';
The Great Git Migration
committed
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
}
/**
* Convert a Views 1 view to a Views 2 view.
*/
function views1_import($imported) {
$view = views_new_view();
$view->name = $imported->name;
$view->description = $imported->description;
if (!empty($imported->page) && !empty($imported->url)) {
$page_display = $view->add_display('page');
}
if (!empty($imported->block)) {
$block_display = $view->add_display('block');
}
$view->init_display();
$handler = &$view->display['default']->handler;
$handler->set_option('title', $imported->page_title);
$handler->set_option('header', $imported->page_header);
$handler->set_option('header_format', $imported->page_header_format);
$handler->set_option('footer', $imported->page_footer);
$handler->set_option('footer_format', $imported->page_footer_format);
$handler->set_option('empty', $imported->page_empty);
$handler->set_option('empty_format', $imported->page_empty_format);
$handler->set_option('use_pager', $imported->use_pager);
$handler->set_option('items_per_page', $imported->nodes_per_page);
$handler->set_option('pager_element', 0);
$handler->set_option('offset', 0);
$access = array('type' => 'none', 'role' => array(), 'perm' => '');
if ($imported->access) {
$access['type'] = 'role';
$access['role'] = drupal_map_assoc($imported->access);
}
$handler->set_option('access', $access);
if (!empty($imported->page) && !empty($imported->url)) {
$handler = &$view->display[$page_display]->handler;
$url = str_replace('$arg', '%', $imported->url);
$handler->set_option('path', $url);
if ($imported->menu) {
$menu = array('type' => 'normal');
if ($imported->menu_tab) {
$menu['type'] = 'tab';
}
if ($imported->menu_tab_default) {
$menu['type'] = 'default tab';
}
$menu['title'] = $imported->menu_title ? $imported->menu_title : $imported->page_title;
$handler->set_option('menu', $menu);
if ($menu['type'] == 'default tab') {
$tab_options = array('type' => 'none');
switch ($imported->menu_tab_default_parent_type) {
case 'tab':
case 'normal':
$tab_options['type'] = $imported->menu_tab_default_parent_type;
break;
}
}
$tab_options['title'] = $imported->menu_parent_title;
$tab_options['weight'] = $imported->menu_parent_tab_weight;
$handler->set_option('tab_options', $tab_options);
}
}
views1_convert_style($view, $handler, $imported->page_type);
if (!empty($imported->block)) {
$handler = &$view->display[$block_display]->handler;
if (!empty($imported->block_title)) {
if (!empty($imported->page)) {
$handler->set_override('title');
}
$handler->set_option('title', $imported->block_title);
}
if (!empty($imported->page)) {
$handler->set_override('use_pager');
}
$handler->set_option('use_pager', FALSE);
if ($imported->nodes_per_block != $imported->nodes_per_page) {
$handler->set_option('items_per_page', $imported->nodes_per_block);
$handler->set_option('offset', 0);
}
if (empty($imported->block_use_page_header)) {
if (!empty($imported->page)) {
$handler->set_override('header');
}
if (!empty($imported->block_header)) {
$handler->set_option('header', $imported->block_header);
$handler->set_option('header_format', $imported->block_header_format);
}
}
if (empty($imported->block_use_page_footer)) {
if (!empty($imported->page)) {
$handler->set_override('footer');
}
if (!empty($imported->block_footer)) {
$handler->set_option('footer', $imported->block_footer);
$handler->set_option('footer_format', $imported->block_footer_format);
}
}
if (empty($imported->block_use_page_empty)) {
if (!empty($imported->page)) {
$handler->set_override('empty');
}
if (!empty($imported->block_empty)) {
$handler->set_option('empty', $imported->block_empty);
$handler->set_option('empty_format', $imported->block_empty_format);
}
}
$handler->set_option('use_more', $imported->block_more);
if (!empty($imported->page)) {
$handler->set_override('style_plugin');
}
views1_convert_style($view, $handler, $imported->block_type);
}
// For each of the fields, arguments, filters, and sorts in the old view,
// check if a handler for this item exists in Views 2 and add it,
// then see if any other modules want to adapt it using hook_views_convert().
foreach ($imported->field as $field) {
$id = $view->add_item('default', 'field', $field['tablename'], $field['field'], array('label' => $field['label']));
if ($view->display_handler->get_option('style_plugin') == 'table') {
$options = $view->display_handler->get_option('style_options');
if (!empty($field['sortable'])) {
$options['info'][$id]['sortable'] = TRUE;
if (!empty($field['defaultsort'])) {
$options['default'] = $id;
}
}
$view->display_handler->set_option('style_options', $options);
}
foreach (module_implements('views_convert') as $module) {
module_invoke($module, 'views_convert', 'default', 'field', $view, $field, $id);
}
}
foreach ($imported->sort as $field) {
$id = $view->add_item('default', 'sort', $field['tablename'], $field['field'], array('order' => $field['sortorder']));
foreach (module_implements('views_convert') as $module) {
module_invoke($module, 'views_convert', 'default', 'sort', $view, $field, $id);
}
}
$actions = array('ignore', 'not found', 'ignore', 'summary asc', 'summary asc', 'summary desc', 'summary asc', 'empty');
foreach ($imported->argument as $id => $field) {
if (!empty($imported->view_args_php)) {
$field['argoptions']['default_action'] = 'default';
$field['argoptions']['default_argument_type'] = 'php';
$field['argoptions']['default_argument_php'] = '$args = eval(\''. str_replace("'", "\\'", $imported->view_args_php) .'\');'."\n";
$field['argoptions']['default_argument_php'] .= 'if (isset($args['. $field['position'] .'])) {'."\n";
$field['argoptions']['default_argument_php'] .= ' return $args['. $field['position'] .'];'."\n";
$field['argoptions']['default_argument_php'] .= '}';
$field['argoptions']['validate_fail'] = $actions[$field['argdefault']];
}
else {
$field['argoptions']['default_action'] = $actions[$field['argdefault']];
}
if (!empty($field['title'])) {
$field['argoptions']['title'] = $field['title'];
}
if (!empty($field['wildcard'])) {
$field['argoptions']['wildcard'] = $field['wildcard'];
}
if (!empty($field['wildcard_substitution'])) {
$field['argoptions']['wildcard_substitution'] = $field['wildcard_substitution'];
}
// Arguments didn't used to be identified by table.name so we just have to
// leave that out.
foreach (module_implements('views_convert') as $module) {
module_invoke($module, 'views_convert', 'default', 'argument', $view, $field, NULL);
}
}
foreach ($imported->filter as $field) {
$options = $field['value'] == '' ? array() : array('value' => $field['value']);
$id = $view->add_item('default', 'filter', $field['tablename'], $field['field'], $options);
foreach (module_implements('views_convert') as $module) {
module_invoke($module, 'views_convert', 'default', 'filter', $view, $field, $id);
}
}
// Exposed filters now get added to the filter array, not as a separate array.
$count = 0;
foreach ($imported->exposed_filter as $field) {
list(, $id) = explode('.', $field['field'], 2);
$item = $view->get_item('default', 'filter', $id);
if (views_get_handler($item['table'], $item['field'], 'filter')) {
$item['exposed'] = TRUE;
// Use the count to emulate the old, hardcoded filter naming.
$item['expose']['identifier'] = 'filter' . $count;
$item['expose']['label'] = $field['label'];
$item['expose']['operator'] = $field['operator'] ? 'op' . $count : '';
$item['expose']['optional'] = $field['optional'];
$item['expose']['single'] = $field['single'];
$view->set_item('default', 'filter', $id, $item);
}
$count++;
foreach (module_implements('views_convert') as $module) {
module_invoke($module, 'views_convert', 'default', 'exposed_filter', $view, $field, $id);
}
}
return $view;
}
function views1_convert_style(&$view, &$handler, $type) {
switch ($type) {
case 'list':
$handler->set_option('style_plugin', 'list');
$handler->set_option('style_options', array('type' => 'ul'));
$handler->set_option('row_plugin', 'fields');
break;
case 'node':
$handler->set_option('row_plugin', 'node');
$handler->set_option('row_options', array('teaser' => FALSE, 'links' => TRUE));
break;
case 'teaser':
$handler->set_option('row_plugin', 'node');
$handler->set_option('row_options', array('teaser' => TRUE, 'links' => TRUE));
break;
case 'table':
$options = array();
$options['columns'] = array();
$options['default'] = '';
$options['info'] = array();
$options['override'] = FALSE;
$options['order'] = 'asc';
$handler->set_option('style_plugin', 'table');
$handler->set_option('style_options', $options);
break;
default:
// Ask around if anybody else knows.
foreach (module_implements('views_convert') as $module) {
module_invoke($module, 'views_convert', $handler->display->id, 'style', $view, $type);
}
}
}
/**
* Load a version 1 view from the database.
*
*/
function views1_load($arg) {
static $cache = array();
$which = is_numeric($arg) ? 'vid' : 'name';
if (isset($cache[$which][$arg])) {
return $cache[$which][$arg];
}
$where = (is_numeric($arg) ? "v.vid = %d" : "v.name = '%s'");
$view = db_fetch_object(db_query("SELECT v.* FROM {view_view} v WHERE $where", $arg));
if (!$view->name) {
return NULL;
}
$view->access = ($view->access ? explode(', ', $view->access) : array());
// load the sorting criteria too.
$result = db_query("SELECT * FROM {view_sort} vs WHERE vid = $view->vid ORDER BY position ASC");
$view->sort = array();
while ($sort = db_fetch_array($result)) {
if (substr($sort['field'], 0, 2) == 'n.') {
$sort['field'] = 'node' . substr($sort['field'], 1);
}
$sort['id'] = $sort['field'];
$bits = explode('.', $sort['field']);
$sort['tablename'] = $bits[0];
$sort['field'] = $bits[1];
$view->sort[$sort['position']] = $sort;
}
$result = db_query("SELECT * FROM {view_argument} WHERE vid = $view->vid ORDER BY position ASC");
$view->argument = array();
while ($arg = db_fetch_array($result)) {
$arg['id'] = $arg['type'];
$view->argument[$arg['position']] = $arg;
}
$result = db_query("SELECT * FROM {view_tablefield} WHERE vid = $view->vid ORDER BY position ASC");
$view->field = array();
while ($arg = db_fetch_array($result)) {
if ($arg['tablename'] == 'n') {
$arg['tablename'] = 'node';
}
$arg['id'] = $arg['fullname'] = "$arg[tablename].$arg[field]";
$arg['queryname'] = "$arg[tablename]_$arg[field]";
$view->field[] = $arg;
}
$result = db_query("SELECT * FROM {view_filter} WHERE vid = $view->vid ORDER BY position ASC");
// TODO - Is it safe to ignore this $filters variable? This function depends
// on lots of additional code needed to call hook_implements and construct
// all the views tables, so using it will add a lot of code to this file.
//$filters = _views_get_filters();
$view->filter = array();
while ($filter = db_fetch_array($result)) {
if (substr($filter['field'], 0, 2) == 'n.') {
$filter['field'] = 'node' . substr($filter['field'], 1);
}
if ($filter['operator'] == 'AND' ||
$filter['operator'] == 'OR' ||
$filter['operator'] == 'NOR') {
// TODO - need another way to identify this type of filter
// without being able to call hook_implements().
//|| $filters[$filter['field']]['value-type'] == 'array' ) {
if ($filter['value'] !== NULL && $filter['value'] !== '') {
$filter['value'] = explode(',', $filter['value']);
}
else {
$filter['value'] = array();
}
}
$filter['id'] = $filter['field'];
$bits = explode('.', $filter['field']);
$filter['tablename'] = $bits[0];
$filter['field'] = $bits[1];
$view->filter[$filter['position']] = $filter;
}
$result = db_query("SELECT * FROM {view_exposed_filter} WHERE vid = $view->vid ORDER BY position ASC");
$view->exposed_filter = array();
while ($arg = db_fetch_array($result)) {
$arg['id'] = $arg['field'];
$view->exposed_filter[] = $arg;
}
$cache['vid'][$view->vid] = $view;
$cache['name'][$view->name] = $view;
return $view;
}
/**
* Delete a version 1 view from the database.
*
*/
function views1_delete($arg) {
static $cache = array();
$where = (is_numeric($arg) ? "v.vid = %d" : "v.name = '%s'");
$view = db_fetch_object(db_query("SELECT v.* FROM {view_view} v WHERE $where", $arg));
if (!$view->name) {
return NULL;
}
db_delete('view_sort')
->condition('vid', $view->vid)
->execute();
db_delete('view_argument')
->condition('vid', $view->vid)
->execute();
db_delete('view_tablefield')
->condition('vid', $view->vid)
->execute();
db_delete('view_filter')
->condition('vid', $view->vid)
->execute();
db_delete('view_exposed_filter')
->condition('vid', $view->vid)
->execute();
db_delete('view_view')
->condition('vid', $view->vid)
->execute();