You are here

function system_update_151 in Drupal 5

Same name and namespace in other branches
  1. 4 database/updates.inc \system_update_151()

File

modules/system/system.install, line 2031

Code

function system_update_151() {
  $ret = array();
  $ts = variable_get('theme_settings', NULL);

  // set up data array so we can loop over both sets of links
  $menus = array(
    0 => array(
      'links_var' => 'primary_links',
      'toggle_var' => 'toggle_primary_links',
      'more_var' => 'primary_links_more',
      'menu_name' => 'Primary links',
      'menu_var' => 'menu_primary_menu',
      'pid' => 0,
    ),
    1 => array(
      'links_var' => 'secondary_links',
      'toggle_var' => 'toggle_secondary_links',
      'more_var' => 'secondary_links_more',
      'menu_name' => 'Secondary links',
      'menu_var' => 'menu_secondary_menu',
      'pid' => 0,
    ),
  );
  for ($loop = 0; $loop <= 1; $loop++) {

    // create new Primary and Secondary links menus
    $menus[$loop]['pid'] = db_next_id('{menu}_mid');
    $ret[] = update_sql("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) " . "VALUES ({$menus[$loop]['pid']}, 0, '', '{$menus[$loop]['menu_name']}', '', 0, 115)");

    // Gather links from various settings into a single array.
    $phptemplate_links = variable_get("phptemplate_" . $menus[$loop]['links_var'], array());
    if (empty($phptemplate_links)) {
      $phptemplate_links = array(
        'text' => array(),
        'link' => array(),
      );
    }
    if (isset($ts) && is_array($ts)) {
      if (is_array($ts[$menus[$loop]['links_var']])) {
        $theme_links = $ts[$menus[$loop]['links_var']];
      }
      else {

        // Convert old xtemplate style links.
        preg_match_all('/<a\\s+.*?href=[\\"\'\\s]?(.*?)[\\"\'\\s]?>(.*?)<\\/a>/i', $ts[$menus[$loop]['links_var']], $urls);
        $theme_links['text'] = $urls[2];
        $theme_links['link'] = $urls[1];
      }
    }
    else {
      $theme_links = array(
        'text' => array(),
        'link' => array(),
      );
    }
    $links['text'] = array_merge($phptemplate_links['text'], $theme_links['text']);
    $links['link'] = array_merge($phptemplate_links['link'], $theme_links['link']);

    // insert all entries from theme links into new menus
    $num_inserted = 0;
    for ($i = 0; $i < count($links['text']); $i++) {
      if ($links['text'][$i] != "" && $links['link'][$i] != "") {
        $num_inserted++;
        $node_unalias = db_fetch_array(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $links['link'][$i]));
        if (isset($node_unalias) && is_array($node_unalias)) {
          $link_path = $node_unalias['src'];
        }
        else {
          $link_path = $links['link'][$i];
        }
        $mid = db_next_id('{menu}_mid');
        $ret[] = update_sql("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) " . "VALUES ({$mid}, {$menus[$loop]['pid']}, '" . db_escape_string($link_path) . "', '" . db_escape_string($links['text'][$i]) . "', '" . db_escape_string($links['description'][$i]) . "', 0, 118)");
      }
    }

    // delete Secondary links if not populated.
    if ($loop == 1 && $num_inserted == 0) {
      db_query("DELETE FROM {menu} WHERE mid={$menus[$loop]['pid']}");
    }

    // Set menu_primary_menu and menu_primary_menu variables if links were
    // imported. If the user had links but the toggle display was off, they
    // will need to disable the new links manually in admins/settings/menu.
    if ($num_inserted == 0) {
      variable_set($menus[$loop]['menu_var'], 0);
    }
    else {
      variable_set($menus[$loop]['menu_var'], $menus[$loop]['pid']);
    }
    variable_del('phptemplate_' . $menus[$loop]['links_var']);
    variable_del('phptemplate_' . $menus[$loop]['links_var'] . '_more');
    variable_del($menus[$loop]['toggle_var']);
    variable_del($menus[$loop]['more_var']);

    // If user has old xtemplate links in a string, leave them in the var.
    if (isset($ts) && is_array($ts) && is_array($ts[$menus[$loop]['links_var']])) {
      variable_del($menus[$loop]['links_var']);
    }
  }
  if (isset($ts) && is_array($ts)) {
    variable_set('theme_settings', $ts);
  }
  $ret[] = update_sql("UPDATE {system} SET status = 1 WHERE name = 'menu'");
  return $ret;
}