cobaltnodes.module 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. function cobaltnodes_init() {
  3. if (user_access('use cobalt')) {
  4. drupal_add_js(drupal_get_path('module', 'cobalt') . '/js/cobalt.nodes.js');
  5. // Add the current node nid as a javascript setting
  6. if (arg(0)=='node' && is_numeric(arg(1))) {
  7. drupal_add_js(array('cobalt' => array('nodes_current' => arg(1))), 'setting');
  8. }
  9. }
  10. }
  11. function cobaltnodes_menu() {
  12. $items = array();
  13. $items['cobalt/data/nodes_update/%'] = array(
  14. 'title' => 'Serialized nodes',
  15. 'page callback' => 'cobaltnodes_json',
  16. 'page arguments' => array('update', 3),
  17. 'access arguments' => array('use cobalt'),
  18. 'type' => MENU_CALLBACK,
  19. );
  20. $items['cobalt/data/nodes_single/%'] = array(
  21. 'title' => 'Serialized node',
  22. 'page callback' => 'cobaltnodes_json',
  23. 'page arguments' => array('single', 3),
  24. 'access arguments' => array('use cobalt'),
  25. 'type' => MENU_CALLBACK,
  26. );
  27. return $items;
  28. }
  29. function cobaltnodes_cron() {
  30. // Delete logged deletions older than two weeks
  31. db_query("DELETE FROM {cobalt_node_deletion} WHERE deleted<:time_ago", array(
  32. ':time_ago' => time() - 86400*14,
  33. ));
  34. }
  35. function cobaltnodes_node_delete($node) {
  36. db_query("INSERT INTO {cobalt_node_deletion}(nid, deleted) VALUES(:nid, :deleted)", array(
  37. ':nid' => $node->nid,
  38. ':deleted' => time()+60*1, // Add a minute for margin
  39. ));
  40. }
  41. function cobaltnodes_json($op, $value) {
  42. $result = array();
  43. // We're skipping the status check on purpose. As we check with the node_access
  44. // function later this allows administrators to access unpublished nodes.
  45. if ($op=='update') {
  46. $res = db_query("SELECT n.nid, n.title, n.type, n.uid, n.status
  47. FROM {node} n
  48. WHERE created > :since OR changed > :since
  49. ORDER BY created DESC
  50. LIMIT 100", array(
  51. ':since' => $value,
  52. ));
  53. }
  54. else if ($op=='single') {
  55. $res = db_query("SELECT nid, title, type, uid, status FROM {node}
  56. WHERE nid = :nid", array(
  57. ':nid' => $value,
  58. ));
  59. }
  60. $nodes = array();
  61. $access_types = array(
  62. 'r' => 'view',
  63. 'w' => 'update',
  64. 'd' => 'delete',
  65. );
  66. foreach ($res as $node) {
  67. $access = '';
  68. foreach ($access_types as $key => $permission) {
  69. if (node_access($permission, $node)) {
  70. $access .= $key;
  71. }
  72. }
  73. if (!empty($access)) {
  74. $nodes[] = array($node->nid, $node->title, $access);
  75. }
  76. }
  77. $result['nodes'] = $nodes;
  78. if ($op=='update') {
  79. $deleted = array();
  80. // Get deletions
  81. $res = db_query("SELECT nid FROM {cobalt_node_deletion} WHERE deleted>:since", array(
  82. ':since' => $value,
  83. ));
  84. foreach ($res as $node) {
  85. $deleted[] = $node->nid;
  86. }
  87. $result['deleted'] = $deleted;
  88. }
  89. print drupal_json_output($result);
  90. exit;
  91. }