<?php
// po poprawkach
namespace admin\factory;

class Layouts {
  
  public static function getLayouts()
  {
    global $db;
    
    $query = $db -> query( 'SELECT id, name FROM pp_layouts' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $layouts[] = $row;
    $query -> closeCursor();
    
    return $layouts;
  }
  
  public static function getLayout( $id, $admin = false )
  {
    global $db, $config, $cache;
    
    $key = 'getLayout:' . $id;
    if ( $admin || !$layout = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT * FROM pp_layouts WHERE id = :id' );
      $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $layout = $row;
      $query -> closeCursor();
      $cache -> store( $key, $layout, $config['cache_expire_long'] );
    }
    return $layout;
  }
  
  public static function getActiveLayout()
  {
    global $db , $site, $cache, $config;

    $key = 'getActiveLayout:' . $site -> _values['id'];
    
    if ( !$layout = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT * FROM pp_layouts_pages, pp_layouts WHERE page_id = :page_id AND pp_layouts.id = layout_id ORDER BY layout_id DESC LIMIT 1' );
      $query -> bindValue( ':page_id', $site -> _values['id'], \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $layout = $row;
      $query -> closeCursor();

      if ( !$layout )
      {
        $query = $db -> prepare( 'SELECT html , css , javascript FROM pp_layouts WHERE enabled = :enabled' );
        $query -> bindValue( ':enabled', 1, \PDO::PARAM_INT );
        $query -> execute();
        if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
          $layout = $row;
        $query -> closeCursor();
      }
      $cache -> store( $key, $layout, $config['cache_expire'] );
    }

    return $layout;
  }

	public static function getSelectedPages( $id )
	{
		global $db, $config, $cache;
		
		$key = 'getSelectedPages:' . $id;
		
		if ( !$pages = $cache -> fetch( $key ) )
		{
			$query = $db -> prepare( 'SELECT page_id FROM pp_layouts_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 static function getPagesAssign( $id = '', $parent_id = 0 )
  {
    global $db;

    $query = $db -> prepare( 'SELECT * FROM pp_pages WHERE parent_id = :parent_id ORDER BY id_menu ASC, o ASC' );
    $query -> bindValue( ':parent_id', $parent_id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $row['title']  = \admin\factory\Pages::getPageTitle( $row['id'] );
      $row['check'] = 0;
      
      if ( $id )
      {
        $query2 = $db -> prepare( 'SELECT page_id FROM pp_layouts_pages WHERE layout_id = :layout_id AND page_id = :page_id' );
        $query2 -> bindValue( ':layout_id',  $id,         \PDO::PARAM_INT );
        $query2 -> bindValue( ':page_id',    $row['id'],  \PDO::PARAM_INT );
        $query2 -> execute();
        if ( $query2 -> rowCount() )
          $row['check'] = 1;
        $query2 -> closeCursor();
      }

      $row['subpages'] = self::getPagesAssign( $id, $row['id'] );
      
      $pages[] = $row;
    }
    $query -> closeCursor();
    
    return $pages;
  }

	public static function addLayout()
  {
		global $db;
    
    $name     = \System::formGet( 'name' );
    $enabled  = \System::formGet( 'enabled' );
    $html     = \System::formGet( 'html' );
    $css      = \System::formGet( 'css' );
    $js       = \System::formGet( 'js' );
    $pages		= \System::formGet( 'pages' );     
    
    if ( $enabled == 1 )
    {
      $query = $db -> prepare( 'UPDATE pp_layouts SET enabled = :enabled' );
      $query -> bindValue( ':enabled', 0, \PDO::PARAM_INT );
      $query -> execute();
      $query -> closeCursor();
    }

    $query = $db -> prepare( 'INSERT INTO pp_layouts ( name , enabled , html , css , javascript ) VALUES ( :name , :enabled , :html , :css , :javascript )' );
    $query -> bindValue( ':name',       $name,      \PDO::PARAM_STR );
    $query -> bindValue( ':enabled',    $enabled,   \PDO::PARAM_INT );
    $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 pp_layouts_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::deleteCacheAdmin();
    return true;
	}

  public static function saveLayout()
  {
    global $db;
    
    $id       = \System::formGetInt( 'id' );
    $name     = \System::formGet( 'name' );
    $enabled  = \System::formGet( 'enabled' );
    $html     = \System::formGet( 'html' );
    $css      = \System::formGet( 'css' );
    $js       = \System::formGet( 'js' );
    $pages		= \System::formGet( 'pages' );
    
    if ( $enabled )
    {
      $query = $db -> prepare( 'UPDATE pp_layouts SET enabled = :enabled' );
      $query -> bindValue( ':enabled', 0, \PDO::PARAM_INT );
      $query -> execute();
      $query -> closeCursor();
    }
    $html = addslashes( $html );

    $query = $db -> prepare( 'UPDATE pp_layouts 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_INT );
    $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 pp_layouts_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 pp_layouts_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::deleteCache();
    return true;
  }

	public static function deleteLayout( $id )
	{
		global $db;

		$query = $db -> prepare( 'SELECT count(id) FROM pp_layouts' );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			if ( $row[0] <= 1 )
				return false;
		}
		$query -> closeCursor();
		
		$query = $db -> prepare( 'DELETE FROM pp_layouts_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 pp_layouts WHERE enabled = :enabled' );
		$query -> bindValue( ':enabled', 1, \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			if ( $row[0] == 0 )
			{
				$query2 = $db -> prepare( 'UPDATE pp_layouts SET enabled = :enabled LIMIT 1' );
				$query2 -> bindValue( ':enabled', 1, \PDO::PARAM_INT );
				$query2 -> execute();
				$query2 -> closeCursor();
			}
		}
		$query -> closeCursor();

		$query = $db -> prepare( 'DELETE FROM pp_layouts WHERE id = :id' );
		$query -> bindValue( ':id', $id, \PDO::PARAM_INT );
		$query -> execute();
    if ( $query -> rowCount() )
    {
      \System::deleteCache();
      return true;
    }
		$query -> closeCursor();
	}
}
?>