/**
 * Change datatype for attribute of select list type.
 */
function ajax_attribute_select_data_type_change_callback()
{
    global $wpdb;
    check_ajax_referer('wpshop_attribute_change_select_data_type_change', 'wpshop_ajax_nonce');
    $result = '';
    $current_attribute = isset($_POST['attribute_id']) ? intval(wpshop_tools::varSanitizer($_POST['attribute_id'])) : null;
    $data_type = isset($_POST['data_type']) ? wpshop_tools::varSanitizer($_POST['data_type']) : null;
    $internal_data_type = isset($_POST['internal_data']) ? wpshop_tools::varSanitizer($_POST['internal_data']) : null;
    $delete_items_of_entity = isset($_POST['delete_items_of_entity']) ? wpshop_tools::varSanitizer($_POST['delete_items_of_entity']) : false;
    $delete_entity = isset($_POST['delete_entity']) ? wpshop_tools::varSanitizer($_POST['delete_entity']) : false;
    if ($data_type == 'internal') {
        $options_list = wpshop_attributes::get_select_option_list_($current_attribute);
        if (!empty($options_list)) {
            foreach ($options_list as $option) {
                /*	Creat the new entity	*/
                $new_post = array('post_title' => $option->name, 'post_name' => $option->value, 'post_status' => 'publish', 'post_type' => $internal_data_type);
                $new_option_id = wp_insert_post($new_post);
                if (!empty($new_option_id)) {
                    $wpdb->update(WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS, array('status' => 'deleted'), array('attribute_id' => $current_attribute));
                }
            }
        }
    } else {
        $post_list = query_posts(array('post_type' => $internal_data_type));
        if (!empty($post_list)) {
            $p = 1;
            $error = false;
            foreach ($post_list as $post) {
                $last_insert = $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'attribute_id' => $current_attribute, 'position' => $p, 'value' => $post->post_name, 'label' => $post->post_title));
                if (is_int($last_insert) && $delete_items_of_entity) {
                    wp_delete_post($post->ID, true);
                } else {
                    $error = true;
                }
                $p++;
            }
            if (!$error && $delete_entity) {
                $post = $wpdb->prepare("SELECT ID FROM " . $wpdb->posts . " WHERE post_type=%s AND post_name=%s", WPSHOP_NEWTYPE_IDENTIFIER_ENTITIES, $internal_data_type);
                wp_delete_post($wpdb->get_var($post), true);
            }
        }
        wp_reset_query();
    }
    /*	Update attribute datatype	*/
    $wpdb->update(WPSHOP_DBT_ATTRIBUTE, array('data_type_to_use' => $data_type, 'default_value' => $internal_data_type), array('id' => $current_attribute));
    $result = wpshop_attributes::get_select_options_list($current_attribute, $editedItem->{$data_type});
    echo json_encode($result);
    die;
}