<?php
ini_set( 'display_errors', 0 );
error_reporting( E_ALL & ~E_NOTICE & ~E_STRICT );
#ini_set( 'display_errors', 1 );
#error_reporting( E_ALL );
require 'configure/config_loader.php';
if( empty( $_POST ) && !empty( $_SESSION['sPostContent'] ) ){
	$_POST = unserialize( base64_decode( $_SESSION['sPostContent'] ) );
	$_SESSION['sPostContent'] = null;
}
header( 'Content-Type: text/html; charset=' . $config['charset'] );

if( isset( $sPhrase ) && !empty( $sPhrase ) ){
	$sPhrase = trim( changeTxt( $sPhrase, 'h' ) );
}
if( !isset( $p ) || empty( $p ) ) $p = 'news';
$aActions = getAction( $p );
$oDB = & DataBase::getInstance( );
$oFoto = & FotoJobs::getInstance( );
$oTpl = & TplParser::getInstance( DIR_TEMPLATES . 'admin/', false );
$oDB->cacheFilesIndexes( $config_db );
$content = null;
$oFile = & FilesAdmin::getInstance( );
$oPage = new PagesAdmin( );
$oProduct = new ProductsAdmin( );
$oOrder = new OrdersAdmin( );
loginActions( $p, 'bSellSmartUser', 'container.tpl' );
checkAdminPrivilages( $aActions );
if( $p == 'news' || $p == 'login' ){
	$aActions['f'] = 'home';
	$a = 'home';
}
$aLanguages = throwLanguages( );
$sLanguage = $config['language'];
$oTpl->assign( 'aLanguages', $aLanguages );
$oTpl->assign( 'sLanguage', $sLanguage );
$oTpl->assign( 'aActions', $aActions );
//if( $_SESSION['iAdminId'] > 0 ){
	if( !empty( $_POST ) || strpos( $a, 'delete' ) !== false || $a == 'add_attribute' || $a == 'add_value' || $a == 'columns' || $a == 'value' || $a == 'enadawca_add' || $a == 'dpd' || strpos( $a, 'protocol' ) !== false || $a == 'groupDelete' ){
		$oDB->save( DB_ADMIN_ACTIONS, Array( 'iAdmin' => $_SESSION['iAdminId'], 'sLang' => LANGUAGE, 'sGet' => var_export( $_GET, true ), 'sPost' => var_export( $_POST, true ), 'iTime' => time() ) );
	}
//}
$aCheckActions = Array( 'p' => 'stronę', 'products' => 'produkt', 'boxes' => 'boks', 'banners' => 'banner' );
if( $_SESSION['iAdminId'] > 0 && !empty( $_POST ) && in_array( $aActions['f'], array_keys( $aCheckActions ) ) ){
	list( $iCanPublicate, $sLogin ) = $oDB->executeSQL( 'SELECT `iCanPublicate`, `sLogin` FROM `' . $oDB->throwTableName( DB_ADMINS ) . '` WHERE `iAdmin` = ' . intval( $_SESSION['iAdminId'] ), true, DataBase::SINGLE_RECORD | MYSQLI_NUM );
	if( !$iCanPublicate ){
		$_POST['iStatus'] = 0;
	}
}
if( $_SESSION['iAdminId'] > 0 && ( $sOption == 'save' ) && in_array( $aActions['f'], array_keys( $aCheckActions ) ) ){
	list( $iCanPublicate, $sLogin ) = $oDB->executeSQL( 'SELECT `iCanPublicate`, `sLogin` FROM `' . $oDB->throwTableName( DB_ADMINS ) . '` WHERE `iAdmin` = ' . intval( $_SESSION['iAdminId'] ), true, DataBase::SINGLE_RECORD | MYSQLI_NUM );
	if( !$iCanPublicate ){
		sendEmail( Array( 'sTopic' => $lang['Admin_auto_status_0_email_topic'], 'sMailContent' => str_replace( Array( '%sLogin%', '%sAction%', '%sLink%' ), Array( $sLogin, $aCheckActions[$aActions['f']], 'http' . ( $_SERVER['HTTPS'] ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . str_replace( '&sOption=save', '', $_SERVER['REQUEST_URI'] ) ), $lang['Admin_auto_status_0_email'] ), 'sSender' => $config['email'] ) );
	}
}

$iOrdersActive = $oDB->executeSQL( 'SELECT COUNT( `iOrder` ) FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `iStatus` <= 50 AND `iStatus` != 8 AND `iStatus` != 2 AND `iStatus` != 14', true, DataBase::SINGLE_VALUE );
$iOrdersReadyToSend = $oDB->executeSQL( 'SELECT COUNT( `iOrder` ) FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `iStatus` = 14', true, DataBase::SINGLE_VALUE );

$oTpl->assign( 'iOrdersActive', $iOrdersActive );
$oTpl->assign( 'iOrdersReadyToSend', $iOrdersReadyToSend );

$sDeclination = $lang['Declination_' . $aActions['f'] ];
$oTpl->assign( 'sDeclination', $sDeclination );

if( isset( $aActions ) && is_file( 'admin_actions/' . $aActions['f'] . '.php' ) ) require 'admin_actions/' . $aActions['f'] . '.php';

$oTpl->assign( 'content', $content );
$oTpl->assign( 'sMsg', $sMsg );
echo $oTpl->parse( 'admin.tpl' );
?>