<?php

/**
 * @file
 * Install, update and uninstall functions for drustack_wysiwyg.
 */

/**
 * Update the use of purified_html as basic_html.
 */
function drustack_wysiwyg_update_7000() {
  $purified_html = db_select('filter_format', 'ff')
    ->fields('ff')
    ->condition('ff.format', 'purified_html')
    ->condition('ff.status', 1)
    ->execute()->fetchColumn();

  if (count($purified_html)) {
    @module_enable(array('drustack_wysiwyg_basic_html'));

    $results = db_select('field_config', 'fc')
      ->fields('fc')
      ->condition('module', 'text')
      ->execute();
    foreach ($results as $result) {
      db_update('field_data_' . $result->field_name)
        ->fields(array(
          $result->field_name . '_format' => 'basic_html',
        ))
        ->condition($result->field_name . '_format', 'purified_html')
        ->execute();
      db_update('field_revision_' . $result->field_name)
        ->fields(array(
          $result->field_name . '_format' => 'basic_html',
        ))
        ->condition($result->field_name . '_format', 'purified_html')
        ->execute();
    }

    db_update('block_custom')
      ->fields(array(
        'format' => 'basic_html',
      ))
      ->condition('format', 'purified_html')
      ->execute();
    db_update('taxonomy_term_data')
      ->fields(array(
        'format' => 'basic_html',
      ))
      ->condition('format', 'purified_html')
      ->execute();
    db_update('users')
      ->fields(array(
        'signature_format' => 'basic_html',
      ))
      ->condition('signature_format', 'purified_html')
      ->execute();
  }
}

/**
 * Update the use of filtered_html as restricted_html.
 */
function drustack_wysiwyg_update_7001() {
  $filtered_html = db_select('filter_format', 'ff')
    ->fields('ff')
    ->condition('ff.format', 'filtered_html')
    ->condition('ff.status', 1)
    ->execute()->fetchColumn();

  if (count($filtered_html)) {
    @module_enable(array('drustack_wysiwyg_restricted_html'));

    $results = db_select('field_config', 'fc')
      ->fields('fc')
      ->condition('module', 'text')
      ->execute();
    foreach ($results as $result) {
      db_update('field_data_' . $result->field_name)
        ->fields(array(
          $result->field_name . '_format' => 'restricted_html',
        ))
        ->condition($result->field_name . '_format', 'filtered_html')
        ->execute();
      db_update('field_revision_' . $result->field_name)
        ->fields(array(
          $result->field_name . '_format' => 'restricted_html',
        ))
        ->condition($result->field_name . '_format', 'filtered_html')
        ->execute();
    }

    db_update('block_custom')
      ->fields(array(
        'format' => 'restricted_html',
      ))
      ->condition('format', 'filtered_html')
      ->execute();
    db_update('taxonomy_term_data')
      ->fields(array(
        'format' => 'restricted_html',
      ))
      ->condition('format', 'filtered_html')
      ->execute();
    db_update('users')
      ->fields(array(
        'signature_format' => 'restricted_html',
      ))
      ->condition('signature_format', 'filtered_html')
      ->execute();
  }
}

/**
 * Update the use of basic_html as full_html.
 */
function drustack_wysiwyg_update_7002() {
  $basic_html = db_select('filter_format', 'ff')
    ->fields('ff')
    ->condition('ff.format', 'basic_html')
    ->condition('ff.status', 1)
    ->execute()->fetchColumn();

  if (count($basic_html)) {
    @module_enable(array('drustack_wysiwyg_full_html'));

    $results = db_select('field_config', 'fc')
      ->fields('fc')
      ->condition('module', 'text')
      ->execute();
    foreach ($results as $result) {
      db_update('field_data_' . $result->field_name)
        ->fields(array(
          $result->field_name . '_format' => 'full_html',
        ))
        ->condition($result->field_name . '_format', 'basic_html')
        ->execute();
      db_update('field_revision_' . $result->field_name)
        ->fields(array(
          $result->field_name . '_format' => 'full_html',
        ))
        ->condition($result->field_name . '_format', 'basic_html')
        ->execute();
    }

    db_update('block_custom')
      ->fields(array(
        'format' => 'full_html',
      ))
      ->condition('format', 'basic_html')
      ->execute();
    db_update('taxonomy_term_data')
      ->fields(array(
        'format' => 'full_html',
      ))
      ->condition('format', 'basic_html')
      ->execute();
    db_update('users')
      ->fields(array(
        'signature_format' => 'full_html',
      ))
      ->condition('signature_format', 'basic_html')
      ->execute();
  }
}
