<?
namespace site;

class FSite {

  public function getSiteMapPages( $id, $parent_id = 0 )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT id FROM pp_pages WHERE enabled = "1" AND 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',  $id,        \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $page = new \site\Site( $row['id'] );
      $page -> _values['subpages'] = self::getSiteMapPages( $id, $row['id'] );
      $pages[] = $page;
    }
    $query -> closeCursor();
    
    return $pages;
  }
  
  public function getSiteMap()
  {
    global $db;
    
    $query = $db -> query( 'SELECT id FROM pp_menu WHERE enabled = "1"' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $sitemap['pages'] = self::getSiteMapPages( $row['id'] );
      $sitemaps[] = $sitemap;
    }
    $query -> closeCursor();
    
    return $sitemaps;
  }

  public static function getContent()
  {
    global $site, $user;
    
    $out    = '';
    $ls     = '';
    $bs     = \System::formGet( 'bs' );
    $cat_id = \System::formGetInt( 'product_category' );

    $link	= 's,' . $site -> _values['id'] . ',[bs],' . \System::seo( $site -> _values['title'] );
    
    switch ( $site -> _values['id_page_type'] )
    {
      case 1:
      case 2:
      case 3:
      case 4:
      case 12:
        $ls = ceil( \article\FArticle::getCountArticles() / $site -> _values['article_number'] );
        
        if ( $bs < 1 )
          $bs = 1;
        else if ( $bs > $ls )
          $bs = $ls;
        
        $from = $site -> _values['article_number'] * ( $bs - 1 );
        
        if ( $from < 0 )
          $from = 0;
        
        $articles = \article\FArticle::getArticles( $from );
        
        if ( $site -> _values['id_page_type'] == 12 )
          $product_categories = \product\FCategory::getCategories( 0, $cat_id );
      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;
        $quest_book   = \guestbook\FGuestBook::getEntries( $from , \guestbook\FGuestBook::getLimit() );
      break;
    }
    
    if ( $site -> _values['only_for_logged'] && !$user )
      return \user\VUser::drawLoginForm();

    switch ( $site -> _values['id_page_type'] )
    {
      case 1:
        $out = \article\VArticle::drawEntryArticles( $articles );
      break;
      case 2:
      case 12:
        $out = \article\VArticle::drawFullArticles( $articles );
      break;
      case 3:
        $out = \article\VArticle::drawListArticles( $articles );
      break;
      case 4:
        $out = \article\VArticle::drawMiniatureArticles( $articles );
      break;
      case 5:
        $out = \guestbook\VGuestBook::drawGuestBook( $quest_book );
      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 ( $site -> _values['contact_form'] )
      $out .= \site\VSite::drawContactForm();

    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( 'site/paging' );
    }

    return $out;
  }

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

    switch( $type )
    {
      case 'registration':
        $site = new \site\Site;
        $site -> _values['id']            = 0;
        $site -> _values['show_title']    = true;
        $site -> _values['title']         = ucfirst( $lang -> getTrans( 'T_REJESTRACJA' ) );
        $site -> _values['id_page_type']  = 'registration';
      break;
      case 'password_recovery':
        $site = new \site\Site;
        $site -> _values['id']            = 0;
        $site -> _values['show_title']    = true;
        $site -> _values['title']         = ucfirst( $lang -> getTrans( 'T_ODZYSKIWANIE_HASLA' ) );
        $site -> _values['id_page_type']  = 'password_recovery';
      break;
      case 'search':
        $site = new \site\Site;
        $site -> _values['id']            = 0;
        $site -> _values['show_title']    = true;
        $site -> _values['title']         = ucfirst( $lang -> getTrans( 'T_WYSZUKIWARKA' ) );
        $site -> _values['id_page_type']  = 'search';
      break;
      case 'sitemap':
        $site = new \site\Site;
        $site -> _values['id']            = 0;
        $site -> _values['show_title']    = true;
        $site -> _values['title']         = ucfirst( $lang -> getTrans( 'T_MAPA_STRONY' ) );
        $site -> _values['id_page_type']  = 'sitemap';
      break;
    }
    return $site;
  }

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

    $key = 'getMainSiteId';

    if ( !$id = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT id FROM pp_pages WHERE enabled = :enabled ORDER BY id_menu ASC, 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;
  }
}
?>
