Skip navigation
Help

aggregator.admin.inc

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

Admin page callbacks for the aggregator module.

Functions & methods

NameDescription
aggregator_admin_overviewMenu callback; displays the aggregator administration page.
aggregator_admin_refresh_feedMenu callback; refreshes a feed, then redirects to the overview page.
aggregator_admin_remove_feed
aggregator_admin_remove_feed_submitRemove all items from a feed and redirect to the overview page.
aggregator_admin_settingsForm builder; Configure the aggregator system.
aggregator_form_categoryForm builder; Generate a form to add/edit/delete aggregator categories.
aggregator_form_category_submitProcess aggregator_form_category form submissions.
aggregator_form_category_validateValidate aggregator_form_feed form submissions.
aggregator_form_feedForm builder; Generate a form to add/edit feed sources.
aggregator_form_feed_submitProcess aggregator_form_feed form submissions.
aggregator_form_feed_validateValidate aggregator_form_feed form submissions.
aggregator_viewDisplays the aggregator administration page.

File

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