Projet

Général

Profil

Paste
Télécharger (6,67 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / content.menu.inc @ 1e39edcb

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains menu item registration for the content tool.
6
 *
7
 * The menu items registered are AJAX callbacks for the things like
8
 * autocomplete and other tools needed by the content types.
9
 */
10

    
11
function ctools_content_menu(&$items) {
12
  $base = array(
13
    'access arguments' => array('access content'),
14
    'type' => MENU_CALLBACK,
15
    'file' => 'includes/content.menu.inc',
16
  );
17
  $items['ctools/autocomplete/%'] = array(
18
    'page callback' => 'ctools_content_autocomplete_entity',
19
    'page arguments' => array(2),
20
  ) + $base;
21
}
22

    
23
/**
24
 * Helper function for autocompletion of entity titles.
25
 */
26
function ctools_content_autocomplete_entity($entity_type, $string = '') {
27
  if ($string != '') {
28
    $entity_info = entity_get_info($entity_type);
29

    
30
    if (!module_exists('entity')) {
31
      module_load_include('inc', 'ctools', 'includes/entity-access');
32
      _ctools_entity_access($entity_info, $entity_type);
33
    }
34

    
35
    // We must query all ids, because if every one of the 10 don't have access
36
    // the user may never be able to autocomplete a node title.
37
    $preg_matches = array();
38
    $matches = array();
39
    $match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches);
40
    if (!$match) {
41
      $match = preg_match('/^id: (\d+)/', $string, $preg_matches);
42
    }
43
    // If an ID match was found, use that ID rather than the whole string.
44
    if ($match) {
45
      $entity_id = $preg_matches[1];
46
      $results = _ctools_getReferencableEntities($entity_type, $entity_info, $entity_id, '=', 1);
47
    }
48
    else {
49
      // We cannot find results if the entity doesn't have a label to search.
50
      if (!isset($entity_info['entity keys']['label'])) {
51
        drupal_json_output(array("[id: NULL]" => '<span class="autocomplete_title">' . t('Entity Type !entity_type does not support autocomplete search.', array('!entity_type' => $entity_type)) . '</span>'));
52
        return;
53
      }
54
      $results = _ctools_getReferencableEntities($entity_type, $entity_info, $string, 'LIKE', 10);
55
    }
56
    foreach ($results as $entity_id => $result) {
57
      $matches[$result['label'] . " [id: $entity_id]"] = '<span class="autocomplete_title">' . check_plain($result['label']) . '</span>';
58
      $matches[$result['label'] . " [id: $entity_id]"] .= isset($result['bundle']) ? ' <span class="autocomplete_bundle">(' . check_plain($result['bundle']) . ')</span>' : '';
59
    }
60

    
61
    drupal_json_output($matches);
62
  }
63
}
64

    
65
/*
66
 * Use well known/tested entity reference code to build our search query
67
 * From EntityReference_SelectionHandler_Generic class
68
 */
69
function _ctools_buildQuery($entity_type, $entity_info, $match = NULL, $match_operator = 'CONTAINS') {
70
  $base_table = $entity_info['base table'];
71
  $label_key = $entity_info['entity keys']['label'];
72
  $query = db_select($base_table)
73
    ->fields($base_table, array($entity_info['entity keys']['id']));
74

    
75
  if (isset($match)) {
76
    if (isset($label_key)) {
77
      $query->condition($base_table . '.' . $label_key, '%' . $match . '%', $match_operator);
78
    }
79
    // This should never happen, but double check just in case.
80
    else {
81
      return array();
82
    }
83
  }
84
  // Add a generic entity access tag to the query.
85
  $query->addTag('ctools');
86

    
87
  // We have to perform two checks. First check is a query alter (with tags)
88
  // in an attempt to only return results that have access. However, this is
89
  // not full-proof since entities many not implement hook_access query tag.
90
  // This is why we have a second check after entity load, before we display
91
  // the label of an entity.
92
  if ($entity_type == 'comment') {
93
    // Adding the 'comment_access' tag is sadly insufficient for comments: core

94
    // requires us to also know about the concept of 'published' and

95
    // 'unpublished'.

96
    if (!user_access('administer comments')) {
97
      $query->condition('comment.status', COMMENT_PUBLISHED);
98
    }
99

    
100
    // Join to a node if the user does not have node access bypass permissions

101
    // to obey node published permissions

102
    if (!user_access('bypass node access')) {
103
      $node_alias = $query->innerJoin('node', 'n', '%alias.nid = comment.nid');
104
      $query->condition($node_alias . '.status', NODE_PUBLISHED);
105
    }
106
    $query->addTag('node_access');
107
  }
108
  else {
109
    $query->addTag($entity_type . '_access');
110
  }
111

    
112
  // Add the sort option.
113
  if (isset($label_key)) {
114
    $query->orderBy($base_table . '.' . $label_key, 'ASC');
115
  }
116

    
117
  return $query;
118
}
119

    
120
/**
121
 * Private function to get referencable entities. Based on code from the
122
 * Entity Reference module.
123
 */
124
function _ctools_getReferencableEntities($entity_type, $entity_info, $match = NULL, $match_operator = 'LIKE', $limit = 0) {
125
  global $user;
126
  $account = $user;
127
  $options = array();
128
  // We're an entity ID, return the id
129
  if (is_numeric($match) && $match_operator == '=') {
130
    if ($entity = array_shift(entity_load($entity_type, array($match)))) {
131
      if (isset($entity_info['access callback']) && function_exists($entity_info['access callback'])) {
132
        if ($entity_info['access callback']('view', $entity, $account, $entity_type)) {
133
          $label = entity_label($entity_type, $entity);
134
          return array(
135
            $match => array(
136
              'label' => !empty($label) ? $label : $entity->{$entity_info['entity keys']['id']},
137
              'bundle' => !empty($entity_info['entity keys']['bundle']) ? check_plain($entity->{$entity_info['entity keys']['bundle']}) : NULL,
138
            ),
139
          );
140
        }
141
      }
142
    }
143
    // If you don't have access, or an access callback or a valid entity, just
144
    // Return back the Entity ID.
145
    return array(
146
      $match =>  array(
147
        'label' => $match,
148
        'bundle' => NULL,
149
        ),
150
    );
151
  }
152

    
153
  // We have matches, build a query to fetch the result.
154
  if ($query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator)) {
155
    if ($limit > 0) {
156
      $query->range(0, $limit);
157
    }
158

    
159
    $results = $query->execute();
160

    
161
    if (!empty($results)) {
162
      foreach ($results as $record) {
163
        $entities = entity_load($entity_type, array($record->{$entity_info['entity keys']['id']}));
164
        $entity = array_shift($entities);
165
        if (isset($entity_info['access callback']) && function_exists($entity_info['access callback'])) {
166
          if ($entity_info['access callback']('view', $entity, $account, $entity_type)) {
167
            $label = entity_label($entity_type, $entity);
168
            $options[$record->{$entity_info['entity keys']['id']}] = array(
169
              'label' => !empty($label) ? $label : $entity->{$entity_info['entity keys']['id']},
170
              'bundle' => !empty($entity_info['entity keys']['bundle']) ? check_plain($entity->{$entity_info['entity keys']['bundle']}) : NULL,
171
            );
172
          }
173
        }
174
      }
175
    }
176
    return $options;
177
  }
178
  return array();
179
}