AggregatorController.php 6.26 KB
Newer Older
1 2
<?php

3
namespace Drupal\aggregator\Controller;
4

5
use Drupal\Component\Utility\Xss;
6
use Drupal\Core\Controller\ControllerBase;
7
use Drupal\Core\Datetime\DateFormatterInterface;
8
use Drupal\aggregator\FeedInterface;
9
use Drupal\Core\Url;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11 12 13 14

/**
 * Returns responses for aggregator module routes.
 */
15
class AggregatorController extends ControllerBase {
16

17 18 19
  /**
   * The date formatter service.
   *
20
   * @var \Drupal\Core\Datetime\DateFormatterInterface
21 22 23 24 25 26
   */
  protected $dateFormatter;

  /**
   * Constructs a \Drupal\aggregator\Controller\AggregatorController object.
   *
27
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
28
   *   The date formatter service.
29
   */
30
  public function __construct(DateFormatterInterface $date_formatter) {
31 32 33 34 35 36 37 38
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
39
      $container->get('date.formatter')
40 41 42
    );
  }

43 44 45 46 47 48 49
  /**
   * Presents the aggregator feed creation form.
   *
   * @return array
   *   A form array as expected by drupal_render().
   */
  public function feedAdd() {
50
    $feed = $this->entityManager()->getStorage('aggregator_feed')->create();
51
    return $this->entityFormBuilder()->getForm($feed);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  }

  /**
   * Builds a listing of aggregator feed items.
   *
   * @param \Drupal\aggregator\ItemInterface[] $items
   *   The items to be listed.
   * @param array|string $feed_source
   *   The feed source URL.
   *
   * @return array
   *   The rendered list of items for the feed.
   */
  protected function buildPageList(array $items, $feed_source = '') {
    // Assemble output.
67
    $build = [
68
      '#type' => 'container',
69 70 71
      '#attributes' => ['class' => ['aggregator-wrapper']],
    ];
    $build['feed_source'] = is_array($feed_source) ? $feed_source : ['#markup' => $feed_source];
72
    if ($items) {
73
      $build['items'] = $this->entityManager()->getViewBuilder('aggregator_item')
74
        ->viewMultiple($items, 'default');
75
      $build['pager'] = ['#type' => 'pager'];
76 77
    }
    return $build;
78 79
  }

80 81 82 83 84 85 86 87 88 89 90 91
  /**
   * Refreshes a feed, then redirects to the overview page.
   *
   * @param \Drupal\aggregator\FeedInterface $aggregator_feed
   *   An object describing the feed to be refreshed.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirection to the admin overview page.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   *   If the query token is missing or invalid.
   */
92
  public function feedRefresh(FeedInterface $aggregator_feed) {
93
    $message = $aggregator_feed->refreshItems()
94 95
      ? $this->t('There is new syndicated content from %site.', ['%site' => $aggregator_feed->label()])
      : $this->t('There is no new syndicated content from %site.', ['%site' => $aggregator_feed->label()]);
96
    drupal_set_message($message);
97
    return $this->redirect('aggregator.admin_overview');
98 99
  }

100 101 102 103 104 105 106
  /**
   * Displays the aggregator administration page.
   *
   * @return array
   *   A render array as expected by drupal_render().
   */
  public function adminOverview() {
107
    $entity_manager = $this->entityManager();
108
    $feeds = $entity_manager->getStorage('aggregator_feed')
109
      ->loadMultiple();
110

111 112
    $header = [$this->t('Title'), $this->t('Items'), $this->t('Last update'), $this->t('Next update'), $this->t('Operations')];
    $rows = [];
113
    /** @var \Drupal\aggregator\FeedInterface[] $feeds */
114
    foreach ($feeds as $feed) {
115
      $row = [];
116
      $row[] = $feed->link();
117
      $row[] = $this->formatPlural($entity_manager->getStorage('aggregator_item')->getItemCount($feed), '1 item', '@count items');
118 119
      $last_checked = $feed->getLastCheckedTime();
      $refresh_rate = $feed->getRefreshRate();
120

121
      $row[] = ($last_checked ? $this->t('@time ago', ['@time' => $this->dateFormatter->formatInterval(REQUEST_TIME - $last_checked)]) : $this->t('never'));
122 123 124 125
      if (!$last_checked && $refresh_rate) {
        $next_update = $this->t('imminently');
      }
      elseif ($last_checked && $refresh_rate) {
126
        $next_update = $next = $this->t('%time left', ['%time' => $this->dateFormatter->formatInterval($last_checked + $refresh_rate - REQUEST_TIME)]);
127 128 129 130 131
      }
      else {
        $next_update = $this->t('never');
      }
      $row[] = $next_update;
132
      $links['edit'] = [
133
        'title' => $this->t('Edit'),
134 135
        'url' => Url::fromRoute('entity.aggregator_feed.edit_form', ['aggregator_feed' => $feed->id()]),
      ];
136
      $links['delete'] = [
137
        'title' => $this->t('Delete'),
138
        'url' => Url::fromRoute('entity.aggregator_feed.delete_form', ['aggregator_feed' => $feed->id()]),
139 140
      ];
      $links['delete_items'] = [
141
        'title' => $this->t('Delete items'),
142
        'url' => Url::fromRoute('aggregator.feed_items_delete', ['aggregator_feed' => $feed->id()]),
143 144
      ];
      $links['update'] = [
145
        'title' => $this->t('Update items'),
146
        'url' => Url::fromRoute('aggregator.feed_refresh', ['aggregator_feed' => $feed->id()]),
147 148 149
      ];
      $row[] = [
        'data' => [
150 151
          '#type' => 'operations',
          '#links' => $links,
152 153
        ],
      ];
154 155
      $rows[] = $row;
    }
156
    $build['feeds'] = [
157
      '#prefix' => '<h3>' . $this->t('Feed overview') . '</h3>',
158
      '#type' => 'table',
159 160
      '#header' => $header,
      '#rows' => $rows,
161 162
      '#empty' => $this->t('No feeds available. <a href=":link">Add feed</a>.', [':link' => $this->url('aggregator.feed_add')]),
    ];
163

164 165 166
    return $build;
  }

167 168 169 170 171 172 173
  /**
   * Displays the most recent items gathered from any feed.
   *
   * @return string
   *   The rendered list of items for the feed.
   */
  public function pageLast() {
174
    $items = $this->entityManager()->getStorage('aggregator_item')->loadAll(20);
175
    $build = $this->buildPageList($items);
176
    $build['#attached']['feed'][] = ['aggregator/rss', $this->config('system.site')->get('name') . ' ' . $this->t('aggregator')];
177
    return $build;
178
  }
179

180 181 182 183 184 185
  /**
   * Route title callback.
   *
   * @param \Drupal\aggregator\FeedInterface $aggregator_feed
   *   The aggregator feed.
   *
186 187
   * @return array
   *   The feed label as a render array.
188 189
   */
  public function feedTitle(FeedInterface $aggregator_feed) {
190
    return ['#markup' => $aggregator_feed->label(), '#allowed_tags' => Xss::getHtmlTagList()];
191 192
  }

193
}