You are here

function workbench_scheduler_admin_edit_schedule in Workbench Scheduler 7

Same name and namespace in other branches
  1. 7.2 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 100
Provides admin functions for Workbench Scheduler.

Code

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

  // Passed a machine name? editing a schedule.
  if ($name) {

    // Attempt to load the schedule.
    if ($schedule = workbench_scheduler_load_schedules($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',
      ),
    ),
  );

  // Fetch a list of the available moderation states.
  $states = workbench_scheduler_state_labels();

  // Save to the form.
  $form['#states'] = $states;

  // Add none option.
  $states = array_merge(array(
    '' => t('None'),
  ), $states);

  // Select list for start state.
  $form['start_state'] = array(
    '#type' => 'select',
    '#title' => t('Start State'),
    '#description' => t('Select the state to be set when a node reaches its "start date"'),
    '#options' => $states,
    '#required' => FALSE,
  );

  // Select list for end state.
  $form['end_state'] = array(
    '#type' => 'select',
    '#title' => t('End State'),
    '#description' => t('Select the state to be set when a node reaches its "end date"'),
    '#options' => $states,
    '#required' => FALSE,
  );

  // 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) {
    $states = workbench_moderation_states();
    $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['start_state']['#default_value'] = $schedule->start_state;
    $form['end_state']['#default_value'] = $schedule->end_state;
    $form['types']['#default_value'] = drupal_map_assoc($schedule->types);
  }

  // Return the form.
  return $form;
}