<?
// po poprawkach
namespace admin\factory;

class Pages
{
  public static function getPageLayout( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT layout_id FROM pp_layouts_pages WHERE page_id = :page_id' );
    $query -> bindValue( ':page_id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row['layout_id'];
    $query -> closeCursor();
    return false;
  }
  
  public static function getSortTypes()
  {
    global $db;

    $sort = array();

    $query = $db -> prepare( 'SELECT id , name FROM pp_page_sorts ORDER BY name ASC' );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $st['id']   = $row['id'];
      $st['name'] = $row['name'];
      $sort[]     = $st;
    }
    $query -> closeCursor();

    return $sort;
  }
  
  public static function getPageTypes()
  {
    global $db;

    $pages = array();

    $query = $db -> prepare( 'SELECT id , name FROM pp_page_types WHERE enabled = :enabled ORDER BY name ASC' );
    $query -> bindValue( ':enabled', 1, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $pg['id']   = $row['id'];
      $pg['name'] = $row['name'];
      $pages[]    = $pg;
    }
    $query -> closeCursor();

    return $pages;
  }
  
  public static function getAssignedArticles( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT article_id FROM pp_articles_pages WHERE page_id = :page_id ORDER BY o DESC' );
    $query -> bindValue( ':page_id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $articles[] = \article\FArticle::loadArticle( $row['article_id'] );
    $query -> closeCursor();
    
    return $articles;
  }
  
  public static function getPageParam( $id )
  {
    global $db;

    $query = $db -> prepare( 'SELECT * FROM pp_pages WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row;
  }

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

		$query = $db -> prepare( 'SELECT id, name FROM pp_langs WHERE enabled = :enabled' );
		$query -> bindValue( ':enabled', 1, \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
      $lg = '';
      
			$query2 = $db -> prepare( 'SELECT * FROM pp_pages_langs WHERE page_id = :page_id AND lang_id = :lang_id' );
			$query2 -> bindValue( ':page_id', $id,        \PDO::PARAM_INT );
			$query2 -> bindValue( ':lang_id', $row['id'], \PDO::PARAM_STR );
			$query2 -> execute();
			if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
				$lg = $row2;
			$query2 -> closeCursor();
      
			$lg['id'] 	= $row['id'];
			$lg['name'] = $row['name'];
			$language[]	= $lg;
		}
		$query -> closeCursor();

		return $language;
	}

  public static function getPageTitle( $id, $language = 'pl' )
  {
    global $db, $cache, $config;

    $key = 'pageTitle:' . $id . ':' . $language;

    if ( !$title = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT title FROM pp_pages_langs WHERE page_id = :page_id AND lang_id = :lang_id' );
      $query -> bindValue( ':page_id', $id,       \PDO::PARAM_INT );
      $query -> bindValue( ':lang_id', $language, \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $title = $row['title'];
      $query -> closeCursor();
      $cache -> store( $key , $title , $config['cache_expire_long' ] );
    }
    if ( $title == '' )
    {
      $key = 'pageTitle:' . $id;
      if ( !$title = $cache -> fetch( $key ) )
      {
        $query = $db -> prepare( 'SELECT title FROM pp_pages_langs WHERE page_id = :page_id AND title != "" LIMIT 1' );
        $query -> bindValue( ':page_id', $id, \PDO::PARAM_STR );
        $query -> execute();
        if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
          $title = $row['title'];
        $query -> closeCursor();
        $cache -> store( $key , $title , $config['cache_expire_long' ] );
      }
    }

    return $title;
  }

	public static function getPages( $menu_id = 1, $parent_id = 0 )
	{
		global $db;
    
		$query = $db -> prepare( 'SELECT id, id_menu, enabled FROM pp_pages WHERE id_menu = :id_menu AND parent_id = :parent_id ORDER BY o ASC' );
    $query -> bindValue( ':parent_id',  $parent_id, \PDO::PARAM_INT );
		$query -> bindValue( ':id_menu',    $menu_id,   \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			$row['title']    = self::getPageTitle( $row['id'] );
      $row['subpages'] = self::getPages( $menu_id, $row['id'] );
			$pages[] = $row;
		}

		return $pages;
	}

	public static function selectMaxOrder()
	{
		global $db;

		$query = $db -> prepare( 'SELECT MAX(o) FROM pp_pages' );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
			$order = $row[0]+1;
		$query -> closeCursor();

		return $order;
	}

  public static function savePage() 
  {
    global $db;
    
    \System::deleteCacheAdmin();
    \System::deleteCache();

    $enabled          = \System::formGet( 'enabled' );
    $show_title       = \System::formGet( 'show_title' );
    $sort_type        = \System::formGet( 'sort_type' );
    $page_type_id     = \System::formGet( 'page_type_id' );
    $link             = \System::formGet( 'link' );
    $page_id          = \System::formGetInt( 'id' );
    $article_number   = \System::formGetInt( 'article_number' );
    $parent_id        = \System::formGetInt( 'parent_id' );
    $only_for_logged  = \System::formGetInt( 'only_for_logged' );
    $menu             = \System::formGetInt( 'menu_id' );
    $contact_form     = \System::formGetInt( 'contact_form' );
    $layout_id        = \System::formGetInt( 'layout_id' );
    
    $query = $db -> prepare( 'UPDATE pp_pages SET contact_form = :contact_form, only_for_logged = :only_for_logged, link = :link, id_menu = :id_menu, id_page_type = :id_page_type, id_sort_type = :id_sort_type, article_number = :article_number, show_title = :show_title, enabled = :enabled, parent_id = :parent_id WHERE id = :id ' );
    $query -> bindValue( ':only_for_logged',  $only_for_logged, \PDO::PARAM_INT );    
    $query -> bindValue( ':id_menu',          $menu,            \PDO::PARAM_INT );
    $query -> bindValue( ':id_page_type',     $page_type_id,    \PDO::PARAM_INT );
    $query -> bindValue( ':id_sort_type',     $sort_type,       \PDO::PARAM_INT );
    $query -> bindValue( ':article_number',   $article_number,  \PDO::PARAM_INT );
    $query -> bindValue( ':show_title',       $show_title,      \PDO::PARAM_INT );
    $query -> bindValue( ':enabled',          $enabled,         \PDO::PARAM_INT );
    $query -> bindValue( ':id',               $page_id,         \PDO::PARAM_INT );
    $query -> bindValue( ':parent_id',        $parent_id,       \PDO::PARAM_INT );
    $query -> bindValue( ':link',             $link,            \PDO::PARAM_STR );
    $query -> bindValue( ':contact_form',     $contact_form,    \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();

    if ( $page_id )
    {
      $query = $db -> prepare( 'DELETE FROM pp_layouts_pages WHERE page_id = :page_id' );
      $query -> bindValue( ':page_id', $page_id, \PDO::PARAM_INT );
      $query -> execute();
      $query -> closeCursor(); 
      
      $query = $db -> prepare( 'INSERT INTO pp_layouts_pages ( layout_id, page_id ) VALUES ( :layout_id, :page_id )' );
      $query -> bindValue( ':layout_id',  $layout_id, \PDO::PARAM_INT );
      $query -> bindValue( ':page_id',    $page_id,   \PDO::PARAM_INT );
      $query -> execute();
      $query -> closeCursor();
        
      $query = $db -> prepare( 'SELECT id FROM pp_langs WHERE enabled = :enabled' );
      $query -> bindValue( ':enabled', 1, \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $title            = \System::formGet( 'title_' . $row['id'] );
        $meta_description = \System::formGet( 'meta_description_' . $row['id'] );
        $meta_keywords	 	= \System::formGet( 'meta_keywords_' . $row['id'] );
        $meta_title       = \System::formGet( 'meta_title_' . $row['id'] );
        $seo_link         = \System::seo( \System::formGet( 'seo_link_' . $row['id'] ) );

        if ( $title )
        {
          $query2	= $db -> prepare( 'SELECT id FROM pp_pages_langs WHERE page_id=:page_id AND lang_id = :lang_id' );
          $query2 -> bindValue( ':page_id', $page_id,   \PDO::PARAM_INT );
          $query2 -> bindValue( ':lang_id', $row['id'], \PDO::PARAM_INT );
          $query2 -> execute();
          if ( $query2 -> rowCount() )
          {
            $query3 = $db -> prepare( 'UPDATE 
                                          pp_pages_langs 
                                        SET
                                          seo_link = :seo_link, title = :title, meta_description = :meta_description, meta_keywords = :meta_keywords, meta_title = :meta_title
                                        WHERE 
                                          page_id = :page_id AND lang_id = :lang_id' );
            $query3 -> bindValue( ':page_id',           $page_id,           \PDO::PARAM_INT );
            $query3 -> bindValue( ':lang_id',           $row['id'],         \PDO::PARAM_INT );
            $query3 -> bindValue( ':title',             $title,             \PDO::PARAM_STR );
            $query3 -> bindValue( ':meta_description',  $meta_description,  \PDO::PARAM_STR );
            $query3 -> bindValue( ':meta_keywords',     $meta_keywords,     \PDO::PARAM_STR );
            $query3 -> bindValue( ':seo_link',          $seo_link,          \PDO::PARAM_STR );
            $query3 -> bindValue( ':meta_title',        $meta_title,        \PDO::PARAM_STR );
            $query3 -> execute();
            $query3 -> closeCursor();
          }
          else
          {
            $query3 = $db -> prepare( 'INSERT INTO 
                                          pp_pages_langs 
                                            ( page_id, lang_id, title, meta_description, meta_keywords, seo_link, meta_title ) 
                                        VALUES 
                                            ( :page_id, :lang_id, :title, :meta_description, :meta_keywords, :seo_link, :meta_title )' );
            $query3 -> bindValue( ':page_id',           $page_id,           \PDO::PARAM_INT );
            $query3 -> bindValue( ':lang_id',           $row['id'],         \PDO::PARAM_INT );
            $query3 -> bindValue( ':title',             $title,             \PDO::PARAM_STR );
            $query3 -> bindValue( ':meta_description',  $meta_description,  \PDO::PARAM_STR );
            $query3 -> bindValue( ':meta_keywords',     $meta_keywords,     \PDO::PARAM_STR );
            $query3 -> bindValue( ':seo_link',          $seo_link,          \PDO::PARAM_STR );
            $query3 -> bindValue( ':meta_title',        $meta_title,        \PDO::PARAM_STR );
            $query3 -> execute();
            $query3 -> closeCursor();
          }
          $query2 -> closeCursor();
        }
      }
      $query -> closeCursor();
      \System::rewriteHtacces();
      return true;
    }
    return false;
  }

	public static function addPage() 
  {
		global $db;

    \System::deleteCacheAdmin();
    \System::deleteCache();

		$enabled          = \System::formGet( 'enabled' );
		$show_title       = \System::formGet( 'show_title' );
		$sort_type        = \System::formGet( 'sort_type' );
		$page_type_id     = \System::formGet( 'page_type_id' );
    $link             = \System::formGet( 'link' );
		$check            = \System::formGet( 'check' );
		$o                = \System::formGetInt( 'o' );
		$article_number   = \System::formGetInt( 'article_number' );
    $parent_id        = \System::formGetInt( 'parent_id' );
    $only_for_logged  = \System::formGetInt( 'only_for_logged' );
		$menu             = \System::formGetInt( 'menu_id' );
    $contact_form     = \System::formGetInt( 'contact_form' );
    $layout_id        = \System::formGetInt( 'layout_id' );
    
		if ( $check != \System::getSessionVar( 'check' ) ) 
    {
			$query = $db -> prepare( 'INSERT INTO pp_pages 
                                  ( only_for_logged, id_menu, id_page_type, id_sort_type, article_number, show_title, enabled, o, parent_id, link, contact_form ) 
                                VALUES 
                                  ( :only_for_logged, :id_menu, :id_page_type, :id_sort_type, :article_number, :show_title, :enabled, :o, :parent_id, :link, :contact_form )' );
      $query -> bindValue( ':only_for_logged',  $only_for_logged, \PDO::PARAM_INT );      
			$query -> bindValue( ':id_menu',          $menu,            \PDO::PARAM_INT );
			$query -> bindValue( ':id_page_type',     $page_type_id,    \PDO::PARAM_INT );
			$query -> bindValue( ':id_sort_type',     $sort_type,       \PDO::PARAM_INT );
			$query -> bindValue( ':article_number',   $article_number,  \PDO::PARAM_INT );
			$query -> bindValue( ':show_title',       $show_title,      \PDO::PARAM_INT );
			$query -> bindValue( ':enabled',          $enabled,         \PDO::PARAM_INT );
			$query -> bindValue( ':o',                $o,               \PDO::PARAM_INT );
      $query -> bindValue( ':parent_id',        $parent_id,       \PDO::PARAM_INT );
      $query -> bindValue( ':link',             $link,            \PDO::PARAM_STR );
      $query -> bindValue( ':contact_form',     $contact_form,    \PDO::PARAM_INT );
			$query -> execute();
			$query -> closeCursor();

			$page_id = $db -> lastInsertId();

			if ( $page_id )
			{
        $query = $db -> prepare( 'INSERT INTO pp_layouts_pages ( layout_id, page_id ) VALUES ( :layout_id, :page_id )' );
        $query -> bindValue( ':layout_id',  $layout_id, \PDO::PARAM_INT );
        $query -> bindValue( ':page_id',    $page_id,   \PDO::PARAM_INT );
        $query -> execute();
        $query -> closeCursor();
        
				$query = $db -> prepare( 'SELECT id FROM pp_langs WHERE enabled = :enabled' );
				$query -> bindValue( ':enabled', 1, \PDO::PARAM_STR );
				$query -> execute();
				if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
				{
					$title              = \System::formGet( 'title_' . $row['id'] );
					$meta_description 	= \System::formGet( 'meta_description_' . $row['id'] );
					$meta_keywords      = \System::formGet( 'meta_keywords_' . $row['id'] );
          $meta_title         = \System::formGet( 'meta_title_' . $row['id'] );
          $seo_link           = \System::seo( \System::formGet( 'seo_link_' . $row['id'] ) );

					if ( $title )
					{
						$query2 = $db -> prepare( 'INSERT INTO 
                                          pp_pages_langs 
                                            ( page_id, lang_id, title, meta_description, meta_keywords, meta_title, seo_link ) 
                                        VALUES 
                                            ( :page_id, :lang_id, :title, :meta_description, :meta_keywords, :meta_title, :seo_link )' );
						$query2 -> bindValue( ':page_id',           $page_id,           \PDO::PARAM_INT );
						$query2 -> bindValue( ':lang_id',           $row['id'],         \PDO::PARAM_INT );
						$query2 -> bindValue( ':title',             $title,             \PDO::PARAM_STR );
						$query2 -> bindValue( ':meta_description',  $meta_description,  \PDO::PARAM_STR );
						$query2 -> bindValue( ':meta_keywords',     $meta_keywords,     \PDO::PARAM_STR );
            $query2 -> bindValue( ':seo_link',          $seo_link,          \PDO::PARAM_STR );
            $query2 -> bindValue( ':meta_title',        $meta_title,        \PDO::PARAM_STR );
						$query2 -> execute();
						$query2 -> closeCursor();
					}
				}
				$query -> closeCursor();

				\System::setSessionVar( 'check', $check );
				\System::rewriteHtacces();
        return true;
			}
		}
    return false;
	}

  public static function deletePage( $id )
	{
		global $db;
    
    $query = $db -> prepare( 'SELECT COUNT( 0 ) FROM pp_pages WHERE parent_id = :parent_id' );
    $query -> bindValue( ':parent_id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $count = $row[0];
    $query -> closeCursor();
    
    if ( $count )
    {
      \System::setAlert( 'Strona nie może być usunięta z powodu przypisanych podstron.' );
      return false;
    }

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

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

		$query = $db -> prepare( 'DELETE FROM pp_pages_langs WHERE page_id = :page_id' );
		$query -> bindValue( ':page_id', $id, \PDO::PARAM_INT );
		$query -> execute();
    if ( $query -> rowCount() )
    {
      \System::setAlert( 'Strona została usunięta.' );
      \System::rewriteHtacces();

      \System::deleteCacheAdmin();
      \System::deleteCache();
    }
		$query -> closeCursor();
	}
}
?>