You are here

function webform_protected_downloads_form_alter in Webform Protected Downloads 7

Same name and namespace in other branches
  1. 6 webform_protected_downloads.module \webform_protected_downloads_form_alter()

Implementation of hook_form_alter().

_state

Parameters

array $form:

string $form_id:

Return value

void

File

./webform_protected_downloads.module, line 946
This file contains hook declarations and functions for the Webform Protected Downloads module.

Code

function webform_protected_downloads_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') !== FALSE) {

    // trigger processing after the form has been submitted and handled by webform
    $form['#submit'][] = 'webform_protected_downloads_process_submissions';
  }

  // update the file upload form, so that protected files can't be deleted
  // and listed
  if (strpos($form_id, '_node_form') !== FALSE && isset($form['#node']) && webform_protected_downloads_node_is_webform($form['#node'])) {
    if (isset($form['#node']->nid) && webform_protected_downloads_node_has_protected_files($form['#node']->nid)) {
      foreach ($form['#node']->wpd['protected_files'] as $file) {
        $file_item =& $form[$file->field][$form[$file->field]['#language']];
        $file_item[$file->weight]['#default_value']['display'] = 0;
        if (!in_array('webform_protected_downloads_file_widget_after_build', $file_item['#after_build'])) {
          $file_item['#after_build'][] = 'webform_protected_downloads_file_widget_after_build';
        }
      }
    }
  }
  switch ($form_id) {

    // disable redirect options on the form configuration form if the redirect
    // to the downloads page has been activated for protected downloads
    case 'webform_configure_form':
      if (webform_protected_downloads_get_configuration($form['nid']['#value'], 'redirect')) {
        $form['submission']['redirection']['redirect']['#attributes']['disabled'] = 'disabled';
        $form['submission']['redirection']['redirect_url']['#attributes']['disabled'] = 'disabled';
        $form['submission']['redirection']['#description'] .= '<br />' . t('This setting has been deactivated, because this webform is configured to redirect to the downloads page for the protected files. You can change this on the <a href="@protected_downloads_page">Protected Downloads page</a>.', array(
          '@protected_downloads_page' => url('node/' . $form['nid']['#value'] . '/protected-downloads'),
        ));
      }
      break;

    // disable the mandatory checkbox on the webform component overview, for
    // the component that is in use as mail for protected downloads
    case 'webform_components_form':
      if (webform_protected_downloads_node_has_protected_files($form['#node']->nid)) {
        $mandatory_field_name = webform_protected_downloads_get_mandatory_field_name();
        foreach ($form['components'] as $cid => &$element) {
          if (webform_protected_downloads_get_configuration($form['#node']->nid, 'mail_field_cid', NULL) == $cid) {
            $element[$mandatory_field_name]['#disabled'] = TRUE;
          }
        }
      }
      break;

    // disable the mandatory checkbox on the webform component edit form, if
    // the current component is the one used as mail for protected downloads
    case 'webform_component_edit_form':
      if (webform_protected_downloads_node_has_protected_files($form['nid']['#value'])) {
        if (webform_protected_downloads_get_configuration($form['nid']['#value'], 'mail_field_cid', NULL) == $form['cid']['#value']) {
          $mandatory_field_name = webform_protected_downloads_get_mandatory_field_name();
          $form['validation'][$mandatory_field_name]['#disabled'] = TRUE;
          $form['validation'][$mandatory_field_name]['#default_value'] = 1;
          $form['validation'][$mandatory_field_name]['#description'] .= '<br />' . t('This field has been disabled because one or more files attached to this webform have been protected. This component is used as the mail address for the confirmation mail that users need in order to access the protected files.');
          $form['display']['disabled']['#default_value'] = 0;
          $form['display']['disabled']['#disabled'] = TRUE;
          $form['display']['disabled']['#description'] .= '<br />' . t('This field has been disabled because one or more files attached to this node have been protected. This component is used as the mail address for the confirmation mail that users need in order to access the protected files so it must be possible to edit it.');
        }
      }
      break;
  }

  // check if this is a webform form and set a message if there is a problem
  // with this webform
  if (strpos($form_id, 'webform_') !== FALSE) {
    if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'webform-results') {
      $node = node_load(arg(1));
      if (webform_protected_downloads_node_is_webform($node) && $node->wpd['valid'] === FALSE) {
        _webform_protected_downloads_set_warning();
      }
    }
  }
}