<?php
if( $a == 'list' ){
	if( isset( $sOption ) ) $content.= $oTpl->parse( 'message_done.tpl' );
	if( $aActions['o1'] == 'export' ){
		$aUsers = throwUsers( $_GET['iAnon'] ? true : false, 'inf', true );
		if( $aActions['o2'] == 'xml' ){
			$xml = new DOMDocument( '1.0', 'utf-8' );
			$xml->formatOutput = true;
			$root = $xml->createElement( 'users' );
			
			foreach( $aUsers as $aUser ){
				$user = $xml->createElement( 'user' );
				foreach( $aUser as $sField => $mField ) if( !empty( $mField ) && $sField != 'sPass' ){
					if( is_array( $mField ) ){
						$groups = $xml->createElement( $sField );
						foreach( $mField as $aGroup ){
							$group = $xml->createElement( 'group' );
							foreach( $aGroup as $sFieldS => $mFieldS ){
								$field = $xml->createElement( $sFieldS, $mFieldS );
								$group->appendChild( $field );
							}
							$groups->appendChild( $group );
						}
						$user->appendChild( $groups );
					} else{
						$field = $xml->createElement( $sField, $mField );
						$user->appendChild( $field );
					}
				}
				$root->appendChild( $user );
			}
			$xml->appendChild( $root );
			header( 'Content-type: application/xml' );
			header( 'Content-Disposition: attachment; filename=users.xml;' );
			echo $xml->saveXML( );
		} elseif( $aActions['o2'] == 'csv' ){
			$aCountries = $oDB->throwFileArraySmall( DB_COUNTRIES, 'iCountry', 'sCountry' );
			header( 'Content-type: text/csv' );
			header( 'Content-Disposition: attachment; filename=users.csv;' );
			echo( iconv( 'UTF-8', 'CP1250', '"' . $lang['Id'] . '";"' . $lang['Login'] . '";"' . $lang['First_name'] . '";"' . $lang['Last_name'] . '";"' . $lang['Street'] . '";"' . $lang['Zip_code'] . '";"' . $lang['City'] . '";"' . $lang['Country'] . '";"' . $lang['Phone'] . '";"' . $lang['Email'] . '";"' . $lang['Register_time'] . '"' . "\n" ) );
			foreach( $aUsers as $aUser ){
				echo( iconv( 'UTF-8', 'CP1250', '"' . $aUser['iUser'] . '";"' . $aUser['sLogin'] . '";"' . $aUser['sFirstName'] . '";"' . $aUser['sLastName'] . '";"' . $aUser['sStreet'] . '";"' . $aUser['sZipCode'] . '";"' . $aUser['sCity'] . '";"' . $aCountries[$aUser['iCountry']] . '";"' . $aUser['sPhone'] . '";"' . $aUser['sEmail'] . '";"' . $aUser['iRegisterTime'] . '"' . "\n" ) );
			}
		}
		exit;
	} else{
		$aUsers = throwUsers( $_GET['iAnon'] ? true : false );
		$oTpl->assign( 'aUsers', $aUsers );
		$oTpl->assign( 'a', $a );
		$oTpl->assign( 'sUrlExt', '&amp;sPhrase=' . $_GET['sPhrase'] . '&amp;iAnon=' . $_GET['iAnon'] );
		$content.= $oTpl->parse( 'users_list.tpl' );
	}
} elseif( $a == 'delete' && isset( $iUser ) && is_numeric( $iUser ) ){
	delUser( $iUser );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=users-list&sOption=del' );
	exit;
} elseif( $a == 'form' ){
	if( isset( $_POST['sOption'] ) ){
		if( checkUserLoginNotExist( $_POST['sLogin'], $_POST['iUser'] ) == true && checkUserLoginNotExist( $_POST['sEmail'], $_POST['iUser'] ) ){
			$iUser = saveUser( $_POST );
			if( !isset( $_POST['aGroupsRelated'] ) || !is_array( $_POST['aGroupsRelated'] ) || ( count( $_POST['aGroupsRelated'] ) == 1 && $_POST['aGroupsRelated'][0] == '%iId%' ) ){
				delUsersToGroups( $iUser );
			} else{
				saveUserToGroups( $iUser, $_POST['aGroupsRelated'] );
			}
			header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $p . '&sOption=save&iUser=' . $iUser . '&activeTabId=' . $_POST['activeTabId'] );
			exit;
		} else $content.= $oTpl->parse( 'message_login_exists.tpl' );
	} else{
		if( isset( $sOption ) ) $content.= $oTpl->parse( 'message_done.tpl' );
		if( isset( $iUser ) && is_numeric( $iUser ) ){
			$aUser = throwUser( $iUser );
			$aUser['aGroupsSelected'] = $oDB->throwArrayRelated( DB_USERS_TO_GROUPS, $iUser, 'iUser', 'iGroup' );
		} else{
			$aUser = Array( 'iUser' => 0, 'aGroupsSelected' => Array( ) );
		}
		$aCountries = $oOrder->throwCountriesArray( 'iCountry', 'sCountry', true );
		$aGroupsTmp = throwGroups( );
		$aGroups = Array( );
		if( is_array( $aGroupsTmp ) ) foreach( $aGroupsTmp as $aGroup ){
			$aGroups[$aGroup['iGroup']] = $aGroup['sName'] . ' (' . $aGroup['sLang'] . ')';
		}
		// SmartOffer:
		if( isset( $tab ) ){
			$iCurrentTime = time();
			$aSmartoffer['aAvailabilities'] = $oDB->throwFileArraySmall( DB_PRODUCTS_AVAILABLE, 'iAvailable', 'sText', 'iAvailable' );
			switch( $tab ){
				case 'smartoffer-user-pages':
					$aSmartoffer['aPages'] = $oDB->executeSQL( 'SELECT `p`.`iPage`, `p`.`sName`, `p`.`iProducts`, `ua`.`iTime` FROM `' . $oDB->throwTableName( DB_PAGES ) . '` AS `p` INNER JOIN `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` USING( `iPage` ) WHERE `ua`.`iType` = 1 AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' ) ORDER BY `ua`.`iTime` DESC', true );
					foreach( $aSmartoffer['aPages'] as &$SOaPage ){
						$SOaPage['aPageTree'] = $oPage->getPagesTree( $SOaPage['iPage'] );
						$SOaPage['sTimeDifference'] = time_difference( $iCurrentTime - $SOaPage['iTime'] );
					}
				break;
				case 'smartoffer-user-products':
					$aSmartoffer['aProducts'] = $oDB->executeSQL( 'SELECT `p`.`iProduct`, `p`.`sName`, IF( `p`.`iPriceTimeFrom` < UNIX_TIMESTAMP() AND `p`.`iPriceTimeTo` > UNIX_TIMESTAMP() AND `p`.`fPriceTimeValue` < `p`.`fPrice`, `p`.`fPriceTimeValue`, `p`.`fPrice` ) AS `fCurrentPrice`, `ua`.`fPrice` AS `fOldPrice`, `p`.`TiAvailable`, `ua`.`iAvailable`, `ua`.`iTime` FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '` AS `p` INNER JOIN `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` USING( `iProduct` ) WHERE `ua`.`iType` = 1 AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' ) ORDER BY `ua`.`iTime` DESC', true );
					foreach( $aSmartoffer['aProducts'] as &$SOaProduct ){
						$SOaProduct['aPageTree'] = $oProduct->getPagesTree( $SOaProduct['iProduct'], $SOaProduct['sName'] );
						$SOaProduct['sTimeDifference'] = time_difference( $iCurrentTime - $SOaProduct['iTime'] );
						if( isset( $oFile->aImagesDefault[2][$SOaProduct['iProduct']] ) ){
							$SOaProduct['aDataImage'] = $oFile->aImagesDefault[2][$SOaProduct['iProduct']];
						}
					}
				break;
				case 'smartoffer-user-basket':
					$aSmartoffer['aBasket'] = $oDB->executeSQL( 'SELECT `p`.`iProduct`, `p`.`sName`, IF( `p`.`iPriceTimeFrom` < UNIX_TIMESTAMP() AND `p`.`iPriceTimeTo` > UNIX_TIMESTAMP() AND `p`.`fPriceTimeValue` < `p`.`fPrice`, `p`.`fPriceTimeValue`, `p`.`fPrice` ) AS `fCurrentPrice`, `ua`.`fPrice` AS `fOldPrice`, `p`.`TiAvailable`, `ua`.`iAvailable`, `ua`.`iTime` FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '` AS `p` INNER JOIN `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` USING( `iProduct` ) WHERE `ua`.`iType` = 4 AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' ) ORDER BY `ua`.`iTime` DESC', true );
					foreach( $aSmartoffer['aBasket'] as &$SOaProduct ){
						$SOaProduct['aPageTree'] = $oProduct->getPagesTree( $SOaProduct['iProduct'], $SOaProduct['sName'] );
						$SOaProduct['sTimeDifference'] = time_difference( $iCurrentTime - $SOaProduct['iTime'] );
						if( isset( $oFile->aImagesDefault[2][$SOaProduct['iProduct']] ) ){
							$SOaProduct['aDataImage'] = $oFile->aImagesDefault[2][$SOaProduct['iProduct']];
						}
					}
				break;
				case 'smartoffer-user-storage':
					$aSmartoffer['aStorage'] = $oDB->executeSQL( 'SELECT `p`.`iProduct`, `p`.`sName`, IF( `p`.`iPriceTimeFrom` < UNIX_TIMESTAMP() AND `p`.`iPriceTimeTo` > UNIX_TIMESTAMP() AND `p`.`fPriceTimeValue` < `p`.`fPrice`, `p`.`fPriceTimeValue`, `p`.`fPrice` ) AS `fCurrentPrice`, `ua`.`fPrice` AS `fOldPrice`, `p`.`TiAvailable`, `ua`.`iAvailable`, `ua`.`iTime` FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '` AS `p` INNER JOIN `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` USING( `iProduct` ) WHERE `ua`.`iType` = 3 AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' ) ORDER BY `ua`.`iTime` DESC', true );
					foreach( $aSmartoffer['aStorage'] as &$SOaProduct ){
						$SOaProduct['aPageTree'] = $oProduct->getPagesTree( $SOaProduct['iProduct'], $SOaProduct['sName'] );
						$SOaProduct['sTimeDifference'] = time_difference( $iCurrentTime - $SOaProduct['iTime'] );
						if( isset( $oFile->aImagesDefault[2][$SOaProduct['iProduct']] ) ){
							$SOaProduct['aDataImage'] = $oFile->aImagesDefault[2][$SOaProduct['iProduct']];
						}
					}
				break;
				case 'smartoffer-user-compare':
					$aSmartoffer['aCompare'] = $oDB->executeSQL( 'SELECT `p`.`iProduct`, `p`.`sName`, IF( `p`.`iPriceTimeFrom` < UNIX_TIMESTAMP() AND `p`.`iPriceTimeTo` > UNIX_TIMESTAMP() AND `p`.`fPriceTimeValue` < `p`.`fPrice`, `p`.`fPriceTimeValue`, `p`.`fPrice` ) AS `fCurrentPrice`, `ua`.`fPrice` AS `fOldPrice`, `p`.`TiAvailable`, `ua`.`iAvailable`, `ua`.`iTime` FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '` AS `p` INNER JOIN `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` USING( `iProduct` ) WHERE `ua`.`iType` = 2 AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' ) ORDER BY `ua`.`iTime` DESC, `sName` ASC, `iProduct` ASC', true );
					foreach( $aSmartoffer['aCompare'] as &$SOaProduct ){
						$SOaProduct['aPageTree'] = $oProduct->getPagesTree( $SOaProduct['iProduct'], $SOaProduct['sName'] );
						$SOaProduct['sTimeDifference'] = time_difference( $iCurrentTime - $SOaProduct['iTime'] );
						if( isset( $oFile->aImagesDefault[2][$SOaProduct['iProduct']] ) ){
							$SOaProduct['aDataImage'] = $oFile->aImagesDefault[2][$SOaProduct['iProduct']];
						}
					}
				break;
				case 'smartoffer-user-filter':
					$aSmartoffer['aFilter'] = $oDB->executeSQL( 'SELECT `p`.`iPage`, `p`.`sName`, `ua`.`sParameters`, `ua`.`iTime` FROM `' . $oDB->throwTableName( DB_PAGES ) . '` AS `p` INNER JOIN `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` USING( `iPage` ) WHERE `p`.`iProducts` > 0 AND `ua`.`sParameters` LIKE \'%fFilterPriceFrom=%\' AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' ) ORDER BY `ua`.`iTime` DESC, `sName` ASC, `iPage` ASC', true );
					$aSmartoffer['aProductsIds'] = $oDB->executeSQL( 'SELECT `ua`.`iProduct` FROM `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` WHERE `ua`.`iType` = 1 AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' )', true, DataBase::SINGLE_COLUMN );
					foreach( $aSmartoffer['aFilter'] as &$SOaPage ){
						$SOaPage['aPageTree'] = $oPage->getPagesTree( $SOaPage['iPage'] );
						$SOaPage['sTimeDifference'] = time_difference( $iCurrentTime - $SOaPage['iTime'] );
						$oldGet = $_GET;
						$_GET = Array();
						parse_str( $SOaPage['sParameters'], $_GET );

						if( isset( $_GET['aFilterAttributes'] ) || isset( $_GET['aFilterSelectAttributes'] ) ){
							if( is_array( $_GET['aFilterSelectAttributes'] ) ) foreach( $_GET['aFilterSelectAttributes'] as $iAttribute => &$aValues ){
								if( is_numeric( $iAttribute ) ){
									if( is_array( $aValues ) ) foreach( $aValues as $iValue ){
										$_GET['aFilterAttributes'][$iAttribute][$iValue] = 1;
									}
								}
							}
							if( is_array( $_GET['aFilterAttributes'] ) ) foreach( $_GET['aFilterAttributes'] as $iAttribute => &$aValues ){
								if( is_numeric( $iAttribute ) ){
									if( is_numeric( $aValues ) || ( is_string( $aValues ) && preg_match( '/^g[0-9]+$/', $aValues ) ) ){
										$aValues = Array( $aValues => 1 );
									}
									if( !is_array( $aValues ) ){
										$aValues = null;
										unset( $_GET['aFilterAttributes'][$iAttribute] );
									}
								}
							}
						}
						$SOaPage['aSearch'] = $_GET;
						
						foreach( $SOaPage['aSearch']['aFilterAttributes'] as $iAttribute => $aValues ){
							$aValues = array_keys( $aValues );
							foreach( $aValues as $iValue ){
								if( $iAttribute == 0 ){
									$SOaPage['aSearch']['aProducers'][] = $oDB->executeSQL( 'SELECT `sName` FROM `' . $oDB->throwTableName( DB_PRODUCERS ) . '` WHERE `iProducer` = ' . intval( $iValue ), true, DataBase::SINGLE_VALUE );;
								} else{
									$SOaPage['aSearch']['asAttributeValue'][ $oDB->executeSQL( 'SELECT `sName` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES ) . '` WHERE `iAttribute` = ' . intval( $iAttribute ), true, DataBase::SINGLE_VALUE ) ][] = $oDB->executeSQL( 'SELECT `sValue` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttributeValue` = ' . intval( $iValue ), true, DataBase::SINGLE_VALUE );
								}
							}
						}

						$oPage->mData = null;
						$SOaPage['aProducts'] = $oProduct->throwProducts( $SOaPage['iPage'], 1000 );
						$_GET = $oldGet;
					}
				break;
				case 'smartoffer-user-searched':
					$aSmartoffer['aSearch'] = $oDB->executeSQL( 'SELECT `p`.`iPage`, `p`.`sName`, `ua`.`sParameters`, `ua`.`iTime` FROM `' . $oDB->throwTableName( DB_PAGES ) . '` AS `p` INNER JOIN `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` USING( `iPage` ) WHERE `ua`.`sParameters` LIKE \'%sPhrase=%\' AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' ) ORDER BY `ua`.`iTime` DESC, `sName` ASC, `iPage` ASC', true );
					$aSmartoffer['aProductsIds'] = $oDB->executeSQL( 'SELECT `ua`.`iProduct` FROM `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` WHERE `ua`.`iType` = 1 AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' )', true, DataBase::SINGLE_COLUMN );
					foreach( $aSmartoffer['aSearch'] as &$SOaPage ){
						$SOaPage['aPageTree'] = $oPage->getPagesTree( $SOaPage['iPage'] );
						$SOaPage['sTimeDifference'] = time_difference( $iCurrentTime - $SOaPage['iTime'] );
						$oldGet = $_GET;
						$_GET = Array();
						parse_str( $SOaPage['sParameters'], $_GET );
						parse_str( $SOaPage['sParameters'] );
						$SOaPage['aSearch'] = $_GET;
						if( !empty( $SOaPage['aSearch']['iProducer'] ) ){
							$SOaPage['aSearch']['sProducer'] = $oDB->executeSQL( 'SELECT `sName` FROM `' . $oDB->throwTableName( DB_PRODUCERS ) . '` WHERE `iProducer` = ' . intval( $SOaPage['aSearch']['iProducer'] ), true, DataBase::SINGLE_VALUE );;
						}
						foreach( $SOaPage['aSearch']['aAttributeValue'] as $iAttribute => $iValue ){
							$SOaPage['aSearch']['asAttributeValue'][ $oDB->executeSQL( 'SELECT `sName` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES ) . '` WHERE `iAttribute` = ' . intval( $iAttribute ), true, DataBase::SINGLE_VALUE ) ] = $oDB->executeSQL( 'SELECT `sValue` FROM `' . $oDB->throwTableName( DB_ATTRIBUTES_VALUES ) . '` WHERE `iAttributeValue` = ' . intval( $iValue ), true, DataBase::SINGLE_VALUE );
						}
						if( !empty( $SOaPage['aSearch']['iCategory'] ) ){
							$SOaPage['aSearch']['aCategory'] = $oPage->throwPage( $SOaPage['aSearch']['iCategory'] );
						}
						$oPage->mData = null;
						$SOaPage['aProducts'] = $oProduct->throwProducts( $SOaPage['iPage'], 1000 );
						foreach( $_GET as $param => $val ){
							unset( $GLOBALS[$param] );
						}
						$_GET = $oldGet;
					}
				break;
				case 'smartoffer-user-advsearcher':
					$aSmartoffer['aSearch'] = $oDB->executeSQL( 'SELECT `p`.`iPage`, `p`.`sName`, `ua`.`sAdvsearcher`, `ua`.`iTime` FROM `' . $oDB->throwTableName( DB_PAGES ) . '` AS `p` INNER JOIN `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` USING( `iPage` ) WHERE `p`.`iProducts` > 0 AND `ua`.`sAdvsearcher` IS NOT NULL AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' ) ORDER BY `ua`.`iTime` DESC, `sName` ASC, `iPage` ASC', true );
					$aSmartoffer['aProductsIds'] = $oDB->executeSQL( 'SELECT `ua`.`iProduct` FROM `' . $oDB->throwTableName( DB_USER_ACTIONS ) . '` `ua` WHERE `ua`.`iType` = 1 AND ( `ua`.`iUser` = ' . intval( $iUser ) . ' OR `ua`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' )', true, DataBase::SINGLE_COLUMN );
					$aAdvsearcherColumns = $oDB->throwFileArraySmall( DB_ADVSEARCHER_COLUMNS, 'iColumn', 'sName' );
					foreach( $aSmartoffer['aSearch'] as &$SOaPage ){
						$SOaPage['aPageTree'] = $oPage->getPagesTree( $SOaPage['iPage'] );
						$SOaPage['sTimeDifference'] = time_difference( $iCurrentTime - $SOaPage['iTime'] );
						$oldSession = $_SESSION;
						$_SESSION = Array();
//						parse_str( $SOaPage['sParameters'], $_GET );
//						parse_str( $SOaPage['sParameters'] );
						$aAdvsearcher = json_decode( $SOaPage['sAdvsearcher'], true );
						foreach( $aAdvsearcher as $iColumn => &$mValue ){
							$_SESSION['iAdvsearcherValue_' . intval( $iColumn )] = intval( $mValue );
							$mValue = $aAdvsearcherColumns[$iColumn] . ': <strong>' . $oDB->executeSQL( 'SELECT `sValue` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_VALUES ) . '` WHERE `iValue` = ' . intval( $mValue ), true, DataBase::SINGLE_VALUE ) . '</strong>';
						}
						$SOaPage['aAdvsearcher'] = $aAdvsearcher;
						$SOaPage['aSearch'] = $_SESSION;
						$oPage->mData = null;
						$SOaPage['aProducts'] = $oProduct->throwProducts( $SOaPage['iPage'], 1000 );
						$_SESSION = $oldSession;
					}
				break;
				case 'smartoffer-user-avail_newsletter':
					$aSmartoffer['aProducts'] = $oDB->executeSQL( 'SELECT `an`.`iProduct`, `an`.`iTime`, `an`.`iTimeSent`, `p`.`sName`, `pvn`.`sName` AS `sVariantName` FROM `' . $oDB->throwTableName( DB_PRODUCTS_AVAIL_NEWSLETTER ) . '` `an` INNER JOIN `' . $oDB->throwTableName( DB_PRODUCTS ) . '` `p` USING( `iProduct` ) LEFT JOIN `' . $oDB->throwTableName( DB_V_PRODUCTS_VARIANTS_NAMES ) . '` `pvn` USING( `iProductVariant` ) WHERE ( `an`.`sEmail` = \'' . $oDB->escape( $aUser['sEmail'] ) . '\' OR `an`.`sPhone` = \'' . $oDB->escape( $aUser['sPhone'] ) . '\' ) ORDER BY `iTime` DESC, `sName` ASC', true );
					foreach( $aSmartoffer['aProducts'] as &$SOaProduct ){
						if( !empty( $SOaProduct['sVariantName'] ) ){
							$SOaProduct['sName'] .= ', ' . $SOaProduct['sVariantName'];
						}
						$SOaProduct['sTimeDifference'] = time_difference( $iCurrentTime - $SOaProduct['iTime'] );
						if( !empty( $SOaProduct['iTimeSent'] ) ){
							$SOaProduct['sTimeDifferenceSent'] = time_difference( $iCurrentTime - $SOaProduct['iTimeSent'] );
						}
						$SOaProduct['aPageTree'] = $oProduct->getPagesTree( $SOaProduct['iProduct'], $SOaProduct['sName'] );
						if( isset( $oFile->aImagesDefault[2][$SOaProduct['iProduct']] ) ){
							$SOaProduct['aDataImage'] = $oFile->aImagesDefault[2][$SOaProduct['iProduct']];
						}
					}
				break;
				default:
					die('Wrong tab!');
				break;
			}
			$oTpl->assign( 'aUser', $aUser );
			$oTpl->assign( 'aSmartoffer', $aSmartoffer );
			echo( $oTpl->parse( 'users_form_'.$tab.'.tpl' ) );
			exit;
		}
		// /SmartOffer
		$oTpl->assign( 'aUser', $aUser );
		$oTpl->assign( 'aCountries', $aCountries );
		$oTpl->assign( 'aGroups', $aGroups );
		$oTpl->assign( 'aSmartoffer', $aSmartoffer );
		$content.= $oTpl->parse( 'users_form.tpl' );
	}
} elseif( $a == 'login' && isset( $iUser ) && is_numeric( $iUser ) ){
	$_SESSION['iUserClient'] = $iUser;
	if( isset( $oPage->aPages[$config['account_page']] ) ) $sUserPage = $oPage->getLinkName( $config['account_page'] );
	else $sUserPage = $oPage->getLinkName( $config['start_page'] );
	//$_SESSION['sEmail'] = $_POST['sLogin'];
	header( 'Location: ' . $sUserPage . ( strpos( $sUserPage, '?' ) !== false ? '&' : '?' ) . 'mes=login_correct' );
	exit;
} elseif( $a == 'groupList' ){
	if( isset( $sOption ) ){
		if( $sOption == 'del' ) $content.= $oTpl->parse( 'message_done.tpl' );
		elseif( $sOption == 'save' ) $content.= $oTpl->parse( 'message_done.tpl' );
	}
	$aUserGroups = throwUsersGroups( LANGUAGE );
	$oTpl->assign( 'aUserGroups', $aUserGroups );
	$content.= $oTpl->parse( 'users_groups_list.tpl' );
} elseif( $a == 'groupDelete' && isset( $iGroup ) && is_numeric( $iGroup ) ){
	delUsersGroup( $iGroup );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=users-groupList&sOption=del' );
	exit;
} elseif( $a == 'groupForm' ){
	if( isset( $_POST['sOption'] ) ){
		$iGroup = saveGroup( $_POST );
		if( isset( $_POST['aPagesRelated'] ) && is_array( $_POST['aPagesRelated'] ) ) saveGroupPages( $iGroup, $_POST['aPagesRelated'] );
		else delPagesRelated( $iGroup );
		if( isset( $_POST['aUsersRelated'] ) && is_array( $_POST['aUsersRelated'] ) ) saveUsersToGroup( $iGroup, $_POST['aUsersRelated'] );
		else delUsersToGroups( null, $iGroup, 'iGroup' );
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=users-groupList&sOption=save' );
		exit;
	}
	if( isset( $iGroup ) && is_numeric( $iGroup ) ) $aGroup = throwGroup( $iGroup );
	if( isset( $iGroup ) && isset( $aGroup ) ){
		//		$aPagesSelected = $oDB->throwArrayRelated( DB_USERS_PAGES, $iGroup, 'iGroup', 'iPage' );
		$aPagesSelected = $oDB->executeSQL( 'SELECT `iPage`, `sName` FROM `' . $oDB->throwTableName( DB_PAGES ) . '` INNER JOIN `' . $oDB->throwTableName( DB_USERS_PAGES ) . '` USING( `iPage` ) WHERE `iGroup` = ' . intval( $iGroup ), true, DataBase::ASSOCIATIVE );
		$aUsersSelected = $oDB->throwArrayRelated( DB_USERS_TO_GROUPS, $iGroup, 'iGroup', 'iUser' );
	} else{
		$aPagesSelected = Array( );
		$aUsersSelected = Array( );
		$aGroup = Array( 'iGroup' => null );
	}
	$aPages = $oPage->throwPagesAuthorized( );
	$aUsers = $oDB->executeSQL( 'SELECT `iUser`, CONCAT( IFNULL( `sFirstName`, \'\' ), \' \', IFNULL( `sLastName`, \'\' ), \' [\', `sLogin`, \']\' ) FROM `' . $oDB->throwTableName( DB_USERS ) . '`', true, DataBase::ASSOCIATIVE );
	$oTpl->assign( 'aGroup', $aGroup );
	$oTpl->assign( 'aUsers', $aUsers );
	$oTpl->assign( 'aPagesSelected', $aPagesSelected );
	$oTpl->assign( 'aUsersSelected', $aUsersSelected );
	$content.= $oTpl->parse( 'users_groups_form.tpl' );
} elseif( $a == 'multiselect' ){
	$aUsers = throwUsers( $_GET['iAnon'] ? true : false );
	$oTpl->assign( 'aUsers', $aUsers );
	$oTpl->assign( 'a', $a );
	$oTpl->assign( 'sUrlExt', '&amp;sPhrase=' . $_GET['sPhrase'] . '&amp;iAnon=' . $_GET['iAnon'] );
	echo $oTpl->parse( 'users_multiselect.tpl' );
	exit;
} elseif( $a == 'groupMultiselect' ){
	$aUserGroups = throwUsersGroups( LANGUAGE );
	$oTpl->assign( 'aUserGroups', $aUserGroups );
	echo $oTpl->parse( 'users_groups_multiselect.tpl' );
	exit;
}
?>