Commit 3e4a3ce355b151a3c741a1296474cdbf75aa5e6e
1 parent
e92b25d6e0
Exists in
master
insert update
Showing 2 changed files with 35 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 | 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) { | ||
69 | foreach ($term as $field_name => $field_value) { | ||
70 | |||
71 | $field = field_info_field($field_name); | ||
72 | |||
73 | |||
74 | if (!$field) continue; | ||
75 | |||
76 | if ($field['type'] == "taxonomy_term_reference") { | ||
77 | unset($term->{"$field_name"}); | ||
78 | } | ||
79 | } | ||
80 | return $term; | ||
81 | } | ||
82 | |||
68 | function sinkronisasi_taxonomy_term_normalization($term) { | 83 | function sinkronisasi_taxonomy_term_normalization($term) { |
69 | foreach ($term as $field_name => $field_value) { | 84 | foreach ($term as $field_name => $field_value) { |
70 | 85 | ||
71 | $field = field_info_field($field_name); | 86 | $field = field_info_field($field_name); |
72 | 87 | ||
73 | 88 | ||
74 | if (!$field) continue; | 89 | if (!$field) continue; |
75 | 90 | ||
76 | if ($field['type'] == "taxonomy_term_reference") { | 91 | if ($field['type'] == "taxonomy_term_reference") { |
77 | 92 | ||
78 | foreach ($field_value as $language => $values_per_language) { | 93 | foreach ($field_value as $language => $values_per_language) { |
79 | foreach ($values_per_language as $idx => $value) { | 94 | foreach ($values_per_language as $idx => $value) { |
80 | 95 | ||
81 | if (empty($value['_name'])) { | 96 | if (empty($value['_name'])) { |
82 | continue; | 97 | continue; |
83 | } | 98 | } |
84 | 99 | ||
85 | $foreign_terms = taxonomy_get_term_by_name($value['_name']); | 100 | $foreign_terms = taxonomy_get_term_by_name($value['_name']); |
86 | $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms)); | 101 | $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms)); |
87 | $foreign_term = array_shift($foreign_terms); | 102 | $foreign_term = array_shift($foreign_terms); |
88 | 103 | ||
89 | if (isset($foreign_term->name)) { | 104 | if (isset($foreign_term->name)) { |
90 | if ($foreign_term->name != $value['_name']) { | 105 | if ($foreign_term->name != $value['_name']) { |
91 | drush_die("213AB2 foreign term name is not match!"); | 106 | drush_die("213AB2 foreign term name is not match!"); |
92 | } | 107 | } |
93 | 108 | ||
94 | $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid; | 109 | $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid; |
95 | } | 110 | } |
96 | } | 111 | } |
97 | } | 112 | } |
98 | } | 113 | } |
99 | 114 | ||
100 | } | 115 | } |
101 | return $term; | 116 | return $term; |
102 | } | 117 | } |
103 | 118 | ||
104 | 119 | ||
105 | function sinkronisasi_fix_vid($term) { | 120 | function sinkronisasi_fix_vid($term) { |
106 | $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); | 121 | $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); |
107 | if (!$vocabulary) { | 122 | if (!$vocabulary) { |
108 | drush_die("CD231A vocabulary not found!"); | 123 | drush_die("CD231A vocabulary not found!"); |
109 | } | 124 | } |
110 | 125 | ||
111 | $term->vid = $vocabulary->vid; | 126 | $term->vid = $vocabulary->vid; |
112 | return $term; | 127 | return $term; |
113 | } | 128 | } |
114 | 129 | ||
115 | function sinkronisasi_import($terms) { | 130 | function sinkronisasi_import($terms) { |
116 | sinkronisasi_import_iterate($terms); | 131 | sinkronisasi_import_iterate_insert($terms); |
132 | sinkronisasi_import_iterate_update($terms); | ||
117 | } | 133 | } |
118 | 134 | ||
119 | function sinkronisasi_import_iterate($terms) { | 135 | function sinkronisasi_import_iterate_insert($terms) { |
120 | foreach ($terms as $tid => $term) { | 136 | foreach ($terms as $tid => $term) { |
121 | $term = sinkronisasi_fix_vid($term); | 137 | $term = sinkronisasi_fix_vid($term); |
122 | $term = sinkronisasi_taxonomy_term_normalization($term); | 138 | $term = sinkronisasi_delete_taxonomy_term_field_type($term); |
123 | $term = sinkronisasi_set_field_original_tid($term); | 139 | $term = sinkronisasi_set_field_original_tid($term); |
124 | 140 | ||
125 | unset($term->tid); | 141 | unset($term->tid); |
126 | taxonomy_term_save($term); | 142 | taxonomy_term_save($term); |
127 | } | 143 | } |
128 | } | 144 | } |
129 | 145 | ||
146 | function sinkronisasi_import_iterate_update($terms) { | ||
147 | foreach ($terms as $tid => $term) { | ||
148 | $term = sinkronisasi_fix_vid($term); | ||
149 | $term = sinkronisasi_taxonomy_term_normalization($term); | ||
150 | $term = sinkronisasi_set_field_original_tid($term); | ||
151 | |||
152 | $original_term = sinkronisasi_get_term_by_field_original_tid($tid); | ||
153 | if (!$original_term) { | ||
154 | drush_die("ED0129 Error getting original term!"); | ||
155 | } | ||
156 | |||
157 | #taxonomy_term_save($term); | ||
158 | } | ||
159 | } | ||
160 | |||
130 | function sinkronisasi_get_term_by_field_original_tid($tid) { | 161 | function sinkronisasi_get_term_by_field_original_tid($tid) { |
131 | $entity_type = "taxonomy_term"; | 162 | $entity_type = "taxonomy_term"; |
132 | 163 | ||
133 | $query = new EntityFieldQuery(); | 164 | $query = new EntityFieldQuery(); |
134 | $query->entityCondition('entity_type', $entity_type); | 165 | $query->entityCondition('entity_type', $entity_type); |
135 | $query->fieldCondition('field_original_tid', 'value', $tid); | 166 | $query->fieldCondition('field_original_tid', 'value', $tid); |
136 | 167 | ||
137 | $result = $query->execute(); | 168 | $result = $query->execute(); |
138 | if (!$result) { | 169 | if (!$result) { |
139 | return; | 170 | return; |
140 | } | 171 | } |
141 | 172 | ||
142 | $tids = array_keys($result[$entity_type]); | 173 | $tids = array_keys($result[$entity_type]); |
143 | $terms = taxonomy_term_load_multiple($tids); | 174 | $terms = taxonomy_term_load_multiple($tids); |
144 | 175 | ||
145 | $term = array_shift($terms); | 176 | $term = array_shift($terms); |
146 | return $term; | 177 | return $term; |
147 | } | 178 | } |
148 | 179 | ||
149 | function sinkronisasi_set_field_original_tid($term) { | 180 | function sinkronisasi_set_field_original_tid($term) { |
150 | $term->field_original_tid['und'][0]['value'] = $term->tid; | 181 | $term->field_original_tid['und'][0]['value'] = $term->tid; |
151 | return $term; | 182 | return $term; |
152 | } | 183 | } |
153 | 184 | ||
154 | function sinkronisasi_export($vocabulary_machine_name) { | 185 | function sinkronisasi_export($vocabulary_machine_name) { |
155 | $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); | 186 | $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); |
156 | $terms = sinkronisasi_get_terms($vocabulary); | 187 | $terms = sinkronisasi_get_terms($vocabulary); |
157 | $terms = sinkronisasi_export_iterate($terms); | 188 | $terms = sinkronisasi_export_iterate($terms); |
158 | 189 | ||
159 | return $terms; | 190 | return $terms; |
160 | } | 191 | } |
161 | 192 | ||
162 | function delete_all_terms_by_vid($vid) { | 193 | function delete_all_terms_by_vid($vid) { |
163 | 194 | ||
164 | $query = new EntityFieldQuery(); | 195 | $query = new EntityFieldQuery(); |
165 | $query->entityCondition('entity_type', 'taxonomy_term'); | 196 | $query->entityCondition('entity_type', 'taxonomy_term'); |
166 | 197 | ||
167 | if (!$vid) { | 198 | if (!$vid) { |
168 | drush_die("Unknown vid"); | 199 | drush_die("Unknown vid"); |
169 | } | 200 | } |
170 | 201 | ||
171 | $query->propertyCondition('vid', $vid); | 202 | $query->propertyCondition('vid', $vid); |
172 | $result = $query->execute(); | 203 | $result = $query->execute(); |
173 | if (!$result) { | 204 | if (!$result) { |
174 | return; | 205 | return; |
175 | } | 206 | } |
176 | 207 | ||
177 | foreach ($result['taxonomy_term'] as $term) { | 208 | foreach ($result['taxonomy_term'] as $term) { |
178 | echo "Deleting "; | 209 | echo "Deleting "; |
179 | echo $term->tid; | 210 | echo $term->tid; |
180 | echo "-> "; | 211 | echo "-> "; |
181 | echo taxonomy_term_delete($term->tid); | 212 | echo taxonomy_term_delete($term->tid); |
182 | echo "\n"; | 213 | echo "\n"; |
183 | } | 214 | } |
184 | } | 215 | } |
185 | 216 |
sinkronisasi_import.php
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | require("sinkronisasi.include.php"); | 3 | require("sinkronisasi.include.php"); |
4 | 4 | ||
5 | if (function_exists('drush_main')) { | 5 | if (function_exists('drush_main')) { |
6 | $machine_name = "jenis_kantor"; | 6 | $machine_name = "jenis_kantor"; |
7 | 7 | ||
8 | $vocabulary = taxonomy_vocabulary_machine_name_load($machine_name); | 8 | $vocabulary = taxonomy_vocabulary_machine_name_load($machine_name); |
9 | |||
10 | delete_all_terms_by_vid($vocabulary->vid); | 9 | delete_all_terms_by_vid($vocabulary->vid); |
11 | 10 | ||
12 | $data = file_get_contents("adhisimon/sinkronisasi-drupal/$machine_name.txt"); | 11 | $data = file_get_contents("adhisimon/sinkronisasi-drupal/$machine_name.txt"); |
13 | $terms = unserialize($data); | 12 | $terms = unserialize($data); |
13 | |||
14 | sinkronisasi_import($terms); | 14 | sinkronisasi_import($terms); |
15 | } | 15 | } |