<?php
namespace admin\factory;

class LayoutManager {

	public function getSelectedPages( $id )
	{
		global $sys , $db , $config , $cache;
		
		$key = 'getSelectedPages:' . $id;
		
		if ( !$pages = $cache -> fetch( $key ) )
		{
			$query = $db -> prepare( 'SELECT page_id FROM pcms_layout_pages WHERE layout_id = :layout_id' );
			$query -> bindValue( ':layout_id' , $id , \PDO::PARAM_INT );
			$query -> execute();
			if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
				$pages[]	= $row['page_id'];
			$query -> closeCursor();
			$cache -> store( $key , $pages , $config['cache_expire'] );
		}
		return $pages;
	}

	public function getPages()
	{
		global $sys , $db , $config , $cache;
		
		$key = 'getPages';
		
		if ( !$pages = $cache -> fetch( $key ) )
		{
			$query = $db -> prepare( 'SELECT id FROM pcms_page WHERE enabled = :enabled' );
			$query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
			$query -> execute();
			if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
			{
				$site 		= \site\FSite::loadSite( $row['id'] );
				$pages[]	= $site;
			}
			$query -> closeCursor();
			$cache -> store( $key , $pages , $config['cache_expire'] );
		}
		return $pages;
	}

	public function saveNew( $name , $enabled , $html , $css , $js , $time , $pages )
	{
		global $lang , $sys , $db;

		if ( $time != $sys -> getSessionVar( 'db_edit_add' ) )
		{
			if ( $enabled == 1 )
			{
				$query = $db -> prepare( 'UPDATE pcms_layout SET enabled = :enabled' );
				$query -> bindValue( ':enabled' , 0 , \PDO::PARAM_STR );
				$query -> execute();
				$query -> closeCursor();
			}

			$query = $db -> prepare( 'INSERT INTO pcms_layout ( name , enabled , html , css , javascript ) VALUES ( :name , :enabled , :html , :css , :javascript )' );
			$query -> bindValue( ':name'        , $name       , \PDO::PARAM_STR );
			$query -> bindValue( ':enabled'     , $enabled    , \PDO::PARAM_STR );
			$query -> bindValue( ':html'        , $html       , \PDO::PARAM_STR );
			$query -> bindValue( ':css'         , $css        , \PDO::PARAM_STR );
			$query -> bindValue( ':javascript'  , $js         , \PDO::PARAM_STR );
			$query -> execute();
			$query -> closeCursor();

			$id = $db -> lastInsertId();
			
			if ( is_array( $pages ) ) foreach ( $pages as $page )
			{
				$query = $db -> prepare( 'INSERT INTO pcms_layout_pages ( layout_id , page_id ) VALUES ( :layout_id , :page_id )' );
				$query -> bindValue( ':layout_id' 	, $id 	, \PDO::PARAM_INT );
				$query -> bindValue( ':page_id' 	, $page	, \PDO::PARAM_INT );
				$query -> execute();
				$query -> closeCursor();
			}

			\System::setSessionVar( 'db_edit_add' , $time );
			\System::deleteCacheAdmin();
			\System::setAlert( $lang -> getTrans( 'T_SZABLON_ZOSTAL_DODANY' ) );
		}
	}

  /* ---------- */
  public function save( $id , $name , $enabled , $html , $css , $js , $ajax = false , $pages )
  {
    global $lang , $sys , $db;

    if ( $enabled == 1 )
    {
      $query = $db -> prepare( 'UPDATE pcms_layout SET enabled = :enabled' );
      $query -> bindValue( ':enabled' , 0 , \PDO::PARAM_STR );
      $query -> execute();
      $query -> closeCursor();
    }

    $query = $db -> prepare( 'UPDATE pcms_layout SET name = :name , enabled = :enabled , html = :html , css = :css , javascript = :javascript WHERE id = :id' );
    $query -> bindValue( ':name'        , $name       , \PDO::PARAM_STR );
    $query -> bindValue( ':enabled'     , $enabled    , \PDO::PARAM_STR );
    $query -> bindValue( ':html'        , $html       , \PDO::PARAM_STR );
    $query -> bindValue( ':css'         , $css        , \PDO::PARAM_STR );
    $query -> bindValue( ':javascript'  , $js         , \PDO::PARAM_STR );
    $query -> bindValue( ':id'          , $id         , \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
	
	$query = $db -> prepare( 'DELETE FROM pcms_layout_pages WHERE layout_id = :layout_id' );
	$query -> bindValue( ':layout_id' , $id , \PDO::PARAM_INT );
	$query -> execute();
	$query -> closeCursor();
	
	if ( is_array( $pages ) ) foreach ( $pages as $page )
	{
		$query = $db -> prepare( 'INSERT INTO pcms_layout_pages ( layout_id , page_id ) VALUES ( :layout_id , :page_id )' );
		$query -> bindValue( ':layout_id' 	, $id 	, \PDO::PARAM_INT );
		$query -> bindValue( ':page_id' 	, $page	, \PDO::PARAM_INT );
		$query -> execute();
		$query -> closeCursor();
	}

    $sys -> deleteCache();
    if ( !$ajax )
      $sys -> setAlert( $lang -> getTrans( 'T_SZABLON_ZOSTAL_ZAPISANY' ) );
  }

	/* --------- */
	public function delete( $id )
	{
		global $lang , $db , $sys;

		$query = $db -> prepare( 'SELECT count(id) FROM pcms_layout' );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			if ( $row[0] <= 1 )
			{
				$sys -> setAlert( $lang -> getTrans( 'T_NIE_MOZNA_USUNAC_SZABLONU' ) );
				return false;
			}
		}
		$query -> closeCursor();

		$query = $db -> prepare( 'DELETE FROM pcms_layout WHERE id = :id' );
		$query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
		$query -> execute();
		$query -> closeCursor();
		
		$query = $db -> prepare( 'DELETE FROM pcms_layout_pages WHERE layout_id = :layout_id' );
		$query -> bindValue( ':layout_id' , $id , \PDO::PARAM_INT );
		$query -> execute();
		$query -> closeCursor();

		$query = $db -> prepare( 'SELECT count(id) FROM pcms_layout WHERE enabled = :enabled' );
		$query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			if ( $row[0] == 0 )
			{
				$query2 = $db -> prepare( 'UPDATE pcms_layout SET enabled = :enabled LIMIT 1' );
				$query2 -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
				$query2 -> execute();
				$query2 -> closeCursor();
			}
		}
		$query -> closeCursor();

		$sys -> deleteCache();
		$sys -> setAlert( $lang -> getTrans( 'T_SZABLON_ZOSTAL_USUNIETY' ) );
	}
}
?>