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($type, $string = '') {
|
27
|
$entity = entity_get_info($type);
|
28
|
if ($string != '') {
|
29
|
// @todo verify the query logic here, it's untested.
|
30
|
// Set up the query
|
31
|
$query = db_select($entity['base table'], 'b');
|
32
|
if ($entity['entity keys']['label']) {
|
33
|
$query->fields('b', array($entity['entity keys']['id'], $entity['entity keys']['label']))->range(0, 10);
|
34
|
}
|
35
|
else {
|
36
|
$query->fields('b', array($entity['entity keys']['id']))->range(0, 10);
|
37
|
}
|
38
|
|
39
|
$preg_matches = array();
|
40
|
$match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches);
|
41
|
if (!$match) {
|
42
|
$match = preg_match('/^id: (\d+)/', $string, $preg_matches);
|
43
|
}
|
44
|
if ($match) {
|
45
|
$query->condition('b.' . $entity['entity keys']['id'], $preg_matches[1]);
|
46
|
}
|
47
|
elseif ($entity['entity keys']['label']) {
|
48
|
$query->condition('b.' . $entity['entity keys']['label'], '%' . db_like($string) . '%', 'LIKE');
|
49
|
}
|
50
|
|
51
|
$matches = array();
|
52
|
if ($type == 'node') {
|
53
|
if (!user_access('bypass node access')) {
|
54
|
// If the user is able to view their own unpublished nodes, allow them
|
55
|
// to see these in addition to published nodes.
|
56
|
if (user_access('view own unpublished content')) {
|
57
|
$query->condition(db_or()
|
58
|
->condition('b.status', NODE_PUBLISHED)
|
59
|
->condition('b.uid', $GLOBALS['user']->uid)
|
60
|
);
|
61
|
}
|
62
|
else {
|
63
|
// If not, restrict the query to published nodes.
|
64
|
$query->condition('b.status', NODE_PUBLISHED);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
$query->addTag('node_access');
|
69
|
$query->join('users', 'u', 'b.uid = u.uid');
|
70
|
$query->addField('u', 'name', 'name');
|
71
|
|
72
|
foreach ($query->execute() as $nodeish) {
|
73
|
$name = empty($nodeish->name) ? variable_get('anonymous', t('Anonymous')) : check_plain($nodeish->name);
|
74
|
$matches[$nodeish->title . " [id: $nodeish->nid]"] = '<span class="autocomplete_title">' . check_plain($nodeish->title) . '</span> <span class="autocomplete_user">(' . t('by @user', array('@user' => $name)) . ')</span>';
|
75
|
}
|
76
|
}
|
77
|
else {
|
78
|
foreach ($query->execute() as $item) {
|
79
|
$id = $item->{$entity['entity keys']['id']};
|
80
|
if ($entity['entity keys']['label']) {
|
81
|
$matches[$item->{$entity['entity keys']['label']} . " [id: $id]"] = '<span class="autocomplete_title">' . check_plain($item->{$entity['entity keys']['label']}) . '</span>';
|
82
|
}
|
83
|
else {
|
84
|
$matches["[id: $id]"] = '<span class="autocomplete_title">' . check_plain($item->{$entity['entity keys']['id']}) . '</span>';
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
drupal_json_output($matches);
|
89
|
}
|
90
|
}
|