1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
/**
|
5 |
|
|
* @file
|
6 |
|
|
* Database functions
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
/**
|
10 |
|
|
* helper function to insert a menu item
|
11 |
|
|
*
|
12 |
|
|
* @param $mlid
|
13 |
|
|
* @param $tid
|
14 |
|
|
* @param $vid
|
15 |
|
|
*/
|
16 |
|
|
function _taxonomy_menu_insert_menu_item($mlid, $tid, $vid) {
|
17 |
|
|
$fields = array(
|
18 |
|
|
'mlid' => $mlid,
|
19 |
|
|
'tid' => $tid,
|
20 |
|
|
'vid' => $vid,
|
21 |
|
|
);
|
22 |
|
|
db_insert('taxonomy_menu')->fields($fields)->execute();
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
/**
|
26 |
|
|
* Return the corresponding menu link id.
|
27 |
|
|
*
|
28 |
|
|
* @param $tid
|
29 |
|
|
* the term's id
|
30 |
|
|
*/
|
31 |
|
|
function _taxonomy_menu_get_mlid($tid, $vid) {
|
32 |
|
|
$where = array(
|
33 |
|
|
':tid' => $tid,
|
34 |
|
|
':vid' => $vid,
|
35 |
|
|
);
|
36 |
|
|
return db_query('SELECT mlid FROM {taxonomy_menu} WHERE tid = :tid AND vid = :vid', $where)->fetchField();
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
/**
|
40 |
|
|
* Retrieve the term / menu relations for a vocab.
|
41 |
|
|
*
|
42 |
|
|
* @param $vid
|
43 |
|
|
* vocabulary's id
|
44 |
|
|
* @return
|
45 |
|
|
* array(tid => mlid)
|
46 |
|
|
*/
|
47 |
|
|
function _taxonomy_menu_get_menu_items($vid) {
|
48 |
|
|
|
49 |
|
|
$result = db_query('SELECT tid, mlid FROM {taxonomy_menu} WHERE vid = :vid', array(':vid' => $vid));
|
50 |
|
|
$menu_items = array();
|
51 |
|
|
$menu_items = $result->fetchAllKeyed();
|
52 |
|
|
return $menu_items;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
/**
|
56 |
|
|
* Delete all links associated with this vocab from both the taxonomy_menu
|
57 |
|
|
* table and the menu_link table.
|
58 |
|
|
*
|
59 |
|
|
* @param $vid
|
60 |
|
|
* vocabulary's id
|
61 |
|
|
*/
|
62 |
|
|
function _taxonomy_menu_delete_all($vid) {
|
63 |
|
|
$menu_terms = _taxonomy_menu_get_menu_items($vid);
|
64 |
|
|
if (!empty($menu_terms)) {
|
65 |
|
|
foreach ($menu_terms as $tid => $mlid) {
|
66 |
|
|
db_delete('menu_links')
|
67 |
|
|
->condition('mlid', $mlid)
|
68 |
|
|
->execute();
|
69 |
|
|
}
|
70 |
|
|
db_delete('taxonomy_menu')
|
71 |
|
|
->condition('vid', $vid)
|
72 |
|
|
->execute();
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
/**
|
77 |
|
|
* Get an array of the tid's related to the node
|
78 |
|
|
*
|
79 |
|
|
* @param $node
|
80 |
|
|
* @return array of tids
|
81 |
|
|
*/
|
82 |
|
|
function _taxonomy_menu_get_node_terms($node) {
|
83 |
|
|
// Get the taxonomy fields.
|
84 |
|
|
$tids = array();
|
85 |
|
|
$result = db_query("SELECT field_name FROM {field_config} WHERE type = 'taxonomy_term_reference'");
|
86 |
|
|
foreach ($result as $field) {
|
87 |
|
|
$field_name = $field->field_name;
|
88 |
|
|
|
89 |
|
|
if (isset($node->$field_name)) {
|
90 |
|
|
$tid_field = $node->$field_name;
|
91 |
|
|
// Loop through all the languages.
|
92 |
|
|
|
93 |
|
|
foreach ($tid_field as $tid_field_languages) {
|
94 |
|
|
// Loop through all the tids
|
95 |
|
|
|
96 |
|
|
foreach ($tid_field_languages as $tid) {
|
97 |
|
|
$tids[] = $tid['tid'];
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
}
|
101 |
|
|
}
|
102 |
|
|
return $tids;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
/**
|
106 |
|
|
* Get an array of the tid's from the parent
|
107 |
|
|
*
|
108 |
|
|
* @param $tid
|
109 |
|
|
* @return array of tid
|
110 |
|
|
*/
|
111 |
|
|
function _taxonomy_menu_get_parents($tid) {
|
112 |
|
|
$output = array();
|
113 |
|
|
$result = taxonomy_get_parents($tid);
|
114 |
|
|
foreach ($result as $key => $item) {
|
115 |
|
|
$output[] = $key;
|
116 |
|
|
}
|
117 |
|
|
return $output;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
/**
|
121 |
|
|
* Delete all rows from {taxomony_menu} associated with this tid
|
122 |
|
|
*
|
123 |
|
|
* @param $vid
|
124 |
|
|
* @param $tid
|
125 |
|
|
*/
|
126 |
|
|
function _taxonomy_menu_delete_item($vid, $tid) {
|
127 |
|
|
$and = db_and()->condition('vid', $vid)->condition('tid', $tid);
|
128 |
|
|
db_delete('taxonomy_menu')->condition($and)->execute();
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
/**
|
132 |
|
|
* Get all of the tid for a given vid
|
133 |
|
|
*
|
134 |
|
|
* @param $vid
|
135 |
|
|
* @return array of $tid
|
136 |
|
|
*/
|
137 |
|
|
function _taxonomy_menu_get_terms($vid) {
|
138 |
|
|
$result = db_select('taxonomy_term_data', 'td')
|
139 |
|
|
->condition('vid', $vid)
|
140 |
|
|
->fields('td', array('tid'))
|
141 |
|
|
->execute();
|
142 |
|
|
return $result->fetchAll();
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/**
|
146 |
|
|
* @TODO Needs Updating since terms are related via node fields
|
147 |
|
|
*
|
148 |
|
|
* used to get the count without children
|
149 |
|
|
*
|
150 |
|
|
* @param $tid
|
151 |
|
|
*/
|
152 |
|
|
function _taxonomy_menu_term_count($tid) {
|
153 |
|
|
$result = db_select('taxonomy_index', 'tn');
|
154 |
|
|
$result->condition('tid', $tid);
|
155 |
|
|
$result->join('node', 'n', 'n.nid = tn.nid AND n.status = 1');
|
156 |
|
|
$result->addExpression('COUNT(n.nid)', 'term_count');
|
157 |
|
|
$temp = $result->execute();
|
158 |
|
|
$temp = $temp->fetchObject();
|
159 |
|
|
return $temp->term_count;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* Get tid for a given mlid
|
164 |
|
|
*
|
165 |
|
|
* @param $mlid
|
166 |
|
|
* @return $tid
|
167 |
|
|
*/
|
168 |
|
|
function _taxonomy_menu_get_tid($mlid) {
|
169 |
|
|
$where = array(
|
170 |
|
|
':mlid' => $mlid,
|
171 |
|
|
);
|
172 |
|
|
return db_query('SELECT tid FROM {taxonomy_menu} WHERE mlid = :mlid', $where)->fetchField();
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
/**
|
176 |
|
|
* Get vid, tid for a given mlid
|
177 |
|
|
*
|
178 |
|
|
* @param $mlid
|
179 |
|
|
* @return array of vid, tid
|
180 |
|
|
*/
|
181 |
|
|
function _taxonomy_menu_get_item($mlid) {
|
182 |
|
|
$result = db_select('taxonomy_menu', 'tm')
|
183 |
|
|
->condition('mlid', $mlid, '=')
|
184 |
|
|
->fields('tm', array('tid', 'vid'))
|
185 |
|
|
->execute();
|
186 |
|
|
return $result->fetch();
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
/**
|
190 |
|
|
* Get the vocabulary for a tid
|
191 |
|
|
* @param $tid array of tids
|
192 |
|
|
* @return $vid
|
193 |
|
|
*/
|
194 |
|
|
function _taxonomy_menu_get_vid_by_tid($tids) {
|
195 |
|
|
if ($tids) {
|
196 |
|
|
$result = db_select('term_data')
|
197 |
|
|
->condition('tid', $tids, 'IN')
|
198 |
|
|
->fields('term_data', array('vid'))
|
199 |
|
|
->distinct()
|
200 |
|
|
->execute();
|
201 |
|
|
$vids = array();
|
202 |
|
|
return $result->fetchAllAssoc('vid');
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
/**
|
207 |
|
|
* Options functions
|
208 |
|
|
*/ |