<?php

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

/**
 * Implementation of hook_uninstall().
 */
function views_bulk_operations_uninstall() {
  variable_del('views_bulk_operations_actions');
  actions_synchronize(actions_list(), TRUE);
}

/**
 * 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();
}

/**
 * Implementation of hook_update_N().
 *
 * Update VBO settings to latest version.
 */
function views_bulk_operations_update_6001() {
  drupal_load('module', 'views');
  drupal_load('module', 'views_bulk_operations');
  views_include('cache');

  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;

        if (!empty($display_options['style_options']['selected_operations'])) foreach (array_filter($display_options['style_options']['selected_operations']) as $key) {
          // Adjust operation key.
          $new_key = str_replace(':', '-', $key);

          // Move selected operations.
          $display_options['style_options']['operations'][$new_key]['selected'] = TRUE;

          // Move operation labels.
          if (isset($display_options['style_options']['labels'][$key])) {
            $display_options['style_options']['operations'][$new_key]['label'] = $display_options['style_options']['labels'][$key];
          }

          // Move operations settings.
          if (isset($display_options['style_options'][$key])) {
            $display_options['style_options']['operations'][$new_key]['settings'] = $display_options['style_options'][$key];
            unset($display_options['style_options'][$key]);
          }

          // Move skip confirmation.
          $display_options['style_options']['operations'][$new_key]['skip_confirmation'] = $display_options['style_options']['skip_confirmation'];
        }

        // Unset old values.
        unset($display_options['style_options']['selected_operations']);
        unset($display_options['style_options']['labels']);
        unset($display_options['style_options']['skip_confirmation']);
      }
    }
    if ($changed) {
      $view->save();
      views_object_cache_clear('view', $name);
    }
  }
  cache_clear_all('*', 'cache_views');
  return array();
}

/**
 * Implementation of hook_update_N().
 *
 * Delete variable views_bulk_operations_actions to pick up new actions.
 */
function views_bulk_operations_update_6002() {
  variable_del('views_bulk_operations_actions');
  return array();
}
