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

Admin page callbacks for the aggregator module.

File

modules/aggregator/aggregator.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the aggregator module.
  5. */
  6. /**
  7. * Menu callback; displays the aggregator administration page.
  8. */
  9. function aggregator_admin_overview() {
  10. return aggregator_view();
  11. }
  12. /**
  13. * Displays the aggregator administration page.
  14. *
  15. * @return
  16. * The page HTML.
  17. */
  18. function aggregator_view() {
  19. $result = db_query('SELECT f.*, 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.etag, f.modified, f.image, f.block ORDER BY f.title');
  20. $output = '<h3>'. t('Feed overview') .'</h3>';
  21. $header = array(t('Title'), t('Items'), t('Last update'), t('Next update'), array('data' => t('Operations'), 'colspan' => '3'));
  22. $rows = array();
  23. while ($feed = db_fetch_object($result)) {
  24. $rows[] = array(
  25. l($feed->title, "aggregator/sources/$feed->fid"),
  26. format_plural($feed->items, '1 item', '@count items'),
  27. ($feed->checked ? t('@time ago', array('@time' => format_interval(time() - $feed->checked))) : t('never')),
  28. ($feed->checked ? t('%time left', array('%time' => format_interval($feed->checked + $feed->refresh - time()))) : t('never')),
  29. l(t('edit'), "admin/content/aggregator/edit/feed/$feed->fid"),
  30. l(t('remove items'), "admin/content/aggregator/remove/$feed->fid"),
  31. l(t('update items'), "admin/content/aggregator/update/$feed->fid", array('query' => array('token' => drupal_get_token("aggregator/update/$feed->fid")))),
  32. );
  33. }
  34. $output .= theme('table', $header, $rows);
  35. $result = db_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');
  36. $output .= '<h3>'. t('Category overview') .'</h3>';
  37. $header = array(t('Title'), t('Items'), t('Operations'));
  38. $rows = array();
  39. while ($category = db_fetch_object($result)) {
  40. $rows[] = array(l($category->title, "aggregator/categories/$category->cid"), format_plural($category->items, '1 item', '@count items'), l(t('edit'), "admin/content/aggregator/edit/category/$category->cid"));
  41. }
  42. $output .= theme('table', $header, $rows);
  43. return $output;
  44. }
  45. /**
  46. * Form builder; Generate a form to add/edit feed sources.
  47. *
  48. * @ingroup forms
  49. * @see aggregator_form_feed_validate()
  50. * @see aggregator_form_feed_submit()
  51. */
  52. function aggregator_form_feed(&$form_state, $edit = array('refresh' => 900, 'title' => '', 'url' => '', 'fid' => NULL)) {
  53. $period = drupal_map_assoc(array(900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
  54. if ($edit['refresh'] == '') {
  55. $edit['refresh'] = 3600;
  56. }
  57. $form['title'] = array('#type' => 'textfield',
  58. '#title' => t('Title'),
  59. '#default_value' => $edit['title'],
  60. '#maxlength' => 255,
  61. '#description' => t('The name of the feed (or the name of the website providing the feed).'),
  62. '#required' => TRUE,
  63. );
  64. $form['url'] = array('#type' => 'textfield',
  65. '#title' => t('URL'),
  66. '#default_value' => $edit['url'],
  67. '#maxlength' => 255,
  68. '#description' => t('The fully-qualified URL of the feed.'),
  69. '#required' => TRUE,
  70. );
  71. $form['refresh'] = array('#type' => 'select',
  72. '#title' => t('Update interval'),
  73. '#default_value' => $edit['refresh'],
  74. '#options' => $period,
  75. '#description' => t('The length of time between feed updates. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status'))),
  76. );
  77. // Handling of categories:
  78. $options = array();
  79. $values = array();
  80. $categories = db_query('SELECT c.cid, c.title, f.fid FROM {aggregator_category} c LEFT JOIN {aggregator_category_feed} f ON c.cid = f.cid AND f.fid = %d ORDER BY title', $edit['fid']);
  81. while ($category = db_fetch_object($categories)) {
  82. $options[$category->cid] = check_plain($category->title);
  83. if ($category->fid) $values[] = $category->cid;
  84. }
  85. if ($options) {
  86. $form['category'] = array('#type' => 'checkboxes',
  87. '#title' => t('Categorize news items'),
  88. '#default_value' => $values,
  89. '#options' => $options,
  90. '#description' => t('New feed items are automatically filed in the checked categories.'),
  91. );
  92. }
  93. $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  94. if ($edit['fid']) {
  95. $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
  96. $form['fid'] = array('#type' => 'hidden', '#value' => $edit['fid']);
  97. }
  98. return $form;
  99. }
  100. /**
  101. * Validate aggregator_form_feed form submissions.
  102. */
  103. function aggregator_form_feed_validate($form, &$form_state) {
  104. if ($form_state['values']['op'] == t('Save')) {
  105. // Ensure URL is valid.
  106. if (!valid_url($form_state['values']['url'], TRUE)) {
  107. form_set_error('url', t('The URL %url is invalid. Please enter a fully-qualified URL, such as http://www.example.com/feed.xml.', array('%url' => $form_state['values']['url'])));
  108. }
  109. // Check for duplicate titles.
  110. if (isset($form_state['values']['fid'])) {
  111. $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE (title = '%s' OR url = '%s') AND fid <> %d", $form_state['values']['title'], $form_state['values']['url'], $form_state['values']['fid']);
  112. }
  113. else {
  114. $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = '%s' OR url = '%s'", $form_state['values']['title'], $form_state['values']['url']);
  115. }
  116. while ($feed = db_fetch_object($result)) {
  117. if (strcasecmp($feed->title, $form_state['values']['title']) == 0) {
  118. form_set_error('title', t('A feed named %feed already exists. Please enter a unique title.', array('%feed' => $form_state['values']['title'])));
  119. }
  120. if (strcasecmp($feed->url, $form_state['values']['url']) == 0) {
  121. form_set_error('url', t('A feed with this URL %url already exists. Please enter a unique URL.', array('%url' => $form_state['values']['url'])));
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Process aggregator_form_feed form submissions.
  128. *
  129. * @todo Add delete confirmation dialog.
  130. */
  131. function aggregator_form_feed_submit($form, &$form_state) {
  132. if ($form_state['values']['op'] == t('Delete')) {
  133. $title = $form_state['values']['title'];
  134. // Unset the title:
  135. unset($form_state['values']['title']);
  136. }
  137. aggregator_save_feed($form_state['values']);
  138. if (isset($form_state['values']['fid'])) {
  139. if (isset($form_state['values']['title'])) {
  140. drupal_set_message(t('The feed %feed has been updated.', array('%feed' => $form_state['values']['title'])));
  141. if (arg(0) == 'admin') {
  142. $form_state['redirect'] = 'admin/content/aggregator/';
  143. return;
  144. }
  145. else {
  146. $form_state['redirect'] = 'aggregator/sources/'. $form_state['values']['fid'];
  147. return;
  148. }
  149. }
  150. else {
  151. watchdog('aggregator', 'Feed %feed deleted.', array('%feed' => $title));
  152. drupal_set_message(t('The feed %feed has been deleted.', array('%feed' => $title)));
  153. if (arg(0) == 'admin') {
  154. $form_state['redirect'] = 'admin/content/aggregator/';
  155. return;
  156. }
  157. else {
  158. $form_state['redirect'] = 'aggregator/sources/';
  159. return;
  160. }
  161. }
  162. }
  163. else {
  164. watchdog('aggregator', 'Feed %feed added.', array('%feed' => $form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'), 'admin/content/aggregator'));
  165. drupal_set_message(t('The feed %feed has been added.', array('%feed' => $form_state['values']['title'])));
  166. }
  167. }
  168. function aggregator_admin_remove_feed($form_state, $feed) {
  169. return confirm_form(
  170. array(
  171. 'feed' => array(
  172. '#type' => 'value',
  173. '#value' => $feed,
  174. ),
  175. ),
  176. t('Are you sure you want to remove all items from the feed %feed?', array('%feed' => $feed['title'])),
  177. 'admin/content/aggregator',
  178. t('This action cannot be undone.'),
  179. t('Remove items'),
  180. t('Cancel')
  181. );
  182. }
  183. /**
  184. * Remove all items from a feed and redirect to the overview page.
  185. *
  186. * @param $feed
  187. * An associative array describing the feed to be cleared.
  188. */
  189. function aggregator_admin_remove_feed_submit($form, &$form_state) {
  190. aggregator_remove($form_state['values']['feed']);
  191. $form_state['redirect'] = 'admin/content/aggregator';
  192. }
  193. /**
  194. * Menu callback; refreshes a feed, then redirects to the overview page.
  195. *
  196. * @param $feed
  197. * An associative array describing the feed to be refreshed.
  198. */
  199. function aggregator_admin_refresh_feed($feed) {
  200. if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'aggregator/update/' . $feed['fid'])) {
  201. return drupal_access_denied();
  202. }
  203. aggregator_refresh($feed);
  204. drupal_goto('admin/content/aggregator');
  205. }
  206. /**
  207. * Form builder; Configure the aggregator system.
  208. *
  209. * @ingroup forms
  210. * @see system_settings_form()
  211. */
  212. function aggregator_admin_settings() {
  213. $items = array(0 => t('none')) + drupal_map_assoc(array(3, 5, 10, 15, 20, 25), '_aggregator_items');
  214. $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
  215. $form['aggregator_allowed_html_tags'] = array(
  216. '#type' => 'textfield', '#title' => t('Allowed HTML tags'), '#size' => 80, '#maxlength' => 255,
  217. '#default_value' => variable_get('aggregator_allowed_html_tags', '<a> <b> <br> <dd> <dl> <dt> <em> <i> <li> <ol> <p> <strong> <u> <ul>'),
  218. '#description' => t('A space-separated list of HTML tags allowed in the content of feed items. (Tags in this list are not removed by Drupal.)')
  219. );
  220. $form['aggregator_summary_items'] = array(
  221. '#type' => 'select', '#title' => t('Items shown in sources and categories pages') ,
  222. '#default_value' => variable_get('aggregator_summary_items', 3), '#options' => $items,
  223. '#description' => t('Number of feed items displayed in feed and category summary pages.')
  224. );
  225. $form['aggregator_clear'] = array(
  226. '#type' => 'select', '#title' => t('Discard items older than'),
  227. '#default_value' => variable_get('aggregator_clear', 9676800), '#options' => $period,
  228. '#description' => t('The length of time to retain feed items before discarding. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status')))
  229. );
  230. $form['aggregator_category_selector'] = array(
  231. '#type' => 'radios', '#title' => t('Category selection type'), '#default_value' => variable_get('aggregator_category_selector', 'checkboxes'),
  232. '#options' => array('checkboxes' => t('checkboxes'), 'select' => t('multiple selector')),
  233. '#description' => t('The type of category selection widget displayed on categorization pages. (For a small number of categories, checkboxes are easier to use, while a multiple selector work well with large numbers of categories.)')
  234. );
  235. return system_settings_form($form);
  236. }
  237. /**
  238. * Form builder; Generate a form to add/edit/delete aggregator categories.
  239. *
  240. * @ingroup forms
  241. * @see aggregator_form_category_validate()
  242. * @see aggregator_form_category_submit()
  243. */
  244. function aggregator_form_category(&$form_state, $edit = array('title' => '', 'description' => '', 'cid' => NULL)) {
  245. $form['title'] = array('#type' => 'textfield',
  246. '#title' => t('Title'),
  247. '#default_value' => $edit['title'],
  248. '#maxlength' => 64,
  249. '#required' => TRUE,
  250. );
  251. $form['description'] = array('#type' => 'textarea',
  252. '#title' => t('Description'),
  253. '#default_value' => $edit['description'],
  254. );
  255. $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  256. if ($edit['cid']) {
  257. $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
  258. $form['cid'] = array('#type' => 'hidden', '#value' => $edit['cid']);
  259. }
  260. return $form;
  261. }
  262. /**
  263. * Validate aggregator_form_feed form submissions.
  264. */
  265. function aggregator_form_category_validate($form, &$form_state) {
  266. if ($form_state['values']['op'] == t('Save')) {
  267. // Check for duplicate titles
  268. if (isset($form_state['values']['cid'])) {
  269. $category = db_fetch_object(db_query("SELECT cid FROM {aggregator_category} WHERE title = '%s' AND cid <> %d", $form_state['values']['title'], $form_state['values']['cid']));
  270. }
  271. else {
  272. $category = db_fetch_object(db_query("SELECT cid FROM {aggregator_category} WHERE title = '%s'", $form_state['values']['title']));
  273. }
  274. if ($category) {
  275. form_set_error('title', t('A category named %category already exists. Please enter a unique title.', array('%category' => $form_state['values']['title'])));
  276. }
  277. }
  278. }
  279. /**
  280. * Process aggregator_form_category form submissions.
  281. *
  282. * @todo Add delete confirmation dialog.
  283. */
  284. function aggregator_form_category_submit($form, &$form_state) {
  285. if ($form_state['values']['op'] == t('Delete')) {
  286. $title = $form_state['values']['title'];
  287. // Unset the title:
  288. unset($form_state['values']['title']);
  289. }
  290. aggregator_save_category($form_state['values']);
  291. if (isset($form_state['values']['cid'])) {
  292. if (isset($form_state['values']['title'])) {
  293. drupal_set_message(t('The category %category has been updated.', array('%category' => $form_state['values']['title'])));
  294. if (arg(0) == 'admin') {
  295. $form_state['redirect'] = 'admin/content/aggregator/';
  296. return;
  297. }
  298. else {
  299. $form_state['redirect'] = 'aggregator/categories/'. $form_state['values']['cid'];
  300. return;
  301. }
  302. }
  303. else {
  304. watchdog('aggregator', 'Category %category deleted.', array('%category' => $title));
  305. drupal_set_message(t('The category %category has been deleted.', array('%category' => $title)));
  306. if (arg(0) == 'admin') {
  307. $form_state['redirect'] = 'admin/content/aggregator/';
  308. return;
  309. }
  310. else {
  311. $form_state['redirect'] = 'aggregator/categories/';
  312. return;
  313. }
  314. }
  315. }
  316. else {
  317. watchdog('aggregator', 'Category %category added.', array('%category' => $form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'), 'admin/content/aggregator'));
  318. drupal_set_message(t('The category %category has been added.', array('%category' => $form_state['values']['title'])));
  319. }
  320. }

Functions

Namesort descending Description
aggregator_admin_overview Menu callback; displays the aggregator administration page.
aggregator_admin_refresh_feed Menu callback; refreshes a feed, then redirects to the overview page.
aggregator_admin_remove_feed
aggregator_admin_remove_feed_submit Remove all items from a feed and redirect to the overview page.
aggregator_admin_settings Form builder; Configure the aggregator system.
aggregator_form_category Form builder; Generate a form to add/edit/delete aggregator categories.
aggregator_form_category_submit Process aggregator_form_category form submissions.
aggregator_form_category_validate Validate aggregator_form_feed form submissions.
aggregator_form_feed Form builder; Generate a form to add/edit feed sources.
aggregator_form_feed_submit Process aggregator_form_feed form submissions.
aggregator_form_feed_validate Validate aggregator_form_feed form submissions.
aggregator_view Displays the aggregator administration page.