Commit de7b90a2b348a99a7005796b5e83b5113b057bca
1 parent
698d6efc2c
Exists in
master
debug
Showing 1 changed file with 1 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 | 48 | ||
49 | foreach ($field_value as $language => $values_per_language) { | 49 | foreach ($field_value as $language => $values_per_language) { |
50 | foreach ($values_per_language as $idx => $value) { | 50 | foreach ($values_per_language as $idx => $value) { |
51 | 51 | ||
52 | if (empty($value['tid'])) { | 52 | if (empty($value['tid'])) { |
53 | continue; | 53 | continue; |
54 | } | 54 | } |
55 | 55 | ||
56 | $foreign_term = taxonomy_term_load($value['tid']); | 56 | $foreign_term = taxonomy_term_load($value['tid']); |
57 | if (isset($foreign_term->name)) { | 57 | if (isset($foreign_term->name)) { |
58 | $term->{"$field_name"}[$language][$idx]['_name'] = $foreign_term->name; | 58 | $term->{"$field_name"}[$language][$idx]['_name'] = $foreign_term->name; |
59 | } | 59 | } |
60 | } | 60 | } |
61 | } | 61 | } |
62 | } | 62 | } |
63 | 63 | ||
64 | } | 64 | } |
65 | return $term; | 65 | return $term; |
66 | } | 66 | } |
67 | 67 | ||
68 | function sinkronisasi_delete_taxonomy_term_field_type($term) { | 68 | function sinkronisasi_delete_taxonomy_term_field_type($term) { |
69 | foreach ($term as $field_name => $field_value) { | 69 | foreach ($term as $field_name => $field_value) { |
70 | 70 | ||
71 | $field = field_info_field($field_name); | 71 | $field = field_info_field($field_name); |
72 | 72 | ||
73 | 73 | ||
74 | if (!$field) continue; | 74 | if (!$field) continue; |
75 | 75 | ||
76 | if ($field['type'] == "taxonomy_term_reference") { | 76 | if ($field['type'] == "taxonomy_term_reference") { |
77 | unset($term->{"$field_name"}); | 77 | unset($term->{"$field_name"}); |
78 | } | 78 | } |
79 | } | 79 | } |
80 | return $term; | 80 | return $term; |
81 | } | 81 | } |
82 | 82 | ||
83 | function sinkronisasi_taxonomy_term_normalization($term) { | 83 | function sinkronisasi_taxonomy_term_normalization($term) { |
84 | foreach ($term as $field_name => $field_value) { | 84 | foreach ($term as $field_name => $field_value) { |
85 | 85 | ||
86 | $field = field_info_field($field_name); | 86 | $field = field_info_field($field_name); |
87 | 87 | ||
88 | 88 | ||
89 | if (!$field) continue; | 89 | if (!$field) continue; |
90 | 90 | ||
91 | if ($field['type'] == "taxonomy_term_reference") { | 91 | if ($field['type'] == "taxonomy_term_reference") { |
92 | 92 | ||
93 | foreach ($field_value as $language => $values_per_language) { | 93 | foreach ($field_value as $language => $values_per_language) { |
94 | foreach ($values_per_language as $idx => $value) { | 94 | foreach ($values_per_language as $idx => $value) { |
95 | 95 | ||
96 | if (empty($value['_name'])) { | 96 | if (empty($value['_name'])) { |
97 | continue; | 97 | continue; |
98 | } | 98 | } |
99 | 99 | ||
100 | $foreign_terms = taxonomy_get_term_by_name($value['_name']); | 100 | $foreign_terms = taxonomy_get_term_by_name($value['_name']); |
101 | $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms)); | 101 | $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms)); |
102 | $foreign_term = array_shift($foreign_terms); | 102 | $foreign_term = array_shift($foreign_terms); |
103 | 103 | ||
104 | if (isset($foreign_term->name)) { | 104 | if (isset($foreign_term->name)) { |
105 | if ($foreign_term->name != $value['_name']) { | 105 | if ($foreign_term->name != $value['_name']) { |
106 | drush_die("213AB2 foreign term name is not match!"); | 106 | drush_die("213AB2 foreign term name is not match!"); |
107 | } | 107 | } |
108 | 108 | ||
109 | $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid; | 109 | $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid; |
110 | } | 110 | } |
111 | } | 111 | } |
112 | } | 112 | } |
113 | } | 113 | } |
114 | 114 | ||
115 | } | 115 | } |
116 | return $term; | 116 | return $term; |
117 | } | 117 | } |
118 | 118 | ||
119 | 119 | ||
120 | function sinkronisasi_fix_vid($term) { | 120 | function sinkronisasi_fix_vid($term) { |
121 | $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); | 121 | $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); |
122 | if (!$vocabulary) { | 122 | if (!$vocabulary) { |
123 | drush_die("CD231A vocabulary not found!"); | 123 | drush_die("CD231A vocabulary not found!"); |
124 | } | 124 | } |
125 | 125 | ||
126 | $term->vid = $vocabulary->vid; | 126 | $term->vid = $vocabulary->vid; |
127 | return $term; | 127 | return $term; |
128 | } | 128 | } |
129 | 129 | ||
130 | function sinkronisasi_import($terms) { | 130 | function sinkronisasi_import($terms) { |
131 | sinkronisasi_import_iterate_insert($terms); | 131 | sinkronisasi_import_iterate_insert($terms); |
132 | sinkronisasi_import_iterate_update($terms); | 132 | sinkronisasi_import_iterate_update($terms); |
133 | } | 133 | } |
134 | 134 | ||
135 | function sinkronisasi_import_iterate_insert($terms) { | 135 | function sinkronisasi_import_iterate_insert($terms) { |
136 | foreach ($terms as $tid => $term) { | 136 | foreach ($terms as $tid => $term) { |
137 | $term = sinkronisasi_fix_vid($term); | 137 | $term = sinkronisasi_fix_vid($term); |
138 | $term = sinkronisasi_delete_taxonomy_term_field_type($term); | 138 | $term = sinkronisasi_delete_taxonomy_term_field_type($term); |
139 | $term = sinkronisasi_set_field_original_tid($term); | 139 | $term = sinkronisasi_set_field_original_tid($term); |
140 | 140 | ||
141 | unset($term->tid); | 141 | unset($term->tid); |
142 | taxonomy_term_save($term); | 142 | taxonomy_term_save($term); |
143 | } | 143 | } |
144 | } | 144 | } |
145 | 145 | ||
146 | function sinkronisasi_import_iterate_update($terms) { | 146 | function sinkronisasi_import_iterate_update($terms) { |
147 | foreach ($terms as $tid => $term) { | 147 | foreach ($terms as $tid => $term) { |
148 | $term = sinkronisasi_fix_vid($term); | 148 | $term = sinkronisasi_fix_vid($term); |
149 | $term = sinkronisasi_taxonomy_term_normalization($term); | 149 | $term = sinkronisasi_taxonomy_term_normalization($term); |
150 | $term = sinkronisasi_set_field_original_tid($term); | 150 | $term = sinkronisasi_set_field_original_tid($term); |
151 | $term = sinkronisasi_pair_with_original($term); | 151 | $term = sinkronisasi_pair_with_original($term); |
152 | $term = sinkronisasi_pair_with_parent($term); | 152 | $term = sinkronisasi_pair_with_parent($term); |
153 | 153 | ||
154 | taxonomy_term_save($term); | 154 | taxonomy_term_save($term); |
155 | } | 155 | } |
156 | } | 156 | } |
157 | 157 | ||
158 | function sinkronisasi_pair_with_original($term) { | 158 | function sinkronisasi_pair_with_original($term) { |
159 | echo $term->tid . "\n"; | ||
159 | $original_term = sinkronisasi_get_term_by_field_original_tid($term->tid); | 160 | $original_term = sinkronisasi_get_term_by_field_original_tid($term->tid); |
160 | if (!$original_term) { | 161 | if (!$original_term) { |
161 | drush_die("ED0129 Error getting original term!"); | 162 | drush_die("ED0129 Error getting original term!"); |
162 | } | 163 | } |
163 | 164 | ||
164 | if (!$original_term->tid) { | 165 | if (!$original_term->tid) { |
165 | drush_die("ED0130 Error getting original term tid!"); | 166 | drush_die("ED0130 Error getting original term tid!"); |
166 | } | 167 | } |
167 | 168 | ||
168 | $term->tid = $original_term->tid; | 169 | $term->tid = $original_term->tid; |
169 | return $term; | 170 | return $term; |
170 | } | 171 | } |
171 | 172 | ||
172 | function sinkronisasi_pair_with_parent($term) { | 173 | function sinkronisasi_pair_with_parent($term) { |
173 | if ($term->_parent) { | 174 | if ($term->_parent) { |
174 | $term->parent = $term->_parent; | 175 | $term->parent = $term->_parent; |
175 | unset($term->_parent); | 176 | unset($term->_parent); |
176 | } | 177 | } |
177 | 178 | ||
178 | return $term; | 179 | return $term; |
179 | } | 180 | } |
180 | 181 | ||
181 | function sinkronisasi_get_term_by_field_original_tid($tid) { | 182 | function sinkronisasi_get_term_by_field_original_tid($tid) { |
182 | $entity_type = "taxonomy_term"; | 183 | $entity_type = "taxonomy_term"; |
183 | 184 | ||
184 | $query = new EntityFieldQuery(); | 185 | $query = new EntityFieldQuery(); |
185 | $query->entityCondition('entity_type', $entity_type); | 186 | $query->entityCondition('entity_type', $entity_type); |
186 | $query->fieldCondition('field_original_tid', 'value', $tid); | 187 | $query->fieldCondition('field_original_tid', 'value', $tid); |
187 | 188 | ||
188 | $result = $query->execute(); | 189 | $result = $query->execute(); |
189 | if (!$result) { | 190 | if (!$result) { |
190 | return; | 191 | return; |
191 | } | 192 | } |
192 | 193 | ||
193 | $tids = array_keys($result[$entity_type]); | 194 | $tids = array_keys($result[$entity_type]); |
194 | $terms = taxonomy_term_load_multiple($tids); | 195 | $terms = taxonomy_term_load_multiple($tids); |
195 | 196 | ||
196 | $term = array_shift($terms); | 197 | $term = array_shift($terms); |
197 | return $term; | 198 | return $term; |
198 | } | 199 | } |
199 | 200 | ||
200 | function sinkronisasi_set_field_original_tid($term) { | 201 | function sinkronisasi_set_field_original_tid($term) { |
201 | $term->field_original_tid['und'][0]['value'] = $term->tid; | 202 | $term->field_original_tid['und'][0]['value'] = $term->tid; |
202 | return $term; | 203 | return $term; |
203 | } | 204 | } |
204 | 205 | ||
205 | function sinkronisasi_export($vocabulary_machine_name) { | 206 | function sinkronisasi_export($vocabulary_machine_name) { |
206 | $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); | 207 | $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); |
207 | $terms = sinkronisasi_get_terms($vocabulary); | 208 | $terms = sinkronisasi_get_terms($vocabulary); |
208 | $terms = sinkronisasi_export_iterate($terms); | 209 | $terms = sinkronisasi_export_iterate($terms); |
209 | 210 | ||
210 | return $terms; | 211 | return $terms; |
211 | } | 212 | } |
212 | 213 | ||
213 | function delete_all_terms_by_vid($vid) { | 214 | function delete_all_terms_by_vid($vid) { |
214 | 215 | ||
215 | $query = new EntityFieldQuery(); | 216 | $query = new EntityFieldQuery(); |
216 | $query->entityCondition('entity_type', 'taxonomy_term'); | 217 | $query->entityCondition('entity_type', 'taxonomy_term'); |
217 | 218 | ||
218 | if (!$vid) { | 219 | if (!$vid) { |
219 | drush_die("Unknown vid"); | 220 | drush_die("Unknown vid"); |
220 | } | 221 | } |
221 | 222 | ||
222 | $query->propertyCondition('vid', $vid); | 223 | $query->propertyCondition('vid', $vid); |
223 | $result = $query->execute(); | 224 | $result = $query->execute(); |
224 | if (!$result) { | 225 | if (!$result) { |
225 | return; | 226 | return; |
226 | } | 227 | } |
227 | 228 | ||
228 | foreach ($result['taxonomy_term'] as $term) { | 229 | foreach ($result['taxonomy_term'] as $term) { |
229 | echo "Deleting "; | 230 | echo "Deleting "; |
230 | echo $term->tid; | 231 | echo $term->tid; |
231 | echo "-> "; | 232 | echo "-> "; |
232 | echo taxonomy_term_delete($term->tid); | 233 | echo taxonomy_term_delete($term->tid); |
233 | echo "\n"; | 234 | echo "\n"; |
234 | } | 235 | } |
235 | } | 236 | } |
236 | 237 |