You are here

function workbench_email_form in Workbench Email 7.3

Same name and namespace in other branches
  1. 7 workbench_email.admin.inc \workbench_email_form()

Administration form to create and delete email transitions.

Parameters

array $form: The form array

array $form_state: The form_state array

Return value

form A ready form array

2 string references to 'workbench_email_form'
workbench_email_ajax_form_submit in ./workbench_email.admin.inc
Ajax form submission callback.
workbench_email_menu in ./workbench_email.module
Implements hook_menu().

File

./workbench_email.admin.inc, line 19
Administrative forms for Workbench Email Module.

Code

function workbench_email_form($form, &$form_state) {

  // List existing states.
  $types = drupal_map_assoc(workbench_moderation_moderate_node_types());
  $transitions = workbench_moderation_transitions();
  $roles = workbench_email_determine_valid_roles();
  if (!$types) {
    drupal_set_message(t('Moderation is not enabled for any content types. Visit
                         the <a href="@url"> content type administration
                         page</a> to enable moderation for one or more types.', array(
      '@url' => url('admin/structure/types'),
    )), 'warning');
    $form['#disabled'] = TRUE;
  }
  elseif (!$roles) {
    drupal_set_message(t('Moderation is not enabled for any roles. Visit the
                         <a href="@url"> user permissions page</a> to enable moderation
                         for one or more roles.', array(
      '@url' => url('admin/people/permissions', array(
        'fragment' => 'module-workbench_moderation',
      )),
    )), 'warning');
    $form['#disabled'] = TRUE;
  }
  else {
    $form['transitions'] = array(
      '#tree' => TRUE,
    );
  }
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'workbench_email') . '/js/workbench_email.js',
  );
  $form['config_container'] = array(
    '#type' => 'fieldset',
    '#title' => t('Configuration'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['config_container']['queue_mail'] = array(
    '#type' => 'checkbox',
    '#title' => t('Queue Mail'),
    '#description' => t('This will send emails on cron instead of on form submission. Useful for sites with lots of users.'),
    '#options' => array(
      0 => t('Off'),
      1 => t('On'),
    ),
    '#weight' => -100,
    '#default_value' => variable_get('workbench_email_queue_mail', FALSE),
  );
  $form['transitions_container'] = array(
    '#type' => 'fieldset',
    '#title' => t('Transitions'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  foreach ($transitions as $transition) {
    $element = array();
    $element['transition'] = array(
      '#type' => 'value',
      '#value' => $transition,
    );
    $element['from_name'] = array(
      '#markup' => check_plain(workbench_moderation_state_label($transition->from_name)),
    );
    $element['to_name'] = array(
      '#markup' => check_plain(workbench_moderation_state_label($transition->to_name)),
    );
    foreach ($roles as $rid => $role) {
      $rid == WORKBENCH_EMAIL_AUTHOR ? $author = 1 : ($author = 0);
      $workbench_email = workbench_email_get($transition, $rid, $author);
      $element[$role]['notify'] = array(
        '#type' => 'checkbox',
        '#title' => check_plain(t('Notify')),
        '#default_value' => $workbench_email ? TRUE : FALSE,
        '#prefix' => '<div class="workbench-email-notify-container">',
        '#suffix' => '</div>',
        '#attributes' => array(
          'class' => array(
            'workbench-email-notify-checkbox',
          ),
        ),
      );
      $default_value = 0;
      if (isset($workbench_email[$transition->from_name . '_to_' . $transition->to_name][$rid])) {
        $default_value = $workbench_email[$transition->from_name . '_to_' . $transition->to_name][$rid]->automatic;
      }
      $display = 'display:none';
      if ($workbench_email) {
        $display = 'display:block';
      }
      $element[$role]['auto_notify'] = array(
        '#type' => 'checkbox',
        '#title' => check_plain(t('Auto Notify')),
        '#default_value' => $default_value ? TRUE : FALSE,
        '#prefix' => '<div class="workbench-email-auto-notify-container" style="' . $display . '">',
        '#suffix' => '</div>',
        '#attributes' => array(
          'class' => array(
            'workbench-email-auto-notify-checkbox',
          ),
        ),
      );
    }
    $form['transitions'][] = $element;
  }
  $form['transitions_container']['help_text_original_author'] = array(
    '#markup' => t('<strong>Original Author</strong>: The original author
    is available for each state transition to provide the ability
    to notify the author of the state changes even the change was
    triggered by someone else.<br />'),
  );
  $form['transitions_container']['help_text_notify'] = array(
    '#markup' => t('<strong>Notify</strong>: When content moves from
    state X to state Y, the user performing the operation will be presented
    with a UI select list to notify user(s) of the given role.<br />'),
  );
  $form['transitions_container']['help_text_auto_notify'] = array(
    '#markup' => t('<strong>Auto Notify</strong>: When content moves from
    state X to state Y, all users within the role with be notified without
    displaying an option to the user performing the action. In the case
    of the author, the single user is notified automatically. Note that
    "Notify" must be selected as well as "Auto Notify".<br />'),
  );
  $workbench_emails = workbench_email_get();
  $style = 'display:none';
  if ($workbench_emails) {
    $style = 'display:block';
  }
  $form['emails'] = array(
    '#type' => 'fieldset',
    '#title' => t('Emails'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attributes' => array(
      'style' => $style,
    ),
    '#prefix' => '<div id="workbench-email-emails-container">',
    '#suffix' => '</div>',
  );
  if ($workbench_emails) {
    $state_labels = workbench_moderation_state_labels();
    foreach ($workbench_emails as $transition_label => $email_transition_set) {
      $state_label = $email_transition_set;
      $transition = array_shift($state_label);
      $label = ucwords($state_labels[$transition->from_name] . ' To ' . $state_labels[$transition->to_name]);
      $transition_title = t('!label', array(
        '!label' => $label,
      ));
      $form['emails'][$transition_label] = array(
        '#type' => 'fieldset',
        '#title' => check_plain($transition_title),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      foreach ($email_transition_set as $rid => $email_transition) {
        $subject = $email_transition->subject;
        $message = $email_transition->message;
        if ($rid) {
          $role = user_role_load($rid);
        }
        else {
          $role = workbench_email_get_author_role();
        }
        $form['emails'][$transition_label][$rid] = array(
          '#type' => 'fieldset',
          '#title' => check_plain(t("Email @role_name", array(
            '@role_name' => ucwords($role->name),
          ))),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        );
        $form['emails'][$transition_label][$rid]['subject'] = array(
          '#type' => 'textfield',
          '#default_value' => $subject,
          '#title' => t('Subject'),
          '#maxlength' => 255,
        );
        $form['emails'][$transition_label][$rid]['message'] = array(
          '#type' => 'textarea',
          '#default_value' => $message,
          '#title' => t('Message'),
          '#size' => 30,
        );
      }
    }
    if ($workbench_emails) {
      $form['#tree'] = TRUE;

      // Display list of available placeholders if token module is installed.
      if (module_exists('token')) {
        module_load_include('inc', 'token', 'token.pages');
        $form['emails']['token_set'] = array(
          '#type' => 'fieldset',
          '#title' => t('Available Tokens'),
          '#collapsible' => TRUE,
        );

        // Use the token_tree_link theme if available.
        if (function_exists('theme_token_tree_link')) {
          $token_tree_theme = 'token_tree_link';
        }
        else {
          $token_tree_theme = 'token_tree';
        }
        $form['emails']['token_set']['tokens'] = array(
          '#theme' => $token_tree_theme,
          '#token_types' => array(
            'node' => 'node',
            'user' => 'user',
          ),
        );
      }
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Configuration'),
    '#ajax' => array(
      'callback' => 'workbench_email_ajax_form_submit',
      'wrapper' => 'workbench-email-emails-container',
      'effect' => 'fade',
    ),
  );
  return $form;
}