<?php

if( $a == 'list' ){
	$aColumns = $oDB->executeSQL( 'SELECT `iColumn`, `sName` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_COLUMNS ) . '` ORDER BY `iColumn`', true );
/*
	$bFirst = true;
	$aAllValues = Array();
	foreach( $aColumns as $aColumn ){
		$aValues = $oDB->executeSQL( 'SELECT `iValue`, `iParentValue`, `sValue` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_VALUES ) . '` WHERE `iColumn` = ' . $aColumn['iColumn'] . ' ORDER BY `sValue`', true, DataBase::ASSOCIATIVE, 'iValue' );
		if( $bFirst ){
			$aValuesRes = $aValues;
			foreach( $aValuesRes as $iValue => &$aValue ){
				$aAllValues[$iValue] =& $aValue;
				unset( $aValue );
			}
		} else{
			foreach( $aValues as $iValue => &$aValue ){
				$aAllValues[$aValue['iParentValue']]['aChildren'][] =& $aValue;
				$aAllValues[$iValue] =& $aValue;
				unset( $aValue );
			}
		}
		$bFirst = false;
	$oTpl->assign( 'aValues', $aValuesRes );
	}
	$oTpl->assign( 'aColumns', $aColumns );
*/
	foreach( $aColumns as $aColumn ){
		$aColumnsNames[$aColumn['iColumn']] = $aColumn;
	}
	$oTpl->assign( 'aColumnsNames', $aColumnsNames );
	if( isset( $_GET['iValue'] ) ){
		$aValue = $oDB->executeSQL( 'SELECT `iValue`, `iParentValue`, `sValue`, `iColumn`, `iPosition` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_VALUES ) . '` WHERE `iValue` = ' . intval( $_GET['iValue'] ), true, DataBase::SINGLE_RECORD );
		$aValue['aChildren'] = $oDB->executeSQL( 'SELECT `iValue`, `iParentValue`, `sValue`, `iColumn`, `iPosition` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_VALUES ) . '` WHERE `iParentValue` = ' . intval( $_GET['iValue'] ) . ' ORDER BY `iPosition`, `sValue`', true );
		$bCanAddNext = false;
	} else{
		$aValue = Array( 'iColumn' => null, 'aChildren' => $oDB->executeSQL( 'SELECT `iValue`, `iParentValue`, `sValue`, `iColumn`, `iPosition` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_VALUES ) . '` WHERE `iParentValue` IS NULL ORDER BY `iPosition`, `sValue`', true ) );
		$bCanAddNext = true;
	}
	$aValue['bCanAdd'] = false;
	foreach( $aColumns as $aColumn ){
		if( $bCanAddNext ){ $aValue['bCanAdd'] = true; $aValue['iAddColumn'] = $aColumn['iColumn']; break; }
		if( $aValue['iColumn'] == $aColumn['iColumn'] ){ $bCanAddNext = true; }
	}
	$oTpl->assign( 'aValue', $aValue );
	
	if( isset( $_POST['sOption'] ) ){
		if( is_array( $_POST['aColumnsNames'] ) ) foreach( $_POST['aColumnsNames'] as $iColumn => $sName ){
			$oDB->save( DB_ADVSEARCHER_COLUMNS, Array( 'iColumn' => $iColumn, 'sName' => $sName ), 'iColumn' );
		}
		if( !empty( $_POST['sNewColumn'] ) ) $oDB->save( DB_ADVSEARCHER_COLUMNS, Array( 'sName' => $_POST['sNewColumn'] ) );
		header( 'Location: ?p=advsearcher-list&sOption=save&activeTabId=' . $_POST['activeTabId'] );
		exit;
	}
	$aColumns = $oDB->executeSQL( 'SELECT `iColumn`, `sName` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_COLUMNS ) . '` ORDER BY `iColumn`', true );
	$oTpl->assign( 'aColumns', $aColumns );

	if( isset( $_GET['sOption'] ) ) $content .= $oTpl->parse( 'message_done.tpl' );
	$content .= $oTpl->parse( 'advsearcher_list.tpl' );
}
elseif( $a == 'delete' && !empty( $iValue ) && is_numeric( $iValue ) ){
	$iParentValue = $oDB->executeSQL( 'SELECT `iParentValue` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_VALUES ) . '` WHERE `iValue` = ' . intval( $iValue ), true, DataBase::SINGLE_VALUE );
	$oDB->deleteInFile( DB_ADVSEARCHER_VALUES, intval( $iValue ), 'iValue' );
	header( 'Location: ?p=advsearcher-list&sOption=delete' . ( !empty( $iParentValue ) ? ( '&iValue=' . $iParentValue ) : null ) );
	exit;
}
elseif( $a == 'value' && !empty( $_POST['sValue'] ) ){
	$iValue = saveAdvsearcherValue( $_POST );
	header( 'Location: ?p=advsearcher-list&sOption=save' . ( !empty( $_POST['iParentValue'] ) ? '&iValue=' . $_POST['iParentValue'] : null ) );
	exit;
}
elseif( $a == 'ajax_value' && !empty( $_GET['sValue'] ) ){
	$iValue = saveAdvsearcherValue( $_GET );
	echo( $iValue );
	exit;
}
elseif( $a == 'column_delete' ){
	$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_COLUMNS ) . '` ORDER BY `iColumn` DESC LIMIT 1' );
	header( 'Location: ?p=advsearcher-list&sOption=save&activeTabId=columns' );
	exit;
}
elseif( $a == 'getsub' && is_numeric( $iValue ) ){
	$aValues = $oDB->executeSQL( 'SELECT `iValue`, `sValue` FROM `' . $oDB->throwTableName( DB_ADVSEARCHER_VALUES ) . '` WHERE `iParentValue` = ' . intval( $iValue ) . ' ORDER BY `iPosition` ASC, `sValue` ASC', true, DataBase::ASSOCIATIVE );
	echo( throwSelectFromArray( $aValues ) );
	exit;
}