Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / includes / content.menu.inc @ 7e72b748

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 EntityReference_SelectionHandler_Generic class to build our search query.
67
 */
68
function _ctools_buildQuery($entity_type, $entity_info, $match = NULL, $match_operator = 'CONTAINS') {
69
  $base_table = $entity_info['base table'];
70
  $label_key = $entity_info['entity keys']['label'];
71
  $query = db_select($base_table)
72
    ->fields($base_table, array($entity_info['entity keys']['id']));
73

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

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

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

94
    // 'unpublished'.

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

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

100
    // to obey node published permissions

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

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

    
116
  return $query;
117
}
118

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

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

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

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