-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreal_taxonomy.module
266 lines (244 loc) · 7.56 KB
/
real_taxonomy.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* Implementation of hook_menu().
*/
function real_taxonomy_menu() {
$items['admin/settings/real-taxonomy'] = array(
'title' => 'Real Taxonomy',
'description' => 'Real taxonomy settings page',
'page callback' => 'drupal_get_form',
'page arguments' => array('real_taxonomy_admin_settings'),
'access arguments' =>array('administer taxonomy'),
'type' => MENU_NORMAL_ITEM,
'file' => 'real_taxonomy.admin.inc',
);
$items['real-taxonomy-reset'] = array(
'title' => 'Real Taxonomy reset',
'page callback' => 'drupal_get_form',
'page arguments' => array('real_taxonomy_reset'),
'access arguments' =>array('administer taxonomy'),
'type' => MENU_CALLBACK,
'file' => 'real_taxonomy.admin.inc',
);
return $items;
}
/**
* Implementation of hook_form_alter().
*/
function real_taxonomy_form_alter(&$form, $form_state, $form_id) {
if($form['#id'] == 'node-form') {
if(isset($form['taxonomy']) && ($vid = variable_get('real_taxonomy_vocabularies', 0))){
foreach($form['taxonomy'] as $key => $value){
// Replace drupal taxonomy form
if($key == $vid){
$default_value = array();
$node = $form['#node'];
if(isset($node->nid)){
foreach($node->taxonomy as $term){
if($term->vid == $vid && ($item = real_taxonomy_get_item_by_tid($term->tid))) {
$default_value[] = $item->id;
}
}
}
// Hide taxonomy form
$form['taxonomy'][$vid]['#access'] = false;
$form['taxonomy']['real_taxonomy'] = array(
'#type' => 'hierarchical_select',
'#title' => $form['taxonomy'][$vid]['#title'],
'#size' => 1,
'#config' => array(
'module' => 'real_taxonomy',
'save_lineage' => 1,
'enforce_deepest' => 0,
'entity_count' => 0,
'resizable' => 1,
'animation_delay' => 400,
'exclusive_lineages' => array(),
'render_flat_select' => 0,
),
'#description' => $form['taxonomy'][$vid]['#description'],
'#weight' => $form['taxonomy'][$vid]['#weight'],
'#required'=> $form['taxonomy'][$vid]['#required'],
'#default_value' => $default_value,
);
$form['taxonomy'][$vid]['#required'] = false;
break;
}
}
}
}
}
/**
* Get elements with parent
* @param parent - id of parent element. 0 for root
* @return objects array
*/
function real_taxonomy_get_items($parent = 0){
$result = db_query("SELECT * FROM {real_taxonomy} WHERE parent = %d", $parent);
$items = array();
while($row = db_fetch_object($result)) {
$items[$row->id] = $row->name;
}
return $items;
};
/**
* Get elements by id
* @param id - element id
* @return object
*/
function real_taxonomy_get_item($id) {
return db_fetch_object(db_query("SELECT * FROM {real_taxonomy} WHERE id = %d", $id));
};
/**
* Get elements by tid
* @param id - element id
* @return object
*/
function real_taxonomy_get_item_by_tid($tid) {
return db_fetch_object(db_query("SELECT * FROM {real_taxonomy} WHERE tid = %d", $tid));
};
/**
* Get parents lineage for element
* @param id - element id
* @return objects array
*/
function real_taxonomy_get_parents($id) {
$parents = array();
while($item = real_taxonomy_get_item($id)) {
$parents[$item->id] = $item;
$id = $item->parent;
}
return $parents;
}
/**
* Implementation of hook_taxonomy().
*/
function real_taxonomy_taxonomy($op, $type, $array = NULL) {
if($type == 'term') {
if($op == 'insert' && isset($array['real_taxonomy_id'])) {
db_query("UPDATE {real_taxonomy} SET tid = %d WHERE id = %d", $array['tid'], $array['real_taxonomy_id']);
}elseif($op == 'delete') {
db_query("UPDATE {real_taxonomy} SET tid = 0 WHERE tid = %d", $array['tid']);
}
}
if($type == 'vocabulary' && $op == 'delete') {
if($array['vid'] == variable_get('real_taxonomy_vocabularies', 0)) {
db_query("UPDATE {real_taxonomy} SET tid = 0");
variable_del('real_taxonomy_vocabularies');
}
}
}
/**
* Implementation of hook_nodeapi().
*/
function real_taxonomy_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
$vid = variable_get('real_taxonomy_vocabularies', 0);
switch ($op) {
case 'presave':
if(!$vid || !isset($node->taxonomy['real_taxonomy'])) {
return;
}
$old_terms = (array)$node->taxonomy[$vid];
$node->taxonomy[$vid] = array();
foreach ($node->taxonomy['real_taxonomy'] as $id){
$item = real_taxonomy_get_item($id);
$parent = real_taxonomy_get_item($item->parent);
$parent = ($parent && isset($parent->tid))? $parent->tid : 0;
if ($item->tid == 0) {
$term = array(
'vid' => $vid,
'name' => $item->name,
'desription' => $item->description,
'parent' => $parent,
'real_taxonomy_id' => $item->id,
);
taxonomy_save_term($term);
$item->tid = $term['tid'];
}
$node->taxonomy[$vid][] = $item->tid;
}
// Если материал пересохранили с новыми значениями, проверяем старые,
// если к старым терминам ничего не привязано - удаляем их
if(count($old_terms)) {
real_taxonomy_delete_unused_terms(array_diff($old_terms, $node->taxonomy[$vid]));
}
if(isset($node->taxonomy['real_taxonomy'])){
unset($node->taxonomy['real_taxonomy']);
}
break;
case 'delete':
$tids = array();
foreach ($node->taxonomy as $term) {
if($term->vid == $vid){
$tids[] = $term->tid;
}
}
real_taxonomy_delete_unused_terms($tids);
break;
}
}
/**
* Delete unused terms
* @param $tids - array of terms tid
*/
function real_taxonomy_delete_unused_terms($tids){
if(!variable_get('real_taxonomy_delete_unused_terms', true)) {
return;
}
foreach($tids as $tid){
$result = taxonomy_select_nodes(array($tid));
$count = 0;
while(db_fetch_array($result)) {
$count++;
}
if($count < 2){
taxonomy_del_term($tid);
}
}
}
/**
* Implementation of hook_hierarchical_select_lineage().
*/
function real_taxonomy_hierarchical_select_lineage($item, $params) {
return array_keys(array_reverse(real_taxonomy_get_parents($item)));
}
/**
* Implementation of hook_hierarchical_select_params().
*/
function real_taxonomy_hierarchical_select_params() {
return array();
}
/**
* Implementation of hook_hierarchical_select_root_level().
*/
function real_taxonomy_hierarchical_select_root_level($params) {
return real_taxonomy_get_items(0);
}
/**
* Implementation of hook_hierarchical_select_children().
*/
function real_taxonomy_hierarchical_select_children($parent, $params) {
return $parent > 0? real_taxonomy_get_items($parent) : array();
}
/**
* Implementation of hook_hierarchical_select_valid_item().
*/
function real_taxonomy_hierarchical_select_valid_item($item, $params) {
return (bool)real_taxonomy_get_item($item);
}
/**
* Implementation of hook_hierarchical_select_item_get_label().
*/
function real_taxonomy_hierarchical_select_item_get_label($item, $params) {
$item = real_taxonomy_get_item($item);
return $item->name;
}
/**
* Implementation of hook_hierarchical_select_implementation_info().
*/
function real_taxonomy_hierarchical_select_implementation_info() {
return array(
'hierarchy type' => t('Custom'),
'entity type' => t('N/A'),
);
}