Projet

Général

Profil

Paste
Télécharger (5,29 ko) Statistiques
| Branche: | Révision:

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

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
  if ($string != '') {
28
    global $user;
29
    $entity_info = entity_get_info($type);
30
    if ($type == 'node') {
31
      $entity_info['entity keys']['bundle field'] = 'type';
32
    }
33

    
34
    // We must query all ids, because if every one of the 10 don't have access
35
    // the user may never be able to autocomplete a node title.
36
    $preg_matches = array();
37
    $matches = array();
38
    $match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches);
39
    if (!$match) {
40
      $match = preg_match('/^id: (\d+)/', $string, $preg_matches);
41
    }
42
    // If an ID match was found, use that ID rather than the whole string.
43
    if ($match) {
44
      $entity_id = $preg_matches[1];
45
      $entity = entity_load($type, array($entity_id));
46

    
47
      // Format results in an array so later we could add attributes to the
48
      // autocomplete text that is returned.
49
      $results = array($entity_id => array(
50
        'label' => $entity[$entity_id]->$entity_info['entity keys']['label'],
51
      ));
52
    }
53
    else {
54
      $results = _ctools_getReferencableEntities($type, $entity_info, $string, 'LIKE', 10);
55
    }
56
    foreach($results as $entity_id => $result) {
57
      if (!$entity_info['entity keys']['label']) {
58
         $matches["[id: $entity_id]"] = '<span class="autocomplete_title">' . $entity_id . '</span>';
59
      }
60
      else {
61
        $matches[$result['label'] . " [id: $entity_id]"] = '<span class="autocomplete_title">' . check_plain($result['label']) . '</span>';
62
        $matches[$result['label'] . " [id: $entity_id]"] .= isset($result['bundle field']) ? ' <span class="autocomplete_bundle">(' . check_plain($result['bundle field']) . ')</span>' : '';
63
      }
64
    }
65

    
66
    drupal_json_output($matches);
67
  }
68
}
69

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

    
79
    if (isset($match)) {
80
      if (isset($entity_info['entity keys']['label'])) {
81
        $query->condition($base_table .'.'. $entity_info['entity keys']['label'], '%' . $match . '%' , $match_operator);
82
      }
83
    }
84

    
85
    // Add a label to the query, if the label exists
86
    if (isset($entity_info['entity keys']['label'])) {
87
      $query->fields($base_table, array($entity_info['entity keys']['label']));
88
    }
89

    
90
    // Add bundle field to the query, if it exists.
91
    if (isset($entity_info['entity keys']['bundle field'])) {
92
      $query->fields($base_table, array($entity_info['entity keys']['bundle field']));
93
    }
94

    
95
    // Add a generic entity access tag to the query.
96
    $query->addTag('ctools');
97

    
98
    if($entity_type == 'comment') {
99
      // Adding the 'comment_access' tag is sadly insufficient for comments: core
100
      // requires us to also know about the concept of 'published' and
101
      // 'unpublished'.
102
      if (!user_access('administer comments')) {
103
        $query->condition('comment.status', COMMENT_PUBLISHED);
104
      }
105
      // Join to a node if the user does not have node access bypass permissions
106
      // to obey node published permissions
107
      if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
108
        $node_alias = $query->innerJoin('node', 'n', '%alias.nid = comment.nid');
109
        $query->condition($node_alias . '.status', NODE_PUBLISHED);
110
      }
111
      $query->addTag('node_access');
112
    }
113
    else {
114
      $query->addTag($entity_type . '_access');
115
    }
116

    
117
    // Add the sort option.
118
    if(isset($entity_info['entity keys']['label'])) {
119
      $query->orderBy($base_table .'.'. $entity_info['entity keys']['label'], 'ASC');
120
    }
121

    
122
    return $query;
123
}
124

    
125
/**
126
 * Private function to get referencable entities. Based on code from the
127
 * Entity Reference module.
128
 */
129
function _ctools_getReferencableEntities($entity_type, $entity_info, $match = NULL, $match_operator = 'LIKE', $limit = 0) {
130
  $options = array();
131

    
132
  $query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator);
133
  if ($limit > 0) {
134
    $query->range(0, $limit);
135
  }
136

    
137
  $results = $query->execute();
138

    
139
  if (!empty($results)) {
140
    foreach ($results as $record) {
141
      $options[$record->{$entity_info['entity keys']['id']}] = array(
142
        'label' => isset($entity_info['entity keys']['label']) ? check_plain($record->{$entity_info['entity keys']['label']}) : $record->{$entity_info['entity keys']['id']},
143
        'bundle field' => isset($entity_info['entity keys']['bundle field']) ? check_plain($record->{$entity_info['entity keys']['bundle field']}) : '',
144
      );
145
    }
146
  }
147

    
148
  return $options;
149
}