<?php

/**
 * @file
 * Implementation of hook_install().
 */
function userreference_install() {
  drupal_load('module', 'content');
  content_notify('install', 'userreference');
}

/**
 * Implementation of hook_uninstall().
 */
function userreference_uninstall() {
  drupal_load('module', 'content');
  content_notify('uninstall', 'userreference');
}

/**
 * Implementation of hook_enable().
 *
 * Notify content module when this module is enabled.
 */
function userreference_enable() {
  drupal_load('module', 'content');
  content_notify('enable', 'userreference');
}

/**
 * Implementation of hook_disable().
 *
 * Notify content module when this module is disabled.
 */
function userreference_disable() {
  drupal_load('module', 'content');
  content_notify('disable', 'userreference');
}

function userreference_update_last_removed() {
  return 4;
}

/**
 * Placeholder update to set newly installed versions to the latest update number.
 */
function userreference_update_6000() {
  if ($abort = content_check_update('userreference')) {
    return $abort;
  }

  $ret = array();
  return $ret;
}

/**
 * Create an index by user reference column for all fields.
 */
function userreference_update_6001(&$sandbox) {
  include_once('./'. drupal_get_path('module', 'content') .'/content.install');
  drupal_load('module', 'content');

  $ret = array();

  if (!isset($sandbox['progress'])) {
    if ($abort = content_check_update('userreference')) {
      return $abort;
    }

    // Get the latest cache values and schema.
    content_clear_type_cache(TRUE, TRUE);
    $types = content_types_install();

    if (empty($types)) {
      return $ret;
    }

    $sandbox['fields'] = array();
    foreach ($types as $type_name => $fields) {
      foreach ($fields as $field) {
        if ($field['type'] == 'userreference') {
          $sandbox['fields'][] = $field;
        }
      }
    }

    if (empty($sandbox['fields'])) {
      return $ret;
    }

    $sandbox['progress'] = 0;
    $sandbox['visited'] = array();
  }

  $field = $sandbox['fields'][$sandbox['progress']];

  // We only want to process a field once -- if we hit it a second time,
  // that means it's its own table and it should have already been updated.
  if (!in_array($field['field_name'], $sandbox['visited'])) {
    $db_info = content_database_info($field);
    $table = $db_info['table'];
    $attributes = $db_info['columns']['uid'];
    $column = $attributes['column'];
    if (!content_db_index_exists($table, $column)) {
      db_add_index($ret, $table, $column, array($column));
    }
    $sandbox['visited'][] = $field['field_name'];
  }

  $sandbox['progress']++;
  $ret['#finished'] = $sandbox['progress'] / count($sandbox['fields']);

  return $ret;
}

/**
 * Convert 'referenceable_status' option from array to integer to match the
 * change in the field settings form where the element has been changed from
 * a checkboxes element (array) to a radios element (integer).
 *
 * Reference: @link http://drupal.org/node/416134 @endlink
 */
function userreference_update_6002() {
  $ret = array();

  drupal_load('module', 'content');

  $result = db_query("SELECT field_name, global_settings FROM {". content_field_tablename() ."} WHERE type = 'userreference'");
  while ($userreference = db_fetch_object($result)) {
    $global_settings = unserialize($userreference->global_settings);

    if (isset($global_settings['referenceable_status']) && is_array($global_settings['referenceable_status'])) {
      $referenceable_status = array_filter($global_settings['referenceable_status']);
      $global_settings['referenceable_status'] = (!empty($referenceable_status) ? 1 : '');

      // We can't use update_sql() here because of curly braces in serialized
      // array.
      db_query("UPDATE {". content_field_tablename() ."} SET global_settings = '%s' WHERE field_name = '%s'", serialize($global_settings), $userreference->field_name);
      $ret[] = array(
        'success' => TRUE,
        'query' => t("The 'referenceable_status' option for %field has been fixed.", array('%field' => $userreference->field_name)),
      );
    }
  }

  // Rebuild content caches only if necessary.
  if (!empty($ret)) {
    content_clear_type_cache();
  }

  return $ret;
}
