content.menu.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @file
  4. * Contains menu item registration for the content tool.
  5. *
  6. * The menu items registered are AJAX callbacks for the things like
  7. * autocomplete and other tools needed by the content types.
  8. */
  9. function ctools_content_menu(&$items) {
  10. $base = array(
  11. 'access arguments' => array('access content'),
  12. 'type' => MENU_CALLBACK,
  13. 'file' => 'includes/content.menu.inc',
  14. );
  15. $items['ctools/autocomplete/%'] = array(
  16. 'page callback' => 'ctools_content_autocomplete_entity',
  17. 'page arguments' => array(2),
  18. ) + $base;
  19. }
  20. /**
  21. * Helper function for autocompletion of entity titles.
  22. */
  23. function ctools_content_autocomplete_entity($type, $string = '') {
  24. $entity = entity_get_info($type);
  25. if ($string != '') {
  26. // @todo verify the query logic here, it's untested.
  27. // Set up the query
  28. $query = db_select($entity['base table'], 'b');
  29. if ($entity['entity keys']['label']) {
  30. $query->fields('b', array($entity['entity keys']['id'], $entity['entity keys']['label']))->range(0, 10);
  31. }
  32. else {
  33. $query->fields('b', array($entity['entity keys']['id']))->range(0, 10);
  34. }
  35. $preg_matches = array();
  36. $match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches);
  37. if (!$match) {
  38. $match = preg_match('/^id: (\d+)/', $string, $preg_matches);
  39. }
  40. if ($match) {
  41. $query->condition('b.' . $entity['entity keys']['id'], $preg_matches[1]);
  42. }
  43. elseif ($entity['entity keys']['label']) {
  44. $query->condition('b.' . $entity['entity keys']['label'], '%' . db_like($string) . '%', 'LIKE');
  45. }
  46. $matches = array();
  47. if ($type == 'node') {
  48. if (!user_access('bypass node access')) {
  49. // If the user is able to view their own unpublished nodes, allow them
  50. // to see these in addition to published nodes.
  51. if (user_access('view own unpublished content')) {
  52. $query->condition(db_or()
  53. ->condition('b.status', NODE_PUBLISHED)
  54. ->condition('b.uid', $GLOBALS['user']->uid)
  55. );
  56. }
  57. else {
  58. // If not, restrict the query to published nodes.
  59. $query->condition('b.status', NODE_PUBLISHED);
  60. }
  61. }
  62. $query->addTag('node_access');
  63. $query->join('users', 'u', 'b.uid = u.uid');
  64. $query->addField('u', 'name', 'name');
  65. foreach ($query->execute() as $nodeish) {
  66. $name = empty($nodeish->name) ? variable_get('anonymous', t('Anonymous')) : check_plain($nodeish->name);
  67. $matches[$nodeish->title . " [id: $nodeish->nid]"] = '<span class="autocomplete_title">' . check_plain($nodeish->title) . '</span> <span class="autocomplete_user">(' . t('by @user', array('@user' => $name)) . ')</span>';
  68. }
  69. }
  70. else {
  71. foreach ($query->execute() as $item) {
  72. $id = $item->{$entity['entity keys']['id']};
  73. if ($entity['entity keys']['label']) {
  74. $matches[$item->{$entity['entity keys']['label']} . " [id: $id]"] = '<span class="autocomplete_title">' . check_plain($item->{$entity['entity keys']['label']}) . '</span>';
  75. }
  76. else {
  77. $matches["[id: $id]"] = '<span class="autocomplete_title">' . check_plain($item->{$entity['entity keys']['id']}) . '</span>';
  78. }
  79. }
  80. }
  81. drupal_json_output($matches);
  82. }
  83. }