sinkronisasi.include.php
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
function sinkronisasi_get_terms($vocabulary) {
$entity_type = "taxonomy_term";
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type);
$query->propertyCondition('vid', $vocabulary->vid);
$result = $query->execute();
if (!$result) {
return;
}
$tids = array_keys($result[$entity_type]);
$terms = taxonomy_term_load_multiple($tids);
return $terms;
}
function sinkronisasi_denormalization($terms) {
foreach ($terms as $tid => $term) {
if (preg_match("/KP2KP Kendal/", $term->name)) {
$parents = taxonomy_get_parents($term->tid);
var_dump($parents); die;
}
$terms[$tid] = sinkronisasi_taxonomy_term_denormalization($term);
}
return $terms;
}
function sinkronisasi_taxonomy_term_denormalization($term) {
foreach ($term as $field_name => $field_value) {
$field = field_info_field($field_name);
if (!$field) continue;
if ($field['type'] == "taxonomy_term_reference") {
foreach ($field_value as $language => $values_per_language) {
foreach ($values_per_language as $idx => $value) {
if (empty($value['tid'])) {
continue;
}
$foreign_term = taxonomy_term_load($value['tid']);
if (isset($foreign_term->name)) {
$term->{"$field_name"}[$language][$idx]['_name'] = $foreign_term->name;
}
}
}
}
}
return $term;
}
function sinkronisasi_taxonomy_term_normalization($term) {
foreach ($term as $field_name => $field_value) {
$field = field_info_field($field_name);
if (!$field) continue;
if ($field['type'] == "taxonomy_term_reference") {
foreach ($field_value as $language => $values_per_language) {
foreach ($values_per_language as $idx => $value) {
if (empty($value['_name'])) {
continue;
}
$foreign_terms = taxonomy_get_term_by_name($value['_name']);
$foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms));
$foreign_term = array_shift($foreign_terms);
if (isset($foreign_term->name)) {
if ($foreign_term->name != $value['_name']) {
drush_die("213AB2 foreign term name is not match!");
}
$term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid;
}
}
}
}
}
return $term;
}
function sinkronisasi_fix_vid($term) {
$vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name);
if (!$vocabulary) {
drush_die("CD231A vocabulary not found!");
}
$term->vid = $vocabulary->vid;
return $term;
}
function sinkronisasi_import_terms($terms) {
foreach ($terms as $tid => $term) {
#$term = sinkronisasi_fix_vid($term);
$term = sinkronisasi_taxonomy_term_normalization($term);
if (preg_match("/KP2KP/", $term->name)) {
var_dump($term);
die;
}
}
}
function sinkronisasi_export($vocabulary_machine_name) {
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
$terms = sinkronisasi_get_terms($vocabulary);
$terms = sinkronisasi_denormalization($terms);
return $terms;
}
function sinkronisasi_export_serialized($vocabulary_machine_name) {
$terms = sinkronisasi_export($vocabulary_machine_name);
echo serialize($terms);
}
function sinkronisasi_import_from_serialized_file($filename) {
$terms = file_get_contents($filename);
$terms = unserialize($terms);
sinkronisasi_import_terms($terms);
}