<?php
if( $a == 'list' ){
	if( isset( $_POST['sOption'] ) ){
		$sOption = 'save';
		$oPage->savePages( $_POST );
	}
	if( isset( $_POST['sDeleteOption'] ) ){
		if( is_array( $_POST['aDelete'] ) ) foreach( $_POST['aDelete'] as $iDelete ) if( is_numeric( $iDelete ) ){
			$sOption = 'delete';
			$oPage->deletePage( $iDelete );
		}
		if( $sOption == 'delete' ) header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-list&sOption=delete' );
	}
	if( isset( $sOption ) ) $content.= $oTpl->parse( 'message_done.tpl' );
	$aPages = $oPage->throwPagesAdminList( );
	$oTpl->assign( 'aPages', $aPages );
	$oTpl->assign( 'aMenuTypes', $aMenuTypes );
	$content.= $oTpl->parse( 'pages_list.tpl' );
} elseif( $a == 'delete' && isset( $iPage ) && is_numeric( $iPage ) ){
	$oPage->deletePage( $iPage );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-list&sOption=del' );
	exit;
} elseif( $a == 'category' ){
	$iTemplate = $oDB->executeSQL( 'SELECT `iPage` FROM `' . $oDB->throwTableName( DB_PAGES ) . '` WHERE `iType` = 99 AND `iProducts` = 1 ORDER BY `iPosition` ASC, `sName` ASC, `iPage` ASC LIMIT 1', true, DataBase::SINGLE_VALUE );
	header( 'Location: admin.php?p=p-form&iTemplate=' . $iTemplate );
	exit;
} elseif( $a == 'form' ){
	$config['iWatermark'] = $config['iWatermarkPages'];
	$oTpl->assign( 'config', $config );
	$json_tags = json_encode( $oDB->executeSQL( 'SELECT DISTINCT `sTag` FROM `' . $oDB->throwTableName( DB_PAGES_TAGS ) . '` UNION SELECT DISTINCT `sTag` FROM `' . $oDB->throwTableName( DB_PRODUCTS_TAGS ) . '`', true, DataBase::SINGLE_COLUMN ) );
	$oTpl->assign( 'json_tags', $json_tags );
	if( isset( $_POST['sOption'] ) ){
		$_POST['iProducts'] = 0;
		if( !empty( $_POST['aBlocks'] ) ) foreach( $_POST['aBlocks'] as $aBlock ){
			if( $aBlock['sType'] == 'PRODUCTS' ) $_POST['iProducts'] = 1;
		}
		if( !empty( $_POST['aNewBlocks'] ) ) foreach( $_POST['aNewBlocks'] as $aBlock ){
			if( $aBlock['sType'] == 'PRODUCTS' ) $_POST['iProducts'] = 1;
		}
		$iPage = $oPage->savePage( $_POST );
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $p . '&sOption=save&iPage=' . $iPage . '&activeTabId=' . $_POST['activeTabId'] );
		exit;
	}
	$aGeneralInfo['aGroups'] = $oDB->throwFileArraySmall( DB_USERS_GROUPS, 'iGroup', 'sName', 'iGroup' );
	$aGeneralInfo['aPages'] = $oPage->throwPagesAdmin( );
	$aGeneralInfo['aTemplates'] = $oPage->throwPagesAdmin( null, 99 );
	$aGeneralInfo['aAdmins'] = throwAdminsForSelect( $_SESSION['iAdminId'] );
	$aGeneralInfo['aForms'] = $oDB->throwFileArraySmall( DB_FORMS, 'iForm', 'sTitle', 'iForm' );
	$aGeneralInfo['aPolls'] = $oDB->throwFileArraySmall( DB_POLLS, 'iPollId', 'sQuestion', 'iPollId' );
	$aGeneralInfo['aBoxes'] = $oDB->throwFileArraySmall( DB_BOXES, 'iBox', 'sName', 'iBox' );
	$aGeneralInfo['aHlFunctions'] = array_combine( $aHlFunctions, $aHlFunctions );
	$aGeneralInfo['aFilePosition'] = $aFilePositionPage;
	$aGeneralInfo['aPhotoPosition'] = $aPhotoPositionPage;
	$aGeneralInfo['iDefaultFilePosition'] = 1;
	$aGeneralInfo['aYesNo'] = Array( 0 => $lang['no'], 1 => $lang['yes'] );
	$aGeneralInfo['aMenuTypes'] = $aMenuTypes;
	if( isset( $iPage ) && is_numeric( $iPage ) ){
		$aPage = $oPage->throwPage( $iPage );
		$aPage['aBlocks'] = $oDB->throwFileArray( DB_PAGES_BLOCKS, Array( 'iPage' => $iPage ) );
		//Generowanie Metatagów domyślnych
		$aMetaPage = $aPage;
		$aMetaPage['aPagesTree'] = $oPage->getPagesTree( $aMetaPage['iPage'] );
		$aMetaPage['sMetaPageTree'] = '';
		foreach( $aMetaPage['aPagesTree']['aPath'] as $aMPage ) $aMetaPage['sMetaPageTree'].= ', ' . $aMPage['sName'];
		$aMetaPage['sTitle'] = strip_tags( $aMetaPage['sName'] . ( !empty( $aMetaPage['sMetaPageTree'] ) ? ' -' . str_replace( ',', '', $aMetaPage['sMetaPageTree'] ) : null ) . ( !empty( $config['title'] ) ? ' - ' . $config['title'] : null ) );
		if( !empty( $aMetaPage['iAdmin'] ) ){
			$aMetaPage['sSignature'] = throwAdmin( $aMetaPage['iAdmin'], true );
		}
		$aMetaPage['sKeywords'] = $aMetaPage['sName'] . $aMetaPage['sMetaPageTree'] . ( !empty( $config['keywords'] ) ? ', ' . $config['keywords'] : null ) . ( !empty( $aMetaPage['sSignature'] ) ? ', ' . $aMetaPage['sSignature'] : null );
		$sMetaDescPart = $aMetaPage['sDescriptionShort'];
		$sMetaDescPart = str_replace( '|n|', ' ', strip_tags( $sMetaDescPart ) );
		$sMetaDescPart = preg_replace( '/\\s+/', ' ', $sMetaDescPart );
		$sMetaDescPart = htmlspecialchars( $sMetaDescPart );
		if( strlen( $sMetaDescPart ) > 200 ){
			$sMetaDescPart = substr( $sMetaDescPart, 0, 200 );
			$sMetaDescPart = preg_replace( '/\\s\\S+$/', '', preg_replace( '/&[^;]+$/', '', $sMetaDescPart ) ) . '...';
		}
		$aMetaPage['sDescription'] = $aMetaPage['sName'] . ' - ' . ( !empty( $sMetaDescPart ) ? $sMetaDescPart : $config['description'] );
		$oTpl->assign( 'aMetaPage', $aMetaPage );
		/* * */
		$aPage['aFiles'] = $oFile->listAllFilesAdmin( $iPage, 1 );
		$aPage['sDescriptionShort'] = changeTxt( $aPage['sDescriptionShort'], 'nlNds' );
		$aPage['aGroups'] = $oDB->executeSQL( 'SELECT `iGroup` FROM `' . $oDB->throwTableName( DB_USERS_PAGES ) . '` WHERE `iPage` = ' . intval( $iPage ), true, DataBase::SINGLE_COLUMN );
	} else{
		if( !isset( $iTemplate ) ){
			$iTemplate = $aGeneralInfo['aTemplates'][1]['iPage'];
		}
		$aPage = $oPage->throwPage( $iTemplate );
		$aPage['iTemplate'] = $iTemplate;
		$aPage['aBlocks'] = $oDB->throwFileArray( DB_PAGES_BLOCKS, Array( 'iPage' => $iTemplate ) );
		$aPage['iPage'] = null;
		$aPage['sName'] = null;
		$aPage['iPosition'] = 0;
		$aPage['iAdmin'] = $_SESSION['iAdminId'];
		$aPage['sDate'] = date( 'Y-m-d H:i:s' );
		$aPage['iMenuSubpages'] = 1;
		if( isset( $aGeneralInfo['aMenuTypes'][3] ) ){
			$aPage['iType'] = 3;
		} elseif( isset( $aGeneralInfo['aMenuTypes'][5] ) ){
			$aPage['iType'] = 5;
		}
	}
	if( isset( $sOption ) ){
		if( isset( $_SESSION['iUpdatedProducts'] ) && isset( $_SESSION['iUpdatedVariants'] ) ){
			$oTpl->assign( 'sDoneMessage', str_replace( Array( '%iUpdatedProducts%', '%iUpdatedVariants%' ), Array( $_SESSION['iUpdatedProducts'], $_SESSION['iUpdatedVariants'] ), $lang['Update_message'] ) );
			$_SESSION['iUpdatedProducts'] = null;
			$_SESSION['iUpdatedVariants'] = null;
		}
		$content.= $oTpl->parse( 'message_done.tpl' );
	}
	$oTpl->assign( 'aPage', $aPage );
	$oTpl->assign( 'aGeneralInfo', $aGeneralInfo );
	$content.= $oTpl->parse( 'pages_form.tpl' );
} elseif( $a == 'multiselect' ){
	$aWhere = Array( );
	if( isset( $_GET['iAuthorized'] ) && $_GET['iAuthorized'] == 1 ){
		$aWhere['iAuthorized'] = 1;
	}
	if( isset( $_GET['iProducts'] ) && $_GET['iProducts'] == 1 ){
		$aWhere['iProducts'] = 1;
	}
	$aPages = $oPage->throwPagesAdminList( $aWhere, isset( $_GET['getFull'] ) && $_GET['getFull'] == 1 );
	$oTpl->assign( 'aPages', $aPages );
	$oTpl->assign( 'aMenuTypes', $aMenuTypes );
	echo $oTpl->parse( 'pages_multiselect.tpl' );
	exit;
} elseif( $a == 'get_margin' && is_numeric( $iPage ) ){
	$aPage = $oPage->aPages[$iPage];
	header( 'Content-type: text/json' );
	echo( json_encode( $aPage['sMargin'] ) );
	exit;
} elseif( $a == 'margin_ajax' && is_numeric( $iPage ) ){
	$aForm = $oPage->throwPage( $iPage );
	$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_PRODUCTS ) .'` `p` SET `fPrice` = ROUND( `fBuyPrice` ' . ( strpos( $aForm['sMargin'], '%' ) !== false ? '* ( 1 + '.str_replace( '%', '', $aForm['sMargin'] ).'/100 )' : '+ '.$aForm['sMargin'] ) . ', 2 ) WHERE EXISTS( SELECT 1 FROM `' . $oDB->throwTableName( DB_PRODUCTS_PAGES ) . '` `pp` WHERE `iPage` = ' . $aForm['iPage'] . ' AND `iMainCategory` = 1 AND `p`.`iProduct` = `pp`.`iProduct` ) AND ( `sMargin` IS NULL OR `sMargin` = \'\' ) AND `fBuyPrice` > 0' );
	$_SESSION['iUpdatedProducts'] = $oDB->affectedRows( );
	$oDB->executeSQL( 'UPDATE `' . $oDB->throwTableName( DB_PRODUCTS_VARIANTS ) .'` `pv` INNER JOIN `' . $oDB->throwTableName( DB_PRODUCTS ) .'` `p` USING( `iProduct` ) SET `pv`.`fPrice` = ROUND( `pv`.`fBuyPrice` ' . ( strpos( $aForm['sMargin'], '%' ) !== false ? '* ( 1 + '.str_replace( '%', '', $aForm['sMargin'] ).'/100 )' : '+ '.$aForm['sMargin'] ) . ', 2 ) WHERE EXISTS( SELECT 1 FROM `' . $oDB->throwTableName( DB_PRODUCTS_PAGES ) . '` `pp` WHERE `iPage` = ' . $aForm['iPage'] . ' AND `iMainCategory` = 1 AND `p`.`iProduct` = `pp`.`iProduct` ) AND ( `pv`.`sMargin` IS NULL OR `pv`.`sMargin` = \'\' ) AND ( `p`.`sMargin` IS NULL OR `p`.`sMargin` = \'\' ) AND `pv`.`fBuyPrice` > 0' );
	$_SESSION['iUpdatedVariants'] = $oDB->affectedRows( );
	if( isset( $_SESSION['iUpdatedProducts'] ) && isset( $_SESSION['iUpdatedVariants'] ) ){
		$oTpl->assign( 'sDoneMessage', str_replace( Array( '%iUpdatedProducts%', '%iUpdatedVariants%' ), Array( $_SESSION['iUpdatedProducts'], $_SESSION['iUpdatedVariants'] ), $lang['Update_message'] ) );
		$_SESSION['iUpdatedProducts'] = null;
		$_SESSION['iUpdatedVariants'] = null;
	}
	echo $oTpl->parse( 'message_done.tpl' );
	exit;
} elseif( $a == 'clone' ){
	if( isset( $iPage ) ){
		if( empty( $iQuantity ) ) $iQuantity = 1;
		if( $iQuantity == 1 ){
			$iPage = $oPage->clonePage( $iPage );
			header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=p-form&iPage=' . $iPage );
			exit;
		} else{
			for( $i = 1; $i <= $iQuantity; $i++ ){
				$oPage->clonePage( $iPage );
			}
			header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=p-list' );
			exit;
		}
	}
} elseif( $a == 'zbozicz_categories' ){
	header( 'Content-type: text/json' );
	readfile( 'https://www.zbozi.cz/static/categories.json' );
	exit;
}
?>