AggregatorController.php 12.6 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\Core\Controller\ControllerBase;
11
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
12 13 14
use Drupal\aggregator\CategoryStorageControllerInterface;
use Drupal\aggregator\FeedInterface;
use Drupal\aggregator\ItemInterface;
15
use Drupal\Core\Database\Connection;
16 17 18
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
19 20 21 22

/**
 * Returns responses for aggregator module routes.
 */
23
class AggregatorController extends ControllerBase implements ContainerInjectionInterface {
24

25
  /**
26
   * The database connection.
27
   *
28
   * @var \Drupal\Core\Database\Connection;
29
   */
30
  protected $database;
31 32

  /**
33
   * The category storage controller.
34
   *
35
   * @var \Drupal\aggregator\CategoryStorageControllerInterface
36
   */
37
  protected $categoryStorage;
38

39
  /**
40
   * Constructs a \Drupal\aggregator\Controller\AggregatorController object.
41
   *
42 43
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
44 45
   * @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage
   *   The category storage service.
46
   */
47
  public function __construct(Connection $database, CategoryStorageControllerInterface $category_storage) {
48
    $this->database = $database;
49
    $this->categoryStorage = $category_storage;
50 51 52 53 54 55
  }

  /**
   * {inheritdoc}
   */
  public static function create(ContainerInterface $container) {
56
    return new static(
57
      $container->get('database'),
58
      $container->get('aggregator.category.storage')
59
    );
60 61 62 63 64 65 66 67 68
  }

  /**
   * Presents the aggregator feed creation form.
   *
   * @return array
   *   A form array as expected by drupal_render().
   */
  public function feedAdd() {
69 70
    $entity_manager = $this->entityManager();
    $feed = $entity_manager->getStorageController('aggregator_feed')
71 72 73
      ->create(array(
        'refresh' => 3600,
      ));
74 75 76 77 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 109 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
    return $entity_manager->getForm($feed);
  }

  /**
   * 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();
    $feed_source = $entity_manager->getRenderController('aggregator_feed')
      ->view($aggregator_feed, 'default');
    // Load aggregator feed item for the particular feed id.
    $items = $entity_manager->getStorageController('aggregator_item')->loadByFeed($aggregator_feed->id());
    // Print the feed items.
    $build = $this->buildPageList($items, $feed_source);
    $build['#title'] = $aggregator_feed->label();
    return $build;
  }

  /**
   * Displays feed items aggregated in a category.
   *
   * @param int $cid
   *   The category id for which to list all of the aggregated items.
   *
   * @return array
   *   The render array with list of items for the feed.
   */
  public function viewCategory($cid) {
    $category = $this->categoryStorage->load($cid);
    $items = $this->entityManager()->getStorageController('aggregator_item')->loadByCategory($cid);
    $build = $this->buildPageList($items);
    $build['#title'] = $category->title;
    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) {
      $build['items'] = $this->entityManager()->getRenderController('aggregator_item')
        ->viewMultiple($items, 'default');
      $build['pager'] = array('#theme' => 'pager');
    }
    return $build;
139 140
  }

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  /**
   * Refreshes a feed, then redirects to the overview page.
   *
   * @param \Drupal\aggregator\FeedInterface $aggregator_feed
   *   An object describing the feed to be refreshed.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object containing the search string.
   *
   * @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.
   */
  public function feedRefresh(FeedInterface $aggregator_feed, Request $request) {
    // @todo CSRF tokens are validated in page callbacks rather than access
    //   callbacks, because access callbacks are also invoked during menu link
    //   generation. Add token support to routing: http://drupal.org/node/755584.
    $token = $request->query->get('token');
    if (!isset($token) || !drupal_valid_token($token, 'aggregator/update/' . $aggregator_feed->id())) {
      throw new AccessDeniedHttpException();
    }

    // @todo after https://drupal.org/node/1972246 find a new place for it.
    aggregator_refresh($aggregator_feed);
166
    return $this->redirect('aggregator.admin_overview');
167 168
  }

169 170 171 172 173 174 175
  /**
   * Displays the aggregator administration page.
   *
   * @return array
   *   A render array as expected by drupal_render().
   */
  public function adminOverview() {
176
    $result = $this->database->query('SELECT f.fid, f.title, f.url, f.refresh, f.checked, f.link, f.description, f.hash, f.etag, f.modified, f.image, COUNT(i.iid) AS items FROM {aggregator_feed} f LEFT JOIN {aggregator_item} i ON f.fid = i.fid GROUP BY f.fid, f.title, f.url, f.refresh, f.checked, f.link, f.description, f.hash, f.etag, f.modified, f.image ORDER BY f.title');
177

178
    $header = array($this->t('Title'), $this->t('Items'), $this->t('Last update'), $this->t('Next update'), $this->t('Operations'));
179 180 181 182 183
    $rows = array();
    foreach ($result as $feed) {
      $row = array();
      $row[] = l($feed->title, "aggregator/sources/$feed->fid");
      $row[] = format_plural($feed->items, '1 item', '@count items');
184 185
      $row[] = ($feed->checked ? $this->t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked))) : $this->t('never'));
      $row[] = ($feed->checked && $feed->refresh ? $this->t('%time left', array('%time' => format_interval($feed->checked + $feed->refresh - REQUEST_TIME))) : $this->t('never'));
186 187
      $links = array();
      $links['edit'] = array(
188
        'title' => $this->t('Edit'),
189 190 191
        'href' => "admin/config/services/aggregator/edit/feed/$feed->fid",
      );
      $links['delete'] = array(
192
        'title' => $this->t('Delete'),
193 194 195
        'href' => "admin/config/services/aggregator/delete/feed/$feed->fid",
      );
      $links['remove'] = array(
196
        'title' => $this->t('Remove items'),
197 198 199
        'href' => "admin/config/services/aggregator/remove/$feed->fid",
      );
      $links['update'] = array(
200
        'title' => $this->t('Update items'),
201 202 203 204 205 206 207 208 209 210 211 212
        'href' => "admin/config/services/aggregator/update/$feed->fid",
        'query' => array('token' => drupal_get_token("aggregator/update/$feed->fid")),
      );
      $row[] = array(
        'data' => array(
          '#type' => 'operations',
          '#links' => $links,
        ),
      );
      $rows[] = $row;
    }
    $build['feeds'] = array(
213
      '#prefix' => '<h3>' . $this->t('Feed overview') . '</h3>',
214 215 216
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
217
      '#empty' => $this->t('No feeds available. <a href="@link">Add feed</a>.', array('@link' => $this->urlGenerator()->generateFromPath('admin/config/services/aggregator/add/feed'))),
218 219 220 221
    );

    $result = $this->database->query('SELECT c.cid, c.title, COUNT(ci.iid) as items FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid GROUP BY c.cid, c.title ORDER BY title');

222
    $header = array($this->t('Title'), $this->t('Items'), $this->t('Operations'));
223 224 225 226 227 228 229
    $rows = array();
    foreach ($result as $category) {
      $row = array();
      $row[] = l($category->title, "aggregator/categories/$category->cid");
      $row[] = format_plural($category->items, '1 item', '@count items');
      $links = array();
      $links['edit'] = array(
230
        'title' => $this->t('Edit'),
231 232
        'href' => "admin/config/services/aggregator/edit/category/$category->cid",
      );
233
      $links['delete'] = array(
234
        'title' => $this->t('Delete'),
235 236
        'href' => "admin/config/services/aggregator/delete/category/$category->cid",
      );
237 238 239 240 241 242 243 244 245
      $row[] = array(
        'data' => array(
          '#type' => 'operations',
          '#links' => $links,
        ),
      );
      $rows[] = $row;
    }
    $build['categories'] = array(
246
      '#prefix' => '<h3>' . $this->t('Category overview') . '</h3>',
247 248 249
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
250
      '#empty' => $this->t('No categories available. <a href="@link">Add category</a>.', array('@link' => $this->urlGenerator()->generateFromPath('admin/config/services/aggregator/add/category'))),
251 252 253 254 255
    );

    return $build;
  }

256 257 258 259 260 261 262
  /**
   * Displays all the categories used by the Aggregator module.
   *
   * @return array
   *   A render array.
   */
  public function categories() {
263
    $entity_manager = $this->entityManager();
264 265 266 267 268 269 270
    $result = $this->database->query('SELECT c.cid, c.title, c.description FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid LEFT JOIN {aggregator_item} i ON ci.iid = i.iid GROUP BY c.cid, c.title, c.description');

    $build = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('aggregator-wrapper')),
      '#sorted' => TRUE,
    );
271
    $aggregator_summary_items = $this->config('aggregator.settings')->get('source.list_max');
272 273 274
    foreach ($result as $category) {
      $summary_items = array();
      if ($aggregator_summary_items) {
275 276 277
        $items = $entity_manager->getStorageController('aggregator_item')->loadByCategory($category->cid);
        if ($items) {
          $summary_items = $entity_manager->getRenderController('aggregator_item')->viewMultiple($items, 'summary');
278 279
        }
      }
280
      $category->url = $this->urlGenerator()->generateFromPath('aggregator/categories/' . $category->cid);
281 282 283 284 285 286 287 288 289
      $build[$category->cid] = array(
        '#theme' => 'aggregator_summary_items',
        '#summary_items' => $summary_items,
        '#source' => $category,
      );
    }
    return $build;
  }

290 291 292 293 294 295 296
  /**
   * Displays the most recent items gathered from any feed.
   *
   * @return string
   *   The rendered list of items for the feed.
   */
  public function pageLast() {
297
    drupal_add_feed('aggregator/rss', $this->config('system.site')->get('name') . ' ' . $this->t('aggregator'));
298

299 300
    $items = $this->entityManager()->getStorageController('aggregator_item')->loadAll();
    return $this->buildPageList($items);
301
  }
302 303 304 305 306 307 308 309

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

312
    $feeds = $entity_manager->getStorageController('aggregator_feed')->loadMultiple();
313 314 315 316 317 318 319 320 321 322

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

    foreach ($feeds as $feed) {
      // Most recent items:
      $summary_items = array();
323
      $aggregator_summary_items = $this->config('aggregator.settings')
324 325
        ->get('source.list_max');
      if ($aggregator_summary_items) {
326 327 328 329
        $items = $entity_manager->getStorageController('aggregator_item')
          ->loadByFeed($feed->id());
        if ($items) {
          $summary_items = $entity_manager->getRenderController('aggregator_item')
330 331 332
            ->viewMultiple($items, 'summary');
        }
      }
333
      $feed->url = $this->urlGenerator()->generateFromRoute('aggregator.feed_view', array('aggregator_feed' => $feed->id()));
334 335 336 337 338 339 340 341 342
      $build[$feed->id()] = array(
        '#theme' => 'aggregator_summary_items',
        '#summary_items' => $summary_items,
        '#source' => $feed,
      );
    }
    $build['feed_icon'] = array(
      '#theme' => 'feed_icon',
      '#url' => 'aggregator/opml',
343
      '#title' => $this->t('OPML feed'),
344 345 346 347
    );
    return $build;
  }

348 349 350 351 352 353 354 355
  /**
   * @todo Remove aggregator_opml().
   */
  public function opmlPage($cid = NULL) {
    module_load_include('pages.inc', 'aggregator');
    return aggregator_page_opml($cid);
  }

356
}