ChecklistapiChecklistForm.php 6.5 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\checklistapi\Form;

5
use Drupal\Component\Utility\Xss;
6
use Drupal\Core\Form\FormInterface;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\Render\Element;
9
use Drupal\user\Entity\User;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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

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

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

    // Progress bar.
31
    $form['progress_bar'] = [
32
      '#theme' => 'checklistapi_progress_bar',
33
      '#message' => ($checklist->hasSavedProgress()) ? t('Last updated @date by @user', [
34
        '@date' => $checklist->getLastUpdatedDate(),
35
        '@user' => $checklist->getLastUpdatedUser(),
36
      ]) : '',
37 38 39
      '#number_complete' => $checklist->getNumberCompleted(),
      '#number_of_items' => $checklist->getNumberOfItems(),
      '#percent_complete' => (int) round($checklist->getPercentComplete()),
40 41 42 43 44
      '#attached' => [
        'library' => [
          'classy/progress',
        ],
      ],
45
    ];
46 47

    // Compact mode.
48
    if (checklistapi_compact_mode_is_on()) {
49
      $form['#attributes']['class'] = ['compact-mode'];
50
    }
51
    $form['compact_mode_link'] = [
52
      '#markup' => '<div class="compact-link"></div>',
53
    ];
54 55

    // General properties.
56 57 58 59
    $form['checklistapi'] = [
      '#attached' => [
        'library' => ['checklistapi/checklistapi'],
      ],
60 61
      '#tree' => TRUE,
      '#type' => 'vertical_tabs',
62
    ];
63 64 65 66 67 68

    // Loop through groups.
    $num_autochecked_items = 0;
    $groups = $checklist->items;
    foreach (Element::children($groups) as $group_key) {
      $group = &$groups[$group_key];
69
      $form[$group_key] = [
70
        '#title' => Xss::filter($group['#title']),
71 72
        '#type' => 'details',
        '#group' => 'checklistapi',
73
      ];
74
      if (!empty($group['#description'])) {
75
        $form[$group_key]['#description'] = Xss::filterAdmin($group['#description']);
76 77 78 79 80
      }

      // Loop through items.
      foreach (Element::children($group) as $item_key) {
        $item = &$group[$item_key];
81
        $saved_item = !empty($checklist->savedProgress['#items'][$item_key]) ? $checklist->savedProgress['#items'][$item_key] : 0;
82
        // Build title.
83
        $title = Xss::filter($item['#title']);
84 85
        if ($saved_item) {
          // Append completion details.
86
          $title .= '<span class="completion-details"> - ' . t('Completed @time by @user', [
87 88
            '@time' => format_date($saved_item['#completed'], 'short'),
            '@user' => User::load($saved_item['#uid'])->getUsername(),
89
          ]) . '</span>';
90 91 92 93 94 95 96 97 98 99 100 101
        }
        // 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.
102
        $description = (isset($item['#description'])) ? '<p>' . Xss::filterAdmin($item['#description']) . '</p>' : '';
103
        // Append links.
104
        $links = [];
105 106
        foreach (Element::children($item) as $link_key) {
          $link = &$item[$link_key];
107
          $links[] = \Drupal::l($link['#text'], $link['#url']);
108 109 110 111 112
        }
        if (count($links)) {
          $description .= '<div class="links">' . implode(' | ', $links) . '</div>';
        }
        // Compile the list item.
113 114
        $form[$group_key][$item_key] = [
          '#attributes' => ['class' => ['checklistapi-item']],
115
          '#default_value' => $default_value,
116
          '#description' => Xss::filterAdmin($description),
117
          '#disabled' => !($user_has_edit_access),
118
          '#title' => Xss::filterAdmin($title),
119 120
          '#type' => 'checkbox',
          '#group' => $group_key,
121 122
          '#parents' => ['checklistapi', $group_key, $item_key],
        ];
123 124 125 126
      }
    }

    // Actions.
127
    $form['actions'] = [
128 129 130
      '#access' => $user_has_edit_access,
      '#type' => 'actions',
      '#weight' => 100,
131
      'save' => [
132 133 134
        '#button_type' => 'primary',
        '#type' => 'submit',
        '#value' => t('Save'),
135 136
      ],
      'clear' => [
137 138
        '#access' => $checklist->hasSavedProgress(),
        '#button_type' => 'danger',
139 140
        '#attributes' => ['class' => ['clear-saved-progress']],
        '#submit' => [[$this, 'clear']],
141 142
        '#type' => 'submit',
        '#value' => t('Clear saved progress'),
143 144
      ],
    ];
145 146 147 148 149

    // 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') {
150
      $args = [
151 152
        '%checklist' => $checklist->title,
        '@num' => $num_autochecked_items,
153
      ];
154 155 156 157 158 159 160 161 162 163 164 165 166
      $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}
   */
167
  public function validateForm(array &$form, FormStateInterface $form_state) {}
168 169 170 171

  /**
   * {@inheritdoc}
   */
172
  public function submitForm(array &$form, FormStateInterface $form_state) {
173 174 175 176
    /** @var \Drupal\checklistapi\ChecklistapiChecklist $checklist */
    $checklist = $form['#checklist'];

    // Save progress.
177
    $values = $form_state->getValue('checklistapi');
178 179 180 181 182 183
    $checklist->saveProgress($values);

    // Preserve the active tab after submission.
    $form_state->setRedirect($checklist->getRouteName(), [], [
      'fragment' => $values['checklistapi__active_tab'],
    ]);
184 185 186 187 188 189 190
  }

  /**
   * Form submission handler for the 'clear' action.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
191 192
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
193
   */
194 195
  public function clear(array &$form, FormStateInterface $form_state) {
    $form_state->setRedirect($form['#checklist']->getRouteName() . '.clear');
196 197 198
  }

}