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_formForm builder; Configure the aggregator system.
aggregator_admin_form_submit
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_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_form_opmlForm builder; Generate a form to import feeds from OPML.
aggregator_form_opml_submitProcess aggregator_form_opml form submissions.
aggregator_form_opml_validateValidate aggregator_form_opml form submissions.
aggregator_viewDisplays the aggregator administration page.
_aggregator_parse_opmlParse an OPML file.

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.fid, f.title, f.url, f.refresh, f.checked, f.link, f.description, f.hash, f.etag, f.modified, f.image, f.block, 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, 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. foreach ($result as $feed) {
  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(REQUEST_TIME - $feed->checked))) : t('never')),
  28. ($feed->checked && $feed->refresh ? t('%time left', array('%time' => format_interval($feed->checked + $feed->refresh - REQUEST_TIME))) : t('never')),
  29. l(t('edit'), "admin/config/services/aggregator/edit/feed/$feed->fid"),
  30. l(t('remove items'), "admin/config/services/aggregator/remove/$feed->fid"),
  31. l(t('update items'), "admin/config/services/aggregator/update/$feed->fid"),
  32. );
  33. }
  34. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('No feeds available. <a href="@link">Add feed</a>.', array('@link' => url('admin/config/services/aggregator/add/feed')))));
  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. foreach ($result as $category) {
  40. $rows[] = array(l($category->title, "aggregator/categories/$category->cid"), format_plural($category->items, '1 item', '@count items'), l(t('edit'), "admin/config/services/aggregator/edit/category/$category->cid"));
  41. }
  42. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('No categories available. <a href="@link">Add category</a>.', array('@link' => url('admin/config/services/aggregator/add/category')))));
  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, &$form_state, stdClass $feed = 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. $period[AGGREGATOR_CLEAR_NEVER] = t('Never');
  55. $form['title'] = array('#type' => 'textfield',
  56. '#title' => t('Title'),
  57. '#default_value' => isset($feed->title) ? $feed->title : '',
  58. '#maxlength' => 255,
  59. '#description' => t('The name of the feed (or the name of the website providing the feed).'),
  60. '#required' => TRUE,
  61. );
  62. $form['url'] = array('#type' => 'textfield',
  63. '#title' => t('URL'),
  64. '#default_value' => isset($feed->url) ? $feed->url : '',
  65. '#maxlength' => 255,
  66. '#description' => t('The fully-qualified URL of the feed.'),
  67. '#required' => TRUE,
  68. );
  69. $form['refresh'] = array('#type' => 'select',
  70. '#title' => t('Update interval'),
  71. '#default_value' => isset($feed->refresh) ? $feed->refresh : 3600,
  72. '#options' => $period,
  73. '#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'))),
  74. );
  75. $form['block'] = array('#type' => 'select',
  76. '#title' => t('News items in block'),
  77. '#default_value' => isset($feed->block) ? $feed->block : 5,
  78. '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
  79. '#description' => t("Drupal can make a block with the most recent news items of this feed. You can <a href=\"@block-admin\">configure blocks</a> to be displayed in the sidebar of your page. This setting lets you configure the number of news items to show in this feed's block. If you choose '0' this feed's block will be disabled.", array('@block-admin' => url('admin/structure/block'))),
  80. );
  81. // Handling of categories.
  82. $options = array();
  83. $values = array();
  84. $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 = :fid ORDER BY title', array(':fid' => isset($feed->fid) ? $feed->fid : NULL));
  85. foreach ($categories as $category) {
  86. $options[$category->cid] = check_plain($category->title);
  87. if ($category->fid) $values[] = $category->cid;
  88. }
  89. if ($options) {
  90. $form['category'] = array(
  91. '#type' => 'checkboxes',
  92. '#title' => t('Categorize news items'),
  93. '#default_value' => $values,
  94. '#options' => $options,
  95. '#description' => t('New feed items are automatically filed in the checked categories.'),
  96. );
  97. }
  98. $form['actions'] = array('#type' => 'actions');
  99. $form['actions']['submit'] = array(
  100. '#type' => 'submit',
  101. '#value' => t('Save'),
  102. );
  103. if (!empty($feed->fid)) {
  104. $form['actions']['delete'] = array(
  105. '#type' => 'submit',
  106. '#value' => t('Delete'),
  107. );
  108. $form['fid'] = array(
  109. '#type' => 'hidden',
  110. '#value' => $feed->fid,
  111. );
  112. }
  113. return $form;
  114. }
  115. /**
  116. * Validate aggregator_form_feed() form submissions.
  117. */
  118. function aggregator_form_feed_validate($form, &$form_state) {
  119. if ($form_state['values']['op'] == t('Save')) {
  120. // Ensure URL is valid.
  121. if (!valid_url($form_state['values']['url'], TRUE)) {
  122. form_set_error('url', t('The URL %url is invalid. Enter a fully-qualified URL, such as http://www.example.com/feed.xml.', array('%url' => $form_state['values']['url'])));
  123. }
  124. // Check for duplicate titles.
  125. if (isset($form_state['values']['fid'])) {
  126. $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE (title = :title OR url = :url) AND fid <> :fid", array(':title' => $form_state['values']['title'], ':url' => $form_state['values']['url'], ':fid' => $form_state['values']['fid']));
  127. }
  128. else {
  129. $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = :title OR url = :url", array(':title' => $form_state['values']['title'], ':url' => $form_state['values']['url']));
  130. }
  131. foreach ($result as $feed) {
  132. if (strcasecmp($feed->title, $form_state['values']['title']) == 0) {
  133. form_set_error('title', t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $form_state['values']['title'])));
  134. }
  135. if (strcasecmp($feed->url, $form_state['values']['url']) == 0) {
  136. form_set_error('url', t('A feed with this URL %url already exists. Enter a unique URL.', array('%url' => $form_state['values']['url'])));
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * Process aggregator_form_feed() form submissions.
  143. *
  144. * @todo Add delete confirmation dialog.
  145. */
  146. function aggregator_form_feed_submit($form, &$form_state) {
  147. if ($form_state['values']['op'] == t('Delete')) {
  148. $title = $form_state['values']['title'];
  149. // Unset the title.
  150. unset($form_state['values']['title']);
  151. }
  152. aggregator_save_feed($form_state['values']);
  153. if (isset($form_state['values']['fid'])) {
  154. if (isset($form_state['values']['title'])) {
  155. drupal_set_message(t('The feed %feed has been updated.', array('%feed' => $form_state['values']['title'])));
  156. if (arg(0) == 'admin') {
  157. $form_state['redirect'] = 'admin/config/services/aggregator/';
  158. return;
  159. }
  160. else {
  161. $form_state['redirect'] = 'aggregator/sources/' . $form_state['values']['fid'];
  162. return;
  163. }
  164. }
  165. else {
  166. watchdog('aggregator', 'Feed %feed deleted.', array('%feed' => $title));
  167. drupal_set_message(t('The feed %feed has been deleted.', array('%feed' => $title)));
  168. if (arg(0) == 'admin') {
  169. $form_state['redirect'] = 'admin/config/services/aggregator/';
  170. return;
  171. }
  172. else {
  173. $form_state['redirect'] = 'aggregator/sources/';
  174. return;
  175. }
  176. }
  177. }
  178. else {
  179. watchdog('aggregator', 'Feed %feed added.', array('%feed' => $form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'), 'admin/config/services/aggregator'));
  180. drupal_set_message(t('The feed %feed has been added.', array('%feed' => $form_state['values']['title'])));
  181. }
  182. }
  183. function aggregator_admin_remove_feed($form, $form_state, $feed) {
  184. return confirm_form(
  185. array(
  186. 'feed' => array(
  187. '#type' => 'value',
  188. '#value' => $feed,
  189. ),
  190. ),
  191. t('Are you sure you want to remove all items from the feed %feed?', array('%feed' => $feed->title)),
  192. 'admin/config/services/aggregator',
  193. t('This action cannot be undone.'),
  194. t('Remove items'),
  195. t('Cancel')
  196. );
  197. }
  198. /**
  199. * Remove all items from a feed and redirect to the overview page.
  200. *
  201. * @param $feed
  202. * An associative array describing the feed to be cleared.
  203. */
  204. function aggregator_admin_remove_feed_submit($form, &$form_state) {
  205. aggregator_remove($form_state['values']['feed']);
  206. $form_state['redirect'] = 'admin/config/services/aggregator';
  207. }
  208. /**
  209. * Form builder; Generate a form to import feeds from OPML.
  210. *
  211. * @ingroup forms
  212. * @see aggregator_form_opml_validate()
  213. * @see aggregator_form_opml_submit()
  214. */
  215. function aggregator_form_opml($form, &$form_state) {
  216. $period = drupal_map_assoc(array(900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
  217. $form['upload'] = array(
  218. '#type' => 'file',
  219. '#title' => t('OPML File'),
  220. '#description' => t('Upload an OPML file containing a list of feeds to be imported.'),
  221. );
  222. $form['remote'] = array(
  223. '#type' => 'textfield',
  224. '#title' => t('OPML Remote URL'),
  225. '#maxlength' => 1024,
  226. '#description' => t('Enter the URL of an OPML file. This file will be downloaded and processed only once on submission of the form.'),
  227. );
  228. $form['refresh'] = array(
  229. '#type' => 'select',
  230. '#title' => t('Update interval'),
  231. '#default_value' => 3600,
  232. '#options' => $period,
  233. '#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'))),
  234. );
  235. $form['block'] = array('#type' => 'select',
  236. '#title' => t('News items in block'),
  237. '#default_value' => 5,
  238. '#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
  239. '#description' => t("Drupal can make a block with the most recent news items of a feed. You can <a href=\"@block-admin\">configure blocks</a> to be displayed in the sidebar of your page. This setting lets you configure the number of news items to show in a feed's block. If you choose '0' these feeds' blocks will be disabled.", array('@block-admin' => url('admin/structure/block'))),
  240. );
  241. // Handling of categories.
  242. $options = array_map('check_plain', db_query("SELECT cid, title FROM {aggregator_category} ORDER BY title")->fetchAllKeyed());
  243. if ($options) {
  244. $form['category'] = array(
  245. '#type' => 'checkboxes',
  246. '#title' => t('Categorize news items'),
  247. '#options' => $options,
  248. '#description' => t('New feed items are automatically filed in the checked categories.'),
  249. );
  250. }
  251. $form['actions'] = array('#type' => 'actions');
  252. $form['actions']['submit'] = array(
  253. '#type' => 'submit',
  254. '#value' => t('Import')
  255. );
  256. return $form;
  257. }
  258. /**
  259. * Validate aggregator_form_opml form submissions.
  260. */
  261. function aggregator_form_opml_validate($form, &$form_state) {
  262. // If both fields are empty or filled, cancel.
  263. if (empty($form_state['values']['remote']) == empty($_FILES['files']['name']['upload'])) {
  264. form_set_error('remote', t('You must <em>either</em> upload a file or enter a URL.'));
  265. }
  266. // Validate the URL, if one was entered.
  267. if (!empty($form_state['values']['remote']) && !valid_url($form_state['values']['remote'], TRUE)) {
  268. form_set_error('remote', t('This URL is not valid.'));
  269. }
  270. }
  271. /**
  272. * Process aggregator_form_opml form submissions.
  273. */
  274. function aggregator_form_opml_submit($form, &$form_state) {
  275. $data = '';
  276. $validators = array('file_validate_extensions' => array('opml xml'));
  277. if ($file = file_save_upload('upload', $validators)) {
  278. $data = file_get_contents($file->uri);
  279. }
  280. else {
  281. $response = drupal_http_request($form_state['values']['remote']);
  282. if (!isset($response->error)) {
  283. $data = $response->data;
  284. }
  285. }
  286. $feeds = _aggregator_parse_opml($data);
  287. if (empty($feeds)) {
  288. drupal_set_message(t('No new feed has been added.'));
  289. return;
  290. }
  291. $form_state['values']['op'] = t('Save');
  292. foreach ($feeds as $feed) {
  293. // Ensure URL is valid.
  294. if (!valid_url($feed['url'], TRUE)) {
  295. drupal_set_message(t('The URL %url is invalid.', array('%url' => $feed['url'])), 'warning');
  296. continue;
  297. }
  298. // Check for duplicate titles or URLs.
  299. $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = :title OR url = :url", array(':title' => $feed['title'], ':url' => $feed['url']));
  300. foreach ($result as $old) {
  301. if (strcasecmp($old->title, $feed['title']) == 0) {
  302. drupal_set_message(t('A feed named %title already exists.', array('%title' => $old->title)), 'warning');
  303. continue 2;
  304. }
  305. if (strcasecmp($old->url, $feed['url']) == 0) {
  306. drupal_set_message(t('A feed with the URL %url already exists.', array('%url' => $old->url)), 'warning');
  307. continue 2;
  308. }
  309. }
  310. $form_state['values']['title'] = $feed['title'];
  311. $form_state['values']['url'] = $feed['url'];
  312. drupal_form_submit('aggregator_form_feed', $form_state);
  313. }
  314. $form_state['redirect'] = 'admin/config/services/aggregator';
  315. }
  316. /**
  317. * Parse an OPML file.
  318. *
  319. * Feeds are recognized as <outline> elements with the attributes "text" and
  320. * "xmlurl" set.
  321. *
  322. * @param $opml
  323. * The complete contents of an OPML document.
  324. *
  325. * @return
  326. * An array of feeds, each an associative array with a "title" and a "url"
  327. * element, or NULL if the OPML document failed to be parsed. An empty
  328. * array will be returned if the document is valid but contains no feeds, as
  329. * some OPML documents do.
  330. */
  331. function _aggregator_parse_opml($opml) {
  332. $feeds = array();
  333. $xml_parser = drupal_xml_parser_create($opml);
  334. if (xml_parse_into_struct($xml_parser, $opml, $values)) {
  335. foreach ($values as $entry) {
  336. if ($entry['tag'] == 'OUTLINE' && isset($entry['attributes'])) {
  337. $item = $entry['attributes'];
  338. if (!empty($item['XMLURL']) && !empty($item['TEXT'])) {
  339. $feeds[] = array('title' => $item['TEXT'], 'url' => $item['XMLURL']);
  340. }
  341. }
  342. }
  343. }
  344. xml_parser_free($xml_parser);
  345. return $feeds;
  346. }
  347. /**
  348. * Menu callback; refreshes a feed, then redirects to the overview page.
  349. *
  350. * @param $feed
  351. * An object describing the feed to be refreshed.
  352. */
  353. function aggregator_admin_refresh_feed($feed) {
  354. aggregator_refresh($feed);
  355. drupal_goto('admin/config/services/aggregator');
  356. }
  357. /**
  358. * Form builder; Configure the aggregator system.
  359. *
  360. * @ingroup forms
  361. */
  362. function aggregator_admin_form($form, $form_state) {
  363. // Global aggregator settings.
  364. $form['aggregator_allowed_html_tags'] = array(
  365. '#type' => 'textfield',
  366. '#title' => t('Allowed HTML tags'),
  367. '#size' => 80,
  368. '#maxlength' => 255,
  369. '#default_value' => variable_get('aggregator_allowed_html_tags', '<a> <b> <br> <dd> <dl> <dt> <em> <i> <li> <ol> <p> <strong> <u> <ul>'),
  370. '#description' => t('A space-separated list of HTML tags allowed in the content of feed items. Disallowed tags are stripped from the content.'),
  371. );
  372. // Make sure configuration is sane.
  373. aggregator_sanitize_configuration();
  374. // Get all available fetchers.
  375. $fetchers = module_implements('aggregator_fetch');
  376. foreach ($fetchers as $k => $module) {
  377. if ($info = module_invoke($module, 'aggregator_fetch_info')) {
  378. $label = $info['title'] . ' <span class="description">' . $info['description'] . '</span>';
  379. }
  380. else {
  381. $label = $module;
  382. }
  383. unset($fetchers[$k]);
  384. $fetchers[$module] = $label;
  385. }
  386. // Get all available parsers.
  387. $parsers = module_implements('aggregator_parse');
  388. foreach ($parsers as $k => $module) {
  389. if ($info = module_invoke($module, 'aggregator_parse_info')) {
  390. $label = $info['title'] . ' <span class="description">' . $info['description'] . '</span>';
  391. }
  392. else {
  393. $label = $module;
  394. }
  395. unset($parsers[$k]);
  396. $parsers[$module] = $label;
  397. }
  398. // Get all available processors.
  399. $processors = module_implements('aggregator_process');
  400. foreach ($processors as $k => $module) {
  401. if ($info = module_invoke($module, 'aggregator_process_info')) {
  402. $label = $info['title'] . ' <span class="description">' . $info['description'] . '</span>';
  403. }
  404. else {
  405. $label = $module;
  406. }
  407. unset($processors[$k]);
  408. $processors[$module] = $label;
  409. }
  410. // Only show basic configuration if there are actually options.
  411. $basic_conf = array();
  412. if (count($fetchers) > 1) {
  413. $basic_conf['aggregator_fetcher'] = array(
  414. '#type' => 'radios',
  415. '#title' => t('Fetcher'),
  416. '#description' => t('Fetchers download data from an external source. Choose a fetcher suitable for the external source you would like to download from.'),
  417. '#options' => $fetchers,
  418. '#default_value' => variable_get('aggregator_fetcher', 'aggregator'),
  419. );
  420. }
  421. if (count($parsers) > 1) {
  422. $basic_conf['aggregator_parser'] = array(
  423. '#type' => 'radios',
  424. '#title' => t('Parser'),
  425. '#description' => t('Parsers transform downloaded data into standard structures. Choose a parser suitable for the type of feeds you would like to aggregate.'),
  426. '#options' => $parsers,
  427. '#default_value' => variable_get('aggregator_parser', 'aggregator'),
  428. );
  429. }
  430. if (count($processors) > 1) {
  431. $basic_conf['aggregator_processors'] = array(
  432. '#type' => 'checkboxes',
  433. '#title' => t('Processors'),
  434. '#description' => t('Processors act on parsed feed data, for example they store feed items. Choose the processors suitable for your task.'),
  435. '#options' => $processors,
  436. '#default_value' => variable_get('aggregator_processors', array('aggregator')),
  437. );
  438. }
  439. if (count($basic_conf)) {
  440. $form['basic_conf'] = array(
  441. '#type' => 'fieldset',
  442. '#title' => t('Basic configuration'),
  443. '#description' => t('For most aggregation tasks, the default settings are fine.'),
  444. '#collapsible' => TRUE,
  445. '#collapsed' => FALSE,
  446. );
  447. $form['basic_conf'] += $basic_conf;
  448. }
  449. // Implementing modules will expect an array at $form['modules'].
  450. $form['modules'] = array();
  451. $form['actions'] = array('#type' => 'actions');
  452. $form['actions']['submit'] = array(
  453. '#type' => 'submit',
  454. '#value' => t('Save configuration'),
  455. );
  456. return $form;
  457. }
  458. function aggregator_admin_form_submit($form, &$form_state) {
  459. if (isset($form_state['values']['aggregator_processors'])) {
  460. $form_state['values']['aggregator_processors'] = array_filter($form_state['values']['aggregator_processors']);
  461. }
  462. system_settings_form_submit($form, $form_state);
  463. }
  464. /**
  465. * Form builder; Generate a form to add/edit/delete aggregator categories.
  466. *
  467. * @ingroup forms
  468. * @see aggregator_form_category_validate()
  469. * @see aggregator_form_category_submit()
  470. */
  471. function aggregator_form_category($form, &$form_state, $edit = array('title' => '', 'description' => '', 'cid' => NULL)) {
  472. $form['title'] = array('#type' => 'textfield',
  473. '#title' => t('Title'),
  474. '#default_value' => $edit['title'],
  475. '#maxlength' => 64,
  476. '#required' => TRUE,
  477. );
  478. $form['description'] = array('#type' => 'textarea',
  479. '#title' => t('Description'),
  480. '#default_value' => $edit['description'],
  481. );
  482. $form['actions'] = array('#type' => 'actions');
  483. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  484. if ($edit['cid']) {
  485. $form['actions']['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
  486. $form['cid'] = array('#type' => 'hidden', '#value' => $edit['cid']);
  487. }
  488. return $form;
  489. }
  490. /**
  491. * Validate aggregator_form_feed form submissions.
  492. */
  493. function aggregator_form_category_validate($form, &$form_state) {
  494. if ($form_state['values']['op'] == t('Save')) {
  495. // Check for duplicate titles
  496. if (isset($form_state['values']['cid'])) {
  497. $category = db_query("SELECT cid FROM {aggregator_category} WHERE title = :title AND cid <> :cid", array(':title' => $form_state['values']['title'], ':cid' => $form_state['values']['cid']))->fetchObject();
  498. }
  499. else {
  500. $category = db_query("SELECT cid FROM {aggregator_category} WHERE title = :title", array(':title' => $form_state['values']['title']))->fetchObject();
  501. }
  502. if ($category) {
  503. form_set_error('title', t('A category named %category already exists. Enter a unique title.', array('%category' => $form_state['values']['title'])));
  504. }
  505. }
  506. }
  507. /**
  508. * Process aggregator_form_category form submissions.
  509. *
  510. * @todo Add delete confirmation dialog.
  511. */
  512. function aggregator_form_category_submit($form, &$form_state) {
  513. if ($form_state['values']['op'] == t('Delete')) {
  514. $title = $form_state['values']['title'];
  515. // Unset the title.
  516. unset($form_state['values']['title']);
  517. }
  518. aggregator_save_category($form_state['values']);
  519. if (isset($form_state['values']['cid'])) {
  520. if (isset($form_state['values']['title'])) {
  521. drupal_set_message(t('The category %category has been updated.', array('%category' => $form_state['values']['title'])));
  522. if (arg(0) == 'admin') {
  523. $form_state['redirect'] = 'admin/config/services/aggregator/';
  524. return;
  525. }
  526. else {
  527. $form_state['redirect'] = 'aggregator/categories/' . $form_state['values']['cid'];
  528. return;
  529. }
  530. }
  531. else {
  532. watchdog('aggregator', 'Category %category deleted.', array('%category' => $title));
  533. drupal_set_message(t('The category %category has been deleted.', array('%category' => $title)));
  534. if (arg(0) == 'admin') {
  535. $form_state['redirect'] = 'admin/config/services/aggregator/';
  536. return;
  537. }
  538. else {
  539. $form_state['redirect'] = 'aggregator/categories/';
  540. return;
  541. }
  542. }
  543. }
  544. else {
  545. watchdog('aggregator', 'Category %category added.', array('%category' => $form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'), 'admin/config/services/aggregator'));
  546. drupal_set_message(t('The category %category has been added.', array('%category' => $form_state['values']['title'])));
  547. }
  548. }