| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @file convert.inc 4 * 5 * Contains forms and routines to convert older views to newer views. 6 */ 7 8 /** 9 * Page callback for the tools - Views 1 convert page 10 */ 11 function views_ui_admin_convert() { 12 if (!db_table_exists('view_view')) { 13 return t('There are no Views 1 views stored in the database to convert.'); 14 } 15 $items = array(); 16 $sorts = array(); 17 18 $header = array( 19 array('data' => t('View name'), 'field' => 'name', 'sort' => 'asc'), 20 array('data' => t('Description')), 21 array('data' => t('Operations')), 22 ); 23 $current_views = views_get_all_views(); 24 25 $result = db_query("SELECT v.* FROM {view_view} v"); 26 while ($view = db_fetch_object($result)) { 27 $ops = array(); 28 if (!isset($current_views[$view->name])) { 29 $ops[] = l(t('Convert'), "admin/build/views1/convert/$view->name"); 30 } 31 else { 32 $ops[] = t('Converted'); 33 } 34 $ops[] = l(t('Delete'), "admin/build/views1/delete/$view->name"); 35 36 $item = array(); 37 $item[] = check_plain($view->name); 38 $item[] = check_plain($view->description); 39 $item[] = implode(' | ', $ops); 40 $items[] = $item; 41 42 $ts = tablesort_init($header); 43 switch ($ts['sql']) { 44 case 'name': 45 default: 46 $sorts[] = $item[0]; 47 break; 48 case 'title': 49 $sorts[] = $item[1]; 50 break; 51 } 52 } 53 54 if (!empty($ts)) { 55 if (strtolower($ts['sort']) == 'desc') { 56 arsort($sorts); 57 } 58 else { 59 asort($sorts); 60 } 61 } 62 63 $i = array(); 64 foreach ($sorts as $id => $title) { 65 $i[] = $items[$id]; 66 } 67 $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.'); 68 $output .= theme('table', $header, $i); 69 70 $output .= drupal_get_form('views_ui_convert_cleanup_form'); 71 return $output; 72 } 73 74 /** 75 * Provide form to clean up Views 1 tables. 76 */ 77 function views_ui_convert_cleanup_form() { 78 $form['verify'] = array( 79 '#type' => 'checkbox', 80 '#title' => t('Remove all Views 1 tables'), 81 '#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.'), 82 '#required' => TRUE, 83 ); 84 85 $form['submit'] = array( 86 '#type' => 'submit', 87 '#value' => t('Clean up'), 88 ); 89 90 return $form; 91 } 92 93 function views_ui_convert_cleanup_form_submit($form, $form_state) { 94 if (empty($form_state['values']['verify'])) { 95 drupal_set_message('Please check the box to verify you want to destroy your Views 1 table data.'); 96 return; 97 } 98 99 $ret = array(); 100 if (db_table_exists('view_view')) { 101 db_drop_table($ret, 'view_view'); 102 } 103 if (db_table_exists('view_sort')) { 104 db_drop_table($ret, 'view_sort'); 105 } 106 if (db_table_exists('view_argument')) { 107 db_drop_table($ret, 'view_argument'); 108 } 109 if (db_table_exists('view_tablefield')) { 110 db_drop_table($ret, 'view_tablefield'); 111 } 112 if (db_table_exists('view_filter')) { 113 db_drop_table($ret, 'view_filter'); 114 } 115 if (db_table_exists('view_exposed_filter')) { 116 db_drop_table($ret, 'view_exposed_filter'); 117 } 118 119 drupal_set_message(t('All Views 1 tables have been removed.')); 120 } 121 122 /** 123 * Page callback for the tools - Views 1 convert page 124 */ 125 function views_ui_convert1($name) { 126 $old = views1_load($name); 127 if (!$old) { 128 return t('Unable to find view.'); 129 } 130 131 $view = views1_import($old); 132 133 if ($view) { 134 views_ui_cache_set($view); 135 drupal_goto('admin/build/views/edit/' . $view->name); 136 } 137 else { 138 return t('Unable to convert view.'); 139 } 140 } 141 142 /** 143 * Page to delete a Views 1 view. 144 */ 145 function views_ui_delete1_confirm(&$form_state, $vid) { 146 $form_state['vid'] = $vid; 147 $form = array(); 148 149 $cancel = 'admin/build/views/tools/convert'; 150 if (!empty($_REQUEST['cancel'])) { 151 $cancel = $_REQUEST['cancel']; 152 } 153 return confirm_form($form, 154 t('Are you sure you want to delete the view %name?', array('%name' => $view->name)), 155 $cancel, 156 t('This action cannot be undone.'), 157 t('Delete'), 158 t('Cancel')); 159 } 160 161 /** 162 * Submit handler to delete a view. 163 */ 164 function views_ui_delete1_confirm_submit(&$form, &$form_state) { 165 views1_delete($form_state['vid']); 166 drupal_set_message(t('The view has been deleted')); 167 $form_state['redirect'] = 'admin/build/views/tools/convert'; 168 } 169 170 /** 171 * Convert a Views 1 view to a Views 2 view. 172 */ 173 function views1_import($imported) { 174 views_include_handlers(); 175 views_module_include('views_convert.inc'); 176 177 $view = views_new_view(); 178 179 $view->name = $imported->name; 180 $view->description = $imported->description; 181 182 if (!empty($imported->page) && !empty($imported->url)) { 183 $page_display = $view->add_display('page'); 184 } 185 if (!empty($imported->block)) { 186 $block_display = $view->add_display('block'); 187 } 188 $view->init_display(); 189 190 $handler = &$view->display['default']->handler; 191 $handler->set_option('title', $imported->page_title); 192 $handler->set_option('header', $imported->page_header); 193 $handler->set_option('header_format', $imported->page_header_format); 194 $handler->set_option('footer', $imported->page_footer); 195 $handler->set_option('footer_format', $imported->page_footer_format); 196 $handler->set_option('empty', $imported->page_empty); 197 $handler->set_option('empty_format', $imported->page_empty_format); 198 199 $handler->set_option('use_pager', $imported->use_pager); 200 $handler->set_option('items_per_page', $imported->nodes_per_page); 201 $handler->set_option('pager_element', 0); 202 $handler->set_option('offset', 0); 203 204 $access = array('type' => 'none', 'role' => array(), 'perm' => ''); 205 if ($imported->access) { 206 $access['type'] = 'role'; 207 $access['role'] = drupal_map_assoc($imported->access); 208 } 209 210 $handler->set_option('access', $access); 211 if (!empty($imported->page) && !empty($imported->url)) { 212 $handler = &$view->display[$page_display]->handler; 213 $url = str_replace('$arg', '%', $imported->url); 214 $handler->set_option('path', $url); 215 if ($imported->menu) { 216 $menu = array('type' => 'normal'); 217 if ($imported->menu_tab) { 218 $menu['type'] = 'tab'; 219 } 220 if ($imported->menu_tab_default) { 221 $menu['type'] = 'default tab'; 222 } 223 $menu['title'] = $imported->menu_title ? $imported->menu_title : $imported->page_title; 224 $handler->set_option('menu', $menu); 225 226 if ($menu['type'] == 'default tab') { 227 $tab_options = array('type' => 'none'); 228 switch ($imported->menu_tab_default_parent_type) { 229 case 'tab': 230 case 'normal': 231 $tab_options['type'] = $imported->menu_tab_default_parent_type; 232 break; 233 } 234 } 235 $tab_options['title'] = $imported->menu_parent_title; 236 $tab_options['weight'] = $imported->menu_parent_tab_weight; 237 $handler->set_option('tab_options', $tab_options); 238 } 239 } 240 241 views1_convert_style($view, $handler, $imported->page_type); 242 243 if (!empty($imported->block)) { 244 $handler = &$view->display[$block_display]->handler; 245 246 if (!empty($imported->block_title)) { 247 if (!empty($imported->page)) { 248 $handler->set_override('title'); 249 } 250 $handler->set_option('title', $imported->block_title); 251 } 252 253 if (!empty($imported->page)) { 254 $handler->set_override('use_pager'); 255 } 256 $handler->set_option('use_pager', FALSE); 257 258 if ($imported->nodes_per_block != $imported->nodes_per_page) { 259 $handler->set_option('items_per_page', $imported->nodes_per_block); 260 $handler->set_option('offset', 0); 261 } 262 263 if (empty($imported->block_use_page_header)) { 264 if (!empty($imported->page)) { 265 $handler->set_override('header'); 266 } 267 if (!empty($imported->block_header)) { 268 $handler->set_option('header', $imported->block_header); 269 $handler->set_option('header_format', $imported->block_header_format); 270 } 271 } 272 if (empty($imported->block_use_page_footer)) { 273 if (!empty($imported->page)) { 274 $handler->set_override('footer'); 275 } 276 if (!empty($imported->block_footer)) { 277 $handler->set_option('footer', $imported->block_footer); 278 $handler->set_option('footer_format', $imported->block_footer_format); 279 } 280 } 281 if (empty($imported->block_use_page_empty)) { 282 if (!empty($imported->page)) { 283 $handler->set_override('empty'); 284 } 285 if (!empty($imported->block_empty)) { 286 $handler->set_option('empty', $imported->block_empty); 287 $handler->set_option('empty_format', $imported->block_empty_format); 288 } 289 } 290 291 $handler->set_option('use_more', $imported->block_more); 292 293 if (!empty($imported->page)) { 294 $handler->set_override('style_plugin'); 295 } 296 views1_convert_style($view, $handler, $imported->block_type); 297 } 298 299 // For each of the fields, arguments, filters, and sorts in the old view, 300 // check if a handler for this item exists in Views 2 and add it, 301 // then see if any other modules want to adapt it using hook_views_convert(). 302 303 foreach ($imported->field as $field) { 304 $id = $view->add_item('default', 'field', $field['tablename'], $field['field'], array('label' => $field['label'])); 305 if ($view->display_handler->get_option('style_plugin') == 'table') { 306 $options = $view->display_handler->get_option('style_options'); 307 if (!empty($field['sortable'])) { 308 $options['info'][$id]['sortable'] = TRUE; 309 if (!empty($field['defaultsort'])) { 310 $options['default'] = $id; 311 } 312 } 313 $view->display_handler->set_option('style_options', $options); 314 } 315 foreach (module_implements('views_convert') as $module) { 316 module_invoke($module, 'views_convert', 'default', 'field', $view, $field, $id); 317 } 318 } 319 foreach ($imported->sort as $field) { 320 $id = $view->add_item('default', 'sort', $field['tablename'], $field['field'], array('order' => $field['sortorder'])); 321 foreach (module_implements('views_convert') as $module) { 322 module_invoke($module, 'views_convert', 'default', 'sort', $view, $field, $id); 323 } 324 } 325 $actions = array('ignore', 'not found', 'ignore', 'summary asc', 'summary asc', 'summary desc', 'summary asc', 'empty'); 326 foreach ($imported->argument as $id => $field) { 327 if (!empty($imported->view_args_php)) { 328 $field['argoptions']['default_action'] = 'default'; 329 $field['argoptions']['default_argument_type'] = 'php'; 330 $field['argoptions']['default_argument_php'] = '$args = eval(\''. str_replace("'", "\\'", $imported->view_args_php) .'\');'."\n"; 331 $field['argoptions']['default_argument_php'] .= 'if (isset($args['. $field['position'] .'])) {'."\n"; 332 $field['argoptions']['default_argument_php'] .= ' return $args['. $field['position'] .'];'."\n"; 333 $field['argoptions']['default_argument_php'] .= '}'; 334 $field['argoptions']['validate_fail'] = $actions[$field['argdefault']]; 335 } 336 else { 337 $field['argoptions']['default_action'] = $actions[$field['argdefault']]; 338 } 339 if (!empty($field['title'])) { 340 $field['argoptions']['title'] = $field['title']; 341 } 342 if (!empty($field['wildcard'])) { 343 $field['argoptions']['wildcard'] = $field['wildcard']; 344 } 345 if (!empty($field['wildcard_substitution'])) { 346 $field['argoptions']['wildcard_substitution'] = $field['wildcard_substitution']; 347 } 348 // Arguments didn't used to be identified by table.name so we just have to 349 // leave that out. 350 foreach (module_implements('views_convert') as $module) { 351 module_invoke($module, 'views_convert', 'default', 'argument', $view, $field, NULL); 352 } 353 } 354 foreach ($imported->filter as $key => $field) { 355 $options = $field['value'] == '' ? array() : array('value' => $field['value']); 356 $id = $view->add_item('default', 'filter', $field['tablename'], $field['field'], $options); 357 foreach (module_implements('views_convert') as $module) { 358 module_invoke($module, 'views_convert', 'default', 'filter', $view, $field, $id); 359 } 360 // Store the id in the exposed filter if there is one. 361 foreach ($imported->exposed_filter as $key => $filter) { 362 if ($field['tablename'] == $filter['tablename'] && $field['field'] == $filter['field']) { 363 $imported->exposed_filter[$key]['id'] = $id; 364 } 365 } 366 } 367 // Exposed filters now get added to the filter array, not as a separate array. 368 $count = 0; 369 foreach ($imported->exposed_filter as $field) { 370 $id = $field['id']; 371 $item = $view->get_item('default', 'filter', $id); 372 if (views_get_handler($item['table'], $item['field'], 'filter')) { 373 $item['exposed'] = TRUE; 374 375 // Use the count to emulate the old, hardcoded filter naming. 376 $item['expose']['identifier'] = 'filter' . $count; 377 $item['expose']['label'] = $field['label']; 378 $item['expose']['operator'] = $field['operator'] ? 'op' . $count : ''; 379 $item['expose']['optional'] = $field['optional']; 380 $item['expose']['single'] = $field['single']; 381 $view->set_item('default', 'filter', $id, $item); 382 } 383 $count++; 384 foreach (module_implements('views_convert') as $module) { 385 module_invoke($module, 'views_convert', 'default', 'exposed_filter', $view, $field, $id); 386 } 387 } 388 389 return $view; 390 } 391 392 function views1_convert_style(&$view, &$handler, $type) { 393 switch ($type) { 394 case 'list': 395 $handler->set_option('style_plugin', 'list'); 396 $handler->set_option('style_options', array('type' => 'ul')); 397 $handler->set_option('row_plugin', 'fields'); 398 break; 399 case 'node': 400 $handler->set_option('row_plugin', 'node'); 401 $handler->set_option('row_options', array('teaser' => FALSE, 'links' => TRUE)); 402 break; 403 case 'teaser': 404 $handler->set_option('row_plugin', 'node'); 405 $handler->set_option('row_options', array('teaser' => TRUE, 'links' => TRUE)); 406 break; 407 case 'table': 408 $options = array(); 409 $options['columns'] = array(); 410 $options['default'] = ''; 411 $options['info'] = array(); 412 $options['override'] = FALSE; 413 $options['order'] = 'asc'; 414 415 $handler->set_option('style_plugin', 'table'); 416 $handler->set_option('style_options', $options); 417 break; 418 default: 419 // Ask around if anybody else knows. 420 foreach (module_implements('views_convert') as $module) { 421 module_invoke($module, 'views_convert', $handler->display->id, 'style', $view, $type); 422 } 423 } 424 } 425 /** 426 * Load a version 1 view from the database. 427 * 428 */ 429 function views1_load($arg) { 430 static $cache = array(); 431 $which = is_numeric($arg) ? 'vid' : 'name'; 432 if (isset($cache[$which][$arg])) { 433 return $cache[$which][$arg]; 434 } 435 436 $where = (is_numeric($arg) ? "v.vid = %d" : "v.name = '%s'"); 437 $view = db_fetch_object(db_query("SELECT v.* FROM {view_view} v WHERE $where", $arg)); 438 439 if (!$view->name) { 440 return NULL; 441 } 442 443 $view->access = ($view->access ? explode(', ', $view->access) : array()); 444 445 // load the sorting criteria too. 446 $result = db_query("SELECT * FROM {view_sort} vs WHERE vid = $view->vid ORDER BY position ASC"); 447 448 $view->sort = array(); 449 while ($sort = db_fetch_array($result)) { 450 if (substr($sort['field'], 0, 2) == 'n.') { 451 $sort['field'] = 'node' . substr($sort['field'], 1); 452 } 453 $sort['id'] = $sort['field']; 454 $bits = explode('.', $sort['field']); 455 $sort['tablename'] = $bits[0]; 456 $sort['field'] = $bits[1]; 457 $view->sort[$sort['position']] = $sort; 458 } 459 460 $result = db_query("SELECT * FROM {view_argument} WHERE vid = $view->vid ORDER BY position ASC"); 461 462 $view->argument = array(); 463 while ($arg = db_fetch_array($result)) { 464 $arg['id'] = $arg['type']; 465 $view->argument[$arg['position']] = $arg; 466 } 467 468 $result = db_query("SELECT * FROM {view_tablefield} WHERE vid = $view->vid ORDER BY position ASC"); 469 470 $view->field = array(); 471 while ($arg = db_fetch_array($result)) { 472 if ($arg['tablename'] == 'n') { 473 $arg['tablename'] = 'node'; 474 } 475 $arg['id'] = $arg['fullname'] = "$arg[tablename].$arg[field]"; 476 $arg['queryname'] = "$arg[tablename]_$arg[field]"; 477 $view->field[] = $arg; 478 } 479 480 $result = db_query("SELECT * FROM {view_filter} WHERE vid = $view->vid ORDER BY position ASC"); 481 482 // TODO - Is it safe to ignore this $filters variable? This function depends 483 // on lots of additional code needed to call hook_implements and construct 484 // all the views tables, so using it will add a lot of code to this file. 485 //$filters = _views_get_filters(); 486 $view->filter = array(); 487 while ($filter = db_fetch_array($result)) { 488 if (substr($filter['field'], 0, 2) == 'n.') { 489 $filter['field'] = 'node' . substr($filter['field'], 1); 490 } 491 492 if ($filter['operator'] == 'AND' || 493 $filter['operator'] == 'OR' || 494 $filter['operator'] == 'NOR') { 495 // TODO - need another way to identify this type of filter 496 // without being able to call hook_implements(). 497 //|| $filters[$filter['field']]['value-type'] == 'array' ) { 498 if ($filter['value'] !== NULL && $filter['value'] !== '') { 499 $filter['value'] = explode(',', $filter['value']); 500 } 501 else { 502 $filter['value'] = array(); 503 } 504 } 505 $filter['id'] = $filter['field']; 506 $bits = explode('.', $filter['field']); 507 $filter['tablename'] = $bits[0]; 508 $filter['field'] = $bits[1]; 509 $view->filter[$filter['position']] = $filter; 510 } 511 512 $result = db_query("SELECT * FROM {view_exposed_filter} WHERE vid = $view->vid ORDER BY position ASC"); 513 514 $view->exposed_filter = array(); 515 while ($arg = db_fetch_array($result)) { 516 $arg['id'] = $arg['field']; 517 $view->exposed_filter[] = $arg; 518 } 519 520 $cache['vid'][$view->vid] = $view; 521 $cache['name'][$view->name] = $view; 522 523 return $view; 524 } 525 526 /** 527 * Delete a version 1 view from the database. 528 * 529 */ 530 function views1_delete($arg) { 531 static $cache = array(); 532 $where = (is_numeric($arg) ? "v.vid = %d" : "v.name = '%s'"); 533 $view = db_fetch_object(db_query("SELECT v.* FROM {view_view} v WHERE $where", $arg)); 534 535 if (!$view->name) { 536 return NULL; 537 } 538 539 540 541 $result = db_query("DELETE FROM {view_sort} WHERE vid = $view->vid"); 542 $result = db_query("DELETE FROM {view_argument} WHERE vid = $view->vid"); 543 $result = db_query("DELETE FROM {view_tablefield} WHERE vid = $view->vid"); 544 $result = db_query("DELETE FROM {view_filter} WHERE vid = $view->vid"); 545 $result = db_query("DELETE FROM {view_exposed_filter} WHERE vid = $view->vid"); 546 $result = db_query("DELETE FROM {view_view} WHERE vid = $view->vid"); 547 } 548
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |