Projet

Général

Profil

Révision 3753f249

Ajouté par Assos Assos il y a plus de 10 ans

Weekly update of contrib modules

Voir les différences:

htmltest/sites/all/modules/ctools/includes/content.menu.inc
24 24
 * Helper function for autocompletion of entity titles.
25 25
 */
26 26
function ctools_content_autocomplete_entity($type, $string = '') {
27
  $entity = entity_get_info($type);
28 27
  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
    }
28
    global $user;
29
    $entity_info = entity_get_info($type);
38 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.
39 33
    $preg_matches = array();
34
    $matches = array();
40 35
    $match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches);
41 36
    if (!$match) {
42 37
      $match = preg_match('/^id: (\d+)/', $string, $preg_matches);
43 38
    }
39
    // If an ID match was found, use that ID rather than the whole string.
44 40
    if ($match) {
45
      $query->condition('b.' . $entity['entity keys']['id'], $preg_matches[1]);
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
      ));
46 49
    }
47
    elseif ($entity['entity keys']['label']) {
48
      $query->condition('b.' . $entity['entity keys']['label'], '%' . db_like($string) . '%', 'LIKE');
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
      }
49 60
    }
50 61

  
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
        }
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);
66 78
      }
79
    }
67 80

  
68
      $query->addTag('node_access');
69
      $query->join('users', 'u', 'b.uid = u.uid');
70
      $query->addField('u', 'name', 'name');
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
    }
71 85

  
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>';
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);
75 101
      }
102
      $query->addTag('node_access');
76 103
    }
77 104
    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
      }
105
      $query->addTag($entity_type . '_access');
87 106
    }
88
    drupal_json_output($matches);
89
  }
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;
90 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
}

Formats disponibles : Unified diff