<?php
// $Id: views_bulk_operations.install,v 1.1.2.6 2010/03/23 00:07:29 kratib Exp $

/**
 * @file
 * Installation and update functions.
 */

/**
 * Implementation of hook_update_N().
 *
 * Replace MD5 hashes with meaningful strings in selected operations arrays.
 */
function views_bulk_operations_update_6000() {
  drupal_load('module', 'views');
  drupal_load('module', 'views_bulk_operations');
  views_include('cache');

  $operations = array();
  foreach (array_keys(_views_bulk_operations_get_object_info()) as $object_type) {
    $hook_name = $object_type .'_operations';
    foreach (module_invoke_all($hook_name) as $operation) {
      if (!empty($operation['callback'])) {
        $key = md5($operation['callback'] . (isset($operation['callback arguments']) ? serialize($operation['callback arguments']) : ''));
        $operations[$key] = $operation['callback'] . (empty($operation['callback arguments']) ? '' : ':'. md5(serialize($operation['callback arguments'])));
      }
    }
  }
  $result = db_query("SELECT * FROM {actions} WHERE parameters > ''");
  while ($action = db_fetch_object($result)) {
    $key = md5($action->aid);
    $parameters = unserialize($action->parameters);
    $operations[$key] = $action->callback . (empty($parameters) ? '' : ':'. md5($action->parameters));
  }
  foreach (array_keys(actions_list()) as $callback) {
    $key = md5($callback);
    $operations[$key] = $callback;
  }
  foreach (views_get_all_views() as $name => $view) {
    $changed = FALSE;
    foreach (array_keys($view->display) as $display) {
      $display_options = &$view->display[$display]->display_options;
      if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'bulk') {
        $changed = TRUE;
        $selected_operations = isset($display_options['style_options']['selected_operations']) && is_array($display_options['style_options']['selected_operations']) ? 
          array_filter($display_options['style_options']['selected_operations']) : array();
        $display_options['style_options']['selected_operations'] = array();
        if (!empty($selected_operations)) foreach ($selected_operations as $operation) {
          // If an old operation is identified, replace it.
          $display_options['style_options']['selected_operations'][] = isset($operations[$operation]) ? $operations[$operation] : $operation;
        }
      }
    }
    if ($changed) {
      $view->save();
      views_object_cache_clear('view', $name);
    }
  }
  cache_clear_all('*', 'cache_views');
  return array();
}

