Projet

Général

Profil

Paste
Télécharger (4,66 ko) Statistiques
| Branche: | Révision:

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

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

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

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

    
62
    drupal_json_output($matches);
63
  }
64
}
65

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

    
75
    if (isset($match)) {
76
      if (isset($entity_info['entity keys']['label'])) {
77
        $query->condition($base_table .'.'. $entity_info['entity keys']['label'], '%' . $match . '%' , $match_operator);
78
      }
79
    }
80

    
81
    // Add a label to the query, if the label exists
82
    if (isset($entity_info['entity keys']['label'])) {
83
      $query->fields($base_table, array($entity_info['entity keys']['label']));
84
    }
85

    
86
    // Add a generic entity access tag to the query.
87
    $query->addTag('ctools');
88

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

    
108
    // Add the sort option.
109
    if(isset($entity_info['entity keys']['label'])) {
110
      $query->orderBy($base_table .'.'. $entity_info['entity keys']['label'], 'ASC');
111
    }
112

    
113
    return $query;
114
}
115

    
116
/**
117
 * Private function to get referencable entities. Based on code from the
118
 * Entity Reference module.
119
 */
120
function _ctools_getReferencableEntities($entity_type, $entity_info, $match = NULL, $match_operator = 'LIKE', $limit = 0) {
121
  $options = array();
122

    
123
  $query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator);
124
  if ($limit > 0) {
125
    $query->range(0, $limit);
126
  }
127

    
128
  $results = $query->execute();
129

    
130
  if (!empty($results)) {
131
    foreach ($results as $record) {
132
      $options[$record->{$entity_info['entity keys']['id']}] = array(
133
        'label' => isset($entity_info['entity keys']['label']) ? check_plain($record->{$entity_info['entity keys']['label']}) : $record->{$entity_info['entity keys']['id']},
134
      );
135
    }
136
  }
137

    
138
  return $options;
139
}