Commit 871731b800283be71e511838e88eb48272d74e5f

Authored by Adhidarma Hadiwinoto
1 parent 760b979546
Exists in master

debug

Showing 1 changed file with 2 additions and 4 deletions Inline Diff

sinkronisasi.include.php
1 <?php 1 <?php
2 2
3 function sinkronisasi_get_terms($vocabulary) { 3 function sinkronisasi_get_terms($vocabulary) {
4 $entity_type = "taxonomy_term"; 4 $entity_type = "taxonomy_term";
5 5
6 $query = new EntityFieldQuery(); 6 $query = new EntityFieldQuery();
7 $query->entityCondition('entity_type', $entity_type); 7 $query->entityCondition('entity_type', $entity_type);
8 $query->propertyCondition('vid', $vocabulary->vid); 8 $query->propertyCondition('vid', $vocabulary->vid);
9 9
10 $result = $query->execute(); 10 $result = $query->execute();
11 if (!$result) { 11 if (!$result) {
12 return; 12 return;
13 } 13 }
14 14
15 $tids = array_keys($result[$entity_type]); 15 $tids = array_keys($result[$entity_type]);
16 $terms = taxonomy_term_load_multiple($tids); 16 $terms = taxonomy_term_load_multiple($tids);
17 return $terms; 17 return $terms;
18 } 18 }
19 19
20 function sinkronisasi_export_iterate($terms) { 20 function sinkronisasi_export_iterate($terms) {
21 foreach ($terms as $tid => $term) { 21 foreach ($terms as $tid => $term) {
22 unset($term->rdf_mapping); 22 unset($term->rdf_mapping);
23 $term = sinkronisasi_taxonomy_term_denormalization($term); 23 $term = sinkronisasi_taxonomy_term_denormalization($term);
24 $term = sinkronisasi_export_set_parent($term); 24 $term = sinkronisasi_export_set_parent($term);
25 25
26 $terms[$tid] = $term; 26 $terms[$tid] = $term;
27 } 27 }
28 return $terms; 28 return $terms;
29 } 29 }
30 30
31 function sinkronisasi_export_set_parent($term) { 31 function sinkronisasi_export_set_parent($term) {
32 $parent_terms = taxonomy_get_parents($term->tid); 32 $parent_terms = taxonomy_get_parents($term->tid);
33 $parent_term = array_shift($parent_terms); 33 $parent_term = array_shift($parent_terms);
34 $term->_parent = $parent_term->tid; 34 $term->_parent = $parent_term->tid;
35 35
36 return $term; 36 return $term;
37 } 37 }
38 38
39 function sinkronisasi_taxonomy_term_denormalization($term) { 39 function sinkronisasi_taxonomy_term_denormalization($term) {
40 foreach ($term as $field_name => $field_value) { 40 foreach ($term as $field_name => $field_value) {
41 41
42 $field = field_info_field($field_name); 42 $field = field_info_field($field_name);
43 43
44 44
45 if (!$field) continue; 45 if (!$field) continue;
46 46
47 if ($field['type'] == "taxonomy_term_reference") { 47 if ($field['type'] == "taxonomy_term_reference") {
48 $vocabulary = $field["settings"]["allowed_values"][0]["vocabulary"]; 48 $vocabulary = $field["settings"]["allowed_values"][0]["vocabulary"];
49 49
50 foreach ($field_value as $language => $values_per_language) { 50 foreach ($field_value as $language => $values_per_language) {
51 foreach ($values_per_language as $idx => $value) { 51 foreach ($values_per_language as $idx => $value) {
52 52
53 if (empty($value['tid'])) { 53 if (empty($value['tid'])) {
54 continue; 54 continue;
55 } 55 }
56 56
57 $foreign_term = taxonomy_term_load($value['tid']); 57 $foreign_term = taxonomy_term_load($value['tid']);
58 if (isset($foreign_term->name)) { 58 if (isset($foreign_term->name)) {
59 $term->{"$field_name"}[$language][$idx]['_name'] = $foreign_term->name; 59 $term->{"$field_name"}[$language][$idx]['_name'] = $foreign_term->name;
60 $term->{"$field_name"}[$language][$idx]['_vocabulary'] = $vocabulary; 60 $term->{"$field_name"}[$language][$idx]['_vocabulary'] = $vocabulary;
61 } 61 }
62 } 62 }
63 } 63 }
64 } 64 }
65 65
66 } 66 }
67 return $term; 67 return $term;
68 } 68 }
69 69
70 function sinkronisasi_delete_taxonomy_term_field_type($term) { 70 function sinkronisasi_delete_taxonomy_term_field_type($term) {
71 foreach ($term as $field_name => $field_value) { 71 foreach ($term as $field_name => $field_value) {
72 72
73 $field = field_info_field($field_name); 73 $field = field_info_field($field_name);
74 74
75 75
76 if (!$field) continue; 76 if (!$field) continue;
77 77
78 if ($field['type'] == "taxonomy_term_reference") { 78 if ($field['type'] == "taxonomy_term_reference") {
79 unset($term->{"$field_name"}); 79 unset($term->{"$field_name"});
80 } 80 }
81 } 81 }
82 return $term; 82 return $term;
83 } 83 }
84 84
85 function sinkronisasi_taxonomy_term_normalization($term) { 85 function sinkronisasi_taxonomy_term_normalization($term) {
86 foreach ($term as $field_name => $field_value) { 86 foreach ($term as $field_name => $field_value) {
87 87
88 $field = field_info_field($field_name); 88 $field = field_info_field($field_name);
89 89
90 90
91 if (!$field) continue; 91 if (!$field) continue;
92 92
93 if ($field['type'] == "taxonomy_term_reference") { 93 if ($field['type'] == "taxonomy_term_reference") {
94 94
95 foreach ($field_value as $language => $values_per_language) { 95 foreach ($field_value as $language => $values_per_language) {
96 foreach ($values_per_language as $idx => $value) { 96 foreach ($values_per_language as $idx => $value) {
97 97
98 if (empty($value['_name'])) { 98 if (empty($value['_name'])) {
99 continue; 99 continue;
100 } 100 }
101 101
102 $foreign_terms = taxonomy_get_term_by_name($value['_name'], $value['_vocabulary']); 102 $foreign_terms = taxonomy_get_term_by_name($value['_name'], $value['_vocabulary']);
103 $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms)); 103 $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms));
104 $foreign_term = array_shift($foreign_terms); 104 $foreign_term = array_shift($foreign_terms);
105 105
106 if (isset($foreign_term->name)) { 106 if (isset($foreign_term->name)) {
107 if ($foreign_term->name != $value['_name']) { 107 if ($foreign_term->name != $value['_name']) {
108 echo $field_name . "\n"; 108 echo $field_name . "\n";
109 echo $foreign_term->name . " != " . $value['_name'] . "\n"; 109 echo $foreign_term->name . " != " . $value['_name'] . "\n";
110 drush_die("213AB2 foreign term name is not match!"); 110 drush_die("213AB2 foreign term name is not match!");
111 } 111 }
112 112
113 $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid; 113 $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid;
114 } 114 }
115 } 115 }
116 } 116 }
117 } 117 }
118 118
119 } 119 }
120 return $term; 120 return $term;
121 } 121 }
122 122
123 123
124 function sinkronisasi_fix_vid($term) { 124 function sinkronisasi_fix_vid($term) {
125 $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); 125 $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name);
126 if (!$vocabulary) { 126 if (!$vocabulary) {
127 var_dump($term); 127 var_dump($term);
128 echo "\n"; 128 echo "\n";
129 drush_die("CD231A vocabulary not found!"); 129 drush_die("CD231A vocabulary not found!");
130 } 130 }
131 131
132 $term->vid = $vocabulary->vid; 132 $term->vid = $vocabulary->vid;
133 return $term; 133 return $term;
134 } 134 }
135 135
136 function sinkronisasi_import($terms) { 136 function sinkronisasi_import($terms) {
137 sinkronisasi_import_iterate_insert($terms); 137 sinkronisasi_import_iterate_insert($terms);
138 sinkronisasi_import_iterate_update($terms); 138 sinkronisasi_import_iterate_update($terms);
139 } 139 }
140 140
141 function sinkronisasi_import_iterate_insert($terms) { 141 function sinkronisasi_import_iterate_insert($terms) {
142 $i = 0; 142 $i = 0;
143 foreach ($terms as $tid => $term) { 143 foreach ($terms as $tid => $term) {
144 $i++; 144 $i++;
145 145
146 echo "$i# ORIGINAL $tid: " . $term->name . "\n"; 146 echo $term->vocabulary_machine_name . " $i# ORIGINAL $tid: " . $term->name . "\n";
147 147
148 $term = sinkronisasi_fix_vid($term); 148 $term = sinkronisasi_fix_vid($term);
149 $term = sinkronisasi_delete_taxonomy_term_field_type($term); 149 $term = sinkronisasi_delete_taxonomy_term_field_type($term);
150 $term = sinkronisasi_set_field_original_tid($term); 150 $term = sinkronisasi_set_field_original_tid($term);
151 151
152 unset($term->tid); 152 unset($term->tid);
153 taxonomy_term_save($term); 153 taxonomy_term_save($term);
154 echo "ORIGINAL: $tid LOCAL: " . $term->tid . " inserted.\n"; 154 echo "ORIGINAL: $tid LOCAL: " . $term->tid . " inserted.\n";
155 155
156 } 156 }
157 } 157 }
158 158
159 function sinkronisasi_import_iterate_update($terms) { 159 function sinkronisasi_import_iterate_update($terms) {
160 $i = 0; 160 $i = 0;
161 foreach ($terms as $tid => $term) { 161 foreach ($terms as $tid => $term) {
162 $i++; 162 $i++;
163 163
164 var_dump($term); 164 echo $term->vocabulary_machine_name . " $i# $tid: " . $term->name . "\n";
165
166 echo "$i# $tid: " . $term->name . "\n";
167 165
168 $term = sinkronisasi_fix_vid($term); 166 $term = sinkronisasi_fix_vid($term);
169 $term = sinkronisasi_taxonomy_term_normalization($term); 167 $term = sinkronisasi_taxonomy_term_normalization($term);
170 $term = sinkronisasi_set_field_original_tid($term); 168 $term = sinkronisasi_set_field_original_tid($term);
171 $term = sinkronisasi_pair_with_local_tid($term); 169 $term = sinkronisasi_pair_with_local_tid($term);
172 $term = sinkronisasi_pair_with_parent($term); 170 $term = sinkronisasi_pair_with_parent($term);
173 171
174 taxonomy_term_save($term); 172 taxonomy_term_save($term);
175 echo "ORIGINAL: $tid LOCAL: " . $term->tid . " updated.\n"; 173 echo "ORIGINAL: $tid LOCAL: " . $term->tid . " updated.\n";
176 174
177 } 175 }
178 } 176 }
179 177
180 function sinkronisasi_pair_with_local_tid($term) { 178 function sinkronisasi_pair_with_local_tid($term) {
181 $tid = $term->tid; 179 $tid = $term->tid;
182 $original_term = sinkronisasi_get_term_by_field_original_tid($tid); 180 $original_term = sinkronisasi_get_term_by_field_original_tid($tid);
183 if (!$original_term) { 181 if (!$original_term) {
184 drush_die("ED0129 Error getting term with original tid is $tid!\n"); 182 drush_die("ED0129 Error getting term with original tid is $tid!\n");
185 } 183 }
186 184
187 $term->tid = $original_term->tid; 185 $term->tid = $original_term->tid;
188 return $term; 186 return $term;
189 } 187 }
190 188
191 function sinkronisasi_pair_with_parent($term) { 189 function sinkronisasi_pair_with_parent($term) {
192 if ($term->_parent) { 190 if ($term->_parent) {
193 $term->parent = sinkronisasi_get_term_by_field_original_tid($term->_parent); 191 $term->parent = sinkronisasi_get_term_by_field_original_tid($term->_parent);
194 unset($term->_parent); 192 unset($term->_parent);
195 } 193 }
196 194
197 return $term; 195 return $term;
198 } 196 }
199 197
200 function sinkronisasi_get_term_by_field_original_tid($tid) { 198 function sinkronisasi_get_term_by_field_original_tid($tid) {
201 $entity_type = "taxonomy_term"; 199 $entity_type = "taxonomy_term";
202 200
203 $query = new EntityFieldQuery(); 201 $query = new EntityFieldQuery();
204 $query->entityCondition('entity_type', $entity_type); 202 $query->entityCondition('entity_type', $entity_type);
205 $query->fieldCondition('field_original_tid', 'value', $tid); 203 $query->fieldCondition('field_original_tid', 'value', $tid);
206 204
207 $result = $query->execute(); 205 $result = $query->execute();
208 if (!$result) { 206 if (!$result) {
209 return; 207 return;
210 } 208 }
211 209
212 $tids = array_keys($result[$entity_type]); 210 $tids = array_keys($result[$entity_type]);
213 $terms = taxonomy_term_load_multiple($tids); 211 $terms = taxonomy_term_load_multiple($tids);
214 212
215 $term = array_shift($terms); 213 $term = array_shift($terms);
216 return $term; 214 return $term;
217 } 215 }
218 216
219 function sinkronisasi_set_field_original_tid($term) { 217 function sinkronisasi_set_field_original_tid($term) {
220 $term->field_original_tid['und'][0]['value'] = $term->tid; 218 $term->field_original_tid['und'][0]['value'] = $term->tid;
221 return $term; 219 return $term;
222 } 220 }
223 221
224 function sinkronisasi_export($vocabulary_machine_name) { 222 function sinkronisasi_export($vocabulary_machine_name) {
225 $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); 223 $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
226 $terms = sinkronisasi_get_terms($vocabulary); 224 $terms = sinkronisasi_get_terms($vocabulary);
227 $terms = sinkronisasi_export_iterate($terms); 225 $terms = sinkronisasi_export_iterate($terms);
228 226
229 return $terms; 227 return $terms;
230 } 228 }
231 229
232 function delete_all_terms_by_vid($vid) { 230 function delete_all_terms_by_vid($vid) {
233 231
234 $query = new EntityFieldQuery(); 232 $query = new EntityFieldQuery();
235 $query->entityCondition('entity_type', 'taxonomy_term'); 233 $query->entityCondition('entity_type', 'taxonomy_term');
236 234
237 if (!$vid) { 235 if (!$vid) {
238 drush_die("Unknown vid"); 236 drush_die("Unknown vid");
239 } 237 }
240 238
241 $query->propertyCondition('vid', $vid); 239 $query->propertyCondition('vid', $vid);
242 $result = $query->execute(); 240 $result = $query->execute();
243 if (!$result) { 241 if (!$result) {
244 return; 242 return;
245 } 243 }
246 244
247 foreach ($result['taxonomy_term'] as $term) { 245 foreach ($result['taxonomy_term'] as $term) {
248 echo "Deleting " . $term->tid . " " . $term->name; 246 echo "Deleting " . $term->tid . " " . $term->name;
249 echo ", status: "; 247 echo ", status: ";
250 echo taxonomy_term_delete($term->tid); 248 echo taxonomy_term_delete($term->tid);
251 echo "\n"; 249 echo "\n";
252 } 250 }
253 } 251 }
254 252