aggregator.pages.inc

  1. 7.x drupal/modules/aggregator/aggregator.pages.inc
  2. 6.x drupal/modules/aggregator/aggregator.pages.inc

User page callbacks for the Aggregator module.

Functions

Namesort descending Description
aggregator_categorize_items Form constructor to build the page list form.
aggregator_categorize_items_submit Form submission handler for aggregator_categorize_items().
aggregator_feed_items_load Loads and optionally filters feed items.
aggregator_page_categories Page callback: Displays all the categories used by the Aggregator module.
aggregator_page_category Page callback: Displays all the items aggregated in a particular category.
aggregator_page_category_form Page callback: Displays a form containing items aggregated in a category.
aggregator_page_last Page callback: Displays the most recent items gathered from any feed.
aggregator_page_opml Page callback: Generates an OPML representation of all feeds.
aggregator_page_rss Page callback: Generates an RSS 0.92 feed of aggregator items or categories.
aggregator_page_source Page callback: Displays all the items captured from the particular feed.
aggregator_page_sources Page callback: Displays all the feeds used by the aggregator.
aggregator_page_source_form Page callback: Displays a form with all items captured from a feed.
template_preprocess_aggregator_feed_source Processes variables for aggregator-feed-source.tpl.php.
template_preprocess_aggregator_item Processes variables for aggregator-item.tpl.php.
template_preprocess_aggregator_summary_item Processes variables for aggregator-summary-item.tpl.php.
template_preprocess_aggregator_summary_items Processes variables for aggregator-summary-items.tpl.php.
template_preprocess_aggregator_wrapper Processes variables for aggregator-wrapper.tpl.php.
theme_aggregator_categorize_items Returns HTML for the aggregator page list form for assigning categories.
theme_aggregator_page_opml Prints the OPML page for the feed.
theme_aggregator_page_rss Prints the RSS page for a feed.
_aggregator_page_list Prints an aggregator page listing a number of feed items.

File

drupal/modules/aggregator/aggregator.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the Aggregator module.
  5. */
  6. /**
  7. * Page callback: Displays the most recent items gathered from any feed.
  8. *
  9. * @return
  10. * The rendered list of items for the feed.
  11. */
  12. function aggregator_page_last() {
  13. drupal_add_feed('aggregator/rss', variable_get('site_name', 'Drupal') . ' ' . t('aggregator'));
  14. $items = aggregator_feed_items_load('sum');
  15. return _aggregator_page_list($items, arg(1));
  16. }
  17. /**
  18. * Page callback: Displays all the items captured from the particular feed.
  19. *
  20. * @param $feed
  21. * The feed for which to display all items.
  22. *
  23. * @return
  24. * The rendered list of items for a feed.
  25. *
  26. * @see aggregator_menu()
  27. */
  28. function aggregator_page_source($feed) {
  29. drupal_set_title($feed->title);
  30. $feed_source = theme('aggregator_feed_source', array('feed' => $feed));
  31. // It is safe to include the fid in the query because it's loaded from the
  32. // database by aggregator_feed_load.
  33. $items = aggregator_feed_items_load('source', $feed);
  34. return _aggregator_page_list($items, arg(3), $feed_source);
  35. }
  36. /**
  37. * Page callback: Displays a form with all items captured from a feed.
  38. *
  39. * @param $feed
  40. * The feed for which to list all of the aggregated items.
  41. *
  42. * @return
  43. * The rendered list of items for the feed.
  44. *
  45. * @see aggregator_page_source()
  46. */
  47. function aggregator_page_source_form($form, $form_state, $feed) {
  48. return aggregator_page_source($feed);
  49. }
  50. /**
  51. * Page callback: Displays all the items aggregated in a particular category.
  52. *
  53. * @param $category
  54. * The category for which to list all of the aggregated items.
  55. *
  56. * @return
  57. * The rendered list of items for the feed.
  58. */
  59. function aggregator_page_category($category) {
  60. drupal_add_feed('aggregator/rss/' . $category['cid'], variable_get('site_name', 'Drupal') . ' ' . t('aggregator - @title', array('@title' => $category['title'])));
  61. // It is safe to include the cid in the query because it's loaded from the
  62. // database by aggregator_category_load.
  63. $items = aggregator_feed_items_load('category', $category);
  64. return _aggregator_page_list($items, arg(3));
  65. }
  66. /**
  67. * Page callback: Displays a form containing items aggregated in a category.
  68. *
  69. * @param $category
  70. * The category for which to list all of the aggregated items.
  71. *
  72. * @return
  73. * The rendered list of items for the feed.
  74. *
  75. * @see aggregator_page_category()
  76. */
  77. function aggregator_page_category_form($form, $form_state, $category) {
  78. return aggregator_page_category($category);
  79. }
  80. /**
  81. * Loads and optionally filters feed items.
  82. *
  83. * @param $type
  84. * The type of filter for the items. Possible values are:
  85. * - sum: No filtering.
  86. * - source: Filter the feed items, limiting the result to items from a
  87. * single source.
  88. * - category: Filter the feed items by category.
  89. * @param $data
  90. * Feed or category data used for filtering. The type and value of $data
  91. * depends on $type:
  92. * - source: $data is an object with $data->fid identifying the feed used to
  93. * as filter.
  94. * - category: $data is an array with $data['cid'] being the category id to
  95. * filter on.
  96. * The $data parameter is not used when $type is 'sum'.
  97. *
  98. * @return
  99. * An array of the feed items.
  100. */
  101. function aggregator_feed_items_load($type, $data = NULL) {
  102. $items = array();
  103. switch ($type) {
  104. case 'sum':
  105. $query = db_select('aggregator_item', 'i');
  106. $query->join('aggregator_feed', 'f', 'i.fid = f.fid');
  107. $query->fields('i');
  108. $query->addField('f', 'title', 'ftitle');
  109. $query->addField('f', 'link', 'flink');
  110. break;
  111. case 'source':
  112. $query = db_select('aggregator_item', 'i');
  113. $query
  114. ->fields('i')
  115. ->condition('i.fid', $data->fid);
  116. break;
  117. case 'category':
  118. $query = db_select('aggregator_category_item', 'c');
  119. $query->leftJoin('aggregator_item', 'i', 'c.iid = i.iid');
  120. $query->leftJoin('aggregator_feed', 'f', 'i.fid = f.fid');
  121. $query
  122. ->fields('i')
  123. ->condition('cid', $data['cid']);
  124. $query->addField('f', 'title', 'ftitle');
  125. $query->addField('f', 'link', 'flink');
  126. break;
  127. }
  128. $result = $query
  129. ->extend('PagerDefault')
  130. ->limit(20)
  131. ->orderBy('i.timestamp', 'DESC')
  132. ->orderBy('i.iid', 'DESC')
  133. ->execute();
  134. foreach ($result as $item) {
  135. $item->categories = db_query('SELECT c.title, c.cid FROM {aggregator_category_item} ci LEFT JOIN {aggregator_category} c ON ci.cid = c.cid WHERE ci.iid = :iid ORDER BY c.title', array(':iid' => $item->iid))->fetchAll();
  136. $items[] = $item;
  137. }
  138. return $items;
  139. }
  140. /**
  141. * Prints an aggregator page listing a number of feed items.
  142. *
  143. * Various menu callbacks use this function to print their feeds.
  144. *
  145. * @param $items
  146. * The items to be listed.
  147. * @param $op
  148. * Which form should be added to the items. Only 'categorize' is now
  149. * recognized.
  150. * @param $feed_source
  151. * The feed source URL.
  152. *
  153. * @return
  154. * The rendered list of items for the feed.
  155. */
  156. function _aggregator_page_list($items, $op, $feed_source = '') {
  157. if (user_access('administer news feeds') && ($op == 'categorize')) {
  158. // Get form data.
  159. $output = aggregator_categorize_items($items, $feed_source);
  160. }
  161. else {
  162. // Assemble themed output.
  163. $output = $feed_source;
  164. foreach ($items as $item) {
  165. $output .= theme('aggregator_item', array('item' => $item));
  166. }
  167. $output = theme('aggregator_wrapper', array('content' => $output));
  168. }
  169. return $output;
  170. }
  171. /**
  172. * Form constructor to build the page list form.
  173. *
  174. * @param $items
  175. * An array of the feed items.
  176. * @param $feed_source
  177. * (optional) The feed source URL. Defaults to an empty string.
  178. *
  179. * @return array
  180. * An array of FAPI elements.
  181. *
  182. * @see aggregator_categorize_items_submit()
  183. * @see theme_aggregator_categorize_items()
  184. * @ingroup forms
  185. */
  186. function aggregator_categorize_items($items, $feed_source = '') {
  187. $form['#submit'][] = 'aggregator_categorize_items_submit';
  188. $form['#theme'] = 'aggregator_categorize_items';
  189. $form['feed_source'] = array(
  190. '#value' => $feed_source,
  191. );
  192. $categories = array();
  193. $done = FALSE;
  194. $form['items'] = array();
  195. $form['categories'] = array(
  196. '#tree' => TRUE,
  197. );
  198. foreach ($items as $item) {
  199. $form['items'][$item->iid] = array('#markup' => theme('aggregator_item', array('item' => $item)));
  200. $form['categories'][$item->iid] = array();
  201. $categories_result = db_query('SELECT c.cid, c.title, ci.iid FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid AND ci.iid = :iid', array(':iid' => $item->iid));
  202. $selected = array();
  203. foreach ($categories_result as $category) {
  204. if (!$done) {
  205. $categories[$category->cid] = check_plain($category->title);
  206. }
  207. if ($category->iid) {
  208. $selected[] = $category->cid;
  209. }
  210. }
  211. $done = TRUE;
  212. $form['categories'][$item->iid] = array(
  213. '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
  214. '#default_value' => $selected,
  215. '#options' => $categories,
  216. '#size' => 10,
  217. '#multiple' => TRUE
  218. );
  219. }
  220. $form['actions'] = array('#type' => 'actions');
  221. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
  222. return $form;
  223. }
  224. /**
  225. * Form submission handler for aggregator_categorize_items().
  226. */
  227. function aggregator_categorize_items_submit($form, &$form_state) {
  228. if (!empty($form_state['values']['categories'])) {
  229. foreach ($form_state['values']['categories'] as $iid => $selection) {
  230. db_delete('aggregator_category_item')
  231. ->condition('iid', $iid)
  232. ->execute();
  233. $insert = db_insert('aggregator_category_item')->fields(array('iid', 'cid'));
  234. $has_values = FALSE;
  235. foreach ($selection as $cid) {
  236. if ($cid && $iid) {
  237. $has_values = TRUE;
  238. $insert->values(array(
  239. 'iid' => $iid,
  240. 'cid' => $cid,
  241. ));
  242. }
  243. }
  244. if ($has_values) {
  245. $insert->execute();
  246. }
  247. }
  248. }
  249. drupal_set_message(t('The categories have been saved.'));
  250. }
  251. /**
  252. * Returns HTML for the aggregator page list form for assigning categories.
  253. *
  254. * @param $variables
  255. * An associative array containing:
  256. * - form: A render element representing the form.
  257. *
  258. * @ingroup themeable
  259. */
  260. function theme_aggregator_categorize_items($variables) {
  261. $form = $variables['form'];
  262. $output = drupal_render($form['feed_source']);
  263. $rows = array();
  264. if (!empty($form['items'])) {
  265. foreach (element_children($form['items']) as $key) {
  266. $rows[] = array(
  267. drupal_render($form['items'][$key]),
  268. array('data' => drupal_render($form['categories'][$key]), 'class' => array('categorize-item')),
  269. );
  270. }
  271. }
  272. $output .= theme('table', array('header' => array('', t('Categorize')), 'rows' => $rows));
  273. $output .= drupal_render($form['submit']);
  274. $output .= drupal_render_children($form);
  275. return theme('aggregator_wrapper', array('content' => $output));
  276. }
  277. /**
  278. * Processes variables for aggregator-wrapper.tpl.php.
  279. *
  280. * @see aggregator-wrapper.tpl.php
  281. */
  282. function template_preprocess_aggregator_wrapper(&$variables) {
  283. $variables['pager'] = theme('pager');
  284. }
  285. /**
  286. * Processes variables for aggregator-item.tpl.php.
  287. *
  288. * @see aggregator-item.tpl.php
  289. */
  290. function template_preprocess_aggregator_item(&$variables) {
  291. $item = $variables['item'];
  292. $variables['feed_url'] = check_url($item->link);
  293. $variables['feed_title'] = check_plain($item->title);
  294. $variables['content'] = aggregator_filter_xss($item->description);
  295. $variables['source_url'] = '';
  296. $variables['source_title'] = '';
  297. if (isset($item->ftitle) && isset($item->fid)) {
  298. $variables['source_url'] = url("aggregator/sources/$item->fid");
  299. $variables['source_title'] = check_plain($item->ftitle);
  300. }
  301. if (date('Ymd', $item->timestamp) == date('Ymd')) {
  302. $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->timestamp)));
  303. }
  304. else {
  305. $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
  306. }
  307. $variables['categories'] = array();
  308. foreach ($item->categories as $category) {
  309. $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/' . $category->cid);
  310. }
  311. }
  312. /**
  313. * Page callback: Displays all the feeds used by the aggregator.
  314. *
  315. * @return
  316. * An HTML-formatted string.
  317. *
  318. * @see aggregator_menu()
  319. */
  320. function aggregator_page_sources() {
  321. $result = db_query('SELECT f.fid, f.title, f.description, f.image, MAX(i.timestamp) AS last FROM {aggregator_feed} f LEFT JOIN {aggregator_item} i ON f.fid = i.fid GROUP BY f.fid, f.title, f.description, f.image ORDER BY last DESC, f.title');
  322. $output = '';
  323. foreach ($result as $feed) {
  324. // Most recent items:
  325. $summary_items = array();
  326. if (variable_get('aggregator_summary_items', 3)) {
  327. $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM {aggregator_item} i WHERE i.fid = :fid ORDER BY i.timestamp DESC', 0, variable_get('aggregator_summary_items', 3), array(':fid' => $feed->fid));
  328. foreach ($items as $item) {
  329. $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
  330. }
  331. }
  332. $feed->url = url('aggregator/sources/' . $feed->fid);
  333. $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $feed));
  334. }
  335. $output .= theme('feed_icon', array('url' => 'aggregator/opml', 'title' => t('OPML feed')));
  336. return theme('aggregator_wrapper', array('content' => $output));
  337. }
  338. /**
  339. * Page callback: Displays all the categories used by the Aggregator module.
  340. *
  341. * @return string
  342. * An HTML formatted string.
  343. *
  344. * @see aggregator_menu()
  345. */
  346. function aggregator_page_categories() {
  347. $result = db_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');
  348. $output = '';
  349. foreach ($result as $category) {
  350. if (variable_get('aggregator_summary_items', 3)) {
  351. $summary_items = array();
  352. $items = db_query_range('SELECT i.title, i.timestamp, i.link, f.title as feed_title, f.link as feed_link FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON i.iid = ci.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE ci.cid = :cid ORDER BY i.timestamp DESC', 0, variable_get('aggregator_summary_items', 3), array(':cid' => $category->cid));
  353. foreach ($items as $item) {
  354. $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
  355. }
  356. }
  357. $category->url = url('aggregator/categories/' . $category->cid);
  358. $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $category));
  359. }
  360. return theme('aggregator_wrapper', array('content' => $output));
  361. }
  362. /**
  363. * Page callback: Generates an RSS 0.92 feed of aggregator items or categories.
  364. *
  365. * @return string
  366. * An HTML formatted string.
  367. */
  368. function aggregator_page_rss() {
  369. $result = NULL;
  370. // arg(2) is the passed cid, only select for that category.
  371. if (arg(2)) {
  372. $category = db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = :cid', array(':cid' => arg(2)))->fetchObject();
  373. $result = db_query_range('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_category_item} c LEFT JOIN {aggregator_item} i ON c.iid = i.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE cid = :cid ORDER BY timestamp DESC, i.iid DESC', 0, variable_get('feed_default_items', 10), array(':cid' => $category->cid));
  374. }
  375. // Or, get the default aggregator items.
  376. else {
  377. $category = NULL;
  378. $result = db_query_range('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_item} i INNER JOIN {aggregator_feed} f ON i.fid = f.fid ORDER BY i.timestamp DESC, i.iid DESC', 0, variable_get('feed_default_items', 10));
  379. }
  380. $feeds = $result->fetchAll();
  381. return theme('aggregator_page_rss', array('feeds' => $feeds, 'category' => $category));
  382. }
  383. /**
  384. * Prints the RSS page for a feed.
  385. *
  386. * @param $variables
  387. * An associative array containing:
  388. * - feeds: An array of the feeds to theme.
  389. * - category: A common category, if any, for all the feeds.
  390. *
  391. * @return void
  392. *
  393. * @ingroup themeable
  394. */
  395. function theme_aggregator_page_rss($variables) {
  396. $feeds = $variables['feeds'];
  397. $category = $variables['category'];
  398. drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  399. $items = '';
  400. $feed_length = variable_get('feed_item_length', 'fulltext');
  401. foreach ($feeds as $feed) {
  402. switch ($feed_length) {
  403. case 'teaser':
  404. $summary = text_summary($feed->description, NULL, variable_get('aggregator_teaser_length', 600));
  405. if ($summary != $feed->description) {
  406. $summary .= '<p><a href="' . check_url($feed->link) . '">' . t('read more') . "</a></p>\n";
  407. }
  408. $feed->description = $summary;
  409. break;
  410. case 'title':
  411. $feed->description = '';
  412. break;
  413. }
  414. $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
  415. }
  416. $site_name = variable_get('site_name', 'Drupal');
  417. $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE));
  418. $description = isset($category) ? t('@site_name - aggregated feeds in category @title', array('@site_name' => $site_name, '@title' => $category->title)) : t('@site_name - aggregated feeds', array('@site_name' => $site_name));
  419. $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  420. $output .= "<rss version=\"2.0\">\n";
  421. $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
  422. $output .= "</rss>\n";
  423. print $output;
  424. }
  425. /**
  426. * Page callback: Generates an OPML representation of all feeds.
  427. *
  428. * @param $cid
  429. * (optional) If set, feeds are exported only from a category with this ID.
  430. * Otherwise, all feeds are exported. Defaults to NULL.
  431. *
  432. * @return
  433. * An OPML formatted string.
  434. */
  435. function aggregator_page_opml($cid = NULL) {
  436. if ($cid) {
  437. $result = db_query('SELECT f.title, f.url FROM {aggregator_feed} f LEFT JOIN {aggregator_category_feed} c on f.fid = c.fid WHERE c.cid = :cid ORDER BY title', array(':cid' => $cid));
  438. }
  439. else {
  440. $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
  441. }
  442. $feeds = $result->fetchAll();
  443. return theme('aggregator_page_opml', array('feeds' => $feeds));
  444. }
  445. /**
  446. * Prints the OPML page for the feed.
  447. *
  448. * @param $variables
  449. * An associative array containing:
  450. * - feeds: An array of the feeds to theme.
  451. *
  452. * @ingroup themeable
  453. */
  454. function theme_aggregator_page_opml($variables) {
  455. $feeds = $variables['feeds'];
  456. drupal_add_http_header('Content-Type', 'text/xml; charset=utf-8');
  457. $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  458. $output .= "<opml version=\"1.1\">\n";
  459. $output .= "<head>\n";
  460. $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) . "</title>\n";
  461. $output .= '<dateModified>' . gmdate(DATE_RFC2822, REQUEST_TIME) . "</dateModified>\n";
  462. $output .= "</head>\n";
  463. $output .= "<body>\n";
  464. foreach ($feeds as $feed) {
  465. $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="' . check_url($feed->url) . "\" />\n";
  466. }
  467. $output .= "</body>\n";
  468. $output .= "</opml>\n";
  469. print $output;
  470. }
  471. /**
  472. * Processes variables for aggregator-summary-items.tpl.php.
  473. *
  474. * @see aggregator-summary-items.tpl.php
  475. */
  476. function template_preprocess_aggregator_summary_items(&$variables) {
  477. $variables['title'] = check_plain($variables['source']->title);
  478. $variables['summary_list'] = theme('item_list', array('items' => $variables['summary_items']));
  479. $variables['source_url'] = $variables['source']->url;
  480. }
  481. /**
  482. * Processes variables for aggregator-summary-item.tpl.php.
  483. *
  484. * @see aggregator-summary-item.tpl.php
  485. */
  486. function template_preprocess_aggregator_summary_item(&$variables) {
  487. $item = $variables['item'];
  488. $variables['feed_url'] = check_url($item->link);
  489. $variables['feed_title'] = check_plain($item->title);
  490. $variables['feed_age'] = t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->timestamp)));
  491. $variables['source_url'] = '';
  492. $variables['source_title'] = '';
  493. if (!empty($item->feed_link)) {
  494. $variables['source_url'] = check_url($item->feed_link);
  495. $variables['source_title'] = check_plain($item->feed_title);
  496. }
  497. }
  498. /**
  499. * Processes variables for aggregator-feed-source.tpl.php.
  500. *
  501. * @see aggregator-feed-source.tpl.php
  502. */
  503. function template_preprocess_aggregator_feed_source(&$variables) {
  504. $feed = $variables['feed'];
  505. $variables['source_icon'] = theme('feed_icon', array('url' => $feed->url, 'title' => t('!title feed', array('!title' => $feed->title))));
  506. if (!empty($feed->image) && !empty($feed->title) && !empty($feed->link)) {
  507. $variables['source_image'] = l(theme('image', array('path' => $feed->image, 'alt' => $feed->title)), $feed->link, array('html' => TRUE, 'attributes' => array('class' => 'feed-image')));
  508. }
  509. else {
  510. $variables['source_image'] = '';
  511. }
  512. $variables['source_description'] = aggregator_filter_xss($feed->description);
  513. $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
  514. if ($feed->checked) {
  515. $variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked)));
  516. }
  517. else {
  518. $variables['last_checked'] = t('never');
  519. }
  520. if (user_access('administer news feeds')) {
  521. $variables['last_checked'] = l($variables['last_checked'], 'admin/config/services/aggregator');
  522. }
  523. }