}
/** gọi tệp tin admin base */
require_once dirname(__FILE__) . '/admin.php';
/** gọi model xử lý taxonomy */
require_once dirname(__FILE__) . '/taxonomy/taxonomy_model.php';
if ($_SERVER['HTTP_HOST'] != parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST)) {
    hm_exit('403 - Truy cập bị từ chối');
}
$key = hm_get('key');
$id = hm_get('id');
$action = hm_get('action');
switch ($action) {
    case 'data':
        $status = hm_get('status', 'public');
        $perpage = hm_get('perpage', '30');
        echo taxonomy_show_data($key, $status, $perpage);
        break;
    case 'add':
        /** Thực hiện thêm taxonomy */
        echo taxonomy_ajax_add($key);
        break;
    case 'edit':
        /** Thực hiện sửa taxonomy */
        echo taxonomy_ajax_edit($id);
        break;
    case 'draft':
        /** Thực hiện xóa taxonomy */
        taxonomy_update_val(array('id' => hm_post('id'), 'value' => array('status' => MySQL::SQLValue('draft'))));
        break;
    case 'delete_permanently':
        /** Thực hiện xóa vĩnh viễn taxonomy */
function taxonomy_ajax_add($key, $id_update = NULL)
{
    global $hmtaxonomy;
    $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET);
    hook_action('taxonomy_ajax_add');
    $tax = $hmtaxonomy->hmtaxonomy;
    if (isset($tax[$key])) {
        $this_tax = $tax[$key];
        foreach ($_POST as $post_key => $post_val) {
            $primary = $this_tax['taxonomy_field']['primary_name_field']['name'];
            /** input này được dùng làm khóa tên chính */
            if ($post_key == $primary) {
                $tax_name = $post_val;
                $parent = hm_post('parent');
                if (!is_numeric($parent)) {
                    $parent = 0;
                }
                if ($parent == $id_update) {
                    $parent = 0;
                }
                if (isset($_POST['slug_of_' . $post_key])) {
                    if (is_numeric($id_update)) {
                        $tax_slug = $_POST['slug_of_' . $post_key];
                    } else {
                        $tax_slug = add_request_uri_custom($_POST['slug_of_' . $post_key]);
                    }
                } else {
                    switch ($_POST['accented_of_' . $post_key]) {
                        case 'true':
                            if (is_numeric($id_update)) {
                                $tax_slug = sanitize_title_with_accented($post_val);
                            } else {
                                $tax_slug = add_request_uri_with_accented($post_val);
                            }
                            break;
                        case 'false':
                            if (is_numeric($id_update)) {
                                $tax_slug = sanitize_title($post_val);
                            } else {
                                $tax_slug = add_request_uri($post_val);
                            }
                            break;
                        default:
                            if (is_numeric($id_update)) {
                                $tax_slug = sanitize_title($post_val);
                            } else {
                                $tax_slug = add_request_uri($post_val);
                            }
                    }
                }
                /** lưu taxonomy vào data base */
                $tableName = DB_PREFIX . 'taxonomy';
                $values["name"] = MySQL::SQLValue($tax_name);
                $values["slug"] = MySQL::SQLValue($tax_slug);
                $values["key"] = MySQL::SQLValue($key);
                $values["parent"] = MySQL::SQLValue($parent);
                $values["status"] = MySQL::SQLValue('public');
                if (is_numeric($id_update)) {
                    unset($values["status"]);
                    $whereArray = array('id' => $id_update);
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                    $insert_id = $id_update;
                    up_date_request_uri_object($tax_slug, $insert_id, 'taxonomy', 'has_object');
                } else {
                    $insert_id = $hmdb->InsertRow($tableName, $values);
                    up_date_request_uri_object($tax_slug, $insert_id, 'taxonomy', 'null_object');
                }
                $latest_array = array('id' => $insert_id, 'name' => $tax_name, 'slug' => $tax_slug, 'key' => $key, 'parent' => $parent);
                unset($values);
            }
        }
        foreach ($_POST as $post_key => $post_val) {
            /** lưu taxonomy field vào data */
            if (is_numeric($insert_id)) {
                $tableName = DB_PREFIX . 'field';
                $values["name"] = MySQL::SQLValue($post_key);
                $values["val"] = MySQL::SQLValue($post_val);
                $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
                $values["object_type"] = MySQL::SQLValue('taxonomy');
                if (is_numeric($id_update)) {
                    $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'object_type' => MySQL::SQLValue('taxonomy'), 'name' => MySQL::SQLValue($post_key));
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                } else {
                    $hmdb->InsertRow($tableName, $values);
                }
                unset($values);
            }
        }
        /** show table */
        $status = hm_get('status', 'public');
        $perpage = hm_get('perpage', '30');
        $return_array = taxonomy_show_data($key, $status, $perpage, false);
        $return_array['latest'] = $latest_array;
        return json_encode($return_array, true);
    }
}