You are here

function panels_update_6302 in Panels 6.3

File

./panels.install, line 1094

Code

function panels_update_6302() {
  $ret = array();
  if (!module_exists('panels')) {
    $ret['#abort'] = array(
      'success' => FALSE,
      'query' => t('The Panels module cannot be updated while disabled. If you wish to update Panels, please enable it. If you do not wish to update Panels, please uninstall it.'),
    );
    return $ret;
  }
  if (!module_exists('page_manager') && db_table_exists('panels_page')) {
    $ret['#abort'] = array(
      'success' => FALSE,
      'query' => t('Conversion of panels pages cannot be completed without page manager module from CTools installed. Please install CTools, activate page manager, and attempt the update again.'),
    );
    return $ret;
  }
  if (!db_table_exists('panels_page')) {
    return $ret;
  }

  // Store the node edit handlers because we merged the edit/add path and we
  // need to be able to keep these together to make sure the names work ok.
  $node_edit_handlers = array();
  page_manager_get_task('page');
  $result = db_query("SELECT * FROM {panels_page}");
  while ($p = db_fetch_object($result)) {
    $page = page_manager_page_new();
    $page->default_handlers = array();

    // Should we check for uniqueness here? It doesn't seem really
    // plausible that there could be page manager pages already.
    $page->name = $p->name;
    $page->task = 'page';

    // could become custom later.
    $page->subtask = $p->name;
    $page->admin_title = $p->name;
    $page->path = $p->path;

    // convert access
    if (!empty($p->access)) {
      $rids = explode(', ', $p->access);

      // For safety, eliminate any non-numeric rids, as we occasionally had
      // problems with nulls and such getting in here:
      foreach ($rids as $id => $rid) {
        if (!is_numeric($rid)) {
          unset($rids[$id]);
        }
      }
      if (empty($rids)) {
        $page->access = array();
      }
      else {

        // The old access style was just a role based system, so let's convert
        // it to that.
        $page->access = array(
          'plugins' => array(
            array(
              'name' => 'role',
              'context' => 'logged-in-user',
              'settings' => array(
                'rids' => array_values($rids),
              ),
            ),
          ),
        );
      }
    }

    // Convert menu stuff.
    $page->menu = array(
      'type' => 'none',
      'title' => '',
      'weight' => 0,
      'name' => 'navigation',
      'parent' => array(
        'type' => 'none',
        'title' => '',
        'weight' => 0,
        'name' => 'navigation',
      ),
    );
    if ($p->menu) {
      if ($p->menu_tab) {
        if ($p->menu_tab_default) {
          $page->menu['type'] = 'default tab';
          $page->menu['parent']['type'] = $p->menu_tab_default_parent_type;
          $page->menu['parent']['title'] = $p->menu_parent_title;
          $page->menu['parent']['weight'] = $p->menu_parent_tab_weight;
        }
        else {
          $page->menu['type'] = 'tab';
        }
      }
      else {
        $page->menu['type'] = 'normal';
      }
      $page->menu['title'] = $p->menu_title;
      $page->menu['weight'] = $p->menu_tab_weight;
    }
    $page->conf = array();
    $displays = unserialize($p->displays);
    $arguments = unserialize($p->arguments);
    foreach ($arguments as $id => $argument) {
      $page->arguments[$argument['keyword']] = array(
        'name' => $argument['name'],
        'identifier' => $argument['identifier'],
        'title' => $argument['title'],
        'id' => $argument['id'],
        'settings' => isset($argument['argument_settings']) ? $argument['argument_settings'] : array(),
      );
      $match = FALSE;
      $bits = explode('/', $page->path);
      foreach ($bits as $pos => $bit) {
        if ($bit == '%') {
          $bits[$pos] = '%' . $argument['keyword'];
          $match = TRUE;
          $page->path = implode('/', $bits);
          break;
        }
      }
      if (!$match) {
        if ($argument['default'] == '404') {
          $page->path .= '/%' . $argument['keyword'];
        }
        else {
          $page->path .= '/!' . $argument['keyword'];
        }
      }

      // save this for later use.
      $arguments[$id]['context'] = 'argument_' . $argument['name'] . '_' . $argument['id'];
    }

    // Reset the task type here if it's one of our overrides. This ensures
    // that we get the right names.
    switch ($p->path) {
      case 'node/%':
        $page->task = 'node_view';
        $page->subtask = '';
        variable_set('page_manager_node_view_disabled', FALSE);
        break;
      case 'node/add/%':

        // It seems nearly impossible to actually upgrade this properly.
        continue;
      case 'node/%/edit':

        // Could we get conflicts here if they had both?
        $page->task = 'node_edit';
        $page->subtask = '';
        variable_set('page_manager_node_edit_disabled', FALSE);
        break;
      case 'taxonomy/term':
      case 'taxonomy/term/%':
        $page->task = 'term_view';
        $page->subtask = '';
        if ($arguments[0]['name'] == 'term') {
          variable_set('page_manager_term_view_type', 'single');
        }
        variable_set('page_manager_term_view_disabled', FALSE);
        break;
      case 'user/%':
        $page->task = 'user_view';
        $page->subtask = '';
        variable_set('page_manager_user_view_disabled', FALSE);
        break;
    }
    if (empty($displays)) {

      // only one display on this panel, mak
      $cache = new stdClass();
      if ($page->task != 'node_edit') {
        $cache->handlers = array();
      }
      else {
        $cache->handlers = $node_edit_handlers;
      }
      _panels_update_create_handler($page, $p, NULL, array(
        'did' => $p->did,
        'title' => t('Panel'),
      ), $arguments, 0, $cache);
      $page->default_handlers = $cache->handlers;
    }
    else {

      // for each display we need to create a new handler.
      $weight = 0;
      $cache = new stdClass();
      if ($page->task != 'node_edit') {
        $cache->handlers = array();
      }
      else {
        $cache->handlers = $node_edit_handlers;
        $weight = count($cache->handlers) + 1;
      }
      foreach ($displays as $origin => $info) {
        if (!isset($info['argument_id'])) {
          $info['argument_id'] = 0;
        }
        _panels_update_create_handler($page, $p, $origin, $info, $arguments, $weight++, $cache);
      }

      // Also add the primary display as a default with no selector.
      //      _panels_update_create_handler($page, $p, NULL, array('did' => $p->did, 'title' => t('Default')), $arguments, $weight++, $cache);
      $page->default_handlers = $cache->handlers;
    }
    if ($page->task != 'page') {

      // just save the handlers.
      foreach ($cache->handlers as $name => $handler) {
        page_manager_save_task_handler($handler);

        // Keep all node edit handlers for later use.
        if ($page->task == 'node_edit') {
          $node_edit_handlers[$name] = $handler;
        }
      }
    }
    else {
      page_manager_page_save($page);
    }
  }
  $ret[] = update_sql("DROP TABLE {panels_page}");

  // Update a couple of pane types that changed and are easily moved:
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("UPDATE {panels_pane} SET type = CONCAT(type, '_', subtype) WHERE type = 'node_form'");
      break;
    case 'pgsql':
      $ret[] = update_sql("UPDATE {panels_pane} SET type = type || '_' || subtype WHERE type = 'node_form'");
  }
  $ret[] = update_sql("UPDATE {panels_pane} SET type = 'node_form_path' WHERE type = 'node_form_url_path'");
  if (module_exists('ctools') && !module_exists('views_content') && db_result(db_query("SELECT pid FROM {panels_pane} WHERE type = 'views'"))) {
    drupal_install_modules(array(
      'views_content',
    ));
  }
  return $ret;
}