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

/**
 * @file
 * Contains \Drupal\comment\Form\CommentAdminOverview.
 */

namespace Drupal\comment\Form;

use Drupal\comment\CommentInterface;
11
use Drupal\comment\CommentStorageInterface;
12
use Drupal\Component\Utility\Unicode;
13
use Drupal\Core\Datetime\Date as DateFormatter;
14
use Drupal\Core\Entity\EntityManagerInterface;
15 16 17 18 19 20 21 22 23 24 25 26
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the comments overview administration form.
 */
class CommentAdminOverview extends FormBase {

  /**
   * The entity storage.
   *
27
   * @var \Drupal\Core\Entity\EntityManagerInterface
28 29 30 31 32 33
   */
  protected $entityManager;

  /**
   * The comment storage.
   *
34
   * @var \Drupal\comment\CommentStorageInterface
35 36 37 38
   */
  protected $commentStorage;

  /**
39
   * The date formatter service.
40 41 42
   *
   * @var \Drupal\Core\Datetime\Date
   */
43
  protected $dateFormatter;
44 45 46 47 48 49 50 51 52 53 54

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Creates a CommentAdminOverview form.
   *
55
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
56
   *   The entity manager service.
57
   * @param \Drupal\comment\CommentStorageInterface $comment_storage
58
   *   The comment storage.
59 60
   * @param \Drupal\Core\Datetime\Date $date_formatter
   *   The date formatter service.
61 62 63
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
64
  public function __construct(EntityManagerInterface $entity_manager, CommentStorageInterface $comment_storage, DateFormatter $date_formatter, ModuleHandlerInterface $module_handler) {
65 66
    $this->entityManager = $entity_manager;
    $this->commentStorage = $comment_storage;
67
    $this->dateFormatter = $date_formatter;
68 69 70 71 72 73 74 75 76
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
77
      $container->get('entity.manager')->getStorage('comment'),
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      $container->get('date'),
      $container->get('module_handler')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'comment_admin_overview';
  }

  /**
   * Form constructor for the comment overview administration form.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   An associative array containing the current state of the form.
   * @param string $type
   *   The type of the overview form ('approval' or 'new').
   *
   * @return array
   *   The form structure.
   */
  public function buildForm(array $form, array &$form_state, $type = 'new') {

    // Build an 'Update options' form.
    $form['options'] = array(
      '#type' => 'details',
      '#title' => $this->t('Update options'),
109
      '#open' => TRUE,
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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
      '#attributes' => array('class' => array('container-inline')),
    );

    if ($type == 'approval') {
      $options['publish'] = $this->t('Publish the selected comments');
    }
    else {
      $options['unpublish'] = $this->t('Unpublish the selected comments');
    }
    $options['delete'] = $this->t('Delete the selected comments');

    $form['options']['operation'] = array(
      '#type' => 'select',
      '#title' => $this->t('Action'),
      '#title_display' => 'invisible',
      '#options' => $options,
      '#default_value' => 'publish',
    );
    $form['options']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Update'),
    );

    // Load the comments that need to be displayed.
    $status = ($type == 'approval') ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED;
    $header = array(
      'subject' => array(
        'data' => $this->t('Subject'),
        'specifier' => 'subject',
      ),
      'author' => array(
        'data' => $this->t('Author'),
        'specifier' => 'name',
        'class' => array(RESPONSIVE_PRIORITY_MEDIUM),
      ),
      'posted_in' => array(
        'data' => $this->t('Posted in'),
        'class' => array(RESPONSIVE_PRIORITY_LOW),
      ),
      'changed' => array(
        'data' => $this->t('Updated'),
        'specifier' => 'changed',
        'sort' => 'desc',
        'class' => array(RESPONSIVE_PRIORITY_LOW),
      ),
      'operations' => $this->t('Operations'),
    );
157
    $cids = $this->commentStorage->getQuery()
158 159 160 161 162
     ->condition('status', $status)
     ->tableSort($header)
     ->pager(50)
     ->execute();

163
    /** @var $comments \Drupal\comment\CommentInterface[] */
164 165 166 167 168 169 170 171 172 173
    $comments = $this->commentStorage->loadMultiple($cids);

    // Build a table listing the appropriate comments.
    $options = array();
    $destination = drupal_get_destination();

    $commented_entity_ids = array();
    $commented_entities = array();

    foreach ($comments as $comment) {
174
      $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
175 176 177
    }

    foreach ($commented_entity_ids as $entity_type => $ids) {
178
      $commented_entities[$entity_type] = $this->entityManager->getStorage($entity_type)->loadMultiple($ids);
179 180 181
    }

    foreach ($comments as $comment) {
182 183
      /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */
      $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
184 185 186 187 188 189 190 191 192
      $username = array(
        '#theme' => 'username',
        '#account' => comment_prepare_author($comment),
      );
      $body = '';
      if (!empty($comment->comment_body->value)) {
        $body = $comment->comment_body->value;
      }
      $comment_permalink = $comment->permalink();
193 194 195
      $attributes = $comment_permalink->getOption('attributes') ?: array();
      $attributes += array('title' => Unicode::truncate($body, 128));
      $comment_permalink->setOption('attributes', $attributes);
196
      $options[$comment->id()] = array(
197
        'title' => array('data' => array('#title' => $comment->getSubject() ?: $comment->id())),
198 199 200
        'subject' => array(
          'data' => array(
            '#type' => 'link',
201
            '#title' => $comment->getSubject(),
202
          ) + $comment_permalink->toRenderArray(),
203 204 205 206 207 208 209
        ),
        'author' => drupal_render($username),
        'posted_in' => array(
          'data' => array(
            '#type' => 'link',
            '#title' => $commented_entity->label(),
            '#access' => $commented_entity->access('view'),
210
          ) + $commented_entity->urlInfo()->toRenderArray(),
211
        ),
212
        'changed' => $this->dateFormatter->format($comment->getChangedTime(), 'short'),
213
      );
214
      $comment_uri_options = $comment->urlInfo()->getOptions();
215 216
      $links = array();
      $links['edit'] = array(
217
        'title' => $this->t('Edit'),
218 219
        'route_name' => 'comment.edit_page',
        'route_parameters' => array('comment' => $comment->id()),
220
        'options' => $comment_uri_options,
221 222 223 224
        'query' => $destination,
      );
      if ($this->moduleHandler->invoke('content_translation', 'translate_access', array($comment))) {
        $links['translate'] = array(
225
          'title' => $this->t('Translate'),
226 227
          'route_name' => 'content_translation.translation_overview_comment',
          'route_parameters' => array('comment' => $comment->id()),
228
          'options' => $comment_uri_options,
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
          'query' => $destination,
        );
      }
      $options[$comment->id()]['operations']['data'] = array(
        '#type' => 'operations',
        '#links' => $links,
      );
    }

    $form['comments'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#empty' => $this->t('No comments available.'),
    );

    $form['pager'] = array('#theme' => 'pager');

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, array &$form_state) {
    $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
    // We can't execute any 'Update options' if no comments were selected.
    if (count($form_state['values']['comments']) == 0) {
      $this->setFormError('', $form_state, $this->t('Select one or more comments to perform the update on.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    $operation = $form_state['values']['operation'];
    $cids = $form_state['values']['comments'];

    foreach ($cids as $cid) {
      // Delete operation handled in \Drupal\comment\Form\ConfirmDeleteMultiple
      // see \Drupal\comment\Controller\AdminController::adminPage().
      if ($operation == 'unpublish') {
        $comment = $this->commentStorage->load($cid);
273
        $comment->setPublished(FALSE);
274 275 276 277
        $comment->save();
      }
      elseif ($operation == 'publish') {
        $comment = $this->commentStorage->load($cid);
278
        $comment->setPublished(TRUE);
279 280 281 282 283 284 285 286 287 288
        $comment->save();
      }
    }
    drupal_set_message($this->t('The update has been performed.'));
    $form_state['redirect_route'] = array(
      'route_name' => 'comment.admin',
    );
  }

}