<?php
function throwAttributesList( ){
	$oDB = & DataBase::getInstance( );
	return $oDB->throwFileArray( DB_ATTRIBUTES );
}
function throwAttributesListForSearch( $sPhrase ){
	$oDB = & DataBase::getInstance( );
	$sql = 'SELECT SQL_CALC_FOUND_ROWS `iAttribute`, `sName`, `iPosition`, `iType`, `iFilterVisible`, `iNameVisible`, `iBasketVisible`' /*`iProductListVisible`, */
			. 'FROM `' . $oDB->throwTableName( DB_ATTRIBUTES ) . '` AS `attributes` 
			WHERE 1';
	if( isset( $sPhrase ) && !empty( $sPhrase ) ){
		preg_match_all( '/(-?)((\\"([^\\"]+)\\")|([^\\"\\s][^\\s]*))/', htmlspecialchars_decode( $sPhrase ), $aWords, PREG_SET_ORDER );
		$aSearchP = Array( );
		foreach( $aWords as $aWord ){
			$sWord = $aWord[4];
			if( empty( $sWord ) ) $sWord = $aWord[5];
			$sWord = $oDB->escape( $sWord );
			if( $aWord[1] == '-' ){
				$aSearchP[] = '( `attributes`.`sName` NOT LIKE \'%' . $sWord . '%\' )';
			} else{
				$aSearchP[] = '( `attributes`.`sName` LIKE \'%' . $sWord . '%\' )';
			}
		}
		$sql.= ' AND (' . implode( ' AND ', $aSearchP ) . ')';
	}
	
	/* Uzupełnienie SQL - ORDER i LIMIT */{
		//Products order
		if( !isset( $iList ) || empty( $iList ) ){
			$iList = $GLOBALS['config']['admin_list'];
		}
		$iPageNumber = $GLOBALS['iPage'];
		if( !isset( $iPageNumber ) || !is_numeric( $iPageNumber ) || $iPageNumber < 1 ) $iPageNumber = 1;
		$iEnd = $iPageNumber * $iList;
		$iStart = $iEnd - $iList;
		$sql.= ' LIMIT ' . $iStart . ',' . $iList;
		/* -- Koniec uzupełnienia SQL */
	}
	$aReturn['aList'] = $oDB->executeSQL( $sql, true );
	$sql = 'SELECT FOUND_ROWS() FROM `' . $oDB->throwTableName( DB_ATTRIBUTES ). '`';
	$iCount = $oDB->executeSQL( $sql, true, DataBase::SINGLE_VALUE );
	$aReturn['aPagesCountData'] = Array( 'iCount' => ceil( $iCount / $iList ), 'iPageNumber' => $iPageNumber );
	
	return $aReturn;
}
function throwAttributesListForSelect( ){
	$oDB = & DataBase::getInstance( );
	return $oDB->throwFileArraySmall( DB_ATTRIBUTES, 'iAttribute', 'sName' );
}
function throwAttributesValuesList( $iAttribute ){
	$oDB = & DataBase::getInstance( );
	$sql = 'SELECT `iAttributeValue`, `sValue`, `iPosition`
						FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '`
						WHERE `iAttribute` = ' . intval( $iAttribute ) . '
						ORDER BY `iPosition`, `sValue`';
	return $oDB->executeSQL( $sql, true );
}
function throwAttributesValuesGroups( $iAttribute ){
	$oDB = & DataBase::getInstance( );
	$sql = 'SELECT `iFilterGroup`, `sValue`, `iPosition`
						FROM `' . $oDB->throwTableName( DB_FILTER_GROUPS ) . '`
						WHERE `iAttribute` = ' . intval( $iAttribute ) . '
						ORDER BY `iPosition`, `sValue`';
	return $oDB->executeSQL( $sql, true );
}
function saveAttribute( $aForm ){
	$oDB = & DataBase::getInstance( );
	if( isset( $aForm['iAttribute'] ) ) $sParam = 'iAttribute';
	else $sParam = null;
	if( empty( $aForm['iPosition'] ) ) $aForm['iPosition'] = 0;
	$aForm['iAttribute'] = $oDB->save( DB_ATTRIBUTES, $aForm, $sParam );
	
	$oDB->deleteInFile( DB_ATTRIBUTES_ALT, $aForm['iAttribute'], 'iAttribute' );
	$aForm['aAltValues'] = explode( "\n", $aForm['sAltValues'] );
	if( is_array( $aForm['aAltValues'] ) ) foreach( $aForm['aAltValues'] as $sAlt ){
		$sAlt = trim( $sAlt );
		if( !empty( $sAlt ) ){
			if( $aForm['iReplaceAltValues'] ){
				$aAttributesAlt = $oDB->executeSQL( 'SELECT `iAttribute` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES ) . '` WHERE `sName` LIKE \'' . $sAlt . '\'', true, DataBase::SINGLE_COLUMN );
				if( is_array( $aAttributesAlt ) ) foreach( $aAttributesAlt as $iAttributeAlt ){
					$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` SET `iAttribute` = ' . intval( $aForm['iAttribute'] ) . ' WHERE `iAttribute` = ' . $iAttributeAlt );
					$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_ATTRIBUTES ) . '` WHERE `iAttribute` = ' . $iAttributeAlt );
				}
				$aValuesToMerge = $oDB->executeSQL( 'SELECT GROUP_CONCAT( `iAttributeValue` ORDER BY `iAttributeValue` SEPARATOR \',\' ) FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttribute` = ' . intval( $aForm['iAttribute'] ) . ' GROUP BY `sValue` HAVING COUNT( `iAttributeValue` ) > 1 ', true, DataBase::SINGLE_COLUMN );
				foreach( $aValuesToMerge as $aValues ){
					$aValues = explode( ',', $aValues, 2 );
					$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_PRODUCTS_ATTRIBUTES ) . '` SET `iAttributeValue` = ' . $aValues[0] . ' WHERE `iAttributeValue` IN( ' . $aValues[1] . ' )' );
					$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_ATTRIBUTES_ALT_VALUES ) . '` SET `iAttributeValue` = ' . $aValues[0] . ' WHERE `iAttributeValue` IN( ' . $aValues[1] . ' )' );
					$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttributeValue` IN( ' . $aValues[1] . ' )' );
				}
			}
			$oDB->save( DB_ATTRIBUTES_ALT, Array( 'iAttribute' => $aForm['iAttribute'], 'sName' => $sAlt ) );
		}
	}
	return( $aForm['iAttribute'] );
	
}
function deleteAttribute( $iAttribute ){
	$oDB = & DataBase::getInstance( );
	$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_PRODUCTS_ATTRIBUTES ) . '` WHERE `iAttributeValue` IN ( SELECT `iAttributeValue` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttribute` = ' . intval( $iAttribute ) . ' )' );
	$oDB->deleteInFile( DB_ATTRIBUTES_VALUES, $iAttribute, 'iAttribute' );
	$oDB->deleteInFile( DB_ATTRIBUTES, $iAttribute, 'iAttribute' );
}
function saveAttributeValue( $iAttribute, $aValue, $iValue = null ){
	$oDB = & DataBase::getInstance( );
	if( isset( $iValue ) ){
		$oDB->update( DB_ATTRIBUTES_VALUES, Array( 'sValue' => $aValue['sValue'], 'iPosition' => $aValue['iPosition'], 'iFilterVisible' => $aValue['iFilterVisible'] ), Array( 'iAttributeValue' => $iValue ) );
		$iAttribute = $oDB->executeSQL( 'SELECT `iAttribute` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttributeValue` = ' . $iValue, true, DataBase::SINGLE_VALUE );
	} else{
		$iValue = $oDB->save( DB_ATTRIBUTES_VALUES, Array( 'iAttribute' => $iAttribute, 'sValue' => $aValue['sValue'], 'iPosition' => $aValue['iPosition'], 'iFilterVisible' => $aValue['iFilterVisible'] ) );
	}
	$oDB->deleteInFile( DB_ATTRIBUTES_ALT_VALUES, $iValue, 'iAttributeValue' );
	$aValue['aAltValues'] = explode( "\n", $aValue['sAltValues'] );
	if( is_array( $aValue['aAltValues'] ) ) foreach( $aValue['aAltValues'] as $sValue ){
		$sValue = trim( $sValue );
		if( !empty( $sValue ) ){
			if( $_POST['iReplaceAltValues'] ){
				$aAttributesAlt = $oDB->executeSQL( 'SELECT `iAttributeValue` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttribute` = ' . $iAttribute . ' AND `sValue` LIKE \'' . $sValue . '\'', true, DataBase::SINGLE_COLUMN );
				if( is_array( $aAttributesAlt ) ) foreach( $aAttributesAlt as $iAttributeAlt ){
					$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_PRODUCTS_ATTRIBUTES ) . '` SET `iAttributeValue` = ' . $iValue . ' WHERE `iAttributeValue` = ' . $iAttributeAlt );
					$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttributeValue` = ' . $iAttributeAlt );
				}
			}
			$oDB->save( DB_ATTRIBUTES_ALT_VALUES, Array( 'iAttributeValue' => $iValue, 'sValue' => $sValue ) );
		}
	}
	return( $iValue );
}
function deleteAttributeValue( $iAttributeValue ){
	$oDB = & DataBase::getInstance( );
	if( $oDB->executeSQL( 'SELECT 1 FROM `' . $oDB->throwTableName( DB_PRODUCTS_ATTRIBUTES ) . '` WHERE `iAttributeValue` = ' . intval( $iAttributeValue ), true, DataBase::SINGLE_VALUE ) ){
		return false;
	}
//	$oDB->deleteInFile( DB_PRODUCTS_ATTRIBUTES, $iAttributeValue, 'iAttributeValue' );
	$oDB->deleteInFile( DB_ATTRIBUTES_FILTER_GROUPS, $iAttributeValue, 'iAttributeValue' );
	$oDB->deleteInFile( DB_ATTRIBUTES_VALUES, $iAttributeValue, 'iAttributeValue' );
	return true;
}
function throwAttributeValuesGroup( $iFilterGroup ){
	$oDB = & DataBase::getInstance( );
	$aGroup = $oDB->throwData( DB_FILTER_GROUPS, $iFilterGroup, 'iFilterGroup' );
	$aGroup['aValues'] = $oDB->executeSQL( 'SELECT `iAttributeValue` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_FILTER_GROUPS ) . '` WHERE `iFilterGroup` = ' . $iFilterGroup, true, DataBase::SINGLE_COLUMN );
	return( $aGroup );
}
function saveAttributeValuesGroup( $aGroup ){
	$oDB = & DataBase::getInstance( );
	if( !empty( $aGroup['iFilterGroup'] ) ){
		$oDB->update( DB_FILTER_GROUPS, Array( 'sValue' => $aGroup['sValue'], 'iPosition' => $aGroup['iPosition'] ), Array( 'iFilterGroup' => $aGroup['iFilterGroup'] ) );
	} else{
		$aGroup['iFilterGroup'] = $oDB->save( DB_FILTER_GROUPS, $aGroup );
	}
	$oDB->deleteInFile( DB_ATTRIBUTES_FILTER_GROUPS, $aGroup['iFilterGroup'], 'iFilterGroup' );
	if( is_array( $aGroup['aValues'] ) ) foreach( $aGroup['aValues'] as $iValue ){
		$oDB->save( DB_ATTRIBUTES_FILTER_GROUPS, Array( 'iAttributeValue' => $iValue, 'iFilterGroup' => $aGroup['iFilterGroup'] ) );
	}
	return( $aGroup['iFilterGroup'] );
}
function deleteAttributeValuesGroup( $iFilterGroup ){
	$oDB = & DataBase::getInstance( );
	$oDB->deleteInFile( DB_ATTRIBUTES_FILTER_GROUPS, $iFilterGroup, 'iFilterGroup' );
	$oDB->deleteInFile( DB_FILTER_GROUPS, $iFilterGroup, 'iFilterGroup' );
}
function throwSelectedAttributes( $iProduct = null ){
	$oDB = & DataBase::getInstance( );
	if( isset( $iProduct ) && is_numeric( $iProduct ) ){
		$sql = 'SELECT DISTINCT `iAttribute`
							FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '`
								NATURAL JOIN `' . $oDB->throwTableName( DB_PRODUCTS_ATTRIBUTES ) . '`
								NATURAL JOIN `' . $oDB->throwTableName( DB_PRODUCTS_VARIANTS ) . '`
							WHERE `iProduct` = ' . intval( $iProduct );
		return $oDB->executeSQL( $sql, true, DataBase::SINGLE_COLUMN );
	} else{
		return Array( );
	}
}
function throwAttributesGroupsList( ){
	$oDB = & DataBase::getInstance( );
	return $oDB->throwFileArray( DB_ATTRIBUTES_GROUPS );
}
function throwAttributesGroupsInfo( ){
	$oDB = & DataBase::getInstance( );
	return $oDB->executeSQL( 'SELECT `g`.`iGroup`, `g`.`sName`, CONCAT( \'[\', GROUP_CONCAT( `a`.`iAttribute` SEPARATOR \',\' ), \']\' ) AS `aAttributesJSON` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_GROUPS ) . '` `g` LEFT JOIN `' . $oDB->throwTableName( DB_ATTRIBUTES_TO_GROUPS ) . '` `a` USING( `iGroup` ) GROUP BY `g`.`sName`, `g`.`iGroup` ORDER BY `g`.`sName`, `g`.`iGroup`', true );
	return $oDB->throwFileArray( DB_ATTRIBUTES_GROUPS );
}
function throwAttribtesGroup( $iGroup ){
	$oDB = & DataBase::getInstance( );
	$aAttributesGroup = $oDB->throwData( DB_ATTRIBUTES_GROUPS, $iGroup, 'iGroup' );
	$aAttributesGroup['aAttributes'] = $oDB->executeSQL( 'SELECT `iAttribute` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_TO_GROUPS ) . '` WHERE `iGroup` = ' . intval( $iGroup ), true, DataBase::SINGLE_COLUMN );
	return $aAttributesGroup;
}
function saveAttributesGroup( $aForm ){
	$oDB = & DataBase::getInstance( );
	$iGroup = $oDB->save( DB_ATTRIBUTES_GROUPS, $aForm, ( !empty( $aForm['iGroup'] ) ? 'iGroup' : null ) );
	$oDB->deleteInFile( DB_ATTRIBUTES_TO_GROUPS, $iGroup, 'iGroup' );
	if( is_array( $aForm['aAttributes'] ) ) foreach( $aForm['aAttributes'] as $iAttribute ){
		$oDB->save( DB_ATTRIBUTES_TO_GROUPS, Array( 'iAttribute' => $iAttribute, 'iGroup' => $iGroup ) );
	}
	return $iGroup;
}
function deleteAttributesGroup( $iGroup ){
	$oDB = & DataBase::getInstance( );
	$oDB->deleteInFile( DB_ATTRIBUTES_TO_GROUPS, $iGroup, 'iGroup' );
	$oDB->deleteInFile( DB_ATTRIBUTES_GROUPS, $iGroup, 'iGroup' );
}

function saveProductAttribute( $iProduct, $aForm ){
	$oDB = & DataBase::getInstance( );
	if( is_array( $aForm ) ) foreach( $aForm as $iAttribute => $aAttribute ){
		foreach( $aAttribute as $iAttributeValue ){
			$sQuery = 'INSERT INTO `' . $oDB->throwTableName( DB_PRODUCTS_ATTRIBUTES_CONST ) . '` ( `iAttribute`, `iAttributeValue`, `iProduct` ) VALUES ( ' . $iAttribute . ', ' . $iAttributeValue . ', ' . $iProduct . ' )';
			$oDB->executeSQL( $sQuery );
		}
	}
}

function deleteProductAttribute( $iProduct ){
	$oDB = & DataBase::getInstance( );
	$sQuery = 'DELETE FROM `' . $oDB->throwTableName( DB_PRODUCTS_ATTRIBUTES_CONST ) . '` WHERE `iProduct` = ' . $iProduct;
	return $oDB->executeSQL( $sQuery );
}

?>