sinkronisasi.include.php
7.33 KB
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
<?php
function sinkronisasi_get_terms($vocabulary) {
$entity_type = "taxonomy_term";
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type);
$query->propertyCondition('vid', $vocabulary->vid);
$result = $query->execute();
if (!$result) {
return;
}
$tids = array_keys($result[$entity_type]);
$terms = taxonomy_term_load_multiple($tids);
return $terms;
}
function sinkronisasi_export_iterate($terms) {
foreach ($terms as $tid => $term) {
unset($term->rdf_mapping);
$term = sinkronisasi_taxonomy_term_denormalization($term);
$term = sinkronisasi_export_set_parent($term);
$terms[$tid] = $term;
}
return $terms;
}
function sinkronisasi_export_set_parent($term) {
$parent_terms = taxonomy_get_parents($term->tid);
$parent_term = array_shift($parent_terms);
$term->_parent = $parent_term->tid;
return $term;
}
function sinkronisasi_taxonomy_term_denormalization($term) {
foreach ($term as $field_name => $field_value) {
$field = field_info_field($field_name);
if (!$field) continue;
if ($field['type'] == "taxonomy_term_reference") {
$vocabulary = $field["settings"]["allowed_values"][0]["vocabulary"];
foreach ($field_value as $language => $values_per_language) {
foreach ($values_per_language as $idx => $value) {
if (empty($value['tid'])) {
continue;
}
$foreign_term = taxonomy_term_load($value['tid']);
if (isset($foreign_term->name)) {
$term->{"$field_name"}[$language][$idx]['_name'] = $foreign_term->name;
$term->{"$field_name"}[$language][$idx]['_vocabulary'] = $vocabulary;
}
}
}
}
}
return $term;
}
function sinkronisasi_delete_taxonomy_term_field_type($term) {
foreach ($term as $field_name => $field_value) {
$field = field_info_field($field_name);
if (!$field) continue;
if ($field['type'] == "taxonomy_term_reference") {
unset($term->{"$field_name"});
}
}
return $term;
}
function sinkronisasi_taxonomy_term_normalization($term) {
foreach ($term as $field_name => $field_value) {
$field = field_info_field($field_name);
if (!$field) continue;
if ($field['type'] == "taxonomy_term_reference") {
foreach ($field_value as $language => $values_per_language) {
foreach ($values_per_language as $idx => $value) {
if (empty($value['_name'])) {
continue;
}
$foreign_terms = taxonomy_get_term_by_name($value['_name'], $value['_vocabulary']);
$foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms));
$foreign_term = array_shift($foreign_terms);
if (isset($foreign_term->name)) {
if ($foreign_term->name != $value['_name']) {
echo $field_name . "\n";
echo $foreign_term->name . " != " . $value['_name'] . "\n";
drush_die("213AB2 foreign term name is not match!");
}
$term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid;
}
}
}
}
}
return $term;
}
function sinkronisasi_fix_vid($term) {
$vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name);
if (!$vocabulary) {
var_dump($term);
echo "\n";
drush_die("CD231A vocabulary not found!");
}
$term->vid = $vocabulary->vid;
return $term;
}
function sinkronisasi_import($terms) {
sinkronisasi_import_iterate_insert($terms);
sinkronisasi_import_iterate_update($terms);
}
function sinkronisasi_import_iterate_insert($terms) {
$i = 0;
foreach ($terms as $tid => $term) {
$i++;
echo $term->vocabulary_machine_name . " $i# ORIGINAL $tid: " . $term->name . "\n";
$term = sinkronisasi_fix_vid($term);
$term = sinkronisasi_delete_taxonomy_term_field_type($term);
$term = sinkronisasi_set_field_original_tid($term);
unset($term->tid);
$status_code = taxonomy_term_save($term);
if ($status_code != SAVED_NEW) {
drush_die("Ada kesalahan saat menyimpan data baru.");
}
echo "ORIGINAL: $tid LOCAL: " . $term->tid . " inserted.\n";
}
}
function sinkronisasi_import_iterate_update($terms) {
$i = 0;
foreach ($terms as $tid => $term) {
$i++;
echo $term->vocabulary_machine_name . " $i# $tid: " . $term->name . "\n";
$term = sinkronisasi_fix_vid($term);
$term = sinkronisasi_taxonomy_term_normalization($term);
$term = sinkronisasi_set_field_original_tid($term);
$term = sinkronisasi_pair_with_local_tid($term);
$term = sinkronisasi_pair_with_parent($term);
taxonomy_term_save($term);
$status_code = taxonomy_term_save($term);
if ($status_code != SAVED_UPDATED) {
drush_die("Ada kesalahan saat mengupdate data.");
}
echo "ORIGINAL: $tid LOCAL: " . $term->tid . " updated.\n";
}
}
function sinkronisasi_pair_with_local_tid($term) {
$tid = $term->tid;
$original_term = sinkronisasi_get_term_by_field_original_tid($tid);
if (!$original_term) {
drush_die("ED0129 Error getting term with original tid is $tid!\n");
}
$term->tid = $original_term->tid;
return $term;
}
function sinkronisasi_pair_with_parent($term) {
if ($term->_parent) {
$parent_term = sinkronisasi_get_term_by_field_original_tid($term->_parent);
if ($parent_term) {
$term->parent = $parent_term->tid;
}
unset($term->_parent);
}
return $term;
}
function sinkronisasi_get_term_by_field_original_tid($tid) {
$entity_type = "taxonomy_term";
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type);
$query->fieldCondition('field_original_tid', 'value', $tid);
$result = $query->execute();
if (!$result) {
return;
}
$tids = array_keys($result[$entity_type]);
$terms = taxonomy_term_load_multiple($tids);
$term = array_shift($terms);
return $term;
}
function sinkronisasi_set_field_original_tid($term) {
$term->field_original_tid['und'][0]['value'] = $term->tid;
return $term;
}
function sinkronisasi_export($vocabulary_machine_name) {
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
$terms = sinkronisasi_get_terms($vocabulary);
$terms = sinkronisasi_export_iterate($terms);
return $terms;
}
function delete_all_terms_by_vid($vid) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'taxonomy_term');
if (!$vid) {
drush_die("Unknown vid");
}
$query->propertyCondition('vid', $vid);
$result = $query->execute();
if (!$result) {
return;
}
foreach ($result['taxonomy_term'] as $term) {
echo "Deleting " . $term->tid;
echo ", status: ";
echo taxonomy_term_delete($term->tid);
echo "\n";
}
}