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

namespace Drupal\checklistapi\Form;

5
use Drupal\Component\Utility\Xss;
6
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7
use Drupal\Core\Form\FormInterface;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\Core\Link;
10
use Drupal\Core\Render\Element;
11
use Drupal\user\Entity\User;
12 13 14
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
15 16 17 18

/**
 * Provides a checklist form.
 */
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
class ChecklistapiChecklistForm implements FormInterface, ContainerInjectionInterface {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs an instance.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(DateFormatterInterface $date_formatter, MessengerInterface $messenger) {
    $this->dateFormatter = $date_formatter;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = $container->get('date.formatter');
    /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
    $messenger = $container->get('messenger');
    return new static($date_formatter, $messenger);
  }
58 59 60 61 62 63 64 65 66 67 68

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

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

    // Progress bar.
74
    $form['progress_bar'] = [
75
      '#theme' => 'checklistapi_progress_bar',
76
      '#message' => ($checklist->hasSavedProgress()) ? t('Last updated @date by @user', [
77
        '@date' => $checklist->getLastUpdatedDate(),
78
        '@user' => $checklist->getLastUpdatedUser(),
79
      ]) : '',
80 81 82
      '#number_complete' => $checklist->getNumberCompleted(),
      '#number_of_items' => $checklist->getNumberOfItems(),
      '#percent_complete' => (int) round($checklist->getPercentComplete()),
83 84 85 86 87
      '#attached' => [
        'library' => [
          'classy/progress',
        ],
      ],
88
    ];
89 90

    // Compact mode.
91
    if (checklistapi_compact_mode_is_on()) {
92
      $form['#attributes']['class'] = ['compact-mode'];
93
    }
94
    $form['compact_mode_link'] = [
95
      '#markup' => '<div class="compact-link"></div>',
96
    ];
97 98

    // General properties.
99 100 101 102
    $form['checklistapi'] = [
      '#attached' => [
        'library' => ['checklistapi/checklistapi'],
      ],
103 104
      '#tree' => TRUE,
      '#type' => 'vertical_tabs',
105
    ];
106 107 108 109 110 111

    // Loop through groups.
    $num_autochecked_items = 0;
    $groups = $checklist->items;
    foreach (Element::children($groups) as $group_key) {
      $group = &$groups[$group_key];
112
      $form[$group_key] = [
113
        '#title' => Xss::filter($group['#title']),
114 115
        '#type' => 'details',
        '#group' => 'checklistapi',
116
      ];
117
      if (!empty($group['#description'])) {
118
        $form[$group_key]['#description'] = Xss::filterAdmin($group['#description']);
119 120 121 122 123
      }

      // Loop through items.
      foreach (Element::children($group) as $item_key) {
        $item = &$group[$item_key];
124
        $saved_item = !empty($checklist->savedProgress['#items'][$item_key]) ? $checklist->savedProgress['#items'][$item_key] : 0;
125
        // Build title.
126
        $title = Xss::filter($item['#title']);
127 128
        if ($saved_item) {
          // Append completion details.
129
          $title .= '<span class="completion-details"> - ' . t('Completed @time by @user', [
130 131
            '@time' => $this->dateFormatter->format($saved_item['#completed'], 'short'),
            '@user' => User::load($saved_item['#uid'])->getAccountName(),
132
          ]) . '</span>';
133 134 135 136 137 138 139 140 141 142 143 144
        }
        // 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.
145
        $description = (isset($item['#description'])) ? '<p>' . Xss::filterAdmin($item['#description']) . '</p>' : '';
146
        // Append links.
147
        $links = [];
148 149
        foreach (Element::children($item) as $link_key) {
          $link = &$item[$link_key];
150
          $links[] = Link::fromTextAndUrl($link['#text'], $link['#url'])->toString();
151 152 153 154 155
        }
        if (count($links)) {
          $description .= '<div class="links">' . implode(' | ', $links) . '</div>';
        }
        // Compile the list item.
156 157
        $form[$group_key][$item_key] = [
          '#attributes' => ['class' => ['checklistapi-item']],
158
          '#default_value' => $default_value,
159
          '#description' => Xss::filterAdmin($description),
160
          '#disabled' => !($user_has_edit_access),
161
          '#title' => Xss::filterAdmin($title),
162 163
          '#type' => 'checkbox',
          '#group' => $group_key,
164 165
          '#parents' => ['checklistapi', $group_key, $item_key],
        ];
166 167 168 169
      }
    }

    // Actions.
170
    $form['actions'] = [
171 172 173
      '#access' => $user_has_edit_access,
      '#type' => 'actions',
      '#weight' => 100,
174
      'save' => [
175 176 177
        '#button_type' => 'primary',
        '#type' => 'submit',
        '#value' => t('Save'),
178 179
      ],
      'clear' => [
180 181
        '#access' => $checklist->hasSavedProgress(),
        '#button_type' => 'danger',
182 183
        '#attributes' => ['class' => ['clear-saved-progress']],
        '#submit' => [[$this, 'clear']],
184 185
        '#type' => 'submit',
        '#value' => t('Clear saved progress'),
186 187
      ],
    ];
188 189 190 191 192

    // 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') {
193
      $args = [
194 195
        '%checklist' => $checklist->title,
        '@num' => $num_autochecked_items,
196
      ];
197 198 199 200 201
      $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)
      );
202
      $this->messenger->addStatus($message);
203 204 205 206 207 208 209
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
210
  public function validateForm(array &$form, FormStateInterface $form_state) {}
211 212 213 214

  /**
   * {@inheritdoc}
   */
215
  public function submitForm(array &$form, FormStateInterface $form_state) {
216 217 218 219
    /** @var \Drupal\checklistapi\ChecklistapiChecklist $checklist */
    $checklist = $form['#checklist'];

    // Save progress.
220
    $values = $form_state->getValue('checklistapi');
221 222 223 224 225 226
    $checklist->saveProgress($values);

    // Preserve the active tab after submission.
    $form_state->setRedirect($checklist->getRouteName(), [], [
      'fragment' => $values['checklistapi__active_tab'],
    ]);
227 228 229 230 231 232 233
  }

  /**
   * Form submission handler for the 'clear' action.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
234 235
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
236
   */
237 238
  public function clear(array &$form, FormStateInterface $form_state) {
    $form_state->setRedirect($form['#checklist']->getRouteName() . '.clear');
239 240 241
  }

}