Commit 760b979546c1eee9b2eafe18fab23fbf8a9cc2ec
1 parent
b6fb238514
Exists in
master
debug
Showing 1 changed file with 2 additions and 0 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 "$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); | ||
165 | |||
164 | echo "$i# $tid: " . $term->name . "\n"; | 166 | echo "$i# $tid: " . $term->name . "\n"; |
165 | 167 | ||
166 | $term = sinkronisasi_fix_vid($term); | 168 | $term = sinkronisasi_fix_vid($term); |
167 | $term = sinkronisasi_taxonomy_term_normalization($term); | 169 | $term = sinkronisasi_taxonomy_term_normalization($term); |
168 | $term = sinkronisasi_set_field_original_tid($term); | 170 | $term = sinkronisasi_set_field_original_tid($term); |
169 | $term = sinkronisasi_pair_with_local_tid($term); | 171 | $term = sinkronisasi_pair_with_local_tid($term); |
170 | $term = sinkronisasi_pair_with_parent($term); | 172 | $term = sinkronisasi_pair_with_parent($term); |
171 | 173 | ||
172 | taxonomy_term_save($term); | 174 | taxonomy_term_save($term); |
173 | echo "ORIGINAL: $tid LOCAL: " . $term->tid . " updated.\n"; | 175 | echo "ORIGINAL: $tid LOCAL: " . $term->tid . " updated.\n"; |
174 | 176 | ||
175 | } | 177 | } |
176 | } | 178 | } |
177 | 179 | ||
178 | function sinkronisasi_pair_with_local_tid($term) { | 180 | function sinkronisasi_pair_with_local_tid($term) { |
179 | $tid = $term->tid; | 181 | $tid = $term->tid; |
180 | $original_term = sinkronisasi_get_term_by_field_original_tid($tid); | 182 | $original_term = sinkronisasi_get_term_by_field_original_tid($tid); |
181 | if (!$original_term) { | 183 | if (!$original_term) { |
182 | drush_die("ED0129 Error getting term with original tid is $tid!\n"); | 184 | drush_die("ED0129 Error getting term with original tid is $tid!\n"); |
183 | } | 185 | } |
184 | 186 | ||
185 | $term->tid = $original_term->tid; | 187 | $term->tid = $original_term->tid; |
186 | return $term; | 188 | return $term; |
187 | } | 189 | } |
188 | 190 | ||
189 | function sinkronisasi_pair_with_parent($term) { | 191 | function sinkronisasi_pair_with_parent($term) { |
190 | if ($term->_parent) { | 192 | if ($term->_parent) { |
191 | $term->parent = sinkronisasi_get_term_by_field_original_tid($term->_parent); | 193 | $term->parent = sinkronisasi_get_term_by_field_original_tid($term->_parent); |
192 | unset($term->_parent); | 194 | unset($term->_parent); |
193 | } | 195 | } |
194 | 196 | ||
195 | return $term; | 197 | return $term; |
196 | } | 198 | } |
197 | 199 | ||
198 | function sinkronisasi_get_term_by_field_original_tid($tid) { | 200 | function sinkronisasi_get_term_by_field_original_tid($tid) { |
199 | $entity_type = "taxonomy_term"; | 201 | $entity_type = "taxonomy_term"; |
200 | 202 | ||
201 | $query = new EntityFieldQuery(); | 203 | $query = new EntityFieldQuery(); |
202 | $query->entityCondition('entity_type', $entity_type); | 204 | $query->entityCondition('entity_type', $entity_type); |
203 | $query->fieldCondition('field_original_tid', 'value', $tid); | 205 | $query->fieldCondition('field_original_tid', 'value', $tid); |
204 | 206 | ||
205 | $result = $query->execute(); | 207 | $result = $query->execute(); |
206 | if (!$result) { | 208 | if (!$result) { |
207 | return; | 209 | return; |
208 | } | 210 | } |
209 | 211 | ||
210 | $tids = array_keys($result[$entity_type]); | 212 | $tids = array_keys($result[$entity_type]); |
211 | $terms = taxonomy_term_load_multiple($tids); | 213 | $terms = taxonomy_term_load_multiple($tids); |
212 | 214 | ||
213 | $term = array_shift($terms); | 215 | $term = array_shift($terms); |
214 | return $term; | 216 | return $term; |
215 | } | 217 | } |
216 | 218 | ||
217 | function sinkronisasi_set_field_original_tid($term) { | 219 | function sinkronisasi_set_field_original_tid($term) { |
218 | $term->field_original_tid['und'][0]['value'] = $term->tid; | 220 | $term->field_original_tid['und'][0]['value'] = $term->tid; |
219 | return $term; | 221 | return $term; |
220 | } | 222 | } |
221 | 223 | ||
222 | function sinkronisasi_export($vocabulary_machine_name) { | 224 | function sinkronisasi_export($vocabulary_machine_name) { |
223 | $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); | 225 | $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); |
224 | $terms = sinkronisasi_get_terms($vocabulary); | 226 | $terms = sinkronisasi_get_terms($vocabulary); |
225 | $terms = sinkronisasi_export_iterate($terms); | 227 | $terms = sinkronisasi_export_iterate($terms); |
226 | 228 | ||
227 | return $terms; | 229 | return $terms; |
228 | } | 230 | } |
229 | 231 | ||
230 | function delete_all_terms_by_vid($vid) { | 232 | function delete_all_terms_by_vid($vid) { |
231 | 233 | ||
232 | $query = new EntityFieldQuery(); | 234 | $query = new EntityFieldQuery(); |
233 | $query->entityCondition('entity_type', 'taxonomy_term'); | 235 | $query->entityCondition('entity_type', 'taxonomy_term'); |
234 | 236 | ||
235 | if (!$vid) { | 237 | if (!$vid) { |
236 | drush_die("Unknown vid"); | 238 | drush_die("Unknown vid"); |
237 | } | 239 | } |
238 | 240 | ||
239 | $query->propertyCondition('vid', $vid); | 241 | $query->propertyCondition('vid', $vid); |
240 | $result = $query->execute(); | 242 | $result = $query->execute(); |
241 | if (!$result) { | 243 | if (!$result) { |
242 | return; | 244 | return; |
243 | } | 245 | } |
244 | 246 | ||
245 | foreach ($result['taxonomy_term'] as $term) { | 247 | foreach ($result['taxonomy_term'] as $term) { |
246 | echo "Deleting " . $term->tid . " " . $term->name; | 248 | echo "Deleting " . $term->tid . " " . $term->name; |
247 | echo ", status: "; | 249 | echo ", status: "; |
248 | echo taxonomy_term_delete($term->tid); | 250 | echo taxonomy_term_delete($term->tid); |
249 | echo "\n"; | 251 | echo "\n"; |
250 | } | 252 | } |
251 | } | 253 | } |
252 | 254 |