feed_path_publisher.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function feed_path_publisher_menu() {
  6. $items = array();
  7. $items['admin/config/content/feed_path_publisher'] = array(
  8. 'title' => 'Feed Path Publisher',
  9. 'description' => 'Settings for feeds across the site.',
  10. 'page callback' => 'feed_path_publisher_list',
  11. 'access arguments' => array('administer site configuration'),
  12. 'type' => MENU_NORMAL_ITEM,
  13. );
  14. $items['admin/config/content/feed_path_publisher/list'] = array(
  15. 'title' => 'List',
  16. 'type' => MENU_DEFAULT_LOCAL_TASK,
  17. 'weight' => -1,
  18. 'access arguments' => array('administer site configuration'),
  19. );
  20. $items['admin/config/content/feed_path_publisher/add'] = array(
  21. 'title' => 'Add Feed',
  22. 'type' => MENU_LOCAL_TASK,
  23. 'page callback' => 'feed_path_publisher_add',
  24. 'access arguments' => array('administer site configuration'),
  25. );
  26. $items['admin/config/content/feed_path_publisher/%/edit'] = array(
  27. 'page callback' => 'feed_path_publisher_edit',
  28. 'page arguments' => array(4),
  29. 'access arguments' => array('administer site configuration'),
  30. );
  31. return $items;
  32. }
  33. /**
  34. * Implements hook_init().
  35. */
  36. function feed_path_publisher_init() {
  37. _feed_path_publisher_add_feeds($_GET['q']);
  38. }
  39. function _feed_path_publisher_add_feeds($internal_path) {
  40. global $user;
  41. $aliased_path = $internal_path;
  42. $front_page_path = $internal_path;
  43. // Add feeds based on aliased paths
  44. if (module_exists('path')) {
  45. $aliased_path = drupal_get_path_alias($internal_path);
  46. }
  47. if (drupal_is_front_page()) {
  48. $front_page_path = '<front>';
  49. }
  50. $res = db_query('SELECT title, feed, fppid
  51. FROM {feed_path_publisher}
  52. WHERE path_prefix = LEFT(:internal_path, LENGTH(path_prefix))
  53. OR path_prefix = LEFT(:aliased_path, LENGTH(path_prefix))
  54. OR path_prefix = LEFT(:front_page_path, LENGTH(path_prefix))
  55. ORDER BY weight, path_prefix',
  56. array(
  57. ':internal_path' => $internal_path,
  58. ':aliased_path' => $aliased_path,
  59. ':front_page_path' => $front_page_path,
  60. ));
  61. foreach ($res as $row) {
  62. $path_rids = db_select('feed_path_publisher_roles', 'fppr')
  63. ->fields('fppr', array('rids'))
  64. ->condition('fppid', $row->fppid)
  65. ->execute()
  66. ->fetchField();
  67. $path_rids = explode(',', $path_rids);
  68. if ($path_rids) {
  69. $show_hide = db_select('feed_path_publisher_roles', 'fppr')
  70. ->fields('fppr', array('show_hide'))
  71. ->condition('fppid', $row->fppid)
  72. ->execute()
  73. ->fetchField();
  74. $role_found = false;
  75. foreach ($user->roles as $key => $value) {
  76. if (in_array($key, $path_rids)) {
  77. $role_found = true;
  78. }
  79. }
  80. if ((!$role_found && $show_hide == 'show') || ($role_found && $show_hide == 'hide')) {
  81. continue;
  82. }
  83. }
  84. drupal_add_feed($row->feed, $row->title);
  85. }
  86. }
  87. /**
  88. * Menu callback for admin/config/content/feed_path_publisher/list
  89. *
  90. * Displays the list of feeds
  91. */
  92. function feed_path_publisher_list() {
  93. drupal_set_title('Feed path publisher');
  94. $result = db_select('feed_path_publisher', 'fpp')
  95. ->fields('fpp')
  96. ->orderBy('weight')
  97. ->orderBy('path_prefix')
  98. ->orderBy('title')
  99. ->execute();
  100. $cols = array(
  101. 'Title',
  102. 'Path Prefix',
  103. 'Feed',
  104. 'Weight',
  105. 'Operations',
  106. );
  107. $rows = array();
  108. while($row = $result->fetchObject()) {
  109. $ops = array();
  110. $ops[] = l('edit', 'admin/config/content/feed_path_publisher/' . $row->fppid . '/edit');
  111. $path_prefix = $row->path_prefix;
  112. $rows[] = array(
  113. check_plain($row->title),
  114. ($row->path_prefix == '' ? '<em>Global</em>' : check_plain($path_prefix)),
  115. check_plain($row->feed),
  116. $row->weight,
  117. implode(' ', $ops),
  118. );
  119. }
  120. if (empty($rows)) {
  121. $rows[] = array(
  122. array(
  123. 'data' => t('Use the Add Feed tab to add new feeds.'),
  124. 'colspan' => count($cols)
  125. ),
  126. );
  127. }
  128. $content = theme('table', array('header' => $cols, 'rows' => $rows));
  129. return $content;
  130. }
  131. /**
  132. * Page callback for admin/config/content/feed_path_publisher/add
  133. *
  134. * Displays feed add form.
  135. */
  136. function feed_path_publisher_add() {
  137. return drupal_get_form('feed_path_publisher_edit_form');
  138. }
  139. /**
  140. * Page callback for admin/config/content/feed_path_publisher/%/edit
  141. *
  142. * Edit form to modify a feed
  143. */
  144. function feed_path_publisher_edit($fppid) {
  145. return drupal_get_form('feed_path_publisher_edit_form', $fppid);
  146. }
  147. /**
  148. * Form to edit feed.
  149. */
  150. function feed_path_publisher_edit_form($form, $unknown = NULL, $fppid = NULL) {
  151. $form = array();
  152. $use_roles = false;
  153. $show_hide = 0;
  154. $checked_roles = array();
  155. $item = (object) NULL;
  156. if (isset($fppid)) {
  157. $item = db_select('feed_path_publisher', 'fpp')
  158. ->fields('fpp')
  159. ->condition('fppid', $fppid)
  160. ->execute()
  161. ->fetchObject();
  162. $item_count = db_select('feed_path_publisher_roles', 'fppr')
  163. ->fields('fppr', array('fppid'))
  164. ->condition('fppid', $fppid)
  165. ->execute()
  166. ->rowCount();
  167. if ($item_count > 0) {
  168. $use_roles = true;
  169. $showhide = db_select('feed_path_publisher_roles', 'fppr')
  170. ->fields('fppr', array('show_hide'))
  171. ->condition('fppid', $fppid)
  172. ->execute()
  173. ->fetchField();
  174. if ($showhide == 'show' ) {
  175. $show_hide = 0;
  176. }
  177. else {
  178. $show_hide = 1;
  179. }
  180. $rids = db_select('feed_path_publisher_roles', 'fppr')
  181. ->fields('fppr', array('rids'))
  182. ->condition('fppid', $fppid)
  183. ->execute()
  184. ->fetchField();
  185. $checked_roles = explode(',', $rids);
  186. }
  187. $form['fppid'] = array(
  188. '#type' => 'hidden',
  189. '#value' => isset($item->fppid) ? $item->fppid : 0,
  190. );
  191. }
  192. $form['title'] = array(
  193. '#type' => 'textfield',
  194. '#title' => t('Title'),
  195. '#default_value' => isset($item->title) ? $item->title : '',
  196. '#required' => TRUE,
  197. );
  198. $form['path_prefix'] = array(
  199. '#type' => 'textfield',
  200. '#title' => t('Path prefix'),
  201. '#default_value' => isset($item->path_prefix) ? $item->path_prefix : '',
  202. '#description' => t('Leaving this blank publishes to the entire site. You can use <front> to have your feed link added only to the home page. Keep in mind that rss.xml will always be added to the homepage if using the default path (node) for the front page.'),
  203. );
  204. $form['feed'] = array(
  205. '#type' => 'textfield',
  206. '#title' => t('Feed'),
  207. '#default_value' => isset($item->feed) ? $item->feed : '',
  208. '#required' => TRUE,
  209. '#description' => t('Relative paths should not begin with a slash.'),
  210. );
  211. $form['weight'] = array(
  212. '#type' => 'weight',
  213. '#title' => t('Weight'),
  214. '#default_value' => isset($item->weight) ? $item->weight : '',
  215. '#description' => t('Optional. In the feed listings, the heavier items will sink and the lighter items will be positioned nearer the top.'),
  216. );
  217. $form['roles'] = array(
  218. '#type' => 'fieldset',
  219. '#title' => t('Role-based feed publishing'),
  220. '#collapsible' => TRUE,
  221. '#collapsed' => ($use_roles ? FALSE : TRUE),
  222. );
  223. $form['roles']['enable_roles'] = array(
  224. '#type' => 'checkbox',
  225. '#title' => t('Enable role-based feed publishing according to role'),
  226. '#default_value' => $use_roles,
  227. '#collapsed' => TRUE,
  228. );
  229. $form['roles']['show_hide'] = array(
  230. '#type' => 'radios',
  231. '#title' => t('Behavior'),
  232. '#default_value' => $show_hide,
  233. '#options' => array(t('Show this feed for the following roles'), t('Hide this feed for the following roles')),
  234. );
  235. $roles = db_select('role', 'role')->fields('role')->execute();
  236. while ($role = $roles->fetchObject()) {
  237. $options[$role->rid] = $role->name;
  238. }
  239. $form['roles']['roles_list'] = array(
  240. '#type' => 'checkboxes',
  241. '#title' => t('Roles'),
  242. '#default_value' => $checked_roles,
  243. '#options' => $options,
  244. );
  245. $form[] = array(
  246. '#type' => 'submit',
  247. '#value' => t('Save'),
  248. );
  249. if (isset($fppid)) {
  250. $form[] = array(
  251. '#type' => 'submit',
  252. '#value' => t('Delete'),
  253. );
  254. }
  255. return $form;
  256. }
  257. /**
  258. * Submit handler for feed_path_publisher_edit_form
  259. */
  260. function feed_path_publisher_edit_form_submit($form, &$form_state) {
  261. $rids = array();
  262. foreach ($form_state['values']['roles_list'] as $role => $value) {
  263. if ($value) {
  264. $rids[] = $role;
  265. }
  266. }
  267. $show_hide = '';
  268. if ($form_state['values']['show_hide'] == 1) {
  269. $show_hide = 'hide';
  270. }
  271. else {
  272. $show_hide = 'show';
  273. }
  274. if (isset($form_state['values']['fppid'])) {
  275. if ($form_state['values']['op'] == t('Delete')) {
  276. db_delete('feed_path_publisher')
  277. ->condition('fppid', $form_state['values']['fppid'])
  278. ->execute();
  279. db_delete('feed_path_publisher_roles')
  280. ->condition('fppid', $form_state['values']['fppid'])
  281. ->execute();
  282. drupal_set_message(t('Feed path deleted.'));
  283. }
  284. else {
  285. db_update('feed_path_publisher')
  286. ->fields(array(
  287. 'title' => $form_state['values']['title'],
  288. 'path_prefix' => $form_state['values']['path_prefix'],
  289. 'feed' => $form_state['values']['feed'],
  290. 'weight' => $form_state['values']['weight'],
  291. ))
  292. ->condition('fppid', $form_state['values']['fppid'])
  293. ->execute();
  294. if ($form_state['values']['enable_roles'] == 1) {
  295. $rids = implode(',', $rids);
  296. $id = db_select('feed_path_publisher_roles', 'fppr')
  297. ->fields('fppr', array('fpprid'))
  298. ->condition('fppid', $form_state['values']['fppid'])
  299. ->execute()
  300. ->fetchField();
  301. if ($id) {
  302. db_update('feed_path_publisher_roles')
  303. ->fields(array(
  304. 'show_hide' => $show_hide,
  305. 'rids' => $rids,
  306. ))
  307. ->condition('fpprid', $id)
  308. ->execute();
  309. }
  310. else {
  311. $id = db_insert('feed_path_publisher_roles')
  312. ->fields(array(
  313. 'fppid' => $form_state['values']['fppid'],
  314. 'show_hide' => $show_hide,
  315. 'rids' => $rids,
  316. ))
  317. ->execute();
  318. }
  319. }
  320. else {
  321. db_delete('feed_path_publisher_roles')
  322. ->condition('fppid', $form_state['values']['fppid'])
  323. ->execute();
  324. }
  325. drupal_set_message(t('Feed path updated.'));
  326. }
  327. }
  328. else {
  329. $id = db_insert('feed_path_publisher')
  330. ->fields(array(
  331. 'title' => $form_state['values']['title'],
  332. 'path_prefix' => $form_state['values']['path_prefix'],
  333. 'feed' => $form_state['values']['feed'],
  334. 'weight' => $form_state['values']['weight'],
  335. ))
  336. ->execute();
  337. if ($form_state['values']['enable_roles'] == 1) {
  338. $fppid = db_query("select last_insert_id()")->fetchField();
  339. if ($rids) {
  340. $rids = implode(',', $rids);
  341. $id = db_insert('feed_path_publisher_roles')
  342. ->fields(array(
  343. 'fppid' => $fppid,
  344. 'show_hide' => $show_hide,
  345. 'rids' => $rids,
  346. ))
  347. ->execute();
  348. }
  349. }
  350. drupal_set_message(t('Feed path created.'));
  351. }
  352. $form_state['redirect'] = 'admin/config/content/feed_path_publisher';
  353. }