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