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 85ad3d82 Assos Assos
<?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 c22e192e Assos Assos
function ctools_content_autocomplete_entity($entity_type, $string = '') {
27 85ad3d82 Assos Assos
  if ($string != '') {
28 c22e192e Assos Assos
    $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 e4c061ad Assos Assos
    }
34 85ad3d82 Assos Assos
35 3753f249 Assos Assos
    // 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 85ad3d82 Assos Assos
    $preg_matches = array();
38 3753f249 Assos Assos
    $matches = array();
39 85ad3d82 Assos Assos
    $match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches);
40
    if (!$match) {
41
      $match = preg_match('/^id: (\d+)/', $string, $preg_matches);
42
    }
43 3753f249 Assos Assos
    // If an ID match was found, use that ID rather than the whole string.
44 85ad3d82 Assos Assos
    if ($match) {
45 3753f249 Assos Assos
      $entity_id = $preg_matches[1];
46 c22e192e Assos Assos
      $results = _ctools_getReferencableEntities($entity_type, $entity_info, $entity_id, '=', 1);
47 85ad3d82 Assos Assos
    }
48 3753f249 Assos Assos
    else {
49 c22e192e Assos Assos
      // 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 3753f249 Assos Assos
      }
54 c22e192e Assos Assos
      $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 85ad3d82 Assos Assos
    }
60
61 3753f249 Assos Assos
    drupal_json_output($matches);
62
  }
63
}
64
65 7e72b748 Assos Assos
/**
66
 * Use EntityReference_SelectionHandler_Generic class to build our search query.
67 3753f249 Assos Assos
 */
68
function _ctools_buildQuery($entity_type, $entity_info, $match = NULL, $match_operator = 'CONTAINS') {
69
  $base_table = $entity_info['base table'];
70 c22e192e Assos Assos
  $label_key = $entity_info['entity keys']['label'];
71
  $query = db_select($base_table)
72
    ->fields($base_table, array($entity_info['entity keys']['id']));
73 3753f249 Assos Assos
74 c22e192e Assos Assos
  if (isset($match)) {
75
    if (isset($label_key)) {
76
      $query->condition($base_table . '.' . $label_key, '%' . $match . '%', $match_operator);
77 3753f249 Assos Assos
    }
78 c22e192e Assos Assos
    // This should never happen, but double check just in case.
79
    else {
80
      return array();
81 3753f249 Assos Assos
    }
82 c22e192e Assos Assos
  }
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 e4c061ad Assos Assos
    }
98
99 c22e192e Assos Assos
    // 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 85ad3d82 Assos Assos
    }
105 c22e192e Assos Assos
    $query->addTag('node_access');
106
  }
107
  else {
108
    $query->addTag($entity_type . '_access');
109
  }
110 3753f249 Assos Assos
111 c22e192e Assos Assos
  // Add the sort option.
112
  if (isset($label_key)) {
113
    $query->orderBy($base_table . '.' . $label_key, 'ASC');
114
  }
115 3753f249 Assos Assos
116 c22e192e Assos Assos
  return $query;
117 85ad3d82 Assos Assos
}
118 3753f249 Assos Assos
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 c22e192e Assos Assos
  global $user;
125
  $account = $user;
126 3753f249 Assos Assos
  $options = array();
127 7e72b748 Assos Assos
  // We're an entity ID, return the id.
128 c22e192e Assos Assos
  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 7e72b748 Assos Assos
      $match => array(
146 c22e192e Assos Assos
        'label' => $match,
147
        'bundle' => NULL,
148 7e72b748 Assos Assos
      ),
149 c22e192e Assos Assos
    );
150 3753f249 Assos Assos
  }
151
152 c22e192e Assos Assos
  // 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 3753f249 Assos Assos
158 c22e192e Assos Assos
    $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 3753f249 Assos Assos
    }
175 c22e192e Assos Assos
    return $options;
176 3753f249 Assos Assos
  }
177 c22e192e Assos Assos
  return array();
178
}