b18bf3d0b0c6be258c09d8839a4e739f16686f35
[yaffs-website] / web / core / modules / comment / src / Form / CommentAdminOverview.php
1 <?php
2
3 namespace Drupal\comment\Form;
4
5 use Drupal\comment\CommentInterface;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Datetime\DateFormatterInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Form\FormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\user\PrivateTempStoreFactory;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Provides the comments overview administration form.
17  */
18 class CommentAdminOverview extends FormBase {
19
20   /**
21    * The entity type manager.
22    *
23    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24    */
25   protected $entityTypeManager;
26
27   /**
28    * The comment storage.
29    *
30    * @var \Drupal\comment\CommentStorageInterface
31    */
32   protected $commentStorage;
33
34   /**
35    * The date formatter service.
36    *
37    * @var \Drupal\Core\Datetime\DateFormatterInterface
38    */
39   protected $dateFormatter;
40
41   /**
42    * The module handler.
43    *
44    * @var \Drupal\Core\Extension\ModuleHandlerInterface
45    */
46   protected $moduleHandler;
47
48   /**
49    * The tempstore factory.
50    *
51    * @var \Drupal\user\PrivateTempStoreFactory
52    */
53   protected $tempStoreFactory;
54
55   /**
56    * Creates a CommentAdminOverview form.
57    *
58    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
59    *   The entity manager service.
60    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
61    *   The date formatter service.
62    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
63    *   The module handler.
64    * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
65    *   The tempstore factory.
66    */
67   public function __construct(EntityTypeManagerInterface $entity_type_manager, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler, PrivateTempStoreFactory $temp_store_factory) {
68     $this->entityTypeManager = $entity_type_manager;
69     $this->commentStorage = $entity_type_manager->getStorage('comment');
70     $this->dateFormatter = $date_formatter;
71     $this->moduleHandler = $module_handler;
72     $this->tempStoreFactory = $temp_store_factory;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public static function create(ContainerInterface $container) {
79     return new static(
80       $container->get('entity_type.manager'),
81       $container->get('date.formatter'),
82       $container->get('module_handler'),
83       $container->get('user.private_tempstore')
84     );
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function getFormId() {
91     return 'comment_admin_overview';
92   }
93
94   /**
95    * Form constructor for the comment overview administration form.
96    *
97    * @param array $form
98    *   An associative array containing the structure of the form.
99    * @param \Drupal\Core\Form\FormStateInterface $form_state
100    *   The current state of the form.
101    * @param string $type
102    *   The type of the overview form ('approval' or 'new').
103    *
104    * @return array
105    *   The form structure.
106    */
107   public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') {
108
109     // Build an 'Update options' form.
110     $form['options'] = [
111       '#type' => 'details',
112       '#title' => $this->t('Update options'),
113       '#open' => TRUE,
114       '#attributes' => ['class' => ['container-inline']],
115     ];
116
117     if ($type == 'approval') {
118       $options['publish'] = $this->t('Publish the selected comments');
119     }
120     else {
121       $options['unpublish'] = $this->t('Unpublish the selected comments');
122     }
123     $options['delete'] = $this->t('Delete the selected comments');
124
125     $form['options']['operation'] = [
126       '#type' => 'select',
127       '#title' => $this->t('Action'),
128       '#title_display' => 'invisible',
129       '#options' => $options,
130       '#default_value' => 'publish',
131     ];
132     $form['options']['submit'] = [
133       '#type' => 'submit',
134       '#value' => $this->t('Update'),
135     ];
136
137     // Load the comments that need to be displayed.
138     $status = ($type == 'approval') ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED;
139     $header = [
140       'subject' => [
141         'data' => $this->t('Subject'),
142         'specifier' => 'subject',
143       ],
144       'author' => [
145         'data' => $this->t('Author'),
146         'specifier' => 'name',
147         'class' => [RESPONSIVE_PRIORITY_MEDIUM],
148       ],
149       'posted_in' => [
150         'data' => $this->t('Posted in'),
151         'class' => [RESPONSIVE_PRIORITY_LOW],
152       ],
153       'changed' => [
154         'data' => $this->t('Updated'),
155         'specifier' => 'changed',
156         'sort' => 'desc',
157         'class' => [RESPONSIVE_PRIORITY_LOW],
158       ],
159       'operations' => $this->t('Operations'),
160     ];
161     $cids = $this->commentStorage->getQuery()
162       ->condition('status', $status)
163       ->tableSort($header)
164       ->pager(50)
165       ->execute();
166
167     /** @var $comments \Drupal\comment\CommentInterface[] */
168     $comments = $this->commentStorage->loadMultiple($cids);
169
170     // Build a table listing the appropriate comments.
171     $options = [];
172     $destination = $this->getDestinationArray();
173
174     $commented_entity_ids = [];
175     $commented_entities = [];
176
177     foreach ($comments as $comment) {
178       $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
179     }
180
181     foreach ($commented_entity_ids as $entity_type => $ids) {
182       $commented_entities[$entity_type] = $this->entityTypeManager
183         ->getStorage($entity_type)
184         ->loadMultiple($ids);
185     }
186
187     foreach ($comments as $comment) {
188       /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */
189       $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
190       $comment_permalink = $comment->permalink();
191       if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
192         $attributes = $comment_permalink->getOption('attributes') ?: [];
193         $attributes += ['title' => Unicode::truncate($body, 128)];
194         $comment_permalink->setOption('attributes', $attributes);
195       }
196       $options[$comment->id()] = [
197         'title' => ['data' => ['#title' => $comment->getSubject() ?: $comment->id()]],
198         'subject' => [
199           'data' => [
200             '#type' => 'link',
201             '#title' => $comment->getSubject(),
202             '#url' => $comment_permalink,
203           ],
204         ],
205         'author' => [
206           'data' => [
207             '#theme' => 'username',
208             '#account' => $comment->getOwner(),
209           ],
210         ],
211         'posted_in' => [
212           'data' => [
213             '#type' => 'link',
214             '#title' => $commented_entity->label(),
215             '#access' => $commented_entity->access('view'),
216             '#url' => $commented_entity->urlInfo(),
217           ],
218         ],
219         'changed' => $this->dateFormatter->format($comment->getChangedTimeAcrossTranslations(), 'short'),
220       ];
221       $comment_uri_options = $comment->urlInfo()->getOptions() + ['query' => $destination];
222       $links = [];
223       $links['edit'] = [
224         'title' => $this->t('Edit'),
225         'url' => $comment->urlInfo('edit-form', $comment_uri_options),
226       ];
227       if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', [$comment])->isAllowed()) {
228         $links['translate'] = [
229           'title' => $this->t('Translate'),
230           'url' => $comment->urlInfo('drupal:content-translation-overview', $comment_uri_options),
231         ];
232       }
233       $options[$comment->id()]['operations']['data'] = [
234         '#type' => 'operations',
235         '#links' => $links,
236       ];
237     }
238
239     $form['comments'] = [
240       '#type' => 'tableselect',
241       '#header' => $header,
242       '#options' => $options,
243       '#empty' => $this->t('No comments available.'),
244     ];
245
246     $form['pager'] = ['#type' => 'pager'];
247
248     return $form;
249   }
250
251   /**
252    * {@inheritdoc}
253    */
254   public function validateForm(array &$form, FormStateInterface $form_state) {
255     $form_state->setValue('comments', array_diff($form_state->getValue('comments'), [0]));
256     // We can't execute any 'Update options' if no comments were selected.
257     if (count($form_state->getValue('comments')) == 0) {
258       $form_state->setErrorByName('', $this->t('Select one or more comments to perform the update on.'));
259     }
260   }
261
262   /**
263    * {@inheritdoc}
264    */
265   public function submitForm(array &$form, FormStateInterface $form_state) {
266     $operation = $form_state->getValue('operation');
267     $cids = $form_state->getValue('comments');
268     /** @var \Drupal\comment\CommentInterface[] $comments */
269     $comments = $this->commentStorage->loadMultiple($cids);
270     if ($operation != 'delete') {
271       foreach ($comments as $comment) {
272         if ($operation == 'unpublish') {
273           $comment->setUnpublished();
274         }
275         elseif ($operation == 'publish') {
276           $comment->setPublished();
277         }
278         $comment->save();
279       }
280       drupal_set_message($this->t('The update has been performed.'));
281       $form_state->setRedirect('comment.admin');
282     }
283     else {
284       $info = [];
285       /** @var \Drupal\comment\CommentInterface $comment */
286       foreach ($comments as $comment) {
287         $langcode = $comment->language()->getId();
288         $info[$comment->id()][$langcode] = $langcode;
289       }
290       $this->tempStoreFactory
291         ->get('comment_multiple_delete_confirm')
292         ->set($this->currentUser()->id(), $info);
293       $form_state->setRedirect('comment.multiple_delete_confirm');
294     }
295   }
296
297 }