<?php
namespace front\factory;

class Page {

  public function getActiveLayout()
  {
    global $db , $site;

    $layout = '';
    $query = $db -> prepare( 'SELECT html , css , javascript FROM pcms_layout_pages , pcms_layout WHERE page_id = :page_id AND pcms_layout.id = layout_id ORDER BY layout_id DESC LIMIT 1' );
    $query -> bindValue( ':page_id' , $site -> get_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 pcms_layout WHERE enabled = :enabled' );
      $query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $layout = $row;
      $query -> closeCursor();
    }

    return $layout;
  }

  public function convertPage( $layout )
  {
    global $site , $xajax, $config;
    
    if ( \System::formGetInt( 'art' ) )
      $layout = \System::getLayout( \article\FArticle::getArticleLayoutId( \System::formGetInt( 'art' ) ) );

    $menu_pattern = '/MENU:[0-9]*/';

    $html = stripslashes( $layout['html'] );
    
    if ( $config['dev'] && file_exists( 'dev.html' ) )
      $html = file_get_contents( 'dev.html' );
    
    if ( $site -> get_show_title() )
      $title = $site -> get_title();
    else
      $title = '';

    $html = str_replace( '[TYTUL_STRONY]'       , $title                          , $html );
    $html = str_replace( '[JEZYKI]'             , \language\VLanguage::show()     , $html );
    $html = str_replace( '[ALERT]'              , \System::getAlert()             , $html );
    $html = str_replace( '[STRONA_GLOWNA]'      , \System::getMainPage()          , $html );
    $html = str_replace( '[CSS]'                , stripslashes( $layout['css'] )  , $html );
    $html = str_replace( '[LICZNIK]'            , \site\VSite::drawVisitCounter() , $html );
    $html = str_replace( '[COPYRIGHT]'          , \site\VSite::drawCopyright()    , $html );
    $html = str_replace( '[BANERY]'             , \site\VSite::drawBanners()      , $html );
    $html = str_replace( '[KONTAKT_MINI]'       , \site\VSite::drawContactMini()  , $html );
    $html = str_replace( '[ZEGAR]'              , \site\VSite::drawClock()        , $html );
    $html = str_replace( '[IMIENINY]'           , \site\VSite::drawNameDay()      , $html );
    $html = str_replace( '[PANEL_UZYTKOWNIKA]'  , \user\VUser::drawUserPanel()    , $html );
    $html = str_replace( '[PAGE_ID]'            , $site -> get_id()               , $html );
    $html = str_replace( '[WYSZUKIWARKA]'       , \site\VSite::drawSearchForm()   , $html );
    $html = str_replace( '[REKLAMA]'            , \site\VSite::drawAdverts()      , $html );
    $html = str_replace( '[SWL]'              	, \site\VSite::drawSWL()          , $html );
    $html = str_replace( '[JAVA_SCRIPT]'        , stripslashes( $layout['javascript'] )             , $html );
    $html = str_replace( '[AJAX]'               , $xajax -> getJavascript( '../resources/xajax/' )  , $html );

    if ( \System::getAlert() )
      \System::setAlert('');

    preg_match_all( $menu_pattern , $html , $menu );
    if ( is_array( $menu[0] ) ) foreach( $menu[0] as $menu_tmp )
    {
      $menu_tmp = explode( ':' , $menu_tmp );
      $html = str_replace( '[MENU:' . $menu_tmp[1] . ']' , \menu\VMenu::show( $menu_tmp[1] ) , $html );
    }

    $keywords     = \System::getSessionVar( 'keywords' );
    $description  = \System::getSessionVar( 'description' );

    if ( \System::formGetInt( 'art' ) )
    {
      $id           = \System::formGetInt( 'art' );
      $keywords     = \article\FArticle::getKeywords( $id );
      $description  = \article\FArticle::getDescription( $id );
      
      $site_title   = \System::getRandomKeyWord( $keywords );
      
      if ( $site_title )
        $site_title .= ' - ';
      
      $site_title .= \article\FArticle::getArticleTitle( $id ) . ' - RSM';
      
      $html = str_replace( '[ZAWARTOSC]' , \article\VArticle::draw( $id )  , $html );
    }
    else
    {
      $keywords       = $site -> get_keywords();      
      $description    = $site -> get_description();
      
      $site_title   = \System::getRandomKeyWord( $keywords );
      
      if ( $site_title )
        $site_title .= ' - ';
      
      $site_title .=  $site -> get_title() . ' - RSM';
      
      if ( $site -> get_id() == 1 )
        $site_title = 'RSM Rzeszów – Rockowa Szkoła Muzyczna, nauka gry na gitarze i nie tylko…';
      
      $html = str_replace( '[ZAWARTOSC]' , \site\FSite::getContent() , $html );
    }

    $html = str_replace( '[TYTUL]'          , $site_title  , $html );
    $html = str_replace( '[SLOWA_KLUCZOWE]' , $keywords    , $html );
    $html = str_replace( '[OPIS_STRONY]'    , $description , $html );

    return $html;
  }
}
?>