aggregator.admin.inc 24 KB

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