function loadBySlug($slug)
 {
     if ($this->getRegisteredByID($slug)) {
         return;
     }
     $category = db_phquery_fetch(DBRFETCH_ASSOC, 'SELECT * FROM ?#CATEGORIES_TABLE WHERE slug=?', $slug);
     LanguagesManager::ml_fillFields($this->__db_table, $category);
     $this->loadFromArray($category);
 }
/**
 * Add to cart product with options
 *
 * @param int $productID
 * @param array $variants  - row is variantID
 * @param int $qty
 */
function cartAddToCart($productID, $variants, $qty = 1, $sample = 0)
{
    if ($qty === '') {
        $qty = 1;
    }
    $qty = max(0, intval($qty));
    $productID = intval($productID);
    $product_data = GetProduct($productID);
    if (!$product_data['ordering_available']) {
        return false;
    }
    if (!$product_data['enabled']) {
        return false;
    }
    $is = intval($product_data['in_stock']);
    $min_order_amount = $product_data['min_order_amount'];
    //$min_order_amount = db_phquery_fetch(DBRFETCH_FIRST, "SELECT min_order_amount FROM ?#PRODUCTS_TABLE WHERE productID=?", $productID );
    if (!isset($_SESSION["log"])) {
        //save shopping cart in the session variables
        //$_SESSION["gids"] contains product IDs
        //$_SESSION["counts"] contains product quantities
        //($_SESSION["counts"][$i] corresponds to $_SESSION["gids"][$i])
        //$_SESSION["configurations"] contains variants
        //$_SESSION[gids][$i] == 0 means $i-element is 'empty'
        if (!isset($_SESSION["gids"])) {
            $_SESSION["gids"] = array();
            $_SESSION["counts"] = array();
            $_SESSION["configurations"] = array();
            $_SESSION["sample"] = array();
        }
        //check for current item in the current shopping cart content
        $item_index = SearchConfigurationInSessionVariable($variants, $productID);
        if ($item_index != -1) {
            //increase current product's quantity
            /*if($_SESSION["counts"][$item_index]+$qty<$min_order_amount){
            	 $qty=$min_order_amount-$_SESSION["counts"][$item_index];
            	 }*/
            //$qty = max($qty,$min_order_amount - $_SESSION["counts"][$item_index],0);
            if (CONF_CHECKSTOCK != 0) {
                $qty = min($qty, $is - $_SESSION["counts"][$item_index]);
            }
            $qty = max($qty, 0);
            $_SESSION["sample"][$item_index] = $sample;
            if (CONF_CHECKSTOCK == 0 || $_SESSION["counts"][$item_index] + $qty <= $is && $is && $qty) {
                if ($sample) {
                    $_SESSION["counts"][$item_index] = 1;
                } else {
                    $_SESSION["counts"][$item_index] += $qty;
                }
            } else {
                return $_SESSION["counts"][$item_index];
            }
        } else {
            //no item - add it to $gids array
            $qty = max($qty, $min_order_amount, 0);
            if (CONF_CHECKSTOCK != 0) {
                $qty = min($qty, $is);
            }
            $qty = max($qty, 0);
            if ($sample) {
                $qty = 1;
            }
            $_SESSION["sample"][] = $sample;
            if (CONF_CHECKSTOCK == 0 || $is >= $qty && $qty) {
                $_SESSION["gids"][] = $productID;
                $_SESSION["counts"][] = $qty;
                $_SESSION["configurations"][] = $variants;
                cartUpdateAddCounter($productID);
            } else {
                return 0;
            }
        }
    } else {
        //authorized customer - get cart from database
        $itemID = SearchConfigurationInDataBase($variants, $productID);
        $customerEntry = Customer::getAuthedInstance();
        if (is_null($customerEntry)) {
            return false;
        }
        if ($itemID != -1) {
            // if this configuration exists in database
            $quantity = db_phquery_fetch(DBRFETCH_FIRST, "SELECT Quantity FROM ?#SHOPPING_CARTS_TABLE WHERE customerID=? AND itemID=?", $customerEntry->customerID, $itemID);
            /*if($quantity+$qty<$min_order_amount){
            	 $qty=$min_order_amount-$quantity;
            	 }*/
            //$qty = max($qty,$min_order_amount - $quantity);
            if (CONF_CHECKSTOCK != 0) {
                $qty = min($qty, $is - $quantity);
            }
            $qty = max($qty, 0);
            if (CONF_CHECKSTOCK == 0 || $quantity + $qty <= $is && $is) {
                if ($sample) {
                    db_phquery("UPDATE ?#SHOPPING_CARTS_TABLE SET Quantity=?, sample=? WHERE customerID=? AND itemID=?", 1, $sample, $customerEntry->customerID, $itemID);
                } else {
                    db_phquery("UPDATE ?#SHOPPING_CARTS_TABLE SET Quantity=?, sample=? WHERE customerID=? AND itemID=?", $quantity + $qty, $sample, $customerEntry->customerID, $itemID);
                }
            } else {
                return $quantity;
            }
        } else {
            //insert new item
            $qty = max($qty, $min_order_amount);
            if (CONF_CHECKSTOCK != 0 && $qty > $is) {
                $qty = min($qty, $is);
            }
            if ($sample) {
                $qty = 1;
            }
            if ((CONF_CHECKSTOCK == 0 || $is >= $qty) && $qty > 0) {
                $itemID = InsertNewItem($variants, $productID);
                InsertItemIntoCart($itemID);
                if ($sample) {
                    db_phquery("UPDATE ?#SHOPPING_CARTS_TABLE SET Quantity=?, sample=? WHERE customerID=? AND itemID=?", 1, $sample, $customerEntry->customerID, $itemID);
                } else {
                    db_phquery("UPDATE ?#SHOPPING_CARTS_TABLE SET Quantity=?, sample=? WHERE customerID=? AND itemID=?", $qty, $sample, $customerEntry->customerID, $itemID);
                }
                cartUpdateAddCounter($productID);
            } else {
                return 0;
            }
        }
    }
    //db_phquery("UPDATE ?#PRODUCTS_TABLE SET add2cart_counter=(add2cart_counter+1) WHERE productID=?",$productID);
    return true;
}
        } else {
            $quantity = $row["Quantity"];
            $price = GetPriceProductWithOption($variants, $r1["productID"]) * $quantity;
        }
        $k += $price;
        $cnt += $quantity;
    }
} elseif (isset($_SESSION["gids"])) {
    //...session vars
    for ($i = 0; $i < count($_SESSION["gids"]); $i++) {
        if (!$_SESSION["gids"][$i]) {
            continue;
        }
        $sum = db_phquery_fetch(DBRFETCH_FIRST, "SELECT Price FROM ?#PRODUCTS_TABLE WHERE productID=?", $_SESSION["gids"][$i]);
        foreach ($_SESSION["configurations"][$i] as $var) {
            $sum += db_phquery_fetch(DBRFETCH_FIRST, "SELECT price_surplus FROM ?#PRODUCTS_OPTIONS_SET_TABLE WHERE variantID=? AND productID=?", $var, $_SESSION["gids"][$i]);
        }
        if ($_SESSION["sample"][$i]) {
            $quantity = 1;
            $q_sample_price = db_phquery('SELECT sample_price FROM SC_categories WHERE categoryID=(SELECT categoryID FROM SC_products WHERE productID=?)', $_SESSION["gids"][$i]);
            $sample_price = db_fetch_assoc($q_sample_price);
            $sum = $sample_price["sample_price"];
        } else {
            $quantity = $_SESSION["counts"][$i];
        }
        $k += $quantity * $sum;
        $cnt += $quantity;
    }
}
$d = oaGetDiscountValue(cartGetCartContent(), is_null($customerEntry) ? null : $customerEntry->Login);
$k = $k - $d;
/**
 * Authorized access check
 *
 */
function checkLogin()
{
    //authorized access check
    if (isset($_SESSION["log"])) {
        //look for user in the database
        $sql = '
			SELECT cust_password FROM ?#CUSTOMERS_TABLE WHERE Login=?
		';
        $row = db_phquery_fetch(DBRFETCH_ROW, $sql, $_SESSION["log"]);
        //found customer - check password
        if (!$row || !isset($_SESSION["pass"]) || strcmp($row[0], $_SESSION["pass"])) {
            unset($_SESSION["log"]);
            unset($_SESSION["pass"]);
        }
    }
}
 $row = db_fetch_row($q);
 if ($row) {
     if (!file_exists(DIR_PRODUCTS_PICTURES . "/" . $row[3])) {
         $row[3] = "";
     }
     $smarty->assign("selected_category", $row);
 }
 //calculate a path to the category
 $smarty->assign("product_category_path", catCalculatePathToCategory($categoryID));
 //reviews number
 $k = db_phquery_fetch(DBRFETCH_FIRST, "SELECT count(*) FROM ?#DISCUSSIONS_TABLE WHERE productID=?", $productID);
 //extra parameters
 $extra = GetExtraParametrs($productID);
 //related items
 $related = array();
 $related_records = db_phquery_fetch(DBRFETCH_ROW_ALL, 'SELECT pr.productID as productID, ' . LanguagesManager::sql_prepareField('name') . ' AS name, Price, slug FROM ?#PRODUCTS_TABLE AS pr JOIN ?#RELATED_PRODUCTS_TABLE AS rel ON (rel.`productID` = pr.`productID`) WHERE Owner=? AND enabled = 1', $productID);
 foreach ($related_records as $r) {
     $r[2] = show_price($r[2]);
     $RelatedPictures = GetPictures($r['productID']);
     foreach ($RelatedPictures as $_RelatedPicture) {
         if (!$_RelatedPicture['default_picture']) {
             continue;
         }
         if (!file_exists(DIR_PRODUCTS_PICTURES . "/" . $_RelatedPicture['thumbnail'])) {
             break;
         }
         $r['pictures'] = array('default' => $_RelatedPicture);
         break;
     }
     $related[] = $r;
 }
        }
        $k += $price;
        $cnt += $quantity;
    }
} elseif (isset($_SESSION["gids"])) {
    //...session vars
    $dbq_price = 'SELECT Price FROM ?#PRODUCTS_TABLE WHERE productID=?';
    $dbq_custom = 'SELECT price_surplus FROM ?#PRODUCTS_OPTIONS_SET_TABLE WHERE variantID=? AND productID=?';
    //TODO: optimize query
    for ($i = 0; $i < count($_SESSION["gids"]); $i++) {
        if (!$_SESSION["gids"][$i]) {
            continue;
        }
        $sum = db_phquery_fetch(DBRFETCH_FIRST, $dbq_price, $_SESSION["gids"][$i]);
        foreach ($_SESSION["configurations"][$i] as $var) {
            $sum += db_phquery_fetch(DBRFETCH_FIRST, $dbq_custom, $var, $_SESSION["gids"][$i]);
        }
        if ($_SESSION["sample"][$i]) {
            $quantity = 1;
            $q_sample_price = db_phquery('SELECT sample_price FROM SC_categories WHERE categoryID=(SELECT categoryID FROM SC_products WHERE productID=?)', $_SESSION["gids"][$i]);
            $sample_price = db_fetch_assoc($q_sample_price);
            $sum = $sample_price["sample_price"];
        } else {
            $quantity = $_SESSION["counts"][$i];
        }
        $k += $quantity * $sum;
        $cnt += $quantity;
    }
}
$d = oaGetDiscountValue(cartGetCartContent(), is_null($customerEntry) ? null : $customerEntry->Login);
$k = $k - $d;