You are here

function workbench_email_form_node_form_alter in Workbench Email 7.3

Same name and namespace in other branches
  1. 7 workbench_email.module \workbench_email_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

Determines the current state and next state. Depending on that criteria builds a form element(s) to allow the user to select users to send an email to.

File

./workbench_email.form.inc, line 14
Code for the Workbench Email Module.

Code

function workbench_email_form_node_form_alter(&$form, $form_state) {
  if (workbench_moderation_node_type_moderated($form['type']['#value'])) {
    $available = FALSE;
    $wrapper_id = 'revision_information';

    // Workbench Moderation uses "options" fieldset in favor
    // of "revision information" if "administer roles" perm is
    // given to content moderator.
    if (isset($form['revision_information']['workbench_moderation_state_new'])) {
      $wrapper_id = 'revision_information';
      $available = TRUE;
    }
    else {
      if (isset($form['options']['workbench_moderation_state_new'])) {
        $wrapper_id = 'options';
        $available = TRUE;
      }
    }
    if (!$available) {
      return;
    }
    $form[$wrapper_id]['workbench_moderation_state_new']['#ajax'] = array(
      'callback' => 'workbench_email_form_node_callback',
      'wrapper' => 'workbench-email-form-node',
      'effect' => 'fade',
      'event' => 'change',
    );
    $form[$wrapper_id]['workbench_email'] = array(
      '#prefix' => '<div id="workbench-email-form-node">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
    );

    // Determine current state.
    if (isset($form['#node']->workbench_moderation['current']->state)) {
      $current_from_state = $form['#node']->workbench_moderation['current']->state;
    }
    else {
      $current_from_state = variable_get('workbench_moderation_default_state_' . $form['type']['#value'], workbench_moderation_state_none());
    }
    if ($current_from_state == workbench_moderation_state_published()) {
      $current_from_state = workbench_moderation_state_none();
    }

    // Initialize to the current state.
    $form_moderation_state = $current_from_state;
    if (!empty($form_state['values']) && isset($form_state['values']['workbench_moderation_state_new'])) {
      $form_moderation_state = $form_state['values']['workbench_moderation_state_new'];
    }
    if (!empty($form_state['values']) && isset($form_state['values'][$wrapper_id]['workbench_moderation_state_new'])) {
      $form_moderation_state = $form_state['values'][$wrapper_id]['workbench_moderation_state_new'];
    }
    $workbench_emails = workbench_email_get();
    foreach ($workbench_emails as $transition => $email_roles) {
      foreach ($email_roles as $rid => $email_transition) {
        $accounts = array();
        $editors = array();
        $flags = workbench_email_get_all_transition_users(array(
          'form' => $form,
          'form_state' => $form_state,
        ), 'form', $email_transition->rid, $accounts, $editors);
        $emails = workbench_email_filter_users($email_transition->rid, $accounts, $editors, $flags);
        if ($email_transition->from_name == $current_from_state && $email_transition->to_name == $form_moderation_state && !$email_transition->automatic && $emails) {
          $settings = array(
            'email_transition' => $email_transition,
            'emails' => $emails,
            'author' => $flags['author'],
            'editors' => $editors,
            'users' => $accounts,
          );
          workbench_email_create_form_element($form, $settings);
        }
      }
    }
    $form['#validate'][] = 'workbench_email_notification_validate';
    $form['actions']['submit']['#submit'][] = 'workbench_email_notification_submit';
  }
}