You are here

function flag_views_bookmark_update_5101 in Flag 5

Same name and namespace in other branches
  1. 6.2 includes/flag.views_bookmark.inc \flag_views_bookmark_update_5101()
  2. 6 includes/flag.views_bookmark.inc \flag_views_bookmark_update_5101()

Update all views that use the 'views_bookmark_nodes_N.uid' field to use the 'views_bookmark_users_N.name' one. See http://drupal.org/node/211112

Update all views that use the 'views_bookmark_nodes_N.vbid' field to use the 'views_bookmark_ops_N.ops' one. See http://drupal.org/node/213488

File

includes/flag.views_bookmark.inc, line 445
flag.view_bookmark.inc

Code

function flag_views_bookmark_update_5101() {
  $ret = array();

  // Ensure views is available.
  include_once drupal_get_path('module', 'views') . '/views.module';

  // Load the cache to ensure _views_get_default_views() is available.
  if (function_exists('views_load_cache')) {
    views_load_cache();
  }

  // Update database views automatically.
  $result = db_query("SELECT vid FROM {view_view}");
  while ($view = db_fetch_object($result)) {
    $view = views_load_view($view->vid);
    $views[$view->name] = $view;
    foreach ($view->field as $delta => $field) {

      // Check if this view uses a 'views_bookmark_nodes_N.uid' field.
      if (strpos($field['tablename'], 'views_bookmark_nodes_') === 0 && $field['field'] == 'uid') {

        // If so, change it to use 'views_bookmark_users_N.name' instead.
        $view->field[$delta]['tablename'] = str_replace('_nodes_', '_users_', $field['tablename']);
        $view->field[$delta]['field'] = 'name';

        // Put the updated message in the return array.
        $ret[] = array(
          'success' => TRUE,
          'query' => t('The view %name has been updated to use the <em>views_bookmark_users_N.name</em> field', array(
            '%name' => $view->name,
          )),
        );
      }

      // Check if this view uses a 'views_bookmark_nodes_N.vbid' field.
      if (strpos($field['tablename'], 'views_bookmark_nodes_') === 0 && $field['field'] == 'vbid') {

        // If so, change it to use 'views_bookmark_ops_N.ops' instead.
        $view->field[$delta]['tablename'] = str_replace('_nodes_', '_ops_', $field['tablename']);
        $view->field[$delta]['field'] = 'ops';

        // Put the updated message in the return array.
        $ret[] = array(
          'success' => TRUE,
          'query' => t('The view %name has been updated to use the <em>views_bookmark_ops_N.ops</em> field', array(
            '%name' => $view->name,
          )),
        );
      }
    }
    _views_save_view($view);
  }

  // Because we can't actually update default views, just give the user a warning.
  $default_views = _views_get_default_views();
  $default_views_warnings = array();
  foreach ($default_views as $view) {
    foreach ($view->field as $delta => $field) {

      // Check if this view uses a 'views_bookmark_nodes_N.uid' field.
      if (strpos($field['tablename'], 'views_bookmark_nodes_') === 0 && $field['field'] == 'uid') {
        $default_views_warnings[] = t('%name needs the %field field updated.', array(
          '%name' => $view->name,
          '%field' => $field['id'],
        ));
      }

      // Check if this view uses a 'views_bookmark_nodes_N.vbid' field.
      if (strpos($field['tablename'], 'views_bookmark_nodes_') === 0 && $field['field'] == 'vbid') {
        $default_views_warnings[] = t('%name needs the %field field updated.', array(
          '%name' => $view->name,
          '%field' => $field['id'],
        ));
      }
    }
  }
  if (!empty($default_views_warnings)) {
    $message = t('The following views are provided by modules and need to be updated in order to work properly with this version of Flag.') . theme('item_list', $default_views_warnings);
    drupal_set_message($message, 'error');
    watchdog('flag', $message, WATCHDOG_ERROR);
  }

  // Clear views caches.
  if (function_exists('views_devel_caches')) {
    $caches = views_devel_caches();
    foreach ($caches as $cache_table) {
      db_query('TRUNCATE {' . $cache_table . '}');
    }
  }
  return $ret;
}