ChecklistapiChecklistForm.php 6.69 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\checklistapi\Form\ChecklistapiChecklistForm.
 */

namespace Drupal\checklistapi\Form;

10
use Drupal\Component\Utility\Xss;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Render\Element;

/**
 * Provides a checklist form.
 */
class ChecklistapiChecklistForm implements FormInterface {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'checklistapi_checklist_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state, $checklist_id = NULL) {
    $form['#checklist'] = $checklist = checklistapi_checklist_load($checklist_id);
    $user_has_edit_access = $checklist->userHasAccess('edit');

    // Progress bar.
    $progress_bar = array(
      '#theme' => 'checklistapi_progress_bar',
      '#message' => ($checklist->hasSavedProgress()) ? t('Last updated @date by !user', array(
        '@date' => $checklist->getLastUpdatedDate(),
        '!user' => $checklist->getLastUpdatedUser(),
39
      )) : '',
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      '#number_complete' => $checklist->getNumberCompleted(),
      '#number_of_items' => $checklist->getNumberOfItems(),
      '#percent_complete' => (int) round($checklist->getPercentComplete()),
    );
    $form['progress_bar'] = array(
      '#type' => 'markup',
      '#markup' => drupal_render($progress_bar),
    );

    // Compact mode.
    if (checklistapi_compact_mode()) {
      $form['#attributes']['class'] = array('compact-mode');
    }
    $compact_link = array('#theme' => 'checklistapi_compact_link');
    $form['compact_mode_link'] = array(
      '#markup' => drupal_render($compact_link),
    );

    // General properties.
    $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[$group_key] = array(
74
        '#title' => Xss::filter($group['#title']),
75 76 77 78
        '#type' => 'details',
        '#group' => 'checklistapi',
      );
      if (!empty($group['#description'])) {
79
        $form[$group_key]['#description'] = Xss::filterAdmin($group['#description']);
80 81 82 83 84 85 86
      }

      // 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.
87
        $title = Xss::filter($item['#title']);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        if ($saved_item) {
          // Append completion details.
          $user = array(
            '#theme' => 'username',
            '#account' => 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' => drupal_render($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.
113
        $description = (isset($item['#description'])) ? '<p>' . Xss::filterAdmin($item['#description']) . '</p>' : '';
114 115 116 117 118 119 120 121 122 123 124 125 126 127
        // 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[$group_key][$item_key] = array(
          '#attributes' => array('class' => array('checklistapi-item')),
          '#default_value' => $default_value,
128
          '#description' => Xss::filterAdmin($description),
129
          '#disabled' => !($user_has_edit_access),
130
          '#title' => Xss::filterAdmin($title),
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
          '#type' => 'checkbox',
          '#group' => $group_key,
          '#parents' => array('checklistapi', $group_key, $item_key),
        );
      }
    }

    // Actions.
    $form['actions'] = array(
      '#access' => $user_has_edit_access,
      '#type' => 'actions',
      '#weight' => 100,
      'save' => array(
        '#button_type' => 'primary',
        '#type' => 'submit',
        '#value' => t('Save'),
      ),
      'clear' => array(
        '#access' => $checklist->hasSavedProgress(),
        '#button_type' => 'danger',
        '#attributes' => array('class' => array('clear-saved-progress')),
        '#submit' => array(array($this, 'clear')),
        '#type' => 'submit',
        '#value' => t('Clear saved progress'),
      ),
    );

    // 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 = \Drupal::translation()->formatPlural(
        $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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, array &$form_state) {}

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    $form['#checklist']->saveProgress($form_state['values']['checklistapi']);
  }

  /**
   * Form submission handler for the 'clear' action.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   A reference to a keyed array containing the current state of the form.
   */
  public function clear(array $form, array &$form_state) {
    $form_state['redirect_route']['route_name'] = $form['#checklist']->getRouteName() . '.clear';
  }

}