Commit 72317c0af5d0159b77d3dec20f8ce40a93ab2895

Authored by Adhidarma Hadiwinoto
1 parent 18fccb501b
Exists in master

term save

Showing 1 changed file with 9 additions and 4 deletions Inline Diff

moongate2kantorterms.php
1 <?php 1 <?php
2 2
3 function mg2kt_moongate_offices() { 3 function mg2kt_moongate_offices() {
4 drush_log("Getting office data from moon_gate_pegawai table", "info"); 4 drush_log("Getting office data from moon_gate_pegawai table", "info");
5 5
6 $result = db_query(" 6 $result = db_query("
7 SELECT 7 SELECT
8 kd_kantor, nm_kantor, 8 kd_kantor, nm_kantor,
9 kd_jns_kantor, jns_kantor, 9 kd_jns_kantor, jns_kantor,
10 kd_kpp 10 kd_kpp
11 FROM moon_gate_pegawai 11 FROM moon_gate_pegawai
12 GROUP BY kd_kantor 12 GROUP BY kd_kantor
13 ORDER BY modified DESC 13 ORDER BY modified DESC
14 "); 14 ");
15 15
16 return $result; 16 return $result;
17 } 17 }
18 18
19 function mg2kt_vocabulary_kantor() { 19 function mg2kt_vocabulary_kantor() {
20 static $vocabulary = null; 20 static $vocabulary = null;
21 21
22 if (!$vocabulary) { 22 if (!$vocabulary) {
23 drush_log("Load vocabulary information", "info"); 23 drush_log("Load vocabulary information", "info");
24 $vocabulary = taxonomy_vocabulary_machine_name_load("kantor"); 24 $vocabulary = taxonomy_vocabulary_machine_name_load("kantor");
25 } 25 }
26 return $vocabulary; 26 return $vocabulary;
27 } 27 }
28 28
29 function mg2kt_load_term($kd_kantor) { 29 function mg2kt_load_term($kd_kantor) {
30 $vocabulary = mg2kt_vocabulary_kantor(); 30 $vocabulary = mg2kt_vocabulary_kantor();
31 31
32 $query = new EntityFieldQuery(); 32 $query = new EntityFieldQuery();
33 $query->entityCondition('entity_type', "taxonomy_term"); 33 $query->entityCondition('entity_type', "taxonomy_term");
34 $query->propertyCondition('vid', $vocabulary->vid); 34 $query->propertyCondition('vid', $vocabulary->vid);
35 $query->fieldCondition('field_kd_kantor', 'value', $kd_kantor); 35 $query->fieldCondition('field_kd_kantor', 'value', $kd_kantor);
36 36
37 37
38 $result = $query->execute(); 38 $result = $query->execute();
39 if (!$result) { 39 if (!$result) {
40 return; 40 return;
41 } 41 }
42 42
43 $tids = array_keys($result["taxonomy_term"]); 43 $tids = array_keys($result["taxonomy_term"]);
44 $terms = taxonomy_term_load_multiple($tids); 44 $terms = taxonomy_term_load_multiple($tids);
45 45
46 $term = array_shift($terms); 46 $term = array_shift($terms);
47 return $term; 47 return $term;
48 } 48 }
49 49
50 function mg2kt_field_value($entity, $field_name, $language = "und") { 50 function mg2kt_field_value($entity, $field_name, $language = "und") {
51 return $entity->{"$field_name"}[$language][0]['value']; 51 return $entity->{"$field_name"}[$language][0]['value'];
52 } 52 }
53 53
54 ### MAIN ### 54 ### MAIN ###
55 if (!function_exists("drush_main")) { 55 if (!function_exists("drush_main")) {
56 return; 56 return;
57 } 57 }
58 58
59 $offices = mg2kt_moongate_offices(); 59 $offices = mg2kt_moongate_offices();
60 60
61 while ($office = $offices->fetchAssoc()) { 61 while ($office = $offices->fetchAssoc()) {
62 drush_log($office['kd_kantor'] . " " . $office['nm_kantor'], "info"); 62 drush_log($office['kd_kantor'] . " " . $office['nm_kantor'], "info");
63 63
64 $modified = false;
65
64 $term = mg2kt_load_term($office['kd_kantor']); 66 $term = mg2kt_load_term($office['kd_kantor']);
65 if (!$term) { 67 if (!$term) {
66 drush_log("Missing term detected on " . $office['kd_kantor'] . " " . $office['nm_kantor'], "warning"); 68 drush_log("Missing term detected on " . $office['kd_kantor'] . " " . $office['nm_kantor'], "warning");
67 continue; 69 continue;
68 } 70 }
69 71
70 if ($office['kd_jns_kantor'] != mg2kt_field_value($term, 'field_kode_jenis_kantor')) { 72 if ($office['kd_jns_kantor'] != mg2kt_field_value($term, 'field_kode_jenis_kantor')) {
71 drush_log("JNS inconsistency detected on " . $office['kd_kantor'] . " " . $office['nm_kantor'], "warning"); 73 drush_log("JNS inconsistency detected on " . $office['kd_kantor'] . " " . $office['nm_kantor'], "warning");
72 drush_log("moongate: " . $office['kd_jns_kantor'] . " taxonomy: " . mg2kt_field_value($term, 'field_kode_jenis_kantor'), "warning"); 74 drush_log("moongate: " . $office['kd_jns_kantor'] . " taxonomy: " . mg2kt_field_value($term, 'field_kode_jenis_kantor'), "warning");
73 continue; 75 continue;
74 } 76 }
75 77
76 if ($office['kd_kpp'] != mg2kt_field_value($term, 'field_kd_kpp')) { 78 if ($office['kd_kpp'] != mg2kt_field_value($term, 'field_kd_kpp')) {
77 drush_log("KD_KPP inconsistency detected on " . $office['kd_kantor'] . " " . $office['nm_kantor'], "warning"); 79 drush_log("KD_KPP inconsistency detected on " . $office['kd_kantor'] . " " . $office['nm_kantor'], "warning");
78 drush_log("moongate: " . $office['kd_kpp'] . " taxonomy: " . mg2kt_field_value($term, 'field_kd_kpp'), "warning"); 80 drush_log("moongate: " . $office['kd_kpp'] . " taxonomy: " . mg2kt_field_value($term, 'field_kd_kpp'), "warning");
79 } 81 }
80 82
81 if ($office['nm_kantor'] != $term->name) { 83 if ($office['nm_kantor'] != $term->name) {
82 drush_log('Modified name detected', "notice"); 84 drush_log('Modified name detected', "notice");
83 drush_log('Old name: "' . $term->name . '"', "notice"); 85 drush_log('Old name: "' . $term->name . '"', "notice");
84 drush_log('New name: "' . $office["nm_kantor"] . '"', "notice"); 86 drush_log('New name: "' . $office["nm_kantor"] . '"', "notice");
85 87
86 drush_log("Replacing old name"); 88 drush_log("Replacing old name", "notice");
89
87 $term->name = $office['nm_kantor']; 90 $term->name = $office['nm_kantor'];
91 $modified = true;
88 } 92 }
89 93
90 94 if ($modified) {
91 95 drush_log("Saving modified data", "ok");
92 96 taxonomy_term_save($term);
97 }
93 } 98 }
94 99