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

User page callbacks for the aggregator module.

File

modules/aggregator/aggregator.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the aggregator module.
  5. */
  6. /**
  7. * Menu callback; displays the most recent items gathered from any feed.
  8. *
  9. * @return
  10. * The items HTML.
  11. */
  12. function aggregator_page_last() {
  13. drupal_add_feed(url('aggregator/rss'), variable_get('site_name', 'Drupal') .' '. t('aggregator'));
  14. $items = aggregator_feed_items_load('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');
  15. return _aggregator_page_list($items, arg(1));
  16. }
  17. /**
  18. * Menu callback; displays all the items captured from a particular feed.
  19. *
  20. * If there are two arguments then this function is the categorize form.
  21. *
  22. * @param $arg1
  23. * If there are two arguments then $arg1 is $form_state. Otherwise, $arg1 is $feed.
  24. * @param $arg2
  25. * If there are two arguments then $arg2 is feed.
  26. * @return
  27. * The items HTML.
  28. */
  29. function aggregator_page_source($arg1, $arg2 = NULL) {
  30. // If there are two arguments then this function is the categorize form, and
  31. // $arg1 is $form_state and $arg2 is $feed. Otherwise, $arg1 is $feed.
  32. $feed = is_array($arg2) ? $arg2 : $arg1;
  33. $feed = (object)$feed;
  34. drupal_set_title(check_plain($feed->title));
  35. $feed_source = theme('aggregator_feed_source', $feed);
  36. // It is safe to include the fid in the query because it's loaded from the
  37. // database by aggregator_feed_load.
  38. $items = aggregator_feed_items_load('SELECT * FROM {aggregator_item} WHERE fid = '. $feed->fid .' ORDER BY timestamp DESC, iid DESC');
  39. return _aggregator_page_list($items, arg(3), $feed_source);
  40. }
  41. /**
  42. * Menu callback; displays all the items aggregated in a particular category.
  43. *
  44. * If there are two arguments then this function is called as a form.
  45. *
  46. * @param $arg1
  47. * If there are two arguments then $arg1 is $form_state. Otherwise, $arg1 is $category.
  48. * @param $arg2
  49. * If there are two arguments then $arg2 is $category.
  50. * @return
  51. * The items HTML.
  52. */
  53. function aggregator_page_category($arg1, $arg2 = NULL) {
  54. // If there are two arguments then we are called as a form, $arg1 is
  55. // $form_state and $arg2 is $category. Otherwise, $arg1 is $category.
  56. $category = is_array($arg2) ? $arg2 : $arg1;
  57. drupal_add_feed(url('aggregator/rss/'. $category['cid']), variable_get('site_name', 'Drupal') .' '. t('aggregator - @title', array('@title' => $category['title'])));
  58. // It is safe to include the cid in the query because it's loaded from the
  59. // database by aggregator_category_load.
  60. $items = aggregator_feed_items_load('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 = '. $category['cid'] .' ORDER BY timestamp DESC, i.iid DESC');
  61. return _aggregator_page_list($items, arg(3));
  62. }
  63. /**
  64. * Load feed items by passing a SQL query.
  65. *
  66. * @param $sql
  67. * The query to be executed.
  68. * @return
  69. * An array of the feed items.
  70. */
  71. function aggregator_feed_items_load($sql) {
  72. $items = array();
  73. if (isset($sql)) {
  74. $result = pager_query($sql, 20);
  75. while ($item = db_fetch_object($result)) {
  76. $result_category = 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 = %d ORDER BY c.title', $item->iid);
  77. $item->categories = array();
  78. while ($item_categories = db_fetch_object($result_category)) {
  79. $item->categories[] = $item_categories;
  80. }
  81. $items[$item->iid] = $item;
  82. }
  83. }
  84. return $items;
  85. }
  86. /**
  87. * Prints an aggregator page listing a number of feed items.
  88. *
  89. * Various menu callbacks use this function to print their feeds.
  90. *
  91. * @param $items
  92. * The items to be listed.
  93. * @param $op
  94. * Which form should be added to the items. Only 'categorize' is now recognized.
  95. * @param $feed_source
  96. * The feed source URL.
  97. * @return
  98. * The items HTML.
  99. */
  100. function _aggregator_page_list($items, $op, $feed_source = '') {
  101. if (user_access('administer news feeds') && ($op == 'categorize')) {
  102. // Get form data.
  103. $output = aggregator_categorize_items($items, $feed_source);
  104. }
  105. else {
  106. // Assemble themed output.
  107. $output = $feed_source;
  108. foreach ($items as $item) {
  109. $output .= theme('aggregator_item', $item);
  110. }
  111. $output = theme('aggregator_wrapper', $output);
  112. }
  113. return $output;
  114. }
  115. /**
  116. * Form builder; build the page list form.
  117. *
  118. * @param $items
  119. * An array of the feed items.
  120. * @param $feed_source
  121. * The feed source URL.
  122. * @return
  123. * The form structure.
  124. * @ingroup forms
  125. * @see aggregator_categorize_items_validate()
  126. * @see aggregator_categorize_items_submit()
  127. */
  128. function aggregator_categorize_items($items, $feed_source = '') {
  129. $form['#submit'][] = 'aggregator_categorize_items_submit';
  130. $form['#validate'][] = 'aggregator_categorize_items_validate';
  131. $form['#theme'] = 'aggregator_categorize_items';
  132. $form['feed_source'] = array('#value' => $feed_source);
  133. $categories = array();
  134. $done = FALSE;
  135. $form['items'] = array();
  136. $form['categories'] = array('#tree' => TRUE);
  137. foreach ($items as $item) {
  138. $form['items'][$item->iid] = array('#value' => theme('aggregator_item', $item));
  139. $form['categories'][$item->iid] = array();
  140. $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 = %d', $item->iid);
  141. $selected = array();
  142. while ($category = db_fetch_object($categories_result)) {
  143. if (!$done) {
  144. $categories[$category->cid] = check_plain($category->title);
  145. }
  146. if ($category->iid) {
  147. $selected[] = $category->cid;
  148. }
  149. }
  150. $done = TRUE;
  151. $form['categories'][$item->iid] = array(
  152. '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
  153. '#default_value' => $selected,
  154. '#options' => $categories,
  155. '#size' => 10,
  156. '#multiple' => TRUE
  157. );
  158. }
  159. $form['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
  160. return $form;
  161. }
  162. /**
  163. * Validate aggregator_categorize_items form submissions.
  164. */
  165. function aggregator_categorize_items_validate($form, &$form_state) {
  166. if (!user_access('administer news feeds')) {
  167. form_error($form, t('You are not allowed to categorize this feed item.'));
  168. }
  169. }
  170. /**
  171. * Process aggregator_categorize_items form submissions.
  172. */
  173. function aggregator_categorize_items_submit($form, &$form_state) {
  174. if (!empty($form_state['values']['categories'])) {
  175. foreach ($form_state['values']['categories'] as $iid => $selection) {
  176. db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d', $iid);
  177. foreach ($selection as $cid) {
  178. if ($cid) {
  179. db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES (%d, %d)', $cid, $iid);
  180. }
  181. }
  182. }
  183. }
  184. drupal_set_message(t('The categories have been saved.'));
  185. }
  186. /**
  187. * Theme the page list form for assigning categories.
  188. *
  189. * @param $form
  190. * An associative array containing the structure of the form.
  191. * @return
  192. * The output HTML.
  193. * @ingroup themeable
  194. */
  195. function theme_aggregator_categorize_items($form) {
  196. $output = drupal_render($form['feed_source']);
  197. $rows = array();
  198. if ($form['items']) {
  199. foreach (element_children($form['items']) as $key) {
  200. if (is_array($form['items'][$key])) {
  201. $rows[] = array(
  202. drupal_render($form['items'][$key]),
  203. array('data' => drupal_render($form['categories'][$key]), 'class' => 'categorize-item'),
  204. );
  205. }
  206. }
  207. }
  208. $output .= theme('table', array('', t('Categorize')), $rows);
  209. $output .= drupal_render($form['submit']);
  210. $output .= drupal_render($form);
  211. return theme('aggregator_wrapper', $output);
  212. }
  213. /**
  214. * Process variables for aggregator-wrapper.tpl.php.
  215. *
  216. * @see aggregator-wrapper.tpl.php
  217. */
  218. function template_preprocess_aggregator_wrapper(&$variables) {
  219. $variables['pager'] = theme('pager', NULL, 20, 0);
  220. }
  221. /**
  222. * Process variables for aggregator-item.tpl.php.
  223. *
  224. * @see aggregator-item.tpl.php
  225. */
  226. function template_preprocess_aggregator_item(&$variables) {
  227. $item = $variables['item'];
  228. $variables['feed_url'] = check_url($item->link);
  229. $variables['feed_title'] = check_plain($item->title);
  230. $variables['content'] = aggregator_filter_xss($item->description);
  231. $variables['source_url'] = '';
  232. $variables['source_title'] = '';
  233. if (isset($item->ftitle) && isset($item->fid)) {
  234. $variables['source_url'] = url("aggregator/sources/$item->fid");
  235. $variables['source_title'] = check_plain($item->ftitle);
  236. }
  237. if (date('Ymd', $item->timestamp) == date('Ymd')) {
  238. $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(time() - $item->timestamp)));
  239. }
  240. else {
  241. $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
  242. }
  243. $variables['categories'] = array();
  244. foreach ($item->categories as $category) {
  245. $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/'. $category->cid);
  246. }
  247. }
  248. /**
  249. * Menu callback; displays all the feeds used by the aggregator.
  250. */
  251. function aggregator_page_sources() {
  252. $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');
  253. $output = '';
  254. while ($feed = db_fetch_object($result)) {
  255. // Most recent items:
  256. $summary_items = array();
  257. if (variable_get('aggregator_summary_items', 3)) {
  258. $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM {aggregator_item} i WHERE i.fid = %d ORDER BY i.timestamp DESC', $feed->fid, 0, variable_get('aggregator_summary_items', 3));
  259. while ($item = db_fetch_object($items)) {
  260. $summary_items[] = theme('aggregator_summary_item', $item);
  261. }
  262. }
  263. $feed->url = url('aggregator/sources/'. $feed->fid);
  264. $output .= theme('aggregator_summary_items', $summary_items, $feed);
  265. }
  266. $output .= theme('feed_icon', url('aggregator/opml'), t('OPML feed'));
  267. return theme('aggregator_wrapper', $output);
  268. }
  269. /**
  270. * Menu callback; displays all the categories used by the aggregator.
  271. */
  272. function aggregator_page_categories() {
  273. $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');
  274. $output = '';
  275. while ($category = db_fetch_object($result)) {
  276. if (variable_get('aggregator_summary_items', 3)) {
  277. $summary_items = array();
  278. $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 = %d ORDER BY i.timestamp DESC', $category->cid, 0, variable_get('aggregator_summary_items', 3));
  279. while ($item = db_fetch_object($items)) {
  280. $summary_items[] = theme('aggregator_summary_item', $item);
  281. }
  282. }
  283. $category->url = url('aggregator/categories/'. $category->cid);
  284. $output .= theme('aggregator_summary_items', $summary_items, $category);
  285. }
  286. return theme('aggregator_wrapper', $output);
  287. }
  288. /**
  289. * Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
  290. */
  291. function aggregator_page_rss() {
  292. $result = NULL;
  293. // arg(2) is the passed cid, only select for that category
  294. if (arg(2)) {
  295. $category = db_fetch_object(db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = %d', arg(2)));
  296. $sql = '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 = %d ORDER BY timestamp DESC, i.iid DESC';
  297. $result = db_query_range($sql, $category->cid, 0, variable_get('feed_default_items', 10));
  298. }
  299. // or, get the default aggregator items
  300. else {
  301. $category = NULL;
  302. $sql = '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';
  303. $result = db_query_range($sql, 0, variable_get('feed_default_items', 10));
  304. }
  305. $feeds = array();
  306. while ($item = db_fetch_object($result)) {
  307. $feeds[] = $item;
  308. }
  309. return theme('aggregator_page_rss', $feeds, $category);
  310. }
  311. /**
  312. * Theme the RSS output.
  313. *
  314. * @param $feeds
  315. * An array of the feeds to theme.
  316. * @param $category
  317. * A common category, if any, for all the feeds.
  318. * @ingroup themeable
  319. */
  320. function theme_aggregator_page_rss($feeds, $category = NULL) {
  321. drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
  322. $items = '';
  323. $feed_length = variable_get('feed_item_length', 'teaser');
  324. foreach ($feeds as $feed) {
  325. switch ($feed_length) {
  326. case 'teaser':
  327. $teaser = node_teaser($feed->description);
  328. if ($teaser != $feed->description) {
  329. $teaser .= '<p><a href="'. check_url($feed->link) .'">'. t('read more') ."</a></p>\n";
  330. }
  331. $feed->description = $teaser;
  332. break;
  333. case 'title':
  334. $feed->description = '';
  335. break;
  336. }
  337. $items .= format_rss_item($feed->ftitle .': '. $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
  338. }
  339. $site_name = variable_get('site_name', 'Drupal');
  340. $url = url((isset($category) ? 'aggregator/categories/'. $category->cid : 'aggregator'), array('absolute' => TRUE));
  341. $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));
  342. $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  343. $output .= "<rss version=\"2.0\">\n";
  344. $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
  345. $output .= "</rss>\n";
  346. print $output;
  347. }
  348. /**
  349. * Menu callback; generates an OPML representation of all feeds.
  350. *
  351. * @param $cid
  352. * If set, feeds are exported only from a category with this ID. Otherwise, all feeds are exported.
  353. * @return
  354. * The output XML.
  355. */
  356. function aggregator_page_opml($cid = NULL) {
  357. if ($cid) {
  358. $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 = %d ORDER BY title', $cid);
  359. }
  360. else {
  361. $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
  362. }
  363. $feeds = array();
  364. while ($item = db_fetch_object($result)) {
  365. $feeds[] = $item;
  366. }
  367. return theme('aggregator_page_opml', $feeds);
  368. }
  369. /**
  370. * Theme the OPML feed output.
  371. *
  372. * @param $feeds
  373. * An array of the feeds to theme.
  374. * @ingroup themeable
  375. */
  376. function theme_aggregator_page_opml($feeds) {
  377. drupal_set_header('Content-Type: text/xml; charset=utf-8');
  378. $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  379. $output .= "<opml version=\"1.1\">\n";
  380. $output .= "<head>\n";
  381. $output .= '<title>'. check_plain(variable_get('site_name', 'Drupal')) ."</title>\n";
  382. $output .= '<dateModified>'. gmdate('r') ."</dateModified>\n";
  383. $output .= "</head>\n";
  384. $output .= "<body>\n";
  385. foreach ($feeds as $feed) {
  386. $output .= '<outline text="'. check_plain($feed->title) .'" xmlUrl="'. check_url($feed->url) ."\" />\n";
  387. }
  388. $output .= "</body>\n";
  389. $output .= "</opml>\n";
  390. print $output;
  391. }
  392. /**
  393. * Process variables for aggregator-summary-items.tpl.php.
  394. *
  395. * @see aggregator-summary-item.tpl.php
  396. */
  397. function template_preprocess_aggregator_summary_items(&$variables) {
  398. $variables['title'] = check_plain($variables['source']->title);
  399. $variables['summary_list'] = theme('item_list', $variables['summary_items']);
  400. $variables['source_url'] = $variables['source']->url;
  401. }
  402. /**
  403. * Process variables for aggregator-summary-item.tpl.php.
  404. *
  405. * @see aggregator-summary-item.tpl.php
  406. */
  407. function template_preprocess_aggregator_summary_item(&$variables) {
  408. $item = $variables['item'];
  409. $variables['feed_url'] = check_url($item->link);
  410. $variables['feed_title'] = check_plain($item->title);
  411. $variables['feed_age'] = t('%age old', array('%age' => format_interval(time() - $item->timestamp)));
  412. $variables['source_url'] = '';
  413. $variables['source_title'] = '';
  414. if (!empty($item->feed_link)) {
  415. $variables['source_url'] = check_url($item->feed_link);
  416. $variables['source_title'] = check_plain($item->feed_title);
  417. }
  418. }
  419. /**
  420. * Process variables for aggregator-feed-source.tpl.php.
  421. *
  422. * @see aggregator-feed-source.tpl.php
  423. */
  424. function template_preprocess_aggregator_feed_source(&$variables) {
  425. $feed = $variables['feed'];
  426. $variables['source_icon'] = theme('feed_icon', $feed->url, t('!title feed', array('!title' => $feed->title)));
  427. $variables['source_image'] = $feed->image;
  428. $variables['source_description'] = aggregator_filter_xss($feed->description);
  429. $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
  430. if ($feed->checked) {
  431. $variables['last_checked'] = t('@time ago', array('@time' => format_interval(time() - $feed->checked)));
  432. }
  433. else {
  434. $variables['last_checked'] = t('never');
  435. }
  436. if (user_access('administer news feeds')) {
  437. $variables['last_checked'] = l($variables['last_checked'], 'admin/content/aggregator');
  438. }
  439. }

Functions

Namesort descending Description
aggregator_categorize_items Form builder; build the page list form.
aggregator_categorize_items_submit Process aggregator_categorize_items form submissions.
aggregator_categorize_items_validate Validate aggregator_categorize_items form submissions.
aggregator_feed_items_load Load feed items by passing a SQL query.
aggregator_page_categories Menu callback; displays all the categories used by the aggregator.
aggregator_page_category Menu callback; displays all the items aggregated in a particular category.
aggregator_page_last Menu callback; displays the most recent items gathered from any feed.
aggregator_page_opml Menu callback; generates an OPML representation of all feeds.
aggregator_page_rss Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
aggregator_page_source Menu callback; displays all the items captured from a particular feed.
aggregator_page_sources Menu callback; displays all the feeds used by the aggregator.
template_preprocess_aggregator_feed_source Process variables for aggregator-feed-source.tpl.php.
template_preprocess_aggregator_item Process variables for aggregator-item.tpl.php.
template_preprocess_aggregator_summary_item Process variables for aggregator-summary-item.tpl.php.
template_preprocess_aggregator_summary_items Process variables for aggregator-summary-items.tpl.php.
template_preprocess_aggregator_wrapper Process variables for aggregator-wrapper.tpl.php.
theme_aggregator_categorize_items Theme the page list form for assigning categories.
theme_aggregator_page_opml Theme the OPML feed output.
theme_aggregator_page_rss Theme the RSS output.
_aggregator_page_list Prints an aggregator page listing a number of feed items.