<?php
namespace front\factory;

class Page {
  
  const menu_pattern      = '/MENU:[0-9]*/';
  const arts_list_pattern = '/ARTYKULY-LISTA:[0-9]*/';
  const lang_pattern      = '/JEZYK:(.*)*]/';
  const container_pattern = '/KONTENER:[0-9]*/';

  public static function convertPage( $layout )
  {
    global $site, $xajax, $config, $lang;
    
    $art_id = \System::formGetInt( 'art' );
    
    if ( $art_id )
    {
      $layout_id  = \article\FArticle::getArticleLayoutId( $art_id );
      $layout     = \admin\factory\Layouts::getLayout( $layout_id );
    }

    $html = stripslashes( $layout['html'] );
    
    if ( ( $config['dev'] or \System::formGet( 'dev' ) == true ) and file_exists( 'dev.html' ) )
      $html = file_get_contents ( 'dev.html' );
    
    if ( \admin\factory\Settings::getSystemSettings( 'facebook_url' ) )
      $html = str_replace( '</body>', \site\VSite::getFacebookLikeBox() . '</body>', $html );
    
    $html = str_replace( '[ALERT]',                 \site\VSite::drawAlert(),                           $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( '[FORMULARZ_KONTAKTOWY]',  \site\VSite::drawContactForm(),                     $html );
    $html = str_replace( '[WYSZUKIWARKA]',          \site\VSite::drawSearchForm(),                      $html );
    $html = str_replace( '[REKLAMA]',               \site\VSite::drawAdverts(),                         $html );
    $html = str_replace( '[PANEL_UZYTKOWNIKA]',     \user\VUser::drawUserPanel(),                       $html );
    $html = str_replace( '[JEZYKI]',                \language\VLanguage::showLanguages(),               $html );
    $html = str_replace( '[NEWSLETTER]',            \newsletter\VNewsletter::drawNewsletterSignForm(),  $html );
    $html = str_replace( '[STRONA_GLOWNA]',         \System::getMainPage(),                             $html );
    $html = str_replace( '[CSS]',                   stripslashes( $layout['css'] ),                     $html );
    $html = str_replace( '[JAVA_SCRIPT]',           stripslashes( $layout['javascript'] ),              $html );
    $html = str_replace( '[PAGE_ID]',               $site -> _values['id'],                             $html );
    $html = str_replace( '[AJAX]',                  $xajax -> getJavascript( '../resources/xajax/' ),   $html );
    $html = str_replace( '[LICZNIK_ODWIEDZIN]',     \site\VSite::drawVisitCounter(),                    $html );
    
    preg_match_all( self::lang_pattern, $html, $lang_t );
    if ( is_array( $lang_t[0] ) ) foreach( $lang_t[0] as $lang_tmp )
    {
      $lang_tmp = explode( ':', $lang_tmp );
      $lang_tmp = substr( $lang_tmp[1], 0, strlen( strip_tags( $lang_tmp[1] ) ) - 1 );
      $html     = str_replace( '[JEZYK:' . $lang_tmp . ']', $lang -> getTrans( $lang_tmp ), $html );
    }

    preg_match_all( self::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 );
    }
    
    preg_match_all( self::arts_list_pattern, $html, $arts_list );
    if ( is_array( $arts_list[0] ) ) foreach( $arts_list[0] as $arts_list_tmp )
    {
      $arts_list_tmp = explode( ':', $arts_list_tmp );
      $html = str_replace( '[ARTYKULY-LISTA:' . $arts_list_tmp[1] . ']', \article\VArticle::drawArticleListStatic( $arts_list_tmp[1] ), $html );
    }
    
    preg_match_all( self::container_pattern, $html, $container_list );
    if ( is_array( $container_list[0] ) ) foreach( $container_list[0] as $container_list_tmp )
    {
      $container_list_tmp = explode( ':', $container_list_tmp );
      $html = str_replace( '[KONTENER:' . $container_list_tmp[1] . ']', \site\VSite::drawStaticContainer( $container_list_tmp[1] ), $html );
    }
    
    /* wyświetlenie artykułu i jego metadanych */
    if ( $art_id )
    {
      $article = \article\FArticle::loadArticle( $art_id );
      
      if ( $article -> _values['meta_keywords'] )
        \System::setSessionVar( 'keywords', $article -> _values['meta_keywords'] );
      
      if ( $article -> _values['meta_description'] )
        \System::setSessionVar( 'description', $article -> _values['meta_description'] );      

      $keywords     = \System::getSessionVar( 'keywords' );
      $description  = \System::getSessionVar( 'description' );   
      
      $site_title = \System::getRandomKeyWord( $keywords ) . ' &#8226; ' . $article -> _values['title']. ' &#8226; ' . \admin\factory\Settings::getSystemSettings( 'firm_name' );
      $html       = str_replace( '[ZAWARTOSC]', \article\VArticle::draw( $art_id ), $html );
    }
    /* wyświetlenie pozostałych rzeczy i ich metadanych */
    else
    {      
      if ( $site -> _values['meta_keywords'] != '' )
        \System::setSessionVar( 'keywords', $site -> _values['meta_keywords'] );
      
      if ( $site -> _values['meta_description'] != '' )
        \System::setSessionVar( 'description', $site -> _values['meta_description'] );

      $keywords     = \System::getSessionVar( 'keywords' );
      $description  = \System::getSessionVar( 'description' );   
      
      $site_title =  \System::getRandomKeyWord( $keywords ) . ' &#8226; ' . $site -> _values['title'] . ' &#8226; ' . \admin\factory\Settings::getSystemSettings( 'firm_name' );
      
      if ( $site -> _values['meta_title'] )
        $site_title = $site -> _values['meta_title'];
      
      $html       = str_replace( '[ZAWARTOSC]', \site\FSite::getContent(), $html );
    }   
    
    if ( $site -> _values['show_title'] )
      $title = \site\VSite::drawTitle( $site -> _values['title'] );
      
    if ( $art_id && $article -> _values['replace_menu_title'] )
      $title = \site\VSite::drawTitle( $article -> _values['title'] );

    $keywords     = \System::getSessionVar( 'keywords' );
    $description  = \System::getSessionVar( 'description' );   
    
    $html = str_replace( '[TYTUL_STRONY]',    $title,       $html );
    $html = str_replace( '[TYTUL]',           $site_title,  $html );
    $html = str_replace( '[SLOWA_KLUCZOWE]',  $keywords,    $html );
    $html = str_replace( '[OPIS_STRONY]',     $description, $html );
    
    if ( strpos( $html, '[PHP]' ) !== false ) 
    {
      $text = explode( '[PHP]', $html );
      $before = $text[0];
      for ( $i = 1; $i < count( $text ); $i++ ) 
      {
        $temp   = explode( '[/PHP]' , $text[$i] );
        $code   = $temp[0];
       
        ob_start();
        eval( $code );
        $out .= ob_get_contents();
        ob_end_clean();
            
        $out .= $temp[1];
      }
      $html = $before . $out;
    }

    return $html;
  }
}
?>