<?
/* 2011-09-26 - dodanie typu strony LINK */

namespace site;

class FSite {

  public function getSiteMap()
  {
    global $db;
      
    $query = $dbs -> query( 'SELECT id FROM pcms_menu WHERE enabled = 1' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $row['pages'] = '';
        
      $query2 = $db -> prepare( 'SELECT id FROM pcms_page WHERE enabled = "1" AND id_menu = :id_menu ORDER BY o ASC' );
      $query2 -> bindValue( ':id_menu', $row['id'], \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
        $row['pages'][] = \site\FSite::loadSite( $row2['id'] );
      $query2 -> closeCursor();
      
      $sitemap[] = $row;
    }
    return $sitemap;
  }

  public function getContent()
  {
    global $site;

    $out  = '';
    $ls   = '';
    $bs   = \System::formGet( 'bs' );

    $link	= 's,' . $site -> get_id() . ',[bs],' . \System::seo( $site -> get_title() );

    switch ( $site -> get_site_type() )
    {
      case 1:
      case 2:
      case 3:
      case 4:
        $ls = ceil( \article\FArticle::getCountArticles() / $site -> get_article_number() );
        if ( $bs < 1 )
          $bs = 1;
        else if ( $bs > $ls )
          $bs = $ls;
        $from = $site -> get_article_number() * ( $bs - 1 );
        if ( $from < 0 )
          $from = 0;
        $articles = \article\FArticle::getArticles( $from );
      break;

      case 5:
        $ls = ceil( \guestbook\FGuestBook::getCountEntries() / \guestbook\FGuestBook::getLimit() );

        if ( $bs < 1 )
          $bs = 1;
        else if ( $bs > $ls )
          $bs = $ls;
        $from = \guestbook\FGuestBook::getLimit() * ( $bs - 1 );
        if ( $from < 0 )
          $from = 0;
        $aEntries   = \guestbook\FGuestBook::getEntries( $from , \guestbook\FGuestBook::getLimit() );
      break;
    }

    switch ( $site -> get_site_type() )
    {
      case 1:
        $out = \site\VSite::drawEntryArticles( $articles );
      break;
      case 2:
        $out = \site\VSite::drawFullArticles( $articles );
      break;
      case 3:
        $out = \site\VSite::drawListArticles( $articles );
      break;
      case 4:
        $out = \site\VSite::drawMiniatureArticles( $articles );
      break;
      case 5:
        $out = \guestbook\VGuestBook::drawGuestBook( $aEntries );
      break;
      case 6:
        $out = \newsletter\VNewsletter::drawNewsletterForm();
      break;
      case 7:
        $out = \site\VSite::drawContactSite();
      break;
      case 'registration':
        $out = \user\VUser::drawRegisterForm();
      break;
      case 'user_panel':
          $out = \user\VUser::drawUserPanel();
      break;
      case 'password_recovery':
        $out = \user\VUser::drawPassRecoveryForm();
      break;
      case 'search':
        $out = \site\VSite::drawSearchFormFull();
      break;
      case 'sitemap':
        $out = \site\VSite::drawSiteMap();
      break;
    }

    if ( $ls > 1 )
    {
      $a	= \System::getPagingVar( "a" , $bs , $ls );
      $b	= \System::getPagingVar( "b" , $bs , $ls );

      $tpl = new \Savant3;
      $tpl -> _a    = $bs-$b;
      $tpl -> _b    = $bs+$a;
      $tpl -> _ls   = $ls;
      $tpl -> _link	= $link;
      $tpl -> _bs   = $bs;
      $out .= $tpl -> fetch( 'templates/site-paging.php' );
    }

    return $out;
  }

  public function loadSiteStatic( $type )
  {
    global $lang;

    switch( $type )
    {
      case 'registration':
        $site = new \site\Site;
        $site -> set_show_title( true );
        $site -> set_title( ucfirst( $lang -> getTrans( 'T_REJESTRACJA' ) ) );
        $site -> set_site_type( 'registration' );
      break;
      case 'password_recovery':
        $site = new \site\Site;
        $site -> set_show_title( true );
        $site -> set_title( ucfirst( $lang -> getTrans( 'T_ODZYSKIWANIE_HASLA' ) ) );
        $site -> set_site_type( 'password_recovery' );
      break;
      case 'search':
        $site = new \site\Site;
        $site -> set_show_title( true );
        $site -> set_title( ucfirst( $lang -> getTrans( 'T_WYSZUKIWARKA' ) ) );
        $site -> set_site_type( 'search' );
      break;
      case 'sitemap':
        $site = new \site\Site;
        $site -> set_show_title( true );
        $site -> set_title( ucfirst( $lang -> getTrans( 'T_MAPA_STRONY' ) ) );
        $site -> set_site_type( 'sitemap' );
      break;
    }
    return $site;
  }

  public function loadSite( $id = '' )
  {
    global $cache , $config , $db;

    $current_lang = \System::getSessionVar( 'current_lang' );
    if ( !$id ) $id = \site\FSite::getMainSiteId();

    $key = 'loadSite:param:' . $id;

    if ( !$params = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT id_menu , id_page_type , id_sort_type , article_number , show_title , link FROM pcms_page WHERE id = :id' );
      $query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $params = $row;
      }
      $query -> closeCursor();
      $cache -> store( $key , $params , $config['cache_expire_long'] );
    }

    $key = 'loadSite:translation:' . $id . ':' . $current_lang;

    if ( !$translation = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT title, meta_description, meta_keywords, seo_link FROM pcms_page_translation WHERE page_id = :page_id AND lang_id = :lang_id' );
      $query -> bindValue( ':page_id' , $id           , \PDO::PARAM_INT );
      $query -> bindValue( ':lang_id' , $current_lang , \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() ) 
      {
        $row['title'] = \System::getPageTitle( $id , $current_lang );
        $translation  = $row;
      } 
      else 
      {
        $row['title']             = \System::getPageTitle( $id , $current_lang );
        $row['meta_description']  = '';
        $row['meta_keywords']     = '';
        $translation              = $row;
      }
      $query -> closeCursor();
      $cache -> store( $key , $translation , $config['cache_expire_long'] );
    }
    
    $site = new \site\Site;
    $site -> set_id( $id );
    $site -> set_menu( $params['id_menu'] );
    $site -> set_sort_type( $params['id_sort_type'] );
    $site -> set_article_number( $params['article_number'] );
    $site -> set_show_title( $params['show_title'] );
    $site -> set_title( $translation['title'] );
    $site -> set_description( $translation['meta_description'] );
    $site -> set_keywords( $translation['meta_keywords'] );
    $site -> set_link( $params['link'] );
    $site -> set_site_type( $params['id_page_type'] );
    $site -> set_seo_link( $translation['seo_link'] );

    return $site;
  }

  public function getMainSiteId()
  {
    global $db , $cache , $config;

    $key = 'getMainSiteId';

    if ( !$id = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT id FROM pcms_page WHERE enabled = :enabled ORDER BY o ASC LIMIT 1' );
      $query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $id = $row['id'];
      }
      $query -> closeCursor();
      $cache -> store( $key , $id , $config['cache_expire_long'] );
    }

    return $id;
  }
}
?>
