You are here

function workbench_scheduler_admin_edit_schedule in Workbench Scheduler 7.2

Same name and namespace in other branches
  1. 7 workbench_scheduler.admin.inc \workbench_scheduler_admin_edit_schedule()

Display a form for adding/editing a schedule.

1 string reference to 'workbench_scheduler_admin_edit_schedule'
workbench_scheduler_menu in ./workbench_scheduler.module
Implements hook_menu().

File

./workbench_scheduler.admin.inc, line 99
Provides admin functions for Workbench Scheduler.

Code

function workbench_scheduler_admin_edit_schedule($form, &$form_state, $name = FALSE) {
  $schedule = FALSE;
  if ($name) {

    // Attempt to load the schedule.
    if ($schedule = workbench_scheduler_machine_name_schedules_load($name)) {

      // Store in the form.
      $form['#schedule'] = $schedule;
    }
  }

  // Label field.
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Schedule Name'),
    '#required' => TRUE,
    '#description' => t('A user friendly name. Only used for admin purposes'),
    '#size' => 40,
    '#maxlength' => 127,
  );

  // Field for machine name.
  $form['name'] = array(
    '#title' => t('Machine Name'),
    '#description' => t('A machine friendly name.'),
    '#type' => 'machine_name',
    '#required' => TRUE,
    '#size' => 25,
    '#maxlength' => 25,
    '#default_value' => $schedule ? $schedule->name : '',
    '#machine_name' => array(
      'exists' => '_workbench_schedule_check_machine_name_exists',
      'source' => array(
        'label',
      ),
    ),
  );

  // Get state labels.
  $states = workbench_moderation_states();

  // Fetch all transitions (don't use states anymore).
  module_load_include('module', 'workbench_moderation');
  $transitions = workbench_moderation_transitions();
  $transition_options = array();

  // Make into an associative array.
  foreach ($transitions as $transition) {
    $transition_options[$transition->id] = array(
      'id' => $transition->id,
      'label' => $transition->name,
      'from_name' => $states[$transition->from_name]->label,
      'to_name' => $states[$transition->to_name]->label,
    );
  }

  // Label  and description.
  $form['transition_label'] = array(
    '#type' => 'markup',
    '#markup' => '<label>' . t('Transitions') . '</label>',
  );
  $form['transition_description'] = array(
    '#type' => 'markup',
    '#markup' => t('Select the transition to use for this schedule. Disabled transitions are being used by another schedule.'),
  );

  // Checkboxes for transitions.
  $form['transition'] = array(
    '#type' => 'tableselect',
    '#title' => t('Transition'),
    '#options' => $transition_options,
    '#multiple' => FALSE,
    '#required' => TRUE,
    '#header' => array(
      'label' => t('Name'),
      'from_name' => t('When content state is . . .'),
      'to_name' => t('Set content to . . .'),
    ),
    '#empty' => t('No transitions are configured with workbench moderation.'),
  );

  // Check to see if there are any schedules using this transition.
  $schedules = workbench_scheduler_schedules_load();
  if (!empty($schedules)) {
    foreach ($schedules as $schedule_obj) {
      if (empty($schedule) || $schedule_obj->sid != $schedule->sid) {
        $form['transition'][$schedule_obj->transition]['#disabled'] = TRUE;
      }
    }
  }

  // Fetch a list of content types that have moderation enabled.
  // From the workbench moderation module.
  module_load_include('module', 'workbench_moderation');
  $types = workbench_moderation_moderate_node_types();
  $info = entity_get_info('node');
  $tmp_types = array();

  // Make into an associative array.
  foreach ($types as $type) {
    $default_state = variable_get('workbench_moderation_default_state_' . $type, workbench_moderation_state_none());
    $label = $info['bundles'][$type]['label'];
    $tmp_types[$type] = array(
      'label' => $label,
      'default_state' => $states[$default_state]->label,
    );
  }

  // Store types to the form.
  $form['#types'] = $types;
  $types = $tmp_types;
  unset($tmp_types);
  $header = array(
    'label' => t('Content Type'),
    'default_state' => t('Default Moderation State'),
  );

  // Label  and description.
  $form['table_label'] = array(
    '#type' => 'markup',
    '#markup' => '<label>' . t('Content Types') . '</label>',
  );
  $form['table_description'] = array(
    '#type' => 'markup',
    '#markup' => t('Select the content types that can use this schedule.'),
  );

  // Checkboxes for content types.
  $form['types'] = array(
    '#type' => 'tableselect',
    '#title' => t('Content Types'),
    '#description' => t('Select the content types that can use this schedule'),
    '#header' => $header,
    '#options' => $types,
    '#required' => TRUE,
    '#empty' => t('No content types are configured with workbench moderation.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $schedule ? t('Update') : t('Save'),
  );

  // Editing a schedule?
  if ($schedule) {

    // Add a delete button.
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );

    // Remove the machine name field.
    unset($form['name']);

    // Set default values for the other fields.
    $form['label']['#default_value'] = $schedule->label;
    $form['transition']['#default_value'] = $schedule->transition;
    $form['types']['#default_value'] = drupal_map_assoc($schedule->types);
  }

  // Return the form.
  return $form;
}