You are here

function flag_views_bookmark_update_7 in Flag 5

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

Add the current user filter to every view using flag filters. Previously this was implied with all filters, now it is optional to increase the flexibility of flag.

For some reason Flag began providing a Node: Node ID argument in the 5.x port. Because this argument should be handled by Views' own node.inc file, update existing arguments to use that one instead.

File

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

Code

function flag_views_bookmark_update_7() {
  $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;

    // Check if this view uses a flag filter.
    foreach ($view->filter as $delta => $filter) {
      if (strpos($filter['id'], 'flag_nodes_') === 0) {

        // If so, add the current user filter, which was previous assumed.
        $view->filter[$delta]['value'] = '***CURRENT_USER***';

        // Put the updated message in the return array.
        $ret[] = array(
          'success' => TRUE,
          'query' => t('The view %name has been updated to work with Flag 1.4 and higher.', array(
            '%name' => $view->name,
          )),
        );
      }
    }
    foreach ($view->argument as $delta => $argument) {
      if ($argument['type'] == 'node_nid') {
        $view->argument[$delta]['type'] = 'nid';
        $view->argument[$delta]['id'] = 'nid';
        $view->argument[$delta]['options'] = 0;

        // Put the updated message in the return array.
        $ret[] = array(
          'success' => TRUE,
          'query' => t('The view %name has been updated to use the <em>Node: ID</em> argument provided by Views node.inc rather than <em>Node: Node ID</em> provided by Flag.', 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->filter as $delta => $filter) {

      // If this view contains a flag filter and the value is not set.
      // The extensive type checking allows for value to be 0, but not NULL or an empty string.
      if (strpos($filter['id'], 'flag_nodes_') === 0 && (strlen($filter['value']) === 0 || $filter['value'] === 'NULL' || $filter['value'] === NULL)) {
        $default_views_warnings[] = t('%name needs the %filter filter updated.', array(
          '%name' => $view->name,
          '%filter' => $filter['id'],
        ));
      }
    }
    foreach ($view->argument as $delta => $argument) {
      if ($argument['type'] == 'node_nid') {
        $default_views_warnings[] = t('%name needs the %argument argument updated.', array(
          '%name' => $view->name,
          '%argument' => $argument['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. ' . 'Refer to the <a href="http://drupal.org/node/49075/release">Flag 1.4 release notes</a> for instructions on how to upgrade the default views in these modules.') . theme('item_list', $default_views_warnings);
    drupal_set_message($message, 'error');
    watchdog('flag', $message, WATCHDOG_ERROR);
  }
  $ret[] = array(
    'success' => TRUE,
    'query' => t('The views argument handler <em>Node: Node ID</em> has been removed. Use the <em>Node: ID</em> handler provided by Views instead.', array(
      '%name' => $view->name,
    )),
  );

  // 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;
}