Projet

Général

Profil

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

root / drupal7 / sites / all / modules / taxonomy_menu / taxonomy_menu.database.inc @ 6d8023f2

1
<?php
2

    
3

    
4
/**
5
 * @file
6
 * Database functions
7
 */
8

    
9
/**
10
 * Inserts a menu item.
11
 *
12
 * @param int $mlid
13
 *   Menu link ID.
14
 * @param int $tid
15
 *   Taxonomy term ID.
16
 * @param int $vid
17
 *   Vocabulary ID.
18
 */
19
function _taxonomy_menu_insert_menu_item($mlid, $tid, $vid) {
20
  $fields = array(
21
    'mlid' => $mlid,
22
    'tid' => $tid,
23
    'vid' => $vid,
24
  );
25
  db_insert('taxonomy_menu')->fields($fields)->execute();
26
}
27

    
28
/**
29
 * Returns the corresponding menu link id.
30
 *
31
 * @param int $tid
32
 *   Taxonomy term ID.
33
 * @param int $vid
34
 *   Vocabulary ID.
35
 * @return int
36
 *   Menu link ID for that taxonomy term.
37
 */
38
function _taxonomy_menu_get_mlid($tid, $vid) {
39
  $where = array(
40
    ':tid' => $tid,
41
    ':vid' => $vid,
42
  );
43
  return db_query('SELECT mlid FROM {taxonomy_menu} WHERE tid = :tid AND vid = :vid', $where)->fetchField();
44
}
45

    
46
/**
47
 * Retrieves the term / menu relations for a vocab.
48
 *
49
 * @param $vid
50
 *   Vocabulary ID.
51
 *
52
 * @return array
53
 *   Relations to menu link ID as an array keyed by taxonomy term ID.
54
 *     array(tid => mlid)
55
 */
56
function _taxonomy_menu_get_menu_items($vid) {
57
  return db_query('SELECT tid, mlid FROM {taxonomy_menu} WHERE vid = :vid', array(':vid' => $vid))->fetchAllKeyed();
58
}
59

    
60
/**
61
 * Deletes all links associated with this vocab from both the taxonomy_menu
62
 * table and the menu_link table.
63
 *
64
 * @param int $vid
65
 *   Vocabulary ID.
66
 */
67
function _taxonomy_menu_delete_all($vid) {
68
  $menu_terms = _taxonomy_menu_get_menu_items($vid);
69
  if (!empty($menu_terms)) {
70
    foreach ($menu_terms as $tid => $mlid) {
71
      db_delete('menu_links')
72
        ->condition('mlid', $mlid)
73
        ->execute();
74
    }
75
    db_delete('taxonomy_menu')
76
      ->condition('vid', $vid)
77
      ->execute();
78
  }
79
}
80

    
81
/**
82
 * Gets an array of the tid's related to the node
83
 *
84
 * @param object $node
85
 *   Node object.
86
 *
87
 * @return array
88
 *   Array of taxonomy term IDs.
89
 */
90
function _taxonomy_menu_get_node_terms($node) {
91
  // Get the taxonomy fields.
92
  $tids = array();
93
  $result = db_query("SELECT field_name FROM {field_config} WHERE type = 'taxonomy_term_reference'");
94
  foreach ($result as $field) {
95
    $field_name = $field->field_name;
96

    
97
    if (isset($node->$field_name)) {
98
      $tid_field = $node->$field_name;
99
      // Loop through all the languages.
100
      foreach ($tid_field as $tid_field_languages) {
101
        // Loop through all the tids
102
        foreach ($tid_field_languages as $tid) {
103
          $tids[] = $tid['tid'];
104
        }
105
      }
106
    }
107
  }
108
  return $tids;
109
}
110

    
111
/**
112
 * Gets the parent tids for a taxonomy term.
113
 *
114
 * @param int $tid
115
 *   Taxonomy term ID.
116
 *
117
 * @return array
118
 *   Array of taxonomy term IDs.
119
 */
120
function _taxonomy_menu_get_parents($tid) {
121
  $output = array();
122
  $result = taxonomy_get_parents($tid);
123
  foreach ($result as $key => $item) {
124
    $output[] = $key;
125
  }
126
  return $output;
127
}
128

    
129
/**
130
 * Deletes all rows from {taxomony_menu} associated with this tid
131
 *
132
 * @param $vid
133
 * @param $tid
134
 * @param int $vid
135
 *   Vocabulary ID.
136
 * @param int $tid
137
 *   Taxonomy term ID.
138
 */
139
function _taxonomy_menu_delete_item($vid, $tid) {
140
  $and = db_and()->condition('vid', $vid)->condition('tid', $tid);
141
  db_delete('taxonomy_menu')->condition($and)->execute();
142
}
143

    
144
/**
145
 * Gets all taxonomy terms for a given vocabulary.
146
 *
147
 * @param int $vid
148
 *   Vocabulary ID.
149
 *
150
 * @return array
151
 *   Array of taxonomy term IDs.
152
 */
153
function _taxonomy_menu_get_terms($vid) {
154
  $result = db_select('taxonomy_term_data', 'td')
155
    ->condition('vid', $vid)
156
    ->fields('td', array('tid'))
157
    ->execute();
158
  return $result->fetchAll();
159
}
160

    
161
/**
162
 * Gets the count of nodes for each term (without children).
163
 *
164
 * @param int $tid
165
 *   Taxonomy term ID.
166
 *
167
 * @return int
168
 *   Count of nodes that reference the term.
169
 *
170
 * @todo Needs updating since terms are related via fields now.
171
 */
172
function _taxonomy_menu_term_count($tid) {
173
  $result = db_select('taxonomy_index', 'tn');
174
  $result->condition('tid', $tid);
175
  $result->join('node', 'n', 'n.nid = tn.nid AND n.status = 1');
176
  $result->addExpression('COUNT(n.nid)', 'term_count');
177
  $temp = $result->execute();
178
  $temp = $temp->fetchObject();
179
  return $temp->term_count;
180
}
181

    
182
/**
183
 * Gets tid for a given mlid.
184
 *
185
 * @param int $mlid
186
 *   Menu link ID.
187
 *
188
 * @return int $tid
189
 *   Taxonomy term ID.
190
 */
191
function _taxonomy_menu_get_tid($mlid) {
192
  return db_query('SELECT tid FROM {taxonomy_menu} WHERE mlid = :mlid', array(':mlid' => $mlid))->fetchField();
193
}
194

    
195
/**
196
 * Gets the vocabulary ID and taxonomy term ID for a given menu link ID.
197
 *
198
 * @param int $mlid
199
 *   Menu link ID.
200
 *
201
 * @return array
202
 *   array(vid, tid)
203
 */
204
function _taxonomy_menu_get_item($mlid) {
205
  $result = db_select('taxonomy_menu', 'tm')
206
    ->condition('mlid', $mlid, '=')
207
    ->fields('tm', array('tid', 'vid'))
208
    ->execute();
209

    
210
  return $result->fetch();
211
}
212

    
213
/**
214
 * Gets the vocabulary for a taxonomy term ID.
215
 *
216
 * @param array $tids
217
 *   Taxonomy term IDs.
218
 *
219
 * @return $vid
220
 *   Vocabulary ID.
221
 */
222
function _taxonomy_menu_get_vid_by_tid($tids) {
223
  if ($tids) {
224
    $result = db_select('term_data')
225
      ->condition('tid', $tids, 'IN')
226
      ->fields('term_data', array('vid'))
227
      ->distinct()
228
      ->execute();
229
    return $result->fetchAllAssoc('vid');
230
  }
231
}