You are here

function field_encrypt_update_7000 in Field Encryption 7

Update database schema to re-used existing field tables.

File

./field_encrypt.install, line 50
Insallation functions for the Field Encryption module.

Code

function field_encrypt_update_7000() {

  // Get all the current entries.
  $results = db_select('field_encrypt', 'fe')
    ->fields('fe')
    ->execute()
    ->fetchAll();

  // Drop the old database table.
  db_drop_table('field_encrypt');
  if (count($results) > 0) {
    module_load_include('inc', 'field_encrypt');
    $fields = array();
    foreach ($results as $result) {
      if (!isset($fields[$result->field_name])) {
        $fields[$result->field_name] = field_info_field($result->field_name);
      }
      $field_info = $fields[$result->field_name];
      $data = unserialize(field_encrypt_decrypt($result->value));
      $table = key($field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
      unset($result->value);
      foreach ($field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table] as $field) {
        if (isset($data[$field])) {
          $result->{$field} = $data[$field];
        }
      }
      foreach (array(
        FIELD_LOAD_CURRENT,
        FIELD_LOAD_REVISION,
      ) as $key) {
        $table = key($field_info['storage']['details']['sql'][$key]);
        drupal_write_record($table, $result);
      }
    }
  }
  $fields = field_info_fields();
  foreach ($fields as $field_info) {
    if (isset($field_info['settings']['field_encrypt']['encrypt']) && $field_info['settings']['field_encrypt']['encrypt']) {
      module_load_include('inc', 'field_encrypt');
      $has_data = field_has_data($field_info['field_name']);
      field_encrypt_do_encrypt($field_info, $has_data);
    }
  }
}