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 |
|
|
/*
|
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 |
c22e192e
|
Assos Assos
|
$label_key = $entity_info['entity keys']['label'];
|
72 |
|
|
$query = db_select($base_table)
|
73 |
|
|
->fields($base_table, array($entity_info['entity keys']['id']));
|
74 |
3753f249
|
Assos Assos
|
|
75 |
c22e192e
|
Assos Assos
|
if (isset($match)) {
|
76 |
|
|
if (isset($label_key)) {
|
77 |
|
|
$query->condition($base_table . '.' . $label_key, '%' . $match . '%', $match_operator);
|
78 |
3753f249
|
Assos Assos
|
}
|
79 |
c22e192e
|
Assos Assos
|
// This should never happen, but double check just in case.
|
80 |
|
|
else {
|
81 |
|
|
return array();
|
82 |
3753f249
|
Assos Assos
|
}
|
83 |
c22e192e
|
Assos Assos
|
}
|
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 |
e4c061ad
|
Assos Assos
|
}
|
99 |
|
|
|
100 |
c22e192e
|
Assos Assos
|
// 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 |
85ad3d82
|
Assos Assos
|
}
|
106 |
c22e192e
|
Assos Assos
|
$query->addTag('node_access');
|
107 |
|
|
}
|
108 |
|
|
else {
|
109 |
|
|
$query->addTag($entity_type . '_access');
|
110 |
|
|
}
|
111 |
3753f249
|
Assos Assos
|
|
112 |
c22e192e
|
Assos Assos
|
// Add the sort option.
|
113 |
|
|
if (isset($label_key)) {
|
114 |
|
|
$query->orderBy($base_table . '.' . $label_key, 'ASC');
|
115 |
|
|
}
|
116 |
3753f249
|
Assos Assos
|
|
117 |
c22e192e
|
Assos Assos
|
return $query;
|
118 |
85ad3d82
|
Assos Assos
|
}
|
119 |
3753f249
|
Assos Assos
|
|
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 |
c22e192e
|
Assos Assos
|
global $user;
|
126 |
|
|
$account = $user;
|
127 |
3753f249
|
Assos Assos
|
$options = array();
|
128 |
c22e192e
|
Assos Assos
|
// 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 |
3753f249
|
Assos Assos
|
}
|
152 |
|
|
|
153 |
c22e192e
|
Assos Assos
|
// 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 |
3753f249
|
Assos Assos
|
|
159 |
c22e192e
|
Assos Assos
|
$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 |
3753f249
|
Assos Assos
|
}
|
176 |
c22e192e
|
Assos Assos
|
return $options;
|
177 |
3753f249
|
Assos Assos
|
}
|
178 |
c22e192e
|
Assos Assos
|
return array();
|
179 |
|
|
} |