sinkronisasi.include.php 7.33 KB
<?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_export_iterate($terms) {
    foreach ($terms as $tid => $term) {
        unset($term->rdf_mapping);
        $term = sinkronisasi_taxonomy_term_denormalization($term);
        $term = sinkronisasi_export_set_parent($term);

        $terms[$tid] = $term;
    }
    return $terms;
}

function sinkronisasi_export_set_parent($term) {
    $parent_terms = taxonomy_get_parents($term->tid);
    $parent_term = array_shift($parent_terms);
    $term->_parent = $parent_term->tid;

    return $term;
}

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") {
            $vocabulary = $field["settings"]["allowed_values"][0]["vocabulary"];

            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;
                        $term->{"$field_name"}[$language][$idx]['_vocabulary'] = $vocabulary;
                    }
                }
            }
        }

    }
    return $term;
}

function sinkronisasi_delete_taxonomy_term_field_type($term) {
    foreach ($term as $field_name => $field_value) {

        $field = field_info_field($field_name);


        if (!$field) continue;

        if ($field['type'] == "taxonomy_term_reference") {
            unset($term->{"$field_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'], $value['_vocabulary']);
                    $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']) {
                            echo $field_name . "\n";
                            echo $foreign_term->name . " != " . $value['_name'] . "\n";
                            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) {
        var_dump($term);
        echo "\n";
        drush_die("CD231A vocabulary not found!");
    }

    $term->vid = $vocabulary->vid;
    return $term;
}

function sinkronisasi_import($terms) {
    sinkronisasi_import_iterate_insert($terms);
    sinkronisasi_import_iterate_update($terms);
}

function sinkronisasi_import_iterate_insert($terms) {
    $i = 0;
    foreach ($terms as $tid => $term) {
        $i++;

        echo $term->vocabulary_machine_name . " $i# ORIGINAL $tid: " . $term->name . "\n";

        $term = sinkronisasi_fix_vid($term);
        $term = sinkronisasi_delete_taxonomy_term_field_type($term);
        $term = sinkronisasi_set_field_original_tid($term);

        unset($term->tid);

        $status_code = taxonomy_term_save($term);
        if ($status_code != SAVED_NEW) {
            drush_die("Ada kesalahan saat menyimpan data baru.");
        }

        echo "ORIGINAL: $tid LOCAL: " . $term->tid . " inserted.\n";

    }
}

function sinkronisasi_import_iterate_update($terms) {
    $i = 0;
    foreach ($terms as $tid => $term) {
        $i++;

        echo $term->vocabulary_machine_name . " $i# $tid: " . $term->name . "\n";

        $term = sinkronisasi_fix_vid($term);
        $term = sinkronisasi_taxonomy_term_normalization($term);
        $term = sinkronisasi_set_field_original_tid($term);
        $term = sinkronisasi_pair_with_local_tid($term);
        $term = sinkronisasi_pair_with_parent($term);

        taxonomy_term_save($term);
        $status_code = taxonomy_term_save($term);
        if ($status_code != SAVED_UPDATED) {
            drush_die("Ada kesalahan saat mengupdate data.");
        }

        echo "ORIGINAL: $tid LOCAL: " . $term->tid . " updated.\n";

    }
}

function sinkronisasi_pair_with_local_tid($term) {
    $tid = $term->tid;
    $original_term = sinkronisasi_get_term_by_field_original_tid($tid);
    if (!$original_term) {
        drush_die("ED0129 Error getting term with original tid is $tid!\n");
    }

    $term->tid = $original_term->tid;
    return $term;
}

function sinkronisasi_pair_with_parent($term) {
    if ($term->_parent) {
        $parent_term = sinkronisasi_get_term_by_field_original_tid($term->_parent);
        if ($parent_term) {
            $term->parent = $parent_term->tid;
        }
        unset($term->_parent);
    }

    return $term;
}

function sinkronisasi_get_term_by_field_original_tid($tid) {
    $entity_type = "taxonomy_term";

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', $entity_type);
    $query->fieldCondition('field_original_tid', 'value', $tid);

    $result = $query->execute();
    if (!$result) {
        return;
    }

    $tids = array_keys($result[$entity_type]);
    $terms = taxonomy_term_load_multiple($tids);

    $term = array_shift($terms);
    return $term;
}

function sinkronisasi_set_field_original_tid($term) {
    $term->field_original_tid['und'][0]['value'] = $term->tid;
    return $term;
}

function sinkronisasi_export($vocabulary_machine_name) {
    $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
    $terms = sinkronisasi_get_terms($vocabulary);
    $terms = sinkronisasi_export_iterate($terms);

    return $terms;
}

function delete_all_terms_by_vid($vid) {

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'taxonomy_term');

    if (!$vid) {
        drush_die("Unknown vid");
    }

    $query->propertyCondition('vid', $vid);
    $result = $query->execute();
    if (!$result) {
        return;
    }

    foreach ($result['taxonomy_term'] as $term) {
        echo "Deleting " . $term->tid;
        echo ", status: ";
        echo taxonomy_term_delete($term->tid);
        echo "\n";
    }
}