You are here

function menu_token_update_7002 in Menu Token 7

Implements hook_update_N().

File

./menu_token.install, line 113
Install file for menu_token module.

Code

function menu_token_update_7002(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    if (db_table_exists('menu_token')) {
      $sandbox['max'] = db_select('menu_token', 'mt')
        ->countQuery()
        ->execute()
        ->fetchField();
    }
  }
  if (!empty($sandbox['max'])) {
    $tokens = db_select('menu_token', 'mt')
      ->fields('mt', array(
      'mlid',
      'link_path',
    ))
      ->orderBy('mlid')
      ->range($sandbox['progress'], 10)
      ->execute()
      ->fetchAllKeyed();
    if (!empty($tokens)) {
      $links = db_select('menu_links', 'ml')
        ->fields('ml', array(
        'mlid',
        'options',
      ))
        ->condition('mlid', array_keys($tokens))
        ->execute()
        ->fetchAllKeyed();
      foreach ($links as $mlid => $options) {
        $options = unserialize($options);
        $options['menu_token_link_path'] = $tokens[$mlid];
        $options['menu_token_link_data'] = array();
        db_update('menu_links')
          ->fields(array(
          'options' => serialize($options),
        ))
          ->condition('mlid', $mlid)
          ->execute();
      }
    }
    $sandbox['progress'] += 10;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished'] >= 1) {
    $sandbox['#finished'] = 1;

    // Drop the deprecated menu_token table if it exists.
    if (db_table_exists('menu_token')) {
      db_drop_table('menu_token');
    }
    return t('The Menu Token module has been updated successfully.');
  }
}