You are here

function yandex_metrics_update_6200 in Yandex.Metrics 6.2

Upgrade from 6.x-1.x to 6.x-2.x.

File

./yandex_metrics.install, line 115
Install, uninstall and update the module.

Code

function yandex_metrics_update_6200() {

  // Enable yandex_metrics_reports.
  module_enable(array(
    'yandex_metrics_reports',
  ));

  // I don't know why but module_enable does not call hook_install.
  // I should call it by myself.
  module_invoke('yandex_metrics_reports', 'install');
  $ret = array();

  // Rename old popular content table to the new table.
  if (db_table_exists('yandex_metrics_popular_content')) {
    db_drop_table($ret, 'yandex_metrics_reports_popular_content');
    db_rename_table($ret, 'yandex_metrics_popular_content', 'yandex_metrics_reports_popular_content');
  }

  // Remap variables.
  variable_set('yandex_metrics_reports_popular_content_date_period', variable_get('yandex_metrics_popular_content_date_period', 'week'));
  variable_set('yandex_metrics_reports_popular_content_links_count', variable_get('yandex_metrics_popular_content_links_count', 10));
  variable_set('yandex_metrics_reports_client_id', variable_get('yandex_metrics_client_id', ''));
  variable_set('yandex_metrics_reports_client_secret', variable_get('yandex_metrics_client_secret', ''));
  variable_set('yandex_metrics_reports_auth_token', variable_get('yandex_metrics_auth_token', ''));
  variable_set('yandex_metrics_reports_counter_id', variable_get('yandex_metrics_counter_id', ''));
  variable_set('yandex_metrics_reports_use_ajax', variable_get('yandex_metrics_use_ajax', TRUE));
  variable_set('yandex_metrics_reports_visits_chart_visible', variable_get('yandex_metrics_visits_chart_visible', TRUE));
  variable_set('yandex_metrics_reports_sources_chart_visible', variable_get('yandex_metrics_sources_chart_visible', TRUE));
  variable_set('yandex_metrics_reports_search_phrases_visible', variable_get('yandex_metrics_search_phrases_visible', TRUE));
  variable_set('yandex_metrics_reports_popular_content_visible', variable_get('yandex_metrics_popular_content_visible', TRUE));
  variable_set('yandex_metrics_reports_geo_chart_visible', variable_get('yandex_metrics_geo_chart_visible', TRUE));

  // Remove unnecessary variables.
  variable_del('yandex_metrics_popular_content_date_period');
  variable_del('yandex_metrics_popular_content_links_count');
  variable_del('yandex_metrics_client_id');
  variable_del('yandex_metrics_client_secret');
  variable_del('yandex_metrics_auth_token');
  variable_del('yandex_metrics_counter_id');
  variable_del('yandex_metrics_use_ajax');
  variable_del('yandex_metrics_visits_chart_visible');
  variable_del('yandex_metrics_sources_chart_visible');
  variable_del('yandex_metrics_search_phrases_visible');
  variable_del('yandex_metrics_popular_content_visible');
  variable_del('yandex_metrics_geo_chart_visible');

  // Clear variables cache.
  cache_clear_all('variables', 'cache');
  if (db_result(db_query("SELECT count(*) FROM {blocks} WHERE module = 'yandex_metrics'")) > 0) {

    // Remove new block records.
    $ret[] = update_sql("DELETE FROM {blocks} WHERE module = 'yandex_metrics_reports'");

    // Assign old block records.
    $ret[] = update_sql("UPDATE {blocks} SET module = 'yandex_metrics_reports' WHERE module = 'yandex_metrics'");
  }

  // Clear CSS and JS caches.
  drupal_clear_css_cache();
  drupal_clear_js_cache();

  // Rebuild the menu.
  menu_rebuild();
  $ret[] = array(
    'success' => TRUE,
    'query' => 'The module has been upgraded to 6.x-2.x',
  );
  return $ret;
}