/**
 * Checks the category slug for a display type, if none set returns default
 * << May need reworking to be more specific to the taxonomy type >>
 * @access public
 *
 * @since 3.8
 * @param $slug(string)
 * @return $slug either from db or 'default' if none set
 */
function wpmlm_get_the_category_display($slug)
{
    global $wpdb;
    $default_display_type = get_option('product_view');
    if (!empty($slug) && is_string($slug)) {
        $category_id = wpmlm_get_the_category_id($slug, 'slug');
        $display_type = wpmlm_get_categorymeta($category_id, 'display_type');
    }
    if (!empty($display_type)) {
        return $display_type;
    } else {
        return $default_display_type;
    }
}
function wpmlm_obtain_the_description()
{
    global $wpdb, $wp_query, $wpmlm_title_data;
    $output = null;
    if (is_numeric($wp_query->query_vars['category_id'])) {
        $category_id = $wp_query->query_vars['category_id'];
    } else {
        if ($_GET['category']) {
            $category_id = absint($_GET['category']);
        }
    }
    if (is_numeric($category_id)) {
        $output = wpmlm_get_categorymeta($category_id, 'description');
    }
    if (is_numeric($_GET['product_id'])) {
        $product_id = absint($_GET['product_id']);
        $output = $wpdb->get_var($wpdb->prepare("SELECT `post_content` FROM `" . $wpdb->posts . "` WHERE `id` = %d LIMIT 1", $product_id));
    }
    return $output;
}
/**
 * wpmlm_save_category_set, Saves the category set data
 * @param nothing
 * @return nothing
 */
function wpmlm_save_category_set($category_id, $tt_id)
{
    global $wpdb;
    if (!empty($_POST)) {
        /* Image Processing Code*/
        if (!empty($_FILES['image']) && preg_match("/\\.(gif|jp(e)*g|png){1}\$/i", $_FILES['image']['name'])) {
            if (function_exists("getimagesize")) {
                if ((int) $_POST['width'] > 10 && (int) $_POST['width'] < 512 && ((int) $_POST['height'] > 10 && (int) $_POST['height'] < 512)) {
                    $width = (int) $_POST['width'];
                    $height = (int) $_POST['height'];
                    image_processing($_FILES['image']['tmp_name'], WPMLM_CATEGORY_DIR . $_FILES['image']['name'], $width, $height);
                } else {
                    image_processing($_FILES['image']['tmp_name'], WPMLM_CATEGORY_DIR . $_FILES['image']['name']);
                }
                $image = esc_sql($_FILES['image']['name']);
            } else {
                $new_image_path = WPMLM_CATEGORY_DIR . basename($_FILES['image']['name']);
                move_uploaded_file($_FILES['image']['tmp_name'], $new_image_path);
                $stat = stat(dirname($new_image_path));
                $perms = $stat['mode'] & 0666;
                @chmod($new_image_path, $perms);
                $image = esc_sql($_FILES['image']['name']);
            }
        } else {
            $image = '';
        }
        //Good to here
        if (isset($_POST['tag_ID'])) {
            //Editing
            $category_id = $_POST['tag_ID'];
            $category = get_term_by('id', $category_id, 'wpmlm_product_category');
            $url_name = $category->slug;
        }
        if (isset($_POST['deleteimage']) && $_POST['deleteimage'] == 1) {
            wpmlm_delete_categorymeta($category_id, 'image');
        } else {
            if ($image != '') {
                wpmlm_update_categorymeta($category_id, 'image', $image);
            }
        }
        if (!empty($_POST['height']) && is_numeric($_POST['height']) && !empty($_POST['width']) && is_numeric($_POST['width']) && $image == null) {
            $imagedata = wpmlm_get_categorymeta($category_id, 'image');
            if ($imagedata != null) {
                $height = $_POST['height'];
                $width = $_POST['width'];
                $imagepath = WPMLM_CATEGORY_DIR . $imagedata;
                $image_output = WPMLM_CATEGORY_DIR . $imagedata;
                image_processing($imagepath, $image_output, $width, $height);
            }
        }
        wpmlm_update_categorymeta($category_id, 'fee', '0');
        wpmlm_update_categorymeta($category_id, 'active', '1');
        wpmlm_update_categorymeta($category_id, 'order', '0');
        if (isset($_POST['display_type'])) {
            wpmlm_update_categorymeta($category_id, 'display_type', esc_sql(stripslashes($_POST['display_type'])));
        }
        if (isset($_POST['image_height'])) {
            wpmlm_update_categorymeta($category_id, 'image_height', esc_sql(stripslashes($_POST['image_height'])));
        }
        if (isset($_POST['image_width'])) {
            wpmlm_update_categorymeta($category_id, 'image_width', esc_sql(stripslashes($_POST['image_width'])));
        }
        if (!empty($_POST['use_additional_form_set'])) {
            wpmlm_update_categorymeta($category_id, 'use_additional_form_set', $_POST['use_additional_form_set']);
            //exit('<pre>'.print_r($_POST,1).'</pre>');
        } else {
            wpmlm_delete_categorymeta($category_id, 'use_additional_form_set');
        }
        if (!empty($_POST['uses_billing_address'])) {
            wpmlm_update_categorymeta($category_id, 'uses_billing_address', 1);
            $uses_additional_forms = true;
        } else {
            wpmlm_update_categorymeta($category_id, 'uses_billing_address', 0);
            $uses_additional_forms = false;
        }
        if (!empty($_POST['countrylist2']) && $category_id > 0) {
            $AllSelected = false;
            $countryList = $wpdb->get_col("SELECT `id` FROM  `" . WPMLM_TABLE_CURRENCY_LIST . "`");
            if ($AllSelected != true) {
                $unselectedCountries = array_diff($countryList, $_POST['countrylist2']);
                //find the countries that are selected
                $selectedCountries = array_intersect($countryList, $_POST['countrylist2']);
                wpmlm_update_categorymeta($category_id, 'target_market', $selectedCountries);
            }
        } elseif (!isset($_POST['countrylist2'])) {
            wpmlm_update_categorymeta($category_id, 'target_market', '');
            $AllSelected = true;
        }
    }
}