You are here

function views1_import in Views (for Drupal 7) 6.3

Same name and namespace in other branches
  1. 6.2 includes/convert.inc \views1_import()

Convert a Views 1 view to a Views 2 view.

2 calls to views1_import()
views_ui_convert1 in includes/convert.inc
Page callback for the tools - Views 1 convert page
views_ui_import_validate in includes/admin.inc
Validate handler to import a view

File

includes/convert.inc, line 173
convert.inc

Code

function views1_import($imported) {
  views_include_handlers();
  views_module_include('views_convert.inc');
  $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 ($field['type'] == 'rss_feed') {
      $feed_display = $view
        ->new_display('feed');
      $feed_display
        ->set_option('style_plugin', 'rss');
      $feed_display
        ->set_option('row_plugin', 'node_rss');
      $feed_display
        ->set_option('path', $view->display[$page_display]->handler
        ->get_option('path') . '/feed');
      $feed_display
        ->set_option('page', $view->display[$page_display]->handler
        ->get_option('pager'));
      $displays = array();
      if (!empty($page_display)) {
        $displays[$page_display] = $page_display;
      }
      if (!empty($block_display)) {
        $displays[$block_display] = $block_display;
      }
      $feed_display
        ->set_option('displays', $displays);
    }
    else {
      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 $key => $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);
    }

    // Store the id in the exposed filter if there is one.
    foreach ($imported->exposed_filter as $key => $filter) {
      if ($field['tablename'] == $filter['tablename'] && $field['field'] == $filter['field']) {
        $imported->exposed_filter[$key]['id'] = $id;
      }
    }
  }

  // Exposed filters now get added to the filter array, not as a separate array.
  $count = 0;
  foreach ($imported->exposed_filter as $field) {
    $id = $field['id'];
    $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;
}