You are here

function workbench_scheduler_admin_manage_node_schedules in Workbench Scheduler 7.2

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

Tableselect form for current schedules applied to a node.

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

File

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

Code

function workbench_scheduler_admin_manage_node_schedules($form, &$form_state, $node) {

  // Getting language (for date formats).
  global $language;

  // Declare schedules form array.
  $schedules = array(
    'schedule_active' => array(),
    'schedule_inactive' => array(),
  );

  // Fetch node schedule information for every revision.
  $node_schedules = workbench_scheduler_load_node_schedule($node->nid);

  // Process schedules.
  _workbench_scheduler_process_node_schedules($node_schedules);

  // Preparing results for render.
  if (!empty($node_schedules)) {
    foreach ($node_schedules as $schedule) {
      if ($schedule->active) {
        $schedule_data['schedule_active'][$schedule->vid][] = $schedule;
      }
      else {
        $schedule_data['schedule_inactive'][$schedule->vid][] = $schedule;
      }
    }
  }
  if (!empty($schedule_data)) {
    foreach ($schedule_data as $status => $status_schedules) {
      foreach ($status_schedules as $vid => $revision_schedules) {
        $items = array();
        foreach ($revision_schedules as $node_schedule) {

          // Loading full schedule data.
          $schedule = workbench_scheduler_schedules_load($node_schedule->sid);

          // Date.
          $date = t('not set');
          if (isset($node_schedule->date)) {
            $date = format_date($node_schedule->date, 'custom', workbench_scheduler_date_format(), NULL, $language->language);
          }

          // TODO: Need to change the way this looks when rendered.
          $completed = $node_schedule->completed ? '(' . t('Completed') . ')' : '';
          $items[] = $schedule->label . ' on ' . $date . ' ' . $completed;
        }

        // Schedule list.
        $schedule_list = array(
          'items' => $items,
        );

        // Build each row of the tableselect.
        $manage_schedules = array(
          'title' => array(
            'data' => array(
              '#title' => $vid,
            ),
          ),
          'vid' => array(
            'data' => l($vid, 'node/' . $node->nid . '/revisions/' . $vid . '/view'),
            'width' => 75,
          ),
          'schedules' => theme('item_list', $schedule_list),
        );

        // Loading the revision.
        $revision = node_load($node->nid, $vid);
        if (workbench_scheduler_node_schedule_access('set', $revision) || workbench_scheduler_node_schedule_access('view', $revision)) {
          $manage_schedules['vid_edit'] = l(t('edit'), 'node/' . $node->nid . '/manage_schedules/' . $vid . '/edit');
        }
        $schedules[$status][$vid] = $manage_schedules;
      }
    }

    // Render Schedules.
    foreach ($schedules as $status => $schedule) {
      if (!empty($schedule)) {

        // Build the the tableselect form, with a hidden field for the nid.
        $form['nid'] = array(
          '#type' => 'hidden',
          '#value' => $node->nid,
        );

        // Fieldsets.
        if ($status == 'schedule_active') {
          $fieldset = array(
            '#type' => 'fieldset',
            '#title' => t('Active Schedule'),
            '#description' => t('Only active schedules are run.'),
          );
        }
        else {
          $fieldset = array(
            '#type' => 'fieldset',
            '#title' => t('Inactive Schedules'),
          );
        }

        // Active/Inactive fieldset.
        $form[$status . '_fieldset'] = $fieldset;
        $records = array();

        // Flattening the array.
        foreach ($schedule as $vid => $schedule_revision) {
          $records[$vid] = $schedule_revision;
        }

        // Output the table of schedules.
        $form[$status . '_fieldset']['schedules'] = array(
          '#type' => 'tableselect',
          '#title' => t('Schedules applied to this node'),
          '#validated' => TRUE,
          '#options' => $records,
          '#multiple' => TRUE,
          '#header' => array(
            'vid' => t('Revision'),
            'schedules' => t('Schedules'),
          ),
        );

        // Only edit if have permissions.
        // Loading the revision.
        $revision = node_load($node->nid, $vid);
        if (workbench_scheduler_node_schedule_access('set', $revision) || workbench_scheduler_node_schedule_access('view', $revision)) {
          $form[$status . '_fieldset']['schedules']['#header']['vid_edit'] = array(
            'data' => t('Operations'),
            'width' => 200,
          );
        }
      }
    }

    // The form submit actions.
    if (workbench_scheduler_node_schedule_access('set', $node)) {
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Delete selected'),
      );
    }
    return $form;
  }
  else {

    // When no schedules applied to this node, display a status message.
    drupal_set_message(t('No schedules applied to this node'), 'status', FALSE);
  }
}