function xtc_product_link($pID, $name = '')
{
    #Check for current category link
    $c_path = $_SERVER['QUERY_STRING'];
    if (strpos($c_path, 'cPath') !== false) {
        $category_path = substr($c_path, strpos($c_path, '=') + 1);
        $categorie_previous = end(explode('_', $category_path));
        $cat_children = array();
        $cat_children = xtc_get_categories_children($categorie_previous);
        foreach ($cat_children as $linked_cat) {
            $category_query_check = "select p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = '" . (int) $pID . "' and p.products_status = '1' and p.products_id = p2c.products_id and p2c.categories_id != 0 and p2c.categories_id = '" . $linked_cat . "'";
            $category_query_check = xtDBquery($category_query_check);
            if (xtc_db_num_rows($category_query_check, true)) {
                $cID = xtc_db_fetch_array($category_query_check)['categories_id'];
                break;
            }
        }
    }
    #Take default categorie id
    if (!isset($cID)) {
        $category_query = "select p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = '" . (int) $pID . "' and p.products_status = '1' and p.products_id = p2c.products_id and p2c.categories_id != 0";
        $category_query = xtDBquery($category_query);
        if (xtc_db_num_rows($category_query, true)) {
            $cID = xtc_db_fetch_array($category_query)['categories_id'];
        }
    }
    #Create category path from category ID
    $categories = array();
    xtc_get_parent_categories($categories, $cID);
    $categories = array_reverse($categories);
    $categories[] = $cID;
    $cPath = implode('_', $categories);
    return 'products_id=' . $pID . '&cPath=' . $cPath;
}
function xtc_get_product_path($products_id)
{
    $cPath = '';
    $category_query = "select p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = '" . (int) $products_id . "' and p.products_status = '1' and p.products_id = p2c.products_id and p2c.categories_id != 0";
    $category_query = xtDBquery($category_query);
    if (xtc_db_num_rows($category_query, true)) {
        #Default category( first picked from table)
        $category = xtc_db_fetch_array($category_query);
        $p_path_full = $_SERVER['REQUEST_URI'];
        $p_path = $_SERVER['QUERY_STRING'];
        #If we display product check linked categories
        if (strpos($p_path, 'products_id') !== false) {
            while ($categoryies = xtc_db_fetch_array($category_query)) {
                $cat_name = xtc_get_categories_name($categoryies['categories_id']);
                $cat_name_slug = shopstat_hrefSmallmask($cat_name);
                $p_path_full_array = explode('/', $p_path_full);
                if ($p_path_full_array[count($p_path_full_array) - 2] === $cat_name_slug) {
                    $category = $categoryies;
                    break;
                }
            }
        }
        # Check if current categorie or its children have linked product
        $c_path = $_SERVER['QUERY_STRING'];
        if (strpos($c_path, 'cPath') !== false) {
            $category_path = substr($c_path, strpos($c_path, '=') + 1);
            $categorie_previous = end(explode('_', $category_path));
            $cat_children = array();
            $cat_children = xtc_get_categories_children($categorie_previous);
            foreach ($cat_children as $linked_cat) {
                $category_query_check = "select p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = '" . (int) $products_id . "' and p.products_status = '1' and p.products_id = p2c.products_id and p2c.categories_id != 0 and p2c.categories_id = '" . $linked_cat . "'";
                $category_query_check = xtDBquery($category_query_check);
                if (xtc_db_num_rows($category_query_check, true)) {
                    $category = xtc_db_fetch_array($category_query_check);
                    break;
                }
            }
        }
        $categories = array();
        xtc_get_parent_categories($categories, $category['categories_id']);
        $categories = array_reverse($categories);
        $cPath = implode('_', $categories);
        if (xtc_not_null($cPath)) {
            $cPath .= '_';
        }
        $cPath .= $category['categories_id'];
    }
    //BOF - Dokuman - 2009-10-02 - removed feature, due to wrong links in category on "last viewed"
    /*
      if($_SESSION['lastpath']!=''){
        $cPath = $_SESSION['lastpath'];
      }
    */
    //EOF - Dokuman - 2009-10-02 - removed feature, due to wrong links in category on "last viewed"
    return $cPath;
}
function xtc_get_categories_children_calc($cat_id, &$cat_children)
{
    $categories_query = "select categories_id, parent_id from " . TABLE_CATEGORIES . "  where parent_id = '" . $cat_id . "'";
    $categories_query = xtDBquery($categories_query);
    if (xtc_db_num_rows($categories_query, true)) {
        while ($categoryies = xtc_db_fetch_array($categories_query)) {
            $cat_children[] = $categoryies['categories_id'];
            xtc_get_categories_children($categoryies['categories_id'], $cat_children);
        }
    }
}