Commit affef26bb6df954a9a5cea7ddd45a823d9406233
1 parent
750d1f80b5
Exists in
master
debug
Showing 1 changed file with 0 additions and 3 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_taxonomy_term_normalization($term) { | 68 | function sinkronisasi_taxonomy_term_normalization($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 | 77 | ||
78 | foreach ($field_value as $language => $values_per_language) { | 78 | foreach ($field_value as $language => $values_per_language) { |
79 | foreach ($values_per_language as $idx => $value) { | 79 | foreach ($values_per_language as $idx => $value) { |
80 | 80 | ||
81 | if (empty($value['_name'])) { | 81 | if (empty($value['_name'])) { |
82 | continue; | 82 | continue; |
83 | } | 83 | } |
84 | 84 | ||
85 | $foreign_terms = taxonomy_get_term_by_name($value['_name']); | 85 | $foreign_terms = taxonomy_get_term_by_name($value['_name']); |
86 | $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms)); | 86 | $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms)); |
87 | $foreign_term = array_shift($foreign_terms); | 87 | $foreign_term = array_shift($foreign_terms); |
88 | 88 | ||
89 | if (isset($foreign_term->name)) { | 89 | if (isset($foreign_term->name)) { |
90 | if ($foreign_term->name != $value['_name']) { | 90 | if ($foreign_term->name != $value['_name']) { |
91 | drush_die("213AB2 foreign term name is not match!"); | 91 | drush_die("213AB2 foreign term name is not match!"); |
92 | } | 92 | } |
93 | 93 | ||
94 | $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid; | 94 | $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid; |
95 | } | 95 | } |
96 | } | 96 | } |
97 | } | 97 | } |
98 | } | 98 | } |
99 | 99 | ||
100 | } | 100 | } |
101 | return $term; | 101 | return $term; |
102 | } | 102 | } |
103 | 103 | ||
104 | 104 | ||
105 | function sinkronisasi_fix_vid($term) { | 105 | function sinkronisasi_fix_vid($term) { |
106 | $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); | 106 | $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); |
107 | if (!$vocabulary) { | 107 | if (!$vocabulary) { |
108 | drush_die("CD231A vocabulary not found!"); | 108 | drush_die("CD231A vocabulary not found!"); |
109 | } | 109 | } |
110 | 110 | ||
111 | $term->vid = $vocabulary->vid; | 111 | $term->vid = $vocabulary->vid; |
112 | return $term; | 112 | return $term; |
113 | } | 113 | } |
114 | 114 | ||
115 | function sinkronisasi_import($terms) { | 115 | function sinkronisasi_import($terms) { |
116 | sinkronisasi_import_iterate($terms); | 116 | sinkronisasi_import_iterate($terms); |
117 | } | 117 | } |
118 | 118 | ||
119 | function sinkronisasi_import_iterate($terms) { | 119 | function sinkronisasi_import_iterate($terms) { |
120 | foreach ($terms as $tid => $term) { | 120 | foreach ($terms as $tid => $term) { |
121 | $term = sinkronisasi_fix_vid($term); | 121 | $term = sinkronisasi_fix_vid($term); |
122 | $term = sinkronisasi_taxonomy_term_normalization($term); | 122 | $term = sinkronisasi_taxonomy_term_normalization($term); |
123 | $term = sinkronisasi_set_field_original_tid($term); | 123 | $term = sinkronisasi_set_field_original_tid($term); |
124 | 124 | ||
125 | unset($term->tid); | 125 | unset($term->tid); |
126 | taxonomy_term_save($term); | 126 | taxonomy_term_save($term); |
127 | } | 127 | } |
128 | } | 128 | } |
129 | 129 | ||
130 | function sinkronisasi_get_term_by_field_original_tid($tid) { | 130 | function sinkronisasi_get_term_by_field_original_tid($tid) { |
131 | $entity_type = "taxonomy_term"; | 131 | $entity_type = "taxonomy_term"; |
132 | 132 | ||
133 | $query = new EntityFieldQuery(); | 133 | $query = new EntityFieldQuery(); |
134 | $query->entityCondition('entity_type', $entity_type); | 134 | $query->entityCondition('entity_type', $entity_type); |
135 | $query->fieldCondition('field_original_tid', 'value', $tid); | 135 | $query->fieldCondition('field_original_tid', 'value', $tid); |
136 | 136 | ||
137 | $result = $query->execute(); | 137 | $result = $query->execute(); |
138 | if (!$result) { | 138 | if (!$result) { |
139 | return; | 139 | return; |
140 | } | 140 | } |
141 | 141 | ||
142 | $tids = array_keys($result[$entity_type]); | 142 | $tids = array_keys($result[$entity_type]); |
143 | $terms = taxonomy_term_load_multiple($tids); | 143 | $terms = taxonomy_term_load_multiple($tids); |
144 | 144 | ||
145 | $term = array_shift($terms); | 145 | $term = array_shift($terms); |
146 | return $term; | 146 | return $term; |
147 | } | 147 | } |
148 | 148 | ||
149 | function sinkronisasi_set_field_original_tid($term) { | 149 | function sinkronisasi_set_field_original_tid($term) { |
150 | $term->field_original_tid['und'][0]['value'] = $term->tid; | 150 | $term->field_original_tid['und'][0]['value'] = $term->tid; |
151 | return $term; | 151 | return $term; |
152 | } | 152 | } |
153 | 153 | ||
154 | function sinkronisasi_export($vocabulary_machine_name) { | 154 | function sinkronisasi_export($vocabulary_machine_name) { |
155 | $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); | 155 | $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); |
156 | $terms = sinkronisasi_get_terms($vocabulary); | 156 | $terms = sinkronisasi_get_terms($vocabulary); |
157 | $terms = sinkronisasi_export_iterate($terms); | 157 | $terms = sinkronisasi_export_iterate($terms); |
158 | 158 | ||
159 | return $terms; | 159 | return $terms; |
160 | } | 160 | } |
161 | 161 | ||
162 | function delete_all_terms_by_vid($vid) { | 162 | function delete_all_terms_by_vid($vid) { |
163 | 163 | ||
164 | $query = new EntityFieldQuery(); | 164 | $query = new EntityFieldQuery(); |
165 | $query->entityCondition('entity_type', 'taxonomy_term'); | 165 | $query->entityCondition('entity_type', 'taxonomy_term'); |
166 | 166 | ||
167 | if (!$vid) { | 167 | if (!$vid) { |
168 | drush_die("Unknown vid"); | 168 | drush_die("Unknown vid"); |
169 | } | 169 | } |
170 | 170 | ||
171 | $vocabulary = taxonomy_vocabulary_load($vid); | ||
172 | var_dump($vocabulary); die; | ||
173 | |||
174 | $query->propertyCondition('vid', $vid); | 171 | $query->propertyCondition('vid', $vid); |
175 | $result = $query->execute(); | 172 | $result = $query->execute(); |
176 | if (!$result) { | 173 | if (!$result) { |
177 | return; | 174 | return; |
178 | } | 175 | } |
179 | 176 | ||
180 | foreach ($result['taxonomy_term'] as $term) { | 177 | foreach ($result['taxonomy_term'] as $term) { |
181 | echo "Deleting "; | 178 | echo "Deleting "; |
182 | echo $term->tid; | 179 | echo $term->tid; |
183 | echo "-> "; | 180 | echo "-> "; |
184 | echo taxonomy_term_delete($term->tid); | 181 | echo taxonomy_term_delete($term->tid); |
185 | echo "\n"; | 182 | echo "\n"; |
186 | } | 183 | } |
187 | } | 184 | } |
188 | 185 |