<?php
if( $a == 'list' ){
	if( isset( $_POST['sOption'] ) ){
		if( is_array( $_POST['aAttributesPositions'] ) ) foreach( $_POST['aAttributesPositions'] as $iAttribute => $iPosition ){
			$oDB->update( DB_ATTRIBUTES, Array( 'iPosition' => $iPosition ), Array( 'iAttribute' => $iAttribute ) );
		}
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-list&sOption=save' . '&activeTabId=' . $_POST['activeTabId'] );
		exit;
	}
	if( isset( $sOption ) ){
		$content.= $oTpl->parse( 'message_done.tpl' );
	}
	$aAttributes = throwAttributesListForSearch( $_POST['sPhrase'] );
	/*if( isset( $_POST['sPhrase'] ) ){
	}else{
	$aAttributes = throwAttributesList( );
	}*/
	$oTpl->assign( 'a', $a );
	$oTpl->assign( 'aAttributes', $aAttributes );
	$aAttributesGroups = throwAttributesGroupsList( );
	$oTpl->assign( 'aAttributesGroups', $aAttributesGroups );
	$content.= $oTpl->parse( 'attributes_list.tpl' );
} elseif( ( $a == 'form' ) ){
	if( isset( $_POST['sName'] ) ){
		$_POST['iAttribute'] = $iAttribute;
		$iAttribute = saveAttribute( $_POST );
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form&sOption=save&iAttribute=' . $iAttribute . '&activeTabId=' . $_POST['activeTabId'] );
		exit;
	}
	if( isset( $sOption ) ){
		$content.= $oTpl->parse( 'message_done.tpl' );
	}
	$aAttribute = $oDB->throwData( DB_ATTRIBUTES, intval( $iAttribute ), 'iAttribute' );
	if( empty( $aAttribute ) ){
		$aAttribute = Array( 'iFilterVisible' => 1, 'iBasketVisible' => 1 );
	} else{
		$aAttributeAlt = $oDB->executeSQL( 'SELECT `sName` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_ALT ) .'` WHERE `iAttribute` = ' . intval( $iAttribute ), true, DataBase::SINGLE_COLUMN );
		$aAttribute['sAltValues'] = implode( "\n", $aAttributeAlt );
	}
	$aAttribute['aValues'] = throwAttributesValuesList( intval( $_GET['iAttribute'] ) );
	$aAttribute['aValuesGroups'] = throwAttributesValuesGroups( intval( $_GET['iAttribute'] ) );
	$oTpl->assign( 'aAttribute', $aAttribute );
	$oTpl->assign( 'aAttributesTypes', $aAttributesTypes );
	$content.= $oTpl->parse( 'attributes_form.tpl' );
} elseif( ( $a == 'delete_attribute' ) && isset( $iAttribute ) ){
	deleteAttribute( $iAttribute );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-list&sOption=delete' );
	exit;
} elseif( ( $a == 'form_value' ) ){
	if( isset( $_POST['sValue'] ) ){
		if( !empty( $iAttributeValue ) ){
			$_POST['iAttributeValue'] = $iAttributeValue;
		} else{
			$iAttributeValue = null;
		}
		$iAttributeValue = saveAttributeValue( $iAttribute, $_POST, $iAttributeValue );
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form_value&sOption=save&iAttributeValue=' . $iAttributeValue );
		exit;
	}
	if( isset( $sOption ) ){
		$content.= $oTpl->parse( 'message_done.tpl' );
	}
	$iAttributeValue = intval( $iAttributeValue );
	if( !empty( $iAttributeValue ) ){
		$aAttributeValue = $oDB->throwData( DB_ATTRIBUTES_VALUES, intval( $iAttributeValue ), 'iAttributeValue' );
		$aAttributeAltValues = $oDB->executeSQL( 'SELECT `sValue` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_ALT_VALUES ) .'` WHERE `iAttributeValue` = ' . intval( $iAttributeValue ), true, DataBase::SINGLE_COLUMN );
		$aAttributeValue['sAltValues'] = implode( "\n", $aAttributeAltValues );
	} else{
		$aAttributeValue = Array( 'iAttribute' => $iAttribute, 'iFilterVisible' => 1, 'iPosition' => 0 );
	}
	$aAttribute = $oDB->throwData( DB_ATTRIBUTES, intval( $aAttributeValue['iAttribute'] ), 'iAttribute' );
	$oTpl->assign( 'aAttributeValue', $aAttributeValue );
	$oTpl->assign( 'aAttribute', $aAttribute );
	$content.= $oTpl->parse( 'attributes_value_form.tpl' );
} elseif( ( $a == 'update_pos' ) ){
	if( is_array( $_POST['aPositions'] ) ) foreach( $_POST['aPositions'] as $iValue => $iPosition ){
		$oDB->update( DB_ATTRIBUTES_VALUES, Array( 'iPosition' => $iPosition ), Array( 'iAttributeValue' => $iValue ) );
	}
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form&sOption=save&iAttribute=' . $iAttribute . '&activeTabId=' . $_POST['activeTabId'] );
	exit;
} elseif( ( $a == 'values_groups_update_pos' ) ){
	if( is_array( $_POST['aPositions'] ) ) foreach( $_POST['aPositions'] as $iFilterGroup => $iPosition ){
		$oDB->update( DB_FILTER_GROUPS, Array( 'iPosition' => $iPosition ), Array( 'iFilterGroup' => $iFilterGroup ) );
	}
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form&sOption=save&iAttribute=' . $iAttribute . '&activeTabId=' . $_POST['activeTabId'] );
	exit;
} elseif( ( $a == 'delete_value' ) && isset( $iAttributeValue ) ){
	if( is_numeric( $iAttributeValue ) ){
		if( deleteAttributeValue( $_GET['iAttributeValue'] ) ){
			header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form&sOption=delete&iAttribute=' . intval( $iAttribute ) );
			exit;
		} else{
			$oTpl->assign( 'sErrorMessage', $lang['Attribute_value_used'] );
			$content .= $oTpl->parse( 'message_other_error.tpl' );
		}
	}
} elseif( $a == 'form_values_group' ){
	if( isset( $_POST['sOption'] ) ){
		$iGroup = saveAttributeValuesGroup( $_POST );
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form_values_group&sOption=save&iFilterGroup=' . $iGroup );
		exit;
	}
	if( isset( $sOption ) ){
		$content.= $oTpl->parse( 'message_done.tpl' );
	}
	if( is_numeric( $iFilterGroup ) ){
		$aGroup = throwAttributeValuesGroup( $iFilterGroup );
	} else{
		$aGroup = Array( 'iAttribute' => intval( $iAttribute ), 'aValues' => Array() );
	}
	$aGeneralInfo['aAttributesValues'] = $oDB->executeSQL( 'SELECT `iAttributeValue`, `sValue` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttribute` = ' . $aGroup['iAttribute'] . ' ORDER BY `iPosition`, `sValue`', true, DataBase::ASSOCIATIVE );
	$oTpl->assign( 'aGroup', $aGroup );
	$oTpl->assign( 'aGeneralInfo', $aGeneralInfo );
	$content .= $oTpl->parse( 'attributes_values_groups_form.tpl' );
} elseif( $a == 'delete_values_group' ){
	$iAttribute = $oDB->executeSQL( 'SELECT `iAttribute` FROM `' . $oDB->throwTableName( DB_FILTER_GROUPS ) . '` WHERE `iFilterGroup` = ' . intval( $iFilterGroup ), true, DataBase::SINGLE_VALUE );
	deleteAttributeValuesGroup( $iFilterGroup );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-form&sOption=delete&iAttribute=' . $iAttribute );
	exit;
} elseif( $a == 'groups_form' ){
	if( isset( $_POST['sOption'] ) ){
		$iGroup = saveAttributesGroup( $_POST );
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-groups_form&sOption=save&iGroup=' . $iGroup );
		exit;
	}
	if( isset( $sOption ) ){
		$content.= $oTpl->parse( 'message_done.tpl' );
	}
	if( isset( $iGroup ) ){
		$aGroup = throwAttribtesGroup( $iGroup );
	}
	if( empty( $aGroup ) ){
		$aGroup = Array( 'aAttributes' => Array(), 'sName' => '' );
	}
	$aGeneralInfo['aAttributes'] = throwAttributesListForSelect( );
	$oTpl->assign( 'aGroup', $aGroup );
	$oTpl->assign( 'aGeneralInfo', $aGeneralInfo );
	$content.= $oTpl->parse( 'attributes_groups_form.tpl' );
} elseif( ( $a == 'groups_delete' ) && isset( $iGroup ) ){
	deleteAttributesGroup( $iGroup );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-list&sOption=delete' );
	exit;
} elseif( $a == 'ajax_value' && isset( $sValue ) && is_numeric( $iAttribute ) ){
	header( 'Content-type: text/json' );
	echo( json_encode( saveAttributeValue( $iAttribute, Array( 'sValue' => $sValue ) ) ) );
	exit;
}
?>