<?php
namespace front\view;
use shop\Product;

class Site
{
  const menu_pattern = '/MENU:[0-9]*/';
  const menu_main_pattern = '/MENU_GLOWNE:[0-9]*/';
  const container_pattern = '/KONTENER:[0-9]*/';
  const language_pattern = '/LANG:[a-zA-Z0-9_-]*/';
  const products_promoted = '/PROMOWANE_PRODUKTY((:([0-9]*))?)/';
  const news_pattern = '/AKTUALNOSCI:([0-9]*)((:([0-9]*))?)/';
  const article_products_category_pattern = '/PRODUKTY_KATEGORIA:([0-9]*)((:([0-9]*))?)/';
  const news_list_pattern = '/AKTUALNOSCI_LISTA:([0-9]*)((:([0-9]*))?)/';
  const top_news_pattern = '/NAJPOULARNIEJSZE_ARTYKULY:([0-9]*)((:([0-9]*))?)/';
  const single_product_pattern = '/PRODUKT:[0-9]*/';
  const products_box = '/PRODUKTY_BOX:[0-9,]*/';
  const produkty_top = '/PRODUKTY_TOP((:([0-9]*))?)/';
  const produkty_new = '/PRODUKTY_NEW((:([0-9]*))?)/';

  public static function show()
  {
    global $page, $settings, $settings, $lang, $lang_id;

    if ( (int) \S::get( 'layout_id' ) )
      $layout = new \cms\Layout( (int) \S::get( 'layout_id' ) );

    if ( \S::get( 'article' ) )
      $layout = \front\factory\Layouts::article_layout( \S::get( 'article' ) );

    if ( \S::get( 'product' ) )
      $layout = \front\factory\Layouts::product_layout( \S::get( 'product' ) );

    if ( \S::get( 'category' ) )
      $layout = \front\factory\Layouts::category_layout( \S::get( 'category' ) );

    if ( !$layout )
      $layout = \front\factory\Layouts::active_layout( $page['id'] );

    if ( $settings['devel'] == true and file_exists( 'devel.html' ) )
      $html = file_get_contents( 'devel.html' );
    else
    {
      if ( \S::is_mobile() and !empty( $layout['m_html'] ) )
        $html = $layout['m_html'];
      else
        $html = $layout['html'];
    }

    if ( $settings['facebook_link'] )
      $html = str_replace( '</body>', \front\view\Site::facebook( $settings['facebook_link'] ) . '</body>', $html );

    $html = str_replace( '[COPYRIGHT]',
                            \front\view\Site::copyright(),
                            $html );

    $html = str_replace( '[BANER_STRONA_GLOWNA]', \front\view\Banners::main_banner( \front\factory\Banners::main_banner() ), $html );
    $html = str_replace( '[BANERY]',              \front\view\Banners::banners( \front\factory\Banners::banners() ), $html );

    $html = str_replace( '[KATEGORIE]', \Tpl::view( 'shop-category/categories', [
                                            'level' => $level,
                                            'current_category' => \S::get( 'category' ),
                                            'categories' => \front\factory\ShopCategory::categories_details()
                                          ] ), $html );

    /* BOX - promowane produkty */
    preg_match_all( self::products_promoted, $html, $products_promoted_list );
    if ( is_array( $products_promoted_list[0] ) ) foreach( $products_promoted_list[0] as $products_promoted_tmp )
    {
      $products_promoted_tmp = explode( ':', $products_promoted_tmp );
      $products_promoted_tmp[1] ? $limit = $products_promoted_tmp[1] : $limit = 6;

      $products_promoted_tmp[1] ? $pattern = '[PROMOWANE_PRODUKTY:' . $products_promoted_tmp[1] . ']' : $pattern = '[PROMOWANE_PRODUKTY]';

      $html = str_replace(  $pattern,
                            \Tpl::view( 'shop-product/promoted-products', [
                              'products' => \front\factory\ShopProduct::promoted_products( $limit )
                            ] ),
                            $html
                          );
    }

    /* BOX - aktualnsci */
    preg_match_all( self::news_pattern, $html, $news_list );
    if ( is_array( $news_list[0] ) ) foreach( $news_list[0] as $news_list_tmp )
    {
      $news_list_tmp = explode( ':', $news_list_tmp );

      $news_list_tmp[2] != '' ? $news_limit = $news_list_tmp[2] : $news_limit = $settings['news_limit'];

      $news_list_tmp[2] != '' ? $pattern = '[AKTUALNOSCI:' . $news_list_tmp[1] . ':' . $news_list_tmp[2] . ']' : $pattern = '[AKTUALNOSCI:' . $news_list_tmp[1] . ']';
      $html = str_replace( $pattern, \front\view\Articles::news(
                                                            $news_list_tmp[1],
                                                            \front\factory\Articles::news( $news_list_tmp[1], $news_limit, $lang_id )
                                                          ), $html );
    }

    $html = str_replace( '[KOSZYK]',
                            \Tpl::view( 'shop-basket/basket-mini', [
                              'basket' => \S::get_session( 'basket' ),
                              'lang_id' => $lang_id,
                              'coupon' => \S::get_session( 'coupon' )
                            ] ),
                            $html );
    $html = str_replace( '[NEWSLETTER]',
                            \front\view\Newsletter::newsletter(),
                            $html );
    $html = str_replace( '[UZYTKOWNIK_MINI_LOGOWANIE]',
                            \front\view\ShopClient::mini_login(),
                            $html );

    if ( \S::is_mobile() and !empty( $layout['m_html'] ) )
      $html = str_replace( '[CSS]',           $layout['m_css'],                 $html );
    else
      $html = str_replace( '[CSS]',           $layout['css'],                   $html );

    if ( \S::is_mobile() and !empty( $layout['m_html'] ) )
      $html = str_replace( '[JAVA_SCRIPT]',   $layout['m_js'],                  $html );
    else
      $html = str_replace( '[JAVA_SCRIPT]',   $layout['js'],                    $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] . ']', \front\view\Menu::menu( \front\factory\Menu::menu_details( $menu_tmp[1] ), $page['id'] ), $html );
    }

    preg_match_all( self::menu_main_pattern, $html, $menu );
    if ( is_array( $menu[0] ) ) foreach( $menu[0] as $menu_tmp )
    {
      $menu_tmp = explode( ':', $menu_tmp );
      $html = str_replace(
        '[MENU_GLOWNE:' . $menu_tmp[1] . ']',
        \Tpl::view( 'menu/main-menu', [
          'menu' => \front\factory\Menu::menu_details( $menu_tmp[1] )
        ] ),
        $html );
    }

    preg_match_all( self::language_pattern, $html, $language_list );
    if ( is_array( $language_list[0] ) ) foreach( $language_list[0] as $language_list_tmp )
    {
      $language_list_tmp = explode( ':', $language_list_tmp );
      $html = str_replace( '[LANG:' . $language_list_tmp[1] . ']', $lang[ $language_list_tmp[1] ], $html );
    }

    //
    // KATEGORIA SKLEPU
    //
    if ( \S::get( 'category' ) )
    {
      $category = \front\factory\ShopCategory::category_details( \S::get( 'category' ) );

      if ( $category['language']['meta_title'] )
        $page['language']['title'] = $category['language']['meta_title'];
      else
        $page['language']['title'] = $category['language']['title'];

      $page['show_title'] = true;
      $page['language']['meta_keywords'] = $category['language']['meta_keywords'];
      $page['language']['meta_description'] = $category['language']['meta_description'];
      $page['language']['page_title'] = $category['language']['category_title'] ? $category['language']['category_title'] : $category['language']['title'];

      // CANONICAL
      $html = str_replace( '[CANONICAL]', '', $html );
    }


    //
    // ARTYKUŁ
    //
    if ( \S::get( 'article' ) )
    {
      $article = \front\factory\Articles::article_details( \S::get( 'article' ), $lang_id );

      if ( $article['language']['meta_title'] )
        $page['language']['title'] = $article['language']['meta_title'];
      else
        $page['language']['title'] = $article['language']['title'];

      $page['show_title'] = false;
      $page['language']['meta_keywords'] = $article['language']['meta_keywords'];
      $page['language']['meta_description'] = $article['language']['meta_description'];

      // CANONICAL
      $html = str_replace( '[CANONICAL]', '', $html );
    }

    //
    // PRODUKT
    //
    if ( \S::get( 'product' ) )
    {
      $product = Product::getFromCache( \S::get( 'product' ), $lang_id, $_GET['permutation_hash'] );

      if ( $product['language']['meta_title'] )
        $page['language']['title'] = $product['language']['meta_title'];
      else
        $page['language']['title'] = $product['language']['name'];

      $page['show_title'] = false;
      $page['language']['meta_keywords'] = $product['language']['meta_keywords'];
      $page['language']['meta_description'] = $product['language']['meta_description'];

      // CANONICAL
      if ( $product['language']['canonical'] )
        $html = str_replace( '[CANONICAL]', '<link rel="canonical" href="' . $product['language']['canonical'] . '">', $html );
      else
        $html = str_replace( '[CANONICAL]', '', $html );
    }

    //
    // PRODUCENT
    //
    if ( \S::get( 'producer_id' ) )
    {
      $producer = new \shop\Producer( \S::get( 'producer_id' ) );

      if ( $producer['languages'][$lang_id]['meta_title'] )
        $page['language']['meta_title'] = $producer['languages'][$lang_id]['meta_title'];
    }

    //
    // STRONA CMS
    //
    if ( $page )
    {
      // CANONICAL
      $html = str_replace( '[CANONICAL]', '', $html );
    }

    $html = str_replace( '[CANONICAL]', '', $html );
    $html = str_replace( '[ZAWARTOSC]', \front\controls\Site::route( $product, $category ), $html );

    /* pojedynczy produkt */
    preg_match_all( self::single_product_pattern, $html, $single_product_array );
    if ( is_array( $single_product_array[0] ) ) foreach( $single_product_array[0] as $single_product )
    {
      $single_product = explode( ':', $single_product );
      $html = str_replace(
        '[PRODUKT:' . $single_product[1] . ']',
        \Tpl::view( 'shop-product/product-mini', [
          'product' => \shop\Product::getFromCache( (int)$single_product[1], $lang_id )
        ] ),
        $html
      );
    }

    /* lista produktów */
    preg_match_all( self::products_box, $html, $products_box_array );
    if ( is_array( $products_box_array[0] ) ) foreach( $products_box_array[0] as $products_box )
    {
      unset( $products_id ); unset( $product_id ); unset( $products );

      $products_box = explode( ':', $products_box );
      $products_id = explode( ',', $products_box[1] );

      foreach ( $products_id as $product_id )
        $products[] = Product::getFromCache( (int)$product_id, $lang_id );


      $html = str_replace(
        '[PRODUKTY_BOX:' . $products_box[1] . ']',
        \Tpl::view( 'shop-product/products-box', [
          'products' => $products
        ] ),
        $html
      );
    }

    /* lista popularnych produktów */
    preg_match_all( self::produkty_top, $html, $products_top_array );
    if ( is_array( $products_top_array[0] ) ) foreach( $products_top_array[0] as $products_top )
    {
      unset( $products_id_arr );
      unset( $limit );
      unset( $product_id );
      unset( $top_products_arr );

      $products_top = explode( ':', $products_top );
      $products_top[1] ? $limit = $products_top[1] : $limit = 6;

      $products_top[1] ? $pattern = '[PRODUKTY_TOP:' . $products_top[1] . ']' : $pattern = '[PRODUKTY_TOP]';

      $products_id_arr = \front\factory\ShopProduct::top_products( $limit );

      foreach ( $products_id_arr as $product_id ){
        $top_products_arr[] = Product::getFromCache( (int)$product_id, $lang_id );
      }

      $html = str_replace(  $pattern,
        \Tpl::view( 'shop-product/products-top', [
          'products' => $top_products_arr,
        ] ),
        $html
      );
    }

    /* lista ostatnio dodanych produktów */
    preg_match_all( self::produkty_new, $html, $products_top_array );
    if ( is_array( $products_top_array[0] ) ) foreach( $products_top_array[0] as $products_top )
    {
      unset( $products_id_arr );
      unset( $limit );
      unset( $product_id );
      unset( $top_products_arr );

      $products_top = explode( ':', $products_top );
      $products_top[1] ? $limit = $products_top[1] : $limit = 10;

      $products_top[1] ? $pattern = '[PRODUKTY_NEW:' . $products_top[1] . ']' : $pattern = '[PRODUKTY_NEW]';

      $products_id_arr = \front\factory\ShopProduct::new_products( $limit );

      
      foreach ( $products_id_arr as $product_id ){
        $top_products_arr[] = Product::getFromCache( (int)$product_id, $lang_id );
      }

      $html = str_replace(  $pattern,
        \Tpl::view( 'shop-product/products-new', [
          'products' => $top_products_arr,
        ] ),
        $html
      );
    }


    $html = str_replace( '[TITLE]', $page['language']['meta_title'] ? $page['language']['meta_title'] . ' | ' . $settings['firm_name'] : $page['language']['title'] . ' | ' . $settings['firm_name'], $html );
    $html = str_replace( '[META_KEYWORDS]', $page['language']['meta_keywords'], $html );
    $html = str_replace( '[META_DESCRIPTION]', $page['language']['meta_description'], $html );
    $html = str_replace( '[JEZYKI]', \front\view\Languages::languages(), $html );
    $html = str_replace( '[TYTUL_STRONY]', self::title( $page['language']['title'], $page['show_title'], $page['language']['page_title'] ), $html );
    $html = str_replace( '[WYSZUKIWARKA]', \shop\Search::simple_form(), $html );

    /* atrybut noindex */
    if ( \S::get( 'article' ) )
    {
      \front\factory\Articles::article_noindex( \S::get( 'article' ) ) ? $noindex = 'noindex' : $noindex = 'all';
      $html = str_replace( '[META_INDEX]', '<meta name="robots" content="' . $noindex . '">', $html );
    }
    else
    {
      $page['language']['noindex'] ? $noindex = 'noindex' : $noindex = 'all';
      $html = str_replace( '[META_INDEX]', '<meta name="robots" content="' . $noindex . '">', $html );
    }

    if ( $page['language']['canonical'] )
      $html = str_replace( '</head>', '<link rel="canonical" href="' . $page['language']['canonical'] . '" /></head>', $html );

    while ( 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;
    }

    /* BOX - blog produkty kategoria */
    preg_match_all( self::article_products_category_pattern, $html, $category_list );
    if ( is_array( $category_list[0] ) ) foreach( $category_list[0] as $category_list_tmp )
    {
      $category_list_tmp = explode( ':', $category_list_tmp );

      $category_list_tmp[2] != '' ? $products_limit = $category_list_tmp[2] : $products_limit = 4;

      $category_list_tmp[2] != '' ? $pattern = '[PRODUKTY_KATEGORIA:' . $category_list_tmp[1] . ':' . $category_list_tmp[2] . ']' : $pattern = '[PRODUKTY_KATEGORIA:' . $category_list_tmp[1] . ']';

      $html = str_replace(
                $pattern,
                \Tpl::view( 'shop-category/blog-category-products', [
                        'products' => \front\factory\ShopCategory::blog_category_products( $category_list_tmp[1], $lang_id, $products_limit )
                      ] ),
                $html );
    }

    // prosta lista aktualności z wybranej podstrony
    preg_match_all( self::news_list_pattern, $html, $news_list );
    if ( is_array( $news_list[0] ) ) foreach( $news_list[0] as $news_list_tmp )
    {
      $news_list_tmp = explode( ':', $news_list_tmp );
      $news_list_tmp[2] != '' ? $news_limit = $news_list_tmp[2] : $news_limit = $settings['news_limit'];
      $news_list_tmp[2] != '' ? $pattern = '[AKTUALNOSCI_LISTA:' . $news_list_tmp[1] . ':' . $news_list_tmp[2] . ']' : $pattern = '[AKTUALNOSCI_LISTA:' . $news_list_tmp[1] . ']';

      $news_list = \Article::getNews( $news_list_tmp[1], $news_limit, $lang_id );
      $view_news_list = \Article::newsList( $news_list );
      $html = str_replace( $pattern, $view_news_list, $html );
    }

    // prosta lista z najpopularniejszymi artykułami
    preg_match_all( self::top_news_pattern, $html, $news_list );
    if ( is_array( $news_list[0] ) ) foreach( $news_list[0] as $news_list_tmp )
    {
      $news_list_tmp = explode( ':', $news_list_tmp );
      $news_list_tmp[2] != '' ? $news_limit = $news_list_tmp[2] : $news_limit = $settings['news_limit'];
      $news_list_tmp[2] != '' ? $pattern = '[NAJPOULARNIEJSZE_ARTYKULY:' . $news_list_tmp[1] . ':' . $news_list_tmp[2] . ']' : $pattern = '[NAJPOULARNIEJSZE_ARTYKULY:' . $news_list_tmp[1] . ']';

      $news_list = \Article::getTopNews( $news_list_tmp[1], $news_limit, $lang_id );
      $view_news_list = \Article::newsList( $news_list );
      $html = str_replace( $pattern, $view_news_list, $html );
    }

    $html = str_replace( '[ALERT]', \front\view\Site::alert(), $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] . ']', \front\view\Scontainers::scontainer( $container_list_tmp[1] ), $html );
    }

    return $html;
  }

  public static function facebook( $facebook_link )
  {
    $tpl = new \Tpl;
    $tpl -> facebook_link = $facebook_link;
    return $tpl -> render( 'site/facebook' );
  }

  static public function title( $title, $show_title, $page_title )
  {
    return \Tpl::view( 'site/title', [
      'title' => $title,
      'page_title' => $page_title,
      'show_title' => $show_title
    ] );
  }

  public static function alert()
  {
    if ( $alert = \S::get_session( 'alert' ) )
    {
      \S::delete_session( 'alert' );

      return $tpl = \Tpl::view( 'site/alert', [
                'alert' => $alert
              ] );
    }

    if ( $error = \S::get_session( 'error' ) )
    {
      \S::delete_session( 'error' );

      $tpl = new \Tpl;
      $tpl -> error = $error;
      return $tpl -> render( 'site/error' );
    }
  }

  public static function copyright()
  {
    $tpl = new \Tpl;
    return $tpl -> render( 'site/copyright' );
  }

  public static function contact()
  {
    $tpl = new \Tpl;
    return $tpl -> render( 'site/contact' );
  }

  public static function cookie_information()
  {
    $tpl = new \Tpl;
    return $tpl -> render( 'site/cookie-information' );
  }
}
?>
