function content_ajax_add($args = array())
{
    global $hmcontent;
    $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET);
    hook_action('content_ajax_add');
    if (isset($args['content_key'])) {
        $key = $args['content_key'];
    } else {
        $key = NULL;
    }
    if (isset($args['id_update'])) {
        $id_update = $args['id_update'];
    } else {
        $id_update = NULL;
    }
    if (isset($args['parent'])) {
        $parent = $args['parent'];
    } else {
        $parent = 0;
    }
    if (isset($args['status'])) {
        $status = $args['status'];
    } else {
        $status = 'public';
    }
    $con = $hmcontent->hmcontent;
    if (isset($_POST['taxonomy'])) {
        $method_post_taxonomy = $_POST['taxonomy'];
    } else {
        $method_post_taxonomy = array();
    }
    if ($key != NULL and isset($con[$key])) {
        $this_con = $con[$key];
        foreach ($_POST as $post_key => $post_val) {
            $primary = $this_con['content_field']['primary_name_field']['name'];
            /** input này được dùng làm khóa tên chính */
            if ($post_key == $primary) {
                $con_name = $post_val;
                if (isset($_POST['slug_of_' . $post_key])) {
                    if (is_numeric($id_update)) {
                        $con_slug = $_POST['slug_of_' . $post_key];
                        $con_slug = str_replace('#', '-', $con_slug);
                    } else {
                        $con_slug = add_request_uri_custom($_POST['slug_of_' . $post_key]);
                    }
                } else {
                    switch ($_POST['accented_of_' . $post_key]) {
                        case 'true':
                            if (is_numeric($id_update)) {
                                $con_slug = sanitize_title_with_accented($post_val);
                            } else {
                                $con_slug = add_request_uri_with_accented($post_val);
                            }
                            break;
                        case 'false':
                            if (is_numeric($id_update)) {
                                $con_slug = sanitize_title($post_val);
                            } else {
                                $con_slug = add_request_uri($post_val);
                            }
                            break;
                        default:
                            if (is_numeric($id_update)) {
                                $con_slug = sanitize_title($post_val);
                            } else {
                                $con_slug = add_request_uri($post_val);
                            }
                    }
                }
                /** lưu content vào database */
                $tableName = DB_PREFIX . 'content';
                $values["name"] = MySQL::SQLValue($con_name);
                $values["slug"] = MySQL::SQLValue($con_slug);
                $values["key"] = MySQL::SQLValue($key);
                $values["status"] = MySQL::SQLValue($status);
                $values["parent"] = MySQL::SQLValue($parent);
                if (is_numeric($id_update)) {
                    $args_con = content_data_by_id($id_update);
                    $key = $args_con['content']->key;
                    $status = $args_con['content']->status;
                    $parent = $args_con['content']->parent;
                    $values["key"] = MySQL::SQLValue($key);
                    $values["status"] = MySQL::SQLValue($status);
                    $values["parent"] = MySQL::SQLValue($parent);
                    $whereArray = array('id' => $id_update);
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                    $insert_id = $id_update;
                    up_date_request_uri_object($con_slug, $insert_id, 'content', 'has_object');
                } else {
                    $insert_id = $hmdb->InsertRow($tableName, $values);
                    up_date_request_uri_object($con_slug, $insert_id, 'content', 'null_object');
                }
                unset($values);
            }
        }
        foreach ($_POST as $post_key => $post_val) {
            /** lưu content field vào data */
            if (is_numeric($insert_id)) {
                if (is_array($post_val)) {
                    $post_val = json_encode($post_val);
                }
                $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('content');
                if (is_numeric($id_update)) {
                    $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'object_type' => MySQL::SQLValue('content'), 'name' => MySQL::SQLValue($post_key));
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                } else {
                    $hmdb->InsertRow($tableName, $values);
                }
                unset($values);
            }
        }
        /** Lưu content time */
        if (is_numeric($insert_id)) {
            $day = $_POST['day'];
            if (!is_numeric($day)) {
                $day = 0;
            }
            $month = $_POST['month'];
            if (!is_numeric($month)) {
                $month = 0;
            }
            $year = $_POST['year'];
            if (!is_numeric($year)) {
                $year = 0;
            }
            $hour = $_POST['hour'];
            if (!is_numeric($hour)) {
                $hour = 0;
            }
            $minute = $_POST['minute'];
            if (!is_numeric($minute)) {
                $minute = 0;
            }
            $public_time = strtotime($day . '-' . $month . '-' . $year . ' ' . $hour . ':' . $minute);
            $values["name"] = MySQL::SQLValue('public_time');
            $values["val"] = MySQL::SQLValue($public_time);
            $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
            $values["object_type"] = MySQL::SQLValue('content');
            if (is_numeric($id_update)) {
                $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'object_type' => MySQL::SQLValue('content'), 'name' => MySQL::SQLValue('public_time'));
                $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
            } else {
                $hmdb->InsertRow($tableName, $values);
            }
            unset($values);
        }
        /** lưu relationship content - taxonomy */
        $tableName = DB_PREFIX . 'relationship';
        if (isset($method_post_taxonomy) and is_array($method_post_taxonomy)) {
            foreach ($method_post_taxonomy as $taxonomy_id) {
                $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
                $values["target_id"] = MySQL::SQLValue($taxonomy_id, MySQL::SQLVALUE_NUMBER);
                $values["relationship"] = MySQL::SQLValue('contax');
                if (is_numeric($id_update)) {
                    $hmdb->AutoInsertUpdate($tableName, $values, $values);
                } else {
                    $hmdb->InsertRow($tableName, $values);
                }
                unset($values);
            }
        }
        /** Gỡ bỏ contax đã bỏ chọn */
        if (is_numeric($id_update)) {
            $id_deletes = array();
            $whereArray = array('object_id' => MySQL::SQLValue($insert_id), 'relationship' => MySQL::SQLValue('contax'));
            $hmdb->SelectRows($tableName, $whereArray);
            if ($hmdb->HasRecords()) {
                while ($row = $hmdb->Row()) {
                    $id_relationship = $row->id;
                    $target_id = $row->target_id;
                    if (!in_array($target_id, $method_post_taxonomy)) {
                        $id_deletes[] = $id_relationship;
                    }
                }
            } else {
                $id_deletes = array();
            }
            foreach ($id_deletes as $id_delete) {
                $whereArray = array('id' => MySQL::SQLValue($id_delete, MySQL::SQLVALUE_NUMBER));
                $hmdb->DeleteRows($tableName, $whereArray);
            }
        }
        /** Gỡ bỏ contag cũ */
        if (is_numeric($id_update)) {
            $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'relationship' => MySQL::SQLValue('contag'));
            $hmdb->DeleteRows($tableName, $whereArray);
        }
        /** lưu tags vào data */
        if (isset($_POST['tags'])) {
            $tags = explode(',', $_POST['tags']);
            $tags = array_map("trim", $tags);
            foreach ($tags as $tag) {
                /** Lưu tag vào bảng taxonomy */
                $tableName = DB_PREFIX . 'taxonomy';
                $tag_slug = sanitize_title($tag);
                $values["name"] = MySQL::SQLValue($tag);
                $values["slug"] = MySQL::SQLValue($tag_slug);
                $values["key"] = MySQL::SQLValue('tag');
                $whereArray = array('key' => MySQL::SQLValue('tag'), 'slug' => MySQL::SQLValue($tag_slug));
                $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                unset($values);
                /** lưu relationship content - tag */
                $tableName = DB_PREFIX . "taxonomy";
                $whereArray = array('key' => MySQL::SQLValue('tag'), 'slug' => MySQL::SQLValue($tag_slug));
                $hmdb->SelectRows($tableName, $whereArray);
                if ($hmdb->HasRecords()) {
                    $row = $hmdb->Row();
                    $tableName = DB_PREFIX . 'relationship';
                    $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
                    $values["target_id"] = MySQL::SQLValue($row->id, MySQL::SQLVALUE_NUMBER);
                    $values["relationship"] = MySQL::SQLValue('contag');
                    if (is_numeric($id_update)) {
                        $hmdb->AutoInsertUpdate($tableName, $values, $values);
                    } else {
                        $hmdb->InsertRow($tableName, $values);
                    }
                    unset($values);
                }
            }
        }
        /** show latest */
        $latest = array('id' => $insert_id, 'name' => $con_name, 'slug' => $con_slug, 'key' => $key);
        return json_encode(array('latest' => $latest));
    }
}
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);
    }
}