content.menu.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if ($string != '') {
  25. global $user;
  26. $entity_info = entity_get_info($type);
  27. if ($type == 'node') {
  28. $entity_info['entity keys']['bundle field'] = 'type';
  29. }
  30. // We must query all ids, because if every one of the 10 don't have access
  31. // the user may never be able to autocomplete a node title.
  32. $preg_matches = array();
  33. $matches = array();
  34. $match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches);
  35. if (!$match) {
  36. $match = preg_match('/^id: (\d+)/', $string, $preg_matches);
  37. }
  38. // If an ID match was found, use that ID rather than the whole string.
  39. if ($match) {
  40. $entity_id = $preg_matches[1];
  41. $entity = entity_load($type, array($entity_id));
  42. // Format results in an array so later we could add attributes to the
  43. // autocomplete text that is returned.
  44. $results = array($entity_id => array(
  45. 'label' => $entity[$entity_id]->$entity_info['entity keys']['label'],
  46. ));
  47. }
  48. else {
  49. $results = _ctools_getReferencableEntities($type, $entity_info, $string, 'LIKE', 10);
  50. }
  51. foreach($results as $entity_id => $result) {
  52. if (!$entity_info['entity keys']['label']) {
  53. $matches["[id: $entity_id]"] = '<span class="autocomplete_title">' . $entity_id . '</span>';
  54. }
  55. else {
  56. $matches[$result['label'] . " [id: $entity_id]"] = '<span class="autocomplete_title">' . check_plain($result['label']) . '</span>';
  57. $matches[$result['label'] . " [id: $entity_id]"] .= isset($result['bundle field']) ? ' <span class="autocomplete_bundle">(' . check_plain($result['bundle field']) . ')</span>' : '';
  58. }
  59. }
  60. drupal_json_output($matches);
  61. }
  62. }
  63. /*
  64. * Use well known/tested entity reference code to build our search query
  65. * From EntityReference_SelectionHandler_Generic class
  66. */
  67. function _ctools_buildQuery($entity_type, $entity_info, $match = NULL, $match_operator = 'CONTAINS') {
  68. $base_table = $entity_info['base table'];
  69. $query = db_select($base_table)
  70. ->fields($base_table, array($entity_info['entity keys']['id']));
  71. if (isset($match)) {
  72. if (isset($entity_info['entity keys']['label'])) {
  73. $query->condition($base_table .'.'. $entity_info['entity keys']['label'], '%' . $match . '%' , $match_operator);
  74. }
  75. }
  76. // Add a label to the query, if the label exists
  77. if (isset($entity_info['entity keys']['label'])) {
  78. $query->fields($base_table, array($entity_info['entity keys']['label']));
  79. }
  80. // Add bundle field to the query, if it exists.
  81. if (isset($entity_info['entity keys']['bundle field'])) {
  82. $query->fields($base_table, array($entity_info['entity keys']['bundle field']));
  83. }
  84. // Add a generic entity access tag to the query.
  85. $query->addTag('ctools');
  86. if($entity_type == 'comment') {
  87. // Adding the 'comment_access' tag is sadly insufficient for comments: core
  88. // requires us to also know about the concept of 'published' and
  89. // 'unpublished'.
  90. if (!user_access('administer comments')) {
  91. $query->condition('comment.status', COMMENT_PUBLISHED);
  92. }
  93. // Join to a node if the user does not have node access bypass permissions
  94. // to obey node published permissions
  95. if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
  96. $node_alias = $query->innerJoin('node', 'n', '%alias.nid = comment.nid');
  97. $query->condition($node_alias . '.status', NODE_PUBLISHED);
  98. }
  99. $query->addTag('node_access');
  100. }
  101. else {
  102. $query->addTag($entity_type . '_access');
  103. }
  104. // Add the sort option.
  105. if(isset($entity_info['entity keys']['label'])) {
  106. $query->orderBy($base_table .'.'. $entity_info['entity keys']['label'], 'ASC');
  107. }
  108. return $query;
  109. }
  110. /**
  111. * Private function to get referencable entities. Based on code from the
  112. * Entity Reference module.
  113. */
  114. function _ctools_getReferencableEntities($entity_type, $entity_info, $match = NULL, $match_operator = 'LIKE', $limit = 0) {
  115. $options = array();
  116. $query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator);
  117. if ($limit > 0) {
  118. $query->range(0, $limit);
  119. }
  120. $results = $query->execute();
  121. if (!empty($results)) {
  122. foreach ($results as $record) {
  123. $options[$record->{$entity_info['entity keys']['id']}] = array(
  124. 'label' => isset($entity_info['entity keys']['label']) ? check_plain($record->{$entity_info['entity keys']['label']}) : $record->{$entity_info['entity keys']['id']},
  125. 'bundle field' => isset($entity_info['entity keys']['bundle field']) ? check_plain($record->{$entity_info['entity keys']['bundle field']}) : '',
  126. );
  127. }
  128. }
  129. return $options;
  130. }