<?php
function saveGroupPages( $iGroup, $aPagesRelated ){
	$bErase = true;
	if( is_array( $aPagesRelated ) ){
		foreach( $aPagesRelated as $iPageRelated ){
			if( is_numeric( $iPageRelated ) ){
				dbAddPagesRelated( $iGroup, $iPageRelated, $bErase );
				$bErase = null;
			}
		}
	} else dbAddPagesRelated( $iGroup, null, $bErase );
}
function dbAddPagesRelated( $iGroup, $iPage, $bErase = true ){
	$oDB = DataBase::getInstance( );
	if( isset( $bErase ) ){
		$oDB->deleteInFile( DB_USERS_PAGES, $iGroup, 'iGroup' );
	}
	if( isset( $iPage ) && is_numeric( $iPage ) ){
		$oDB->save( DB_USERS_PAGES, Array( 'iGroup' => $iGroup, 'iPage' => $iPage ), null );
	}
}
function delPagesRelated( $iGroup ){
	dbAddPagesRelated( $iGroup, null, true );
}
function saveUsersToGroup( $iGroup, $aRelated ){
	$oDB = DataBase::getInstance( );
	$sErase = 'iGroup';
	foreach( $aRelated as $iKey => $iRelated ){
		if( !is_numeric( $iRelated ) ) unset( $aRelated[$iKey] );
	}
	$aGroupInfo = $oDB->executeSQL( 'SELECT `fDiscount`, COUNT( `iPage` ) AS `count` FROM `' . $oDB->throwTableName( DB_USERS_GROUPS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_USERS_PAGES ) . '` USING( `iGroup` ) WHERE `iInformEmail` = 1 AND `iGroup` = ' . intval( $iGroup ), true, DataBase::SINGLE_RECORD );
	if( !empty( $aGroupInfo ) ){
		$aRelatedOld = $oDB->executeSQL( 'SELECT `iUser` FROM `' . $oDB->throwTableName( DB_USERS_TO_GROUPS ) . '` WHERE `iGroup` = ' . intval( $iGroup ), true, DataBase::SINGLE_COLUMN );
		$aRelatedAdded = array_diff( $aRelated, $aRelatedOld );
		if( !empty( $aRelatedAdded ) ){
			foreach( $aRelatedAdded as $iUser ){
				informUserOnGroup( $iUser, $aGroupInfo['fDiscount'] > 0, $aGroupInfo['count'] > 0 );
			}
		}
	}
	foreach( $aRelated as $iRelated ){
		if( is_numeric( $iRelated ) ){
			dbAddUsersToGroups( $iRelated, $iGroup, $sErase );
			$sErase = null;
		}
	}
}
function saveUserToGroups( $iUser, $aRelated ){
	$oDB = DataBase::getInstance( );
	$sErase = 'iUser';
	foreach( $aRelated as $iKey => $iRelated ){
		if( !is_numeric( $iRelated ) ) unset( $aRelated[$iKey] );
	}
	$aRelatedOld = $oDB->executeSQL( 'SELECT `iGroup` FROM `' . $oDB->throwTableName( DB_USERS_TO_GROUPS ) . '` WHERE `iUser` = ' . intval( $iUser ), true, DataBase::SINGLE_COLUMN );
	$aRelatedAdded = array_diff( $aRelated, $aRelatedOld );
	if( !empty( $aRelatedAdded ) ){
		$aRelatedAdded = $oDB->executeSQL( 'SELECT `fDiscount`, COUNT( `iPage` ) AS `count` FROM `' . $oDB->throwTableName( DB_USERS_GROUPS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_USERS_PAGES ) . '` USING( `iGroup` ) WHERE `iInformEmail` = 1 AND `iGroup` IN ( ' . implode( ', ', $aRelatedAdded ) . ' )', true, DataBase::SINGLE_RECORD );
		if( !empty( $aRelatedAdded ) ){
			informUserOnGroup( $iUser, $aRelatedAdded['fDiscount'] > 0, $aRelatedAdded['count'] > 0 );
		}
	}
	foreach( $aRelated as $iRelated ){
		if( is_numeric( $iRelated ) ){
			dbAddUsersToGroups( $iUser, $iRelated, $sErase, $iUser );
			$sErase = null;
		}
	}
}
function dbAddUsersToGroups( $iUser, $iGroup, $sErase = 'iGroup', $iErase = null ){
	if( !isset( $iErase ) ) switch( $sErase ){
		case 'iUser':
			$iErase = $iUser;
		break;
		case 'iGroup':
		default:
			$iErase = $iGroup;
		break;
	}
	$oDB = DataBase::getInstance( );
	if( isset( $sErase ) ){
		$oDB->deleteInFile( DB_USERS_TO_GROUPS, $iErase, $sErase );
	}
	if( isset( $iUser ) && isset( $iGroup ) ){
		$oDB->save( DB_USERS_TO_GROUPS, Array( 'iUser' => $iUser, 'iGroup' => $iGroup ), null );
	}
}
function delUsersToGroups( $iUser, $iGroup = null, $sErase = 'iUser' ){
	dbAddUsersToGroups( $iUser, $iGroup, $sErase );
}
function throwUsers( $bShowAnon = true, $iList = null, $bFullGroupInfo = false ){
	$oDB = DataBase::getInstance( );
	if( isset( $_GET['sPhrase'] ) ){
		$aWords = preg_split( '/\\s+/', $_GET['sPhrase'] );
		foreach( $aWords as $iWord => $sWord ){
			if( empty( $sWord ) ) unset( $aWords[$iWord] );
		}
	} else{
		$aWords = null;
	}
	if( !empty( $aWords ) ){
		$aFieldsSearch = Array( 'sFirstName', 'sLastName', 'sEmail', 'sLogin', 'sCompanyName', 'sStreet', 'sZipCode', 'sCity', 'sPhone', 'sNip' );
		foreach( $aWords as &$sWord ){
			$sWord = '( `u`.`' . implode( '` LIKE \'%' . $oDB->escape( $sWord ) . '%\' OR `u`.`', $aFieldsSearch ) . '` LIKE \'%' . $oDB->escape( $sWord ) . '%\' )';
		}
		$sql = 'WHERE ' . implode( ' AND ', $aWords ) . ( $bShowAnon ? null : ' AND ( `u`.`sPass` IS NOT NULL OR `u`.`sOuterAuthorisation` IS NOT NULL )' );
	} else{
		$sql = ( $bShowAnon ? null : ' WHERE ( `u`.`sPass` IS NOT NULL OR `u`.`sOuterAuthorisation` IS NOT NULL )' );
	}

	if( $bFullGroupInfo ){
		$sql = 'SELECT SQL_CALC_FOUND_ROWS `u`.* FROM `' . $oDB->throwTableName( DB_USERS ) . '` `u` ' . $sql . ' ORDER BY `iUser` ASC';
	} else{
		$sql = 'SELECT SQL_CALC_FOUND_ROWS `u`.*, GROUP_CONCAT( `g`.`sName` SEPARATOR \', \' ) AS `sGroups` FROM `' . $oDB->throwTableName( DB_USERS ) . '` `u` LEFT JOIN `' . $oDB->throwTableName( DB_USERS_TO_GROUPS ) . '` `ug` USING( `iUser` ) LEFT JOIN `' . $oDB->throwTableName( DB_USERS_GROUPS ) . '` `g` USING( `iGroup` ) ' . $sql . ' GROUP BY `u`.`iUser` ORDER BY `iUser` ASC';
	}

	if( !isset( $iList ) ){
		$iList = $GLOBALS['config']['admin_list'];
	}
	if( $iList != 'inf' ){
		$iPageNumber = $GLOBALS['iPage'];
		if( !isset( $iPageNumber ) || !is_numeric( $iPageNumber ) || $iPageNumber < 1 ) $iPageNumber = 1;
		if( isset( $_POST['sort'] ) || isset( $_POST['productsPerPage'] ) ){
			$iPageNumber = 1;
			$GLOBALS['aActions']['o2'] = 1;
		}
		$iEnd = $iPageNumber * $iList;
		$iStart = $iEnd - $iList;
		$sql.= ' LIMIT ' . $iStart . ',' . $iList;
		$aUsers = $oDB->executeSQL( $sql, true );
		$sql = 'SELECT FOUND_ROWS()';
		$iCount = $oDB->executeSQL( $sql, true, DataBase::SINGLE_VALUE );
		$aReturn['aPagesCountData'] = Array( 'iCount' => ceil( $iCount / $iList ), 'iPageNumber' => $iPageNumber );
		if( is_array( $aUsers ) ) foreach( $aUsers as &$aUser ){
			$aUser = changeMassTxt( $aUser );
		}
		$aReturn['aUsers'] = $aUsers;
		return $aReturn;
	} else{
		$aUsers = $oDB->executeSQL( $sql, true );
		if( is_array( $aUsers ) ) foreach( $aUsers as &$aUser ){
			$aUser = changeMassTxt( $aUser );
			$aUser['aGroups'] = $oDB->executeSQL( 'SELECT `g`.`iGroup`, `sName` FROM `' . $oDB->throwTableName( DB_USERS_GROUPS ) . '` `g` INNER JOIN `' . $oDB->throwTableName( DB_USERS_TO_GROUPS ) . '` USING( `iGroup` ) WHERE `iUser` = ' . intval( $aUser['iUser'] ), true );
		}
		return $aUsers;
	}
}
function delUser( $iUser ){
	$oDB = DataBase::getInstance( );
	$oDB->deleteInFile( DB_USERS, $iUser, 'iUser' );
	delUsersToGroups( $iUser );
	$oDB->update( DB_ORDERS, Array( 'iUser' => null ), Array( 'iUser' => $iUser ) );
}
function checkListUsersGroups( $aList, $sLang ){
	return( $aList['sLang'] != $sLang ) ? null : true;
}
function throwUsersGroups( $sLang = null ){
	$oDB = DataBase::getInstance( );
	$sql = 'SELECT * FROM `' . $oDB->throwTableName( DB_USERS_GROUPS ) . '`';
	if( isset( $sLang ) ) $sql.= ' WHERE `sLang` = \'' . $oDB->escape( $sLang ) . '\'';
	$sql.= ' ORDER BY `iGroup` ASC';
	return $oDB->executeSQL( $sql, true );
}
function throwGroup( $iGroup ){
	$oDB = DataBase::getInstance( );
	$aData = $oDB->throwData( DB_USERS_GROUPS, $iGroup, 'iGroup' );
	if( isset( $aData ) && is_array( $aData ) && isset( $aData['sLang'] ) && $aData['sLang'] == LANGUAGE ){
		$aData['sName'] = changeTxt( $aData['sName'] );
		return $aData;
	} else return null;
}
function throwGroups( ){
	$oDB = DataBase::getInstance( );
	return $oDB->throwFileArray( DB_USERS_GROUPS );
}
function delUsersGroup( $iGroup ){
	$oDB = DataBase::getInstance( );
	$oDB->deleteInFile( DB_USERS_GROUPS, $iGroup, 'iGroup' );
	dbAddUsersToGroups( null, $iGroup, 'iGroup' );
	dbAddPagesRelated( null, $iGroup, null, true );
}
function saveGroup( $aForm ){
	$oDB = DataBase::getInstance( );
	if( is_numeric( $aForm['iGroup'] ) ){
		$sParam = 'iGroup';
	} else{
		$sParam = null;
	}
	$aForm['sName'] = changeTxt( $aForm['sName'] );
	$aForm['sLang'] = LANGUAGE;
	$aForm['iGroup'] = $oDB->save( DB_USERS_GROUPS, $aForm, $sParam );
	return $aForm['iGroup'];
}
?>