<?php

/**
 * Implementation of hook_permission().
 */
function webform_encrypt_permission() {
  return array(
    'view encrypted values' => array(
      'title' => t('View Encrypted Values in Webform Results'),
      'description' => t('Users that do not have this permission will see placeholder text.'),
    ),
  );
}

/**
 * Implementation of hook_form_alter()
 */
function webform_encrypt_form_alter(&$form, $form_state, $form_id) {
  // Add our fields to the component add/edit form.
  if ($form_id == 'webform_component_edit_form') {

    $component = $form_state['build_info']['args'][1];

    // Exclude webform component types that don't make sense to encrypt.
    $excluded_types = array('fieldset', 'file', 'markup', 'pagebreak');
    if (!in_array($form['type']['#value'], $excluded_types)) {

      // Add settings for encryption.
      $form['encryption'] = array(
        '#type' => 'fieldset',
        '#title' => t('Encryption'),
        '#tree' => TRUE,
      );
      $form['encryption']['encrypt'] = array(
        '#type' => 'checkbox',
        '#title' => t('Encrypt this field\'s value'),
        '#description' => t('!link to edit encryption settings.', array('!link' => l('Click here', 'admin/config/system/encrypt'))),
        '#default_value' => isset($component['extra']['encrypt']) ? $component['extra']['encrypt'] : 0,
      );
    }

  }
}

/**
 * Implementation of hook_webform_component_presave().
 * Save encryption settings for a component.
 */
function webform_encrypt_webform_component_presave(&$component) {
  if (!empty($component['encryption'])) {
    $component['extra'] = array_merge($component['extra'], $component['encryption']);
    unset($component['encryption']);
  }
}

/**
 * Implementation of hook_webform_submission_presave().
 * Encrypt the value if the component has been marked as such.
 */
function webform_encrypt_webform_submission_presave($node, &$submission) {
  dpm($node);
  foreach ($submission->data as $cid => $entry) {
    if (isset($node->webform['components'][$cid]['extra']['encrypt']) &&
        $node->webform['components'][$cid]['extra']['encrypt']) {
      $submission->data[$cid][0] = encrypt($entry[0], array('base64' => TRUE));
    }
  }
}
/**
 * Implementation of hook_webform_submission_render_alter().
 * Decrypt values when displaying webform submissions.
 * Implements hook_webform_submission_load().
 *
 * Decrypt values when loading a submission
 */       
function webform_encrypt_webform_submission_load(array &$submissions) {
  foreach ($submissions as &$submission) {
    // Loop through the data, the key is the `cid` that can be used to
    // load the component
    foreach ($submission->data as $cid => &$value) {
      // The last parameter is only used when loading a "new" cid
      $component = webform_menu_component_load($cid, $submission->nid, NULL);

      // Continue when the component isn't encrypted at all.
      if (empty($component['extra']['encrypt']) || empty($value[0])) {
        continue;
      }
      // Decript
      if (user_access('view encrypted values')) {
        $value[0] = decrypt($value[0], array('base64' => TRUE));
      }
      else {
        $value[0] = t('[Value Encrypted]');
      }
    }
  }
}
