<?php
$_SESSION['S_Start']=1;

require_once ('inc/core.php');

include_once( 'password_protect.php' );

# istawienie jezyka
if( ! isset( $_SESSION['S_LangId']  ) ){
	$_SESSION['S_LangId'] = 1; // pol
}
$_SESSION['S_LangId'] = ( isset($_POST['lang']) ? $_POST['lang'] : $_SESSION['S_LangId'] );
$page = ( isset($_GET['page']) ? $_GET['page'] : ( isset($_POST['page']) ? $_POST['page'] : -1 ) );
// ############## sprawdzenie czy nie szuka sie usera ##############################
require_once( 'inc/lang.php');
$arraMenu = getMenu();
$files = array();
$files[-1] = "Wybierz";
  $sql = "SELECT menu_id , menu_name FROM t_menu WHERE lang_id = ".$_SESSION['S_LangId'].'';
  $count=0;
	$count_row = $dbh->prepare($sql);
	$count_row->execute();
	$count = $count_row->rowCount();
	if ( $count > 0 ) {
			foreach( $dbh->query($sql) as $row ){
		    $files[$row['menu_id']] = $row['menu_name'];	
			}
	}

$subsmarty -> assign("file_selected", $page );
$subsmarty -> assign("files", $files );


if( $_POST['co']== 'save' ){
    try{
    
      $dbh->beginTransaction();
      $sql = "DELETE FROM t_pages WHERE menu_id=".$page;
      $dbh->exec($sql);
      
      $sql = "INSERT INTO t_pages(menu_id, page_content) VALUES ( ? , ?)";
      $sth = $dbh->prepare($sql);
      $sth->bindValue(1, $page , PDO::PARAM_INT);

      $sth->bindValue(2, stripslashes( $_POST['FCKeditor1'] ), PDO::PARAM_STR);
      $sth->execute();
      $error = 0;
      $dbh->commit();
   }
   catch(PDOException $e)
   {
      $dbh->rollBack();
      //echo 'Połączenie nie mogło zostać utworzone: ' . $e->getMessage();
      echo 
      $error = 1;
      $error_msg .= "Nie udalo się zapisać do bazy danych";
   }

}




$_SESSION['page_data'] = "";

	$sql = "SELECT page_content FROM t_pages WHERE menu_id = ".$page.'';
  $count=0;
	$count_row = $dbh->prepare($sql);
	$count_row->execute();
	$count = $count_row->rowCount();
  $content = '';
	if ( $count > 0 ) {
			foreach( $dbh->query($sql) as $row ){
		    $content = $row['page_content'];	
			}
	}
	
	
	$_SESSION['page_data'] = $content;
	//echo $page_data;




// ############## tresc php ##############################
$subsmarty -> assign("error_msg", $error_msg );
// ###############  display *.tpl ########################
$Strona = new cwebpage ();
$Strona -> setTitle ( '' );
//$Strona -> setJSScripts ( 'pages.js' );
$Strona -> setCsses ( 'style.css' );
$Strona -> setContent ( $subsmarty->fetch(TEMPLATES_DIR.'/admin_content.tpl.html'));
$smarty->assign('Content', $Strona -> Content );
$smarty->assign('Title', $Strona -> Title );
$smarty->assign('JSScripts', $Strona -> JSScripts );
$smarty->assign('Csses', $Strona -> Csses );
//$smarty->debugging = true;
$smarty->display(TEMPLATES_DIR.'/index.tpl.html');

?>

