<?php
/**
 * @file
 * Provides install, upgrade and un-install functions for menu_block.
 */

/**
 * Implements hook_uninstall().
 */
function menu_block_uninstall() {
  // Delete menu block variables.
  foreach (variable_get('menu_block_ids', array()) AS $delta) {
    variable_del("menu_block_{$delta}_title_link");
    variable_del("menu_block_{$delta}_admin_title");
    variable_del("menu_block_{$delta}_parent");
    variable_del("menu_block_{$delta}_level");
    variable_del("menu_block_{$delta}_follow");
    variable_del("menu_block_{$delta}_depth");
    variable_del("menu_block_{$delta}_expanded");
    variable_del("menu_block_{$delta}_sort");
  }
  variable_del('menu_block_ids');
  variable_del('menu_block_suppress_core');
  variable_del('menu_block_menu_order');
  // Remove block configurations.
  db_delete('block')
    ->condition('module', 'menu_block')
    ->execute();
  db_delete('block_role')
    ->condition('module', 'menu_block')
    ->execute();
  cache_clear_all();
}

/**
 * Implements hook_enable().
 */
function menu_block_enable() {
  drupal_set_message(t('To use menu blocks, find the "Add menu block" link on the <a href="@url">administer blocks page</a>.', array('@url' => url('admin/structure/block'))));
}

/**
 * Implements hook_install().
 */
function menu_block_install() {
  // No-op.
}

/**
 * Converts pre-5.x-1.0 block names to the new format.
 */
function menu_block_update_5100() {
  $delta = 0;
  $enabled_blocks = array();

  // Find the old enabled blocks.
  foreach (variable_get('menu_block_enabled_blocks', array()) AS $old_delta => $enabled) {
    list($mid, $level) = explode('-', $old_delta);
    if ($enabled) {
      $enabled_blocks[++$delta] = TRUE;
      variable_set("menu_block_{$delta}_mid", $mid);
      variable_set("menu_block_{$delta}_level", $level);
      variable_set("menu_block_{$delta}_depth", variable_get("menu_block_depth_{$mid}_{$level}", 0));
      variable_set("menu_block_{$delta}_expanded", variable_get("menu_block_expanded_{$mid}_{$level}", 0));
    }
    // Remove any of the old-style variables.
    variable_del("menu_block_depth_{$mid}_{$level}");
    variable_del("menu_block_expanded_{$mid}_{$level}");
  }
  variable_set('menu_block_enabled_blocks', $enabled_blocks);

  return t('A pre-release version of Menu block has been detected. All menu blocks from the pre-release version have been given a new delta and are no longer placed in any block regions; their block placement should be re-configured immediately.');
}

/**
 * Converts menu block-tracking data to new format.
 */
function menu_block_update_5200() {
  $block_ids = array();
  foreach (variable_get('menu_block_enabled_blocks', array()) AS $delta => $enabled) {
    if ($enabled) {
      $block_ids[] = $delta; // Build new $menu_block_ids.
      // Convert $menu_block_DELTA_mid to $menu_block_DELTA_menu_name.
      $mid = variable_get("menu_block_{$delta}_mid", 1);
      variable_set("menu_block_{$delta}_menu_name", $mid);
      // If we weren't upgraded to 5.x-2.x before the Drupal 6 upgrade, the
      // mid-to-menu_name conversion is not possible.
      variable_set("menu_block_{$delta}_title", $mid == 2 ? 'primary-links' : 'navigation');
      variable_del("menu_block_{$delta}_mid");
    }
    else {
      // Delete un-enabled menu block.
      variable_del("menu_block_{$delta}_mid");
      variable_del("menu_block_{$delta}_level");
      variable_del("menu_block_{$delta}_depth");
      variable_del("menu_block_{$delta}_expanded");
      db_delete('block')
        ->condition('module', 'menu_block')
        ->condition('delta', $delta)
        ->execute();
      db_delete('block_role')
        ->condition('module', 'menu_block')
        ->condition('delta', $delta)
        ->execute();
    }
  }
  // Finish conversion of $menu_block_enabled_blocks to $menu_block_ids.
  sort($block_ids);
  variable_set('menu_block_ids', $block_ids);
  variable_del('menu_block_enabled_blocks');
  cache_clear_all();

  return t('A 5.x-1.x version of Menu block has been detected and an attempt was made to upgrade it. Unfortunately, you should have upgraded to Menu block 5.x-2.x before your upgrade to Drupal 7. You may need to re-configure all your menu blocks.');
}

/**
 * Converts to Drupal 6 menu names.
 */
function menu_block_update_6200() {
  $menus = menu_get_menus();
  foreach (variable_get('menu_block_ids', array()) AS $delta) {
    // Drupal 6 uses the menu title to create the new menu_name.
    $menu_name = preg_replace('/[^a-zA-Z0-9]/', '-', strtolower(variable_get("menu_block_{$delta}_title", '')));
    // If we can't find the new menu_name, default to the navigation menu.
    if (empty($menus[$menu_name])) {
      $menu_name = 'navigation';
    }
    variable_set("menu_block_{$delta}_menu_name", $menu_name);
    variable_del("menu_block_{$delta}_title");
  }
  return t('The 5.x-2.x version of Menu block has been upgraded to the 6.x-2.0 data storage format.');
}

/**
 * Converts the menu names to parent items.
 */
function menu_block_update_6201() {
  $menus = menu_get_menus();
  foreach (variable_get('menu_block_ids', array()) AS $delta) {
    variable_set("menu_block_{$delta}_parent", variable_get("menu_block_{$delta}_menu_name", 'navigation') . ':0');
    variable_del("menu_block_{$delta}_menu_name");
  }
  return t('The 6.x-2.0 version of Menu block has been upgraded to the 6.x-2.1 data storage format.');
}

/**
 * Converts to Drupal 7 menu names.
 */
function menu_block_update_7200() {
  foreach (variable_get('menu_block_ids', array()) AS $delta) {
    $menu_name = '';
    list($old_menu_name, $parent_mlid) = explode(':', variable_get("menu_block_{$delta}_parent", ' : '));
    switch ($old_menu_name) {
      case 'primary-links':
        $menu_name = 'main-menu';
        break;
      case 'secondary-links':
        $menu_name = 'secondary-menu';
        break;
      case 'navigation':
        $menu_name = 'management';
        break;
    }
    if ($menu_name) {
      variable_set("menu_block_{$delta}_parent", $menu_name . ':' . $parent_mlid);
    }
  }
  return t('The 6.x-2.x version of Menu block has been upgraded to use the new menu names in Drupal 7. To use menu blocks in Drupal 7, find the "Add menu block" link on the <a href="@url">administer blocks page</a>.', array('@url' => url('admin/structure/block')));
}

/**
 * Helper function to fix custom menus in Drupal 7.0.
 */
function menu_block_fix_custom_menus() {
  // Make sure all custom menus are present in the active menus variable so that
  // their items may appear in the active trail.
  // @see menu_set_active_menu_names()
  $active_menus = variable_get('menu_default_active_menus', array_keys(menu_list_system_menus()));
  $update_variable = FALSE;
  foreach (menu_get_names() as $menu_name) {
    if (!in_array($menu_name, $active_menus) && (strpos($menu_name, 'menu-') === 0)) {
      $active_menus[] = $menu_name;
      $update_variable = TRUE;
    }
  }
  if ($update_variable) {
    variable_set('menu_default_active_menus', $active_menus);
  }
  // Clear the menu cache.
  cache_clear_all(NULL, 'cache_menu');
}

/**
 * Add missing custom menus to active menus list.
 */
function menu_block_update_7202() {
  menu_block_fix_custom_menus();
}
