AggregatorController.php 8.43 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\aggregator\Controller\AggregatorController.
6 7
 */

8
namespace Drupal\aggregator\Controller;
9

10
use Drupal\Component\Utility\Xss;
11 12
use Drupal\Core\Controller\ControllerBase;
use Drupal\aggregator\FeedInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15 16 17 18

/**
 * Returns responses for aggregator module routes.
 */
19
class AggregatorController extends ControllerBase {
20

21 22 23 24 25 26 27
  /**
   * Presents the aggregator feed creation form.
   *
   * @return array
   *   A form array as expected by drupal_render().
   */
  public function feedAdd() {
28
    $feed = $this->entityManager()->getStorage('aggregator_feed')
29 30 31
      ->create(array(
        'refresh' => 3600,
      ));
32
    return $this->entityFormBuilder()->getForm($feed);
33 34 35 36 37 38 39 40 41 42 43 44 45
  }

  /**
   * Displays all the items captured from the particular feed.
   *
   * @param \Drupal\aggregator\FeedInterface $aggregator_feed
   *   The feed for which to display all items.
   *
   * @return array
   *   The rendered list of items for the feed.
   */
  public function viewFeed(FeedInterface $aggregator_feed) {
    $entity_manager = $this->entityManager();
46
    $feed_source = $entity_manager->getViewBuilder('aggregator_feed')
47 48
      ->view($aggregator_feed, 'default');
    // Load aggregator feed item for the particular feed id.
49
    $items = $entity_manager->getStorage('aggregator_item')->loadByFeed($aggregator_feed->id(), 20);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    // Print the feed items.
    $build = $this->buildPageList($items, $feed_source);
    return $build;
  }

  /**
   * 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.
    $build = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('aggregator-wrapper')),
    );
    $build['feed_source'] = is_array($feed_source) ? $feed_source : array('#markup' => $feed_source);
    if ($items) {
74
      $build['items'] = $this->entityManager()->getViewBuilder('aggregator_item')
75 76 77 78
        ->viewMultiple($items, 'default');
      $build['pager'] = array('#theme' => 'pager');
    }
    return $build;
79 80
  }

81 82 83 84 85 86 87 88 89 90 91 92
  /**
   * 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.
   */
93
  public function feedRefresh(FeedInterface $aggregator_feed) {
94 95 96 97
    $message = $aggregator_feed->refreshItems()
      ? $this->t('There is new syndicated content from %site.', array('%site' => $aggregator_feed->label()))
      : $this->t('There is no new syndicated content from %site.', array('%site' => $aggregator_feed->label()));
    drupal_set_message($message);
98
    return $this->redirect('aggregator.admin_overview');
99 100
  }

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

112
    $header = array($this->t('Title'), $this->t('Items'), $this->t('Last update'), $this->t('Next update'), $this->t('Operations'));
113
    $rows = array();
114
    foreach ($feeds as $feed) {
115
      $row = array();
116
      $row[] = l($feed->label(), "aggregator/sources/" . $feed->id());
117
      $row[] = format_plural($entity_manager->getStorage('aggregator_item')->getItemCount($feed), '1 item', '@count items');
118 119 120 121
      $last_checked = $feed->getLastCheckedTime();
      $refresh_rate = $feed->getRefreshRate();
      $row[] = ($last_checked ? $this->t('@time ago', array('@time' => format_interval(REQUEST_TIME - $last_checked))) : $this->t('never'));
      $row[] = ($last_checked && $refresh_rate ? $this->t('%time left', array('%time' => format_interval($last_checked + $refresh_rate - REQUEST_TIME))) : $this->t('never'));
122
      $links['edit'] = array(
123
        'title' => $this->t('Edit'),
124
        'route_name' => 'aggregator.feed_configure',
125
        'route_parameters' => array('aggregator_feed' => $feed->id()),
126 127
      );
      $links['delete'] = array(
128
        'title' => $this->t('Delete'),
129
        'route_name' => 'aggregator.feed_delete',
130
        'route_parameters' => array('aggregator_feed' => $feed->id()),
131
      );
132 133
      $links['delete_items'] = array(
        'title' => $this->t('Delete items'),
134
        'route_name' => 'aggregator.feed_items_delete',
135
        'route_parameters' => array('aggregator_feed' => $feed->id()),
136 137
      );
      $links['update'] = array(
138
        'title' => $this->t('Update items'),
139
        'route_name' => 'aggregator.feed_refresh',
140
        'route_parameters' => array('aggregator_feed' => $feed->id()),
141 142 143 144 145 146 147 148 149 150
      );
      $row[] = array(
        'data' => array(
          '#type' => 'operations',
          '#links' => $links,
        ),
      );
      $rows[] = $row;
    }
    $build['feeds'] = array(
151
      '#prefix' => '<h3>' . $this->t('Feed overview') . '</h3>',
152
      '#type' => 'table',
153 154
      '#header' => $header,
      '#rows' => $rows,
155
      '#empty' => $this->t('No feeds available. <a href="@link">Add feed</a>.', array('@link' => $this->url('aggregator.feed_add'))),
156 157
    );

158 159 160
    return $build;
  }

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

  /**
   * Displays all the feeds used by the Aggregator module.
   *
   * @return array
   *   A render array as expected by drupal_render().
   */
  public function sources() {
181
    $entity_manager = $this->entityManager();
182

183
    $feeds = $entity_manager->getStorage('aggregator_feed')->loadMultiple();
184 185 186 187 188 189 190 191 192 193

    $build = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('aggregator-wrapper')),
      '#sorted' => TRUE,
    );

    foreach ($feeds as $feed) {
      // Most recent items:
      $summary_items = array();
194
      $aggregator_summary_items = $this->config('aggregator.settings')
195 196
        ->get('source.list_max');
      if ($aggregator_summary_items) {
197
        $items = $entity_manager->getStorage('aggregator_item')
198
          ->loadByFeed($feed->id(), 20);
199
        if ($items) {
200
          $summary_items = $entity_manager->getViewBuilder('aggregator_item')
201 202 203
            ->viewMultiple($items, 'summary');
        }
      }
204
      $feed->url = $this->url('aggregator.feed_view', array('aggregator_feed' => $feed->id()));
205 206 207 208
      $build[$feed->id()] = array(
        '#theme' => 'aggregator_summary_items',
        '#summary_items' => $summary_items,
        '#source' => $feed,
209 210 211
        '#cache' => array(
          'tags' => $feed->getCacheTag(),
        ),
212 213 214 215 216
      );
    }
    $build['feed_icon'] = array(
      '#theme' => 'feed_icon',
      '#url' => 'aggregator/opml',
217
      '#title' => $this->t('OPML feed'),
218 219 220 221
    );
    return $build;
  }

222
  /**
223
   * Generates an OPML representation of all feeds.
224 225 226
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response containing the OPML.
227
   */
228
  public function opmlPage() {
229
     $feeds = $this->entityManager()
230
      ->getStorage('aggregator_feed')
231
      ->loadMultiple();
232 233 234 235 236 237 238 239 240 241 242 243 244

    $feeds = $result->fetchAll();
    $aggregator_page_opml = array(
      '#theme' => 'aggregator_page_opml',
      '#feeds' => $feeds,
    );
    $output = drupal_render($aggregator_page_opml);

    $response = new Response();
    $response->headers->set('Content-Type', 'text/xml; charset=utf-8');
    $response->setContent($output);

    return $response;
245 246
  }

247 248 249 250 251 252 253 254 255 256 257 258 259
  /**
   * Route title callback.
   *
   * @param \Drupal\aggregator\FeedInterface $aggregator_feed
   *   The aggregator feed.
   *
   * @return string
   *   The feed label.
   */
  public function feedTitle(FeedInterface $aggregator_feed) {
    return Xss::filter($aggregator_feed->label());
  }

260
}