<?php
function throwLangVariables( $sLang ){
	if( is_file( DIR_LANG . $sLang . '.php' ) ){
		include DIR_LANG . $sLang . '.php';
		foreach( $lang as &$sValue ){
			$sValue = changeTxt( $sValue, '' );
			$sValue = htmlspecialchars( $sValue );
//			$sValue = str_replace( '|n|', '\n', $sValue );
		}
		return $lang;
	}
}
function throwLanguages( ){
	$oDir = dir( DIR_LANG );
	$oDB = DataBase::getInstance( );
	while( false !== ( $sFile = $oDir->read( ) ) ){
		$sFileName = is_file( DIR_LANG . $sFile ) ? $oDB->throwNameOfFile( $sFile ) : null;
		if( isset( $sFileName ) && strlen( $sFileName ) == 2 ){
			$aLanguages[$sFileName] = $sFileName;
		}
	}
	$oDir->close( );
	if( isset( $aLanguages ) ) return $aLanguages;
}
function addLanguage( $sLanguage, $sLanguageFrom ){
	$oDB = DataBase::getInstance( );
	$sDb = MYSQL_DB;
	$sql = 'SELECT `table_name` FROM `information_schema`.`tables` WHERE `table_schema`=\'' . $sDb . '\' AND `table_name` LIKE \'' . MYSQL_TABLE_PREFIX . $sLanguageFrom . '_%\' AND `table_type` = \'BASE TABLE\'';
	$aTables = $oDB->executeSQL( $sql, true, DataBase::SINGLE_COLUMN );
	$sql = null;
	foreach( $aTables as $sTable ){
		$sTable = substr( $sTable, strlen( MYSQL_TABLE_PREFIX ) + 2 );
		$sql.= 'CREATE TABLE `%%' . $sTable . '` LIKE `&&' . $sTable . '`
INSERT INTO `%%' . $sTable . '` SELECT * FROM `&&' . $sTable . '`
';
	}
	$sql.= <<<'SQL'
DROP function IF EXISTS `%%_calculateProductPriceForUser`;
CREATE FUNCTION `%%_calculateProductPriceForUser` (piUser INT, piProduct INT, piPromotions INT) RETURNS DOUBLE READS SQL DATA BEGIN DECLARE xiActiveDiscount INT; DECLARE xfPrice DOUBLE; DECLARE xfUserMultiply DOUBLE; DECLARE xfDiscount DOUBLE; DECLARE xfProductMultiply DOUBLE; DECLARE xfMultiply DOUBLE; SELECT `iActiveDiscount`, `fPrice` FROM `%%_products` WHERE `iProduct` = piProduct INTO xiActiveDiscount, xfPrice; IF ((piUser IS NOT NULL) AND (xiActiveDiscount > 0) ) THEN SELECT MAX(`fDiscount`) FROM `$$_users_groups` INNER JOIN `$$_users_to_groups` USING(`iGroup`) WHERE `iUser` = piUser INTO xfDiscount; IF (xfDiscount IS NULL) THEN SET xfDiscount = 0; END IF; SET xfUserMultiply = 1 - xfDiscount; SELECT MIN(`fMultiply`) FROM `%%_products_discounts` INNER JOIN (`$$_users_groups` INNER JOIN `$$_users_to_groups` USING (`iGroup`) ) USING (`iGroup`) WHERE ((`iUser` = piUser) AND (`iProduct` = piProduct) ) INTO xfProductMultiply; IF (xfProductMultiply IS NULL) THEN SET xfProductMultiply = 1; END IF; IF (piPromotions > 0) THEN RETURN xfPrice * xfUserMultiply * xfProductMultiply; ELSE IF (xfUserMultiply > xfProductMultiply) THEN SET xfMultiply = xfProductMultiply; ELSE SET xfMultiply = xfUserMultiply; END IF; RETURN xfPrice * xfMultiply; END IF; ELSE RETURN xfPrice; END IF; END
DROP function IF EXISTS `%%_calculateProductVariantPriceForUser`;
CREATE FUNCTION `%%_calculateProductVariantPriceForUser` (piUser INT, piProductVariant INT, piPromotions INT) RETURNS DOUBLE READS SQL DATA BEGIN DECLARE xiProduct INT; DECLARE xiActiveDiscount INT; DECLARE xfPrice DOUBLE; DECLARE xfUserMultiply DOUBLE; DECLARE xfDiscount DOUBLE; DECLARE xfProductMultiply DOUBLE; DECLARE xfMultiply DOUBLE; SELECT IFNULL(`pv`.`iActiveDiscount`, `p`.`iActiveDiscount`), IFNULL(`pv`.`fPrice`, `p`.`fPrice`), `iProduct` FROM `%%_products_variants` AS `pv` LEFT JOIN `%%_products` AS `p` USING(`iProduct`) WHERE `iProductVariant` = piProductVariant INTO xiActiveDiscount, xfPrice, xiProduct; IF ((piUser IS NOT NULL) AND (xiActiveDiscount > 0) ) THEN SELECT MAX(`fDiscount`) FROM `$$_users_groups` INNER JOIN `$$_users_to_groups` USING(`iGroup`) WHERE `iUser` = piUser INTO xfDiscount; IF (xfDiscount IS NULL) THEN SET xfDiscount = 0; END IF; SET xfUserMultiply = 1 - xfDiscount; SELECT MIN(`fMultiply`) FROM `%%_products_discounts` INNER JOIN (`$$_users_groups` INNER JOIN `$$_users_to_groups` USING (`iGroup`) ) USING (`iGroup`) WHERE ((`iUser` = piUser) AND (`iProduct` = xiProduct) ) INTO xfProductMultiply; IF (xfProductMultiply IS NULL) THEN SET xfProductMultiply = 1; END IF; IF (piPromotions > 0) THEN RETURN xfPrice * xfUserMultiply * xfProductMultiply; ELSE IF (xfUserMultiply > xfProductMultiply) THEN SET xfMultiply = xfProductMultiply; ELSE SET xfMultiply = xfUserMultiply; END IF; RETURN xfPrice * xfMultiply; END IF; ELSE RETURN xfPrice; END IF; END
DROP function IF EXISTS `%%_getProductCanBuyForUser`;
CREATE FUNCTION `%%_getProductCanBuyForUser` (piUser INT, piProduct INT) RETURNS INT READS SQL DATA BEGIN DECLARE xiCanBuy INT; SELECT `iCanBuyAll` FROM `%%_products` WHERE `iProduct` = piProduct INTO xiCanBuy; IF ((piUser IS NOT NULL) AND (xiCanBuy < 1) ) THEN SELECT MAX(`iCanBuy`) FROM `%%_products_discounts` INNER JOIN (`$$_users_groups` INNER JOIN `$$_users_to_groups` USING (`iGroup`) ) USING (`iGroup`) WHERE ((`iUser` = piUser) AND (`iProduct` = piProduct) ) INTO xiCanBuy; END IF; RETURN xiCanBuy; END
DROP function IF EXISTS `%%_getProductVisibleForUser`;
CREATE FUNCTION `%%_getProductVisibleForUser` (piUser INT, piProduct INT) RETURNS INT READS SQL DATA BEGIN DECLARE xiVisible INT; SELECT `iVisibleAll` FROM `%%_products` WHERE `iProduct` = piProduct INTO xiVisible; IF ((piUser IS NOT NULL) AND (xiVisible < 1) ) THEN SELECT MAX(`iVisible`) FROM `%%_products_discounts` INNER JOIN (`$$_users_groups` INNER JOIN `$$_users_to_groups` USING (`iGroup`) ) USING (`iGroup`) WHERE ((`iUser` = piUser) AND (`iProduct` = piProduct) ) INTO xiVisible; END IF; RETURN xiVisible; END
DROP function IF EXISTS `%%_getProductVisiblePriceForUser`;
CREATE FUNCTION `%%_getProductVisiblePriceForUser` (piUser INT, piProduct INT) RETURNS INT READS SQL DATA BEGIN DECLARE xiVisiblePrice INT; SELECT `iVisiblePriceAll` FROM `%%_products` WHERE `iProduct` = piProduct INTO xiVisiblePrice; IF ((piUser IS NOT NULL) AND (xiVisiblePrice < 1) ) THEN SELECT MAX(`iVisiblePrice`) FROM `%%_products_discounts` INNER JOIN (`$$_users_groups` INNER JOIN `$$_users_to_groups` USING (`iGroup`) ) USING (`iGroup`) WHERE ((`iUser` = piUser) AND (`iProduct` = piProduct) ) INTO xiVisiblePrice; END IF; RETURN xiVisiblePrice; END
DROP TRIGGER IF EXISTS `%%_products_variants_AINS`
CREATE TRIGGER `%%_products_variants_AINS` AFTER INSERT ON `%%_products_variants` FOR EACH ROW BEGIN UPDATE `%%_products` SET `TiStock` = `TiStock` + GREATEST(NEW.`TiStock`, 0), `TiVariantsCount` = `TiVariantsCount` + 1 WHERE `iProduct` = NEW.`iProduct`; UPDATE `%%_products` `p` SET `p`.`TiAvailable` = (SELECT MIN(`pv`.`TiAvailable`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = NEW.`iProduct`), `p`.`TiCanBuy` = (SELECT MAX(`pv`.`TiCanBuy`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = NEW.`iProduct`), `p`.`TiCanBuyOutOfStock` = (SELECT MAX(`pv`.`TiCanBuyOutOfStock`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = NEW.`iProduct`) WHERE `p`.`iProduct` = NEW.`iProduct`; END
DROP TRIGGER IF EXISTS `%%_products_variants_AUPD`
CREATE TRIGGER `%%_products_variants_AUPD` AFTER UPDATE ON `%%_products_variants` FOR EACH ROW BEGIN IF NEW.`TiStock` != OLD.`TiStock` OR NEW.`TiCanBuy` != OLD.`TiCanBuy` OR NEW.`TiCanBuyOutOfStock` != OLD.`TiCanBuyOutOfStock` OR NEW.`TiAvailable` != OLD.`TiAvailable` OR NEW.`iProduct` != OLD.`iProduct` THEN UPDATE `%%_products` SET `TiStock` = `TiStock` - GREATEST(OLD.`TiStock`, 0), `TiVariantsCount` = `TiVariantsCount` - 1 WHERE `iProduct` = OLD.`iProduct`; UPDATE `%%_products` `p` SET `p`.`TiAvailable` = (SELECT MIN(`pv`.`TiAvailable`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = OLD.`iProduct`), `p`.`TiCanBuy` = (SELECT MAX(`pv`.`TiCanBuy`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = OLD.`iProduct`), `p`.`TiCanBuyOutOfStock` = (SELECT MAX(`pv`.`TiCanBuyOutOfStock`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = OLD.`iProduct`) WHERE `p`.`iProduct` = OLD.`iProduct`; UPDATE `%%_products` SET `TiStock` = `TiStock` + GREATEST(NEW.`TiStock`, 0), `TiVariantsCount` = `TiVariantsCount` + 1 WHERE `iProduct` = NEW.`iProduct`; UPDATE `%%_products` `p` SET `p`.`TiAvailable` = (SELECT MIN(`pv`.`TiAvailable`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = NEW.`iProduct`), `p`.`TiCanBuy` = (SELECT MAX(`pv`.`TiCanBuy`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = NEW.`iProduct`), `p`.`TiCanBuyOutOfStock` = (SELECT MAX(`pv`.`TiCanBuyOutOfStock`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = NEW.`iProduct`) WHERE `p`.`iProduct` = NEW.`iProduct`; END IF; END
DROP TRIGGER IF EXISTS `%%_products_variants_ADEL`
CREATE TRIGGER `%%_products_variants_ADEL` AFTER DELETE ON `%%_products_variants` FOR EACH ROW BEGIN DELETE FROM `%%_v_products_variants_names` WHERE `iProductVariant` = OLD.`iProductVariant`; UPDATE `%%_products` SET `TiStock` = `TiStock` - GREATEST(OLD.`TiStock`, 0), `TiVariantsCount` = `TiVariantsCount` - 1 WHERE `iProduct` = OLD.`iProduct`; UPDATE `%%_products` `p` SET `p`.`TiAvailable` = (SELECT MIN(`pv`.`TiAvailable`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = OLD.`iProduct`), `p`.`TiCanBuy` = (SELECT MAX(`pv`.`TiCanBuy`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = OLD.`iProduct`), `p`.`TiCanBuyOutOfStock` = (SELECT MAX(`pv`.`TiCanBuyOutOfStock`) FROM `%%_products_variants` `pv` WHERE `pv`.`iProduct` = OLD.`iProduct`) WHERE `p`.`iProduct` = OLD.`iProduct`; END
DROP TRIGGER IF EXISTS `%%_attributes_AUPD`
CREATE TRIGGER `%%_attributes_AUPD` AFTER UPDATE ON `%%_attributes` FOR EACH ROW BEGIN DELETE FROM `%%_v_products_variants_names` WHERE EXISTS(SELECT 1 FROM `%%_products_attributes` `pa` INNER JOIN `%%_attributes_values` `av` USING(`iAttributeValue`) WHERE `pa`.`iProductVariant` = `iProductVariant` AND `av`.`iAttribute` = NEW.`iAttribute`) ; INSERT INTO `%%_v_products_variants_names` (`iProductVariant`, `sName`) SELECT `iProductVariant`, GROUP_CONCAT(CONCAT(`sName`, ': ', `sValue`) SEPARATOR ', ') AS `sAttributes` FROM `%%_products_attributes` LEFT JOIN `%%_attributes_values` USING(`iAttributeValue`) LEFT JOIN `%%_attributes` USING(`iAttribute`) WHERE `iBasketVisible` = 1 AND EXISTS(SELECT 1 FROM `%%_products_attributes` `pa` INNER JOIN `%%_attributes_values` `av` USING(`iAttributeValue`) WHERE `pa`.`iProductVariant` = `iProductVariant` AND `av`.`iAttribute` = NEW.`iAttribute`) GROUP BY `iProductVariant`; END
DROP TRIGGER IF EXISTS `%%_attributes_ADEL`
CREATE TRIGGER `%%_attributes_ADEL` AFTER DELETE ON `%%_attributes` FOR EACH ROW BEGIN DELETE FROM `%%_v_products_variants_names` WHERE EXISTS(SELECT 1 FROM `%%_products_attributes` `pa` INNER JOIN `%%_attributes_values` `av` USING(`iAttributeValue`) WHERE `pa`.`iProductVariant` = `iProductVariant` AND `av`.`iAttribute` = OLD.`iAttribute`) ; INSERT INTO `%%_v_products_variants_names` (`iProductVariant`, `sName`) SELECT `iProductVariant`, GROUP_CONCAT(CONCAT(`sName`, ': ', `sValue`) SEPARATOR ', ') AS `sAttributes` FROM `%%_products_attributes` LEFT JOIN `%%_attributes_values` USING(`iAttributeValue`) LEFT JOIN `%%_attributes` USING(`iAttribute`) WHERE `iBasketVisible` = 1 AND EXISTS(SELECT 1 FROM `%%_products_attributes` `pa` INNER JOIN `%%_attributes_values` `av` USING(`iAttributeValue`) WHERE `pa`.`iProductVariant` = `iProductVariant` AND `av`.`iAttribute` = OLD.`iAttribute`) GROUP BY `iProductVariant`; END
DROP TRIGGER IF EXISTS `%%_attributes_values_AUPD`
CREATE TRIGGER `%%_attributes_values_AUPD` AFTER UPDATE ON `%%_attributes_values` FOR EACH ROW BEGIN DELETE FROM `%%_v_products_variants_names` WHERE EXISTS(SELECT 1 FROM `%%_products_attributes` `pa` WHERE `pa`.`iProductVariant` = `iProductVariant` AND `pa`.`iAttributeValue` = NEW.`iAttributeValue`) ; INSERT INTO `%%_v_products_variants_names` (`iProductVariant`, `sName`) SELECT `iProductVariant`, GROUP_CONCAT(CONCAT(`sName`, ': ', `sValue`) SEPARATOR ', ') AS `sAttributes` FROM `%%_products_attributes` LEFT JOIN `%%_attributes_values` USING(`iAttributeValue`) LEFT JOIN `%%_attributes` USING(`iAttribute`) WHERE `iBasketVisible` = 1 AND EXISTS(SELECT 1 FROM `%%_products_attributes` `pa` WHERE `pa`.`iProductVariant` = `iProductVariant` AND `pa`.`iAttributeValue` = NEW.`iAttributeValue`) GROUP BY `iProductVariant`; END
DROP TRIGGER IF EXISTS `%%_attributes_values_ADEL`
CREATE TRIGGER `%%_attributes_values_ADEL` AFTER DELETE ON `%%_attributes_values` FOR EACH ROW BEGIN DELETE FROM `%%_v_products_variants_names` WHERE EXISTS(SELECT 1 FROM `%%_products_attributes` `pa` WHERE `pa`.`iProductVariant` = `iProductVariant` AND `pa`.`iAttributeValue` = OLD.`iAttributeValue`) ; INSERT INTO `%%_v_products_variants_names` (`iProductVariant`, `sName`) SELECT `iProductVariant`, GROUP_CONCAT(CONCAT(`sName`, ': ', `sValue`) SEPARATOR ', ') AS `sAttributes` FROM `%%_products_attributes` LEFT JOIN `%%_attributes_values` USING(`iAttributeValue`) LEFT JOIN `%%_attributes` USING(`iAttribute`) WHERE `iBasketVisible` = 1 AND EXISTS(SELECT 1 FROM `%%_products_attributes` `pa` WHERE `pa`.`iProductVariant` = `iProductVariant` AND `pa`.`iAttributeValue` = OLD.`iAttributeValue`) GROUP BY `iProductVariant`; END
DROP TRIGGER IF EXISTS `%%_products_attributes_AINS`
CREATE TRIGGER `%%_products_attributes_AINS` AFTER INSERT ON `%%_products_attributes` FOR EACH ROW BEGIN DELETE FROM `%%_v_products_variants_names` WHERE `iProductVariant` = NEW.`iProductVariant`; INSERT INTO `%%_v_products_variants_names` (`iProductVariant`, `sName`) SELECT `iProductVariant`, GROUP_CONCAT(CONCAT(`sName`, ': ', `sValue`) SEPARATOR ', ') AS `sAttributes` FROM `%%_products_attributes` LEFT JOIN `%%_attributes_values` USING(`iAttributeValue`) LEFT JOIN `%%_attributes` USING(`iAttribute`) WHERE `iBasketVisible` = 1 AND `iProductVariant` = NEW.`iProductVariant` GROUP BY `iProductVariant`; END
DROP TRIGGER IF EXISTS `%%_products_attributes_ADEL`
CREATE TRIGGER `%%_products_attributes_ADEL` AFTER DELETE ON `%%_products_attributes` FOR EACH ROW BEGIN DELETE FROM `%%_v_products_variants_names` WHERE `iProductVariant` = OLD.`iProductVariant`; INSERT INTO `%%_v_products_variants_names` (`iProductVariant`, `sName`) SELECT `iProductVariant`, GROUP_CONCAT(CONCAT(`sName`, ': ', `sValue`) SEPARATOR ', ') AS `sAttributes` FROM `%%_products_attributes` LEFT JOIN `%%_attributes_values` USING(`iAttributeValue`) LEFT JOIN `%%_attributes` USING(`iAttribute`) WHERE `iBasketVisible` = 1 AND `iProductVariant` = OLD.`iProductVariant` GROUP BY `iProductVariant`; END
DROP TRIGGER IF EXISTS `%%_default_availability_AUPD`
CREATE TRIGGER `%%_default_availability_AUPD` AFTER UPDATE ON `%%_default_availability` FOR EACH ROW BEGIN UPDATE `%%_products_variants` `pv` INNER JOIN(SELECT MIN(IF(`sp`.`iStock` > 0, IFNULL(`sp`.`iAvailableOnStock`, IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) ), IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) ) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` GROUP BY `sp`.`iProductVariant`) `sq` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a` USING(`iAvailable`) INNER JOIN(SELECT IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` GROUP BY `sp`.`iProductVariant`) `sq2` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a2` ON(`a2`.`iAvailable` = `sq2`.`iAvailable`) SET `pv`.`TiAvailable` = `a`.`iAvailable`, `pv`.`TiCanBuy` = `a`.`iCanBuy`, `pv`.`TiCanBuyOutOfStock` = `a2`.`iCanBuy`; UPDATE `%%_stores_products` `sp` SET `sp`.`TiAvailableOnStock` = IFNULL(`sp`.`iAvailableOnStock`, (SELECT IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) FROM `%%_stores` `s` INNER JOIN `%%_default_availability` `da` WHERE `iStore` = `sp`.`iStore`) ) ; UPDATE `%%_stores_products` `sp` SET `sp`.`TiAvailableOutOfStock` = IFNULL(`sp`.`iAvailableOutOfStock`, (SELECT IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) FROM `%%_stores` `s` INNER JOIN `%%_default_availability` `da` WHERE `iStore` = `sp`.`iStore`) ) ; END
DROP TRIGGER IF EXISTS `%%_stores_AUPD`
CREATE TRIGGER `%%_stores_AUPD` AFTER UPDATE ON `%%_stores` FOR EACH ROW BEGIN UPDATE `%%_products_variants` `pv` INNER JOIN(SELECT MIN(IF(`sp`.`iStock` > 0, IFNULL(`sp`.`iAvailableOnStock`, IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) ), IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) ) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `s`.`iStore` = NEW.`iStore` GROUP BY `sp`.`iProductVariant`) `sq` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a` USING(`iAvailable`) INNER JOIN(SELECT IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `s`.`iStore` = NEW.`iStore` GROUP BY `sp`.`iProductVariant`) `sq2` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a2` ON(`a2`.`iAvailable` = `sq2`.`iAvailable`) SET `pv`.`TiAvailable` = `a`.`iAvailable`, `pv`.`TiCanBuy` = `a`.`iCanBuy`, `pv`.`TiCanBuyOutOfStock` = `a2`.`iCanBuy`; UPDATE `%%_stores_products` `sp` SET `sp`.`TiAvailableOnStock` = IFNULL(`sp`.`iAvailableOnStock`, (SELECT IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) FROM `%%_stores` `s` INNER JOIN `%%_default_availability` `da` WHERE `iStore` = NEW.`iStore`) ) WHERE `iStore` = NEW.`iStore`; UPDATE `%%_stores_products` `sp` SET `sp`.`TiAvailableOutOfStock` = IFNULL(`sp`.`iAvailableOutOfStock`, (SELECT IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) FROM `%%_stores` `s` INNER JOIN `%%_default_availability` `da` WHERE `iStore` = NEW.`iStore`) ) WHERE `iStore` = NEW.`iStore`; END
DROP TRIGGER IF EXISTS `%%_stores_ADEL`
CREATE TRIGGER `%%_stores_ADEL` AFTER DELETE ON `%%_stores` FOR EACH ROW BEGIN DELETE FROM `%%_stores_products` WHERE `iStore` = OLD.`iStore`; END
DROP TRIGGER IF EXISTS `%%_stores_products_BINS`
CREATE TRIGGER `%%_stores_products_BINS` BEFORE INSERT ON `%%_stores_products` FOR EACH ROW BEGIN SET NEW.`TiAvailableOnStock` = IFNULL(NEW.`iAvailableOnStock`, (SELECT IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) FROM `%%_stores` `s` INNER JOIN `%%_default_availability` `da` WHERE `iStore` = NEW.`iStore`) ) ; SET NEW.`TiAvailableOutOfStock` = IFNULL(NEW.`iAvailableOutOfStock`, (SELECT IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) FROM `%%_stores` `s` INNER JOIN `%%_default_availability` `da` WHERE `iStore` = NEW.`iStore`) ) ; END
DROP TRIGGER IF EXISTS `%%_stores_products_AINS`
CREATE TRIGGER `%%_stores_products_AINS` AFTER INSERT ON `%%_stores_products` FOR EACH ROW BEGIN UPDATE `%%_products_variants` `pv` SET `pv`.`TiStock` = `pv`.`TiStock` + GREATEST(NEW.`iStock`, 0) WHERE `iProductVariant` = NEW.`iProductVariant`; UPDATE `%%_products_variants` `pv` INNER JOIN(SELECT IF(`sp`.`iStock` > 0, IFNULL(`sp`.`iAvailableOnStock`, IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) ), IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = NEW.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a` USING(`iAvailable`) INNER JOIN(SELECT IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = NEW.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq2` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a2` ON(`a2`.`iAvailable` = `sq2`.`iAvailable`) SET `pv`.`TiAvailable` = `a`.`iAvailable`, `pv`.`TiCanBuy` = `a`.`iCanBuy`, `pv`.`TiCanBuyOutOfStock` = `a2`.`iCanBuy`; END
DROP TRIGGER IF EXISTS `%%_stores_products_BUPD`
CREATE TRIGGER `%%_stores_products_BUPD` BEFORE UPDATE ON `%%_stores_products` FOR EACH ROW BEGIN IF NEW.`iStore` != OLD.`iStore` OR NEW.`iProductVariant` != OLD.`iProductVariant` THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cant change store id/product variant id - in order to do so, delete record and insert another one'; END IF; SET NEW.`TiAvailableOnStock` = IFNULL(NEW.`iAvailableOnStock`, (SELECT IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) FROM `%%_stores` `s` INNER JOIN `%%_default_availability` `da` WHERE `iStore` = NEW.`iStore`) ) ; SET NEW.`TiAvailableOutOfStock` = IFNULL(NEW.`iAvailableOutOfStock`, (SELECT IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) FROM `%%_stores` `s` INNER JOIN `%%_default_availability` `da` WHERE `iStore` = NEW.`iStore`) ) ; END
DROP TRIGGER IF EXISTS `%%_stores_products_AUPD`
CREATE TRIGGER `%%_stores_products_AUPD` AFTER UPDATE ON `%%_stores_products` FOR EACH ROW BEGIN IF OLD.`iProductVariant` = NEW.`iProductVariant` THEN UPDATE `%%_products_variants` `pv` SET `pv`.`TiStock` = `pv`.`TiStock` + GREATEST(NEW.`iStock`, 0) - GREATEST(OLD.`iStock`, 0) WHERE `iProductVariant` = NEW.`iProductVariant`; UPDATE `%%_products_variants` `pv` INNER JOIN(SELECT IF(`sp`.`iStock` > 0, IFNULL(`sp`.`iAvailableOnStock`, IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) ), IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = NEW.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a` USING(`iAvailable`) INNER JOIN(SELECT IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = NEW.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq2` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a2` ON(`a2`.`iAvailable` = `sq2`.`iAvailable`) SET `pv`.`TiAvailable` = `a`.`iAvailable`, `pv`.`TiCanBuy` = `a`.`iCanBuy`, `pv`.`TiCanBuyOutOfStock` = `a2`.`iCanBuy`; ELSE UPDATE `%%_products_variants` `pv` SET `pv`.`TiStock` = `pv`.`TiStock` + GREATEST(NEW.`iStock`, 0) WHERE `iProductVariant` = NEW.`iProductVariant`; UPDATE `%%_products_variants` `pv` SET `pv`.`TiStock` = `pv`.`TiStock` - GREATEST(OLD.`iStock`, 0) WHERE `iProductVariant` = OLD.`iProductVariant`; UPDATE `%%_products_variants` `pv` INNER JOIN(SELECT IF(`sp`.`iStock` > 0, IFNULL(`sp`.`iAvailableOnStock`, IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) ), IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = NEW.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a` USING(`iAvailable`) INNER JOIN(SELECT IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = NEW.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq2` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a2` ON(`a2`.`iAvailable` = `sq2`.`iAvailable`) SET `pv`.`TiAvailable` = `a`.`iAvailable`, `pv`.`TiCanBuy` = `a`.`iCanBuy`, `pv`.`TiCanBuyOutOfStock` = `a2`.`iCanBuy`; UPDATE `%%_products_variants` `pv` INNER JOIN(SELECT IF(`sp`.`iStock` > 0, IFNULL(`sp`.`iAvailableOnStock`, IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) ), IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = OLD.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a` USING(`iAvailable`) INNER JOIN(SELECT IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = OLD.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq2` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a2` ON(`a2`.`iAvailable` = `sq2`.`iAvailable`) SET `pv`.`TiAvailable` = `a`.`iAvailable`, `pv`.`TiCanBuy` = `a`.`iCanBuy`, `pv`.`TiCanBuyOutOfStock` = `a2`.`iCanBuy`; END IF; END
DROP TRIGGER IF EXISTS `%%_stores_products_ADEL`
CREATE TRIGGER `%%_stores_products_ADEL` AFTER DELETE ON `%%_stores_products` FOR EACH ROW BEGIN UPDATE `%%_products_variants` `pv` SET `pv`.`TiStock` = `pv`.`TiStock` - GREATEST(OLD.`iStock`, 0) WHERE `iProductVariant` = OLD.`iProductVariant`; UPDATE `%%_products_variants` `pv` INNER JOIN(SELECT IF(`sp`.`iStock` > 0, IFNULL(`sp`.`iAvailableOnStock`, IFNULL(`s`.`iAvailableOnStock`, `da`.`iAvailableOnStock`) ), IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = OLD.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a` USING(`iAvailable`) INNER JOIN(SELECT IFNULL(`sp`.`iAvailableOutOfStock`, IFNULL(`s`.`iAvailableOutOfStock`, `da`.`iAvailableOutOfStock`) ) AS `iAvailable`, `sp`.`iProductVariant` FROM `%%_stores_products` `sp` INNER JOIN `%%_stores` `s` USING(`iStore`) INNER JOIN `%%_default_availability` `da` WHERE `sp`.`iProductVariant` = OLD.`iProductVariant` ORDER BY `iAvailable` ASC LIMIT 1) `sq2` USING(`iProductVariant`) INNER JOIN `%%_products_available` `a2` ON(`a2`.`iAvailable` = `sq2`.`iAvailable`) SET `pv`.`TiAvailable` = `a`.`iAvailable`, `pv`.`TiCanBuy` = `a`.`iCanBuy`, `pv`.`TiCanBuyOutOfStock` = `a2`.`iCanBuy`; END
DROP TRIGGER IF EXISTS `%%_forms_AFTER_DELETE`
CREATE DEFINER = CURRENT_USER TRIGGER `%%_forms_AFTER_DELETE` AFTER DELETE ON `%%_forms` FOR EACH ROW BEGIN DELETE FROM `%%_forms_fields` WHERE `iForm` = OLD.`iForm`; END
DROP TRIGGER IF EXISTS `%%_forms_fields_AFTER_DELETE`
CREATE DEFINER = CURRENT_USER TRIGGER `%%_forms_fields_AFTER_DELETE` AFTER DELETE ON `%%_forms_fields` FOR EACH ROW BEGIN DELETE FROM `%%_forms_fields_values` WHERE `iFormField` = OLD.`iFormField`; END
SQL;
	if( is_file( DIR_LANG . $sLanguage . '.php' ) ) return null;
	if( !is_file( DIR_LANG . $sLanguageFrom . '.php' ) ) return null;
	copy( 'configure/lang_' . $sLanguageFrom . '.php', 'configure/lang_' . $sLanguage . '.php' );
	copy( 'configure/carriers_' . $sLanguageFrom . '.php', 'configure/carriers_' . $sLanguage . '.php' );
	copy( DIR_TEMPLATES . 'admin/' . $sLanguageFrom . '_allegro_template.tpl', DIR_TEMPLATES . 'admin/' . $sLanguage . '_allegro_template.tpl' );
	copy( DIR_TEMPLATES . 'admin/' . $sLanguageFrom . '_ebay_template.tpl', DIR_TEMPLATES . 'admin/' . $sLanguage . '_ebay_template.tpl' );
	copy( DIR_TEMPLATES . 'admin/' . $sLanguageFrom . '_swistak_template.tpl', DIR_TEMPLATES . 'admin/' . $sLanguage . '_swistak_template.tpl' );
	copy( DIR_LANG . $sLanguageFrom . '.php', DIR_LANG . $sLanguage . '.php' );
	$oDir = dir( DIR_DB );
	while( false !== ( $sFile = $oDir->read( ) ) ){
		$sFileName = $oDB->throwNameOfFile( $sFile );
		if( isset( $sFileName ) && substr( $sFileName, 0, 3 ) == $sLanguageFrom . '_' ){
			if( strpos( $sFileName, '.def' ) !== false ){
				copy( DIR_DB . $sFile, DIR_DB . $sLanguage . substr( $sFile, 2 ) );
			}
		}
	}
	$oDir->close( );
	$sql = str_replace( Array( '%%', '&&', '$$_' ), Array( MYSQL_TABLE_PREFIX . $sLanguage, MYSQL_TABLE_PREFIX . $sLanguageFrom, MYSQL_TABLE_PREFIX ), $sql );
	$aSqls = explode( "\n", $sql );
	foreach( $aSqls as $sql ){
		$sql = trim( $sql );
		if( !empty( $sql ) ){
			$oDB->executeSQL( $sql );
		}
	}
}
function deleteLanguage( $sLanguage ){
	$aLanguages = throwLanguages( );
	if( !in_array( $sLanguage, $aLanguages ) || ( count( $aLanguages ) <= 1 ) ) return false;
	$oDB = DataBase::getInstance( );
	$sDb = MYSQL_DB;
	if( is_file( DIR_LANG . $sLanguage . '.php' ) ) unlink( DIR_LANG . $sLanguage . '.php' );
	if( is_file( 'configure/lang_' . $sLanguage . '.php' ) ) unlink( 'configure/lang_' . $sLanguage . '.php' );
	if( is_file( 'configure/carriers_' . $sLanguage . '.php' ) ) unlink( 'configure/carriers_' . $sLanguage . '.php' );
	if( is_file( DIR_TEMPLATES . 'admin/' . $sLanguage . '_allegro_template.tpl' ) ) unlink( DIR_TEMPLATES . 'admin/' . $sLanguage . '_allegro_template.tpl' );
	if( is_file( DIR_TEMPLATES . 'admin/' . $sLanguage . '_ebay_template.tpl' ) ) unlink( DIR_TEMPLATES . 'admin/' . $sLanguage . '_ebay_template.tpl' );
	if( is_file( DIR_TEMPLATES . 'admin/' . $sLanguage . '_swistak_template.tpl' ) ) unlink( DIR_TEMPLATES . 'admin/' . $sLanguage . '_swistak_template.tpl' );
	$oDir = dir( DIR_DB );
	while( false !== ( $sFile = $oDir->read( ) ) ){
		$sFileName = $oDB->throwNameOfFile( $sFile );
		if( isset( $sFileName ) && substr( $sFileName, 0, 3 ) == $sLanguage . '_' ){
			unlink( DIR_DB . $sFile );
		}
	}
	$oDir->close( );
	$sql = 'SELECT `table_name` FROM `information_schema`.`tables` WHERE `table_schema`=\'' . $sDb . '\' AND `table_name` LIKE \'' . MYSQL_TABLE_PREFIX . $sLanguage . '_%\' AND `table_type` = \'BASE TABLE\'';
	$aTables = $oDB->executeSQL( $sql, true, DataBase::SINGLE_COLUMN );
	$sql = null;
	foreach( $aTables as $sTable ){
		$oDB->executeSQL( 'DROP TABLE `' . $sTable . '`' );
	}
	$oDB->executeSQL( 'DROP function IF EXISTS `' . MYSQL_TABLE_PREFIX . $sLanguage . '_calculateProductPriceForUser`;' );
	$oDB->executeSQL( 'DROP function IF EXISTS `' . MYSQL_TABLE_PREFIX . $sLanguage . '_calculateProductVariantPriceForUser`;' );
	$oDB->executeSQL( 'DROP function IF EXISTS `' . MYSQL_TABLE_PREFIX . $sLanguage . '_getProductCanBuyForUser`;' );
	$oDB->executeSQL( 'DROP function IF EXISTS `' . MYSQL_TABLE_PREFIX . $sLanguage . '_getProductVisibleForUser`;' );
	$oDB->executeSQL( 'DROP function IF EXISTS `' . MYSQL_TABLE_PREFIX . $sLanguage . '_getProductVisiblePriceForUser`;' );
	//Deaktywacja usuwanego jêzyka
	$aEnabledLangs = json_decode( htmlspecialchars_decode( $GLOBALS['config']['enabled_langs'] ) );
	foreach( $aEnabledLangs as $iKey => $mVal ){
		if( $mVal == $sLanguage ) unset( $aEnabledLangs[$iKey] );
	}
	sort( $aEnabledLangs );
	$aSave = Array( 'enabled_langs' => json_encode( $aEnabledLangs ) );
	saveVariables( $aSave, 'configure/general.php' );
}
?>