You are here

function checklistapi_checklist_form in Checklist API 7

Page callback: Form constructor for the checklist form.

Parameters

string $id: The checklist ID.

See also

checklistapi_checklist_form_submit()

checklistapi_menu()

1 string reference to 'checklistapi_checklist_form'
checklistapi_menu in ./checklistapi.module
Implements hook_menu().

File

./checklistapi.pages.inc, line 54
Page callbacks for the Checklist API module.

Code

function checklistapi_checklist_form($form, &$form_state, $id) {
  $form['#checklist'] = $checklist = checklistapi_checklist_load($id);
  $form['progress_bar'] = array(
    '#type' => 'markup',
    '#markup' => theme('checklistapi_progress_bar', array(
      'message' => $checklist
        ->hasSavedProgress() ? t('Last updated @date by !user', array(
        '@date' => $checklist
          ->getLastUpdatedDate(),
        '!user' => $checklist
          ->getLastUpdatedUser(),
      )) : ' ',
      'number_complete' => $checklist
        ->getNumberCompleted(),
      'number_of_items' => $checklist
        ->getNumberOfItems(),
      'percent_complete' => round($checklist
        ->getPercentComplete()),
    )),
  );
  if (checklistapi_compact_mode()) {
    $form['#attributes']['class'] = array(
      'compact-mode',
    );
  }
  $form['compact_mode_link'] = array(
    '#markup' => theme('checklistapi_compact_link'),
  );
  $form['checklistapi'] = array(
    '#attached' => array(
      'css' => array(
        drupal_get_path('module', 'checklistapi') . '/checklistapi.css',
      ),
      'js' => array(
        drupal_get_path('module', 'checklistapi') . '/checklistapi.js',
      ),
    ),
    '#tree' => TRUE,
    '#type' => 'vertical_tabs',
  );

  // Loop through groups.
  $num_autochecked_items = 0;
  $groups = $checklist->items;
  foreach (element_children($groups) as $group_key) {
    $group =& $groups[$group_key];
    $form['checklistapi'][$group_key] = array(
      '#title' => filter_xss($group['#title']),
      '#type' => 'fieldset',
    );
    if (!empty($group['#description'])) {
      $form['checklistapi'][$group_key]['#description'] = filter_xss_admin($group['#description']);
    }

    // Loop through items.
    foreach (element_children($group) as $item_key) {
      $item =& $group[$item_key];
      $saved_item = !empty($checklist->savedProgress[$item_key]) ? $checklist->savedProgress[$item_key] : 0;

      // Build title.
      $title = filter_xss($item['#title']);
      if ($saved_item) {

        // Append completion details.
        $user = user_load($saved_item['#uid']);
        $title .= t('<span class="completion-details"> - Completed @time by !user</a>', array(
          '@time' => format_date($saved_item['#completed'], 'short'),
          '!user' => theme('username', array(
            'account' => $user,
          )),
        ));
      }

      // Set default value.
      $default_value = FALSE;
      if ($saved_item) {
        $default_value = TRUE;
      }
      elseif (!empty($item['#default_value'])) {
        if ($default_value = $item['#default_value']) {
          $num_autochecked_items++;
        }
      }

      // Get description.
      $description = isset($item['#description']) ? '<p>' . filter_xss_admin($item['#description']) . '</p>' : '';

      // Append links.
      $links = array();
      foreach (element_children($item) as $link_key) {
        $link =& $item[$link_key];
        $options = !empty($link['#options']) && is_array($link['#options']) ? $link['#options'] : array();
        $links[] = l($link['#text'], $link['#path'], $options);
      }
      if (count($links)) {
        $description .= '<div class="links">' . implode(' | ', $links) . '</div>';
      }

      // Compile the list item.
      $form['checklistapi'][$group_key][$item_key] = array(
        '#attributes' => array(
          'class' => array(
            'checklistapi-item',
          ),
        ),
        '#default_value' => $default_value,
        '#description' => filter_xss_admin($description),
        '#disabled' => !($user_has_edit_access = $checklist
          ->userHasAccess('edit')),
        '#title' => filter_xss_admin($title),
        '#type' => 'checkbox',
      );
    }
  }
  $form['actions'] = array(
    '#access' => $user_has_edit_access,
    '#type' => 'actions',
    '#weight' => 100,
    'save' => array(
      '#submit' => array(
        'checklistapi_checklist_form_submit',
      ),
      '#type' => 'submit',
      '#value' => t('Save'),
    ),
    'clear' => array(
      '#access' => $checklist
        ->hasSavedProgress(),
      '#attributes' => array(
        'class' => array(
          'clear-saved-progress',
        ),
      ),
      '#href' => $checklist->path . '/clear',
      '#title' => t('Clear saved progress'),
      '#type' => 'link',
    ),
  );

  // Alert the user of autochecked items. Only set the message on GET requests
  // to prevent it from reappearing after saving the form. (Testing the request
  // method may not be the "correct" way to accomplish this.)
  if ($num_autochecked_items && $_SERVER['REQUEST_METHOD'] == 'GET') {
    $args = array(
      '%checklist' => $checklist->title,
      '@num' => $num_autochecked_items,
    );
    $message = format_plural($num_autochecked_items, t('%checklist found 1 unchecked item that was already completed and checked it for you. Save the form to record the change.', $args), t('%checklist found @num unchecked items that were already completed and checked them for you. Save the form to record the changes.', $args));
    drupal_set_message($message, 'status');
  }
  return $form;
}