<?php
namespace front\controls;

class Site 
{
  public static function route()
  {
    global $page, $lang_id;
    
    if ( \S::get( 'search' ) )
    {
      if ( $_POST['search_txt'] )
      {
        \S::set_session( 'search_txt', \S::get( 'search_txt' ) );
        header( "Location: " . $_SERVER['REQUEST_URI'] );
        exit;
      }
      return \front\view\Search::search_results( 
                                  \front\factory\Search::search_results( \S::get_session( 'search_txt' ), $lang_id ) 
                                );
    }
    
    if ( \S::get( 'tag' ) )
      return \front\view\Articles::articles_list( 
                                  \front\factory\Articles::articles_by_tags( \S::get( 'tag' ), $lang_id ) 
                                );
    
    if ( \S::get( 'article' ) )
    {
      $article = \front\factory\Articles::article_details( \S::get( 'article' ), $lang_id );
      
      if ( $article['password'] and !\S::get_session( 'article-' . $article['id'] . '-' . $article['password'] ) )
        return \front\view\Articles::password_view( [ 'article' => $article ] );
      else
        return \front\view\Articles::article( [ 'article' => $article ] );
    }
        
    if ( $page['page_type'] !== null )
    {
      switch ( $page['page_type'] )
      {
        /* pełne artykuły */
        case 0:
          return \front\view\Articles::full_articles_list( $page, $lang_id, \S::get( 'bs' ) );
        break;

        /* wprowadzenia */
        case 1:
          return \front\view\Articles::entry_articles_list( $page, $lang_id, \S::get( 'bs' ) );
        break;

        /* miniaturki */
        case 2:
          return \front\view\Articles::miniature_articles_list( $page, $lang_id, \S::get( 'bs' ) );
        break;

        /* strona kontaktu */
        case 4:
          $out = \front\view\Articles::full_articles_list( $page, $lang_id, \S::get( 'bs' ) );
          $out .= \front\view\Site::contact();
          return $out;
        break;
      }
    }
    
    if ( !\S::get( 'module' ) || !\S::get( 'action' ) )
      return false;
    
    $class  = '\front\controls\\' . ucfirst( \S::get( 'module' ) );
    $action = \S::get( 'action' );
    
    if ( class_exists( $class ) and method_exists( new $class, $action ) )
      return call_user_func_array( array( $class, $action ), array() );
  }
  
  public static function check_url_params()
  {
    global $lang, $config;

    $a = \S::get( 'a' );
    
    switch ( $a )
    {
      case 'page':
        $page = \front\factory\Pages::page_details( \S::get( 'id' ) );
        \S::set_session( 'page', $page );
      break;
    
      case 'change_language':
        \S::set_session( 'current-lang', \S::get( 'id' ) );
        header( 'Location: /' );
        exit;
      break;
    
      case 'newsletter-sign':
        if ( \front\factory\Newsletter::newsletter_signin( \S::get( 'email' ) ) )
          \S::alert( 'Dziękujemy za zapisanie się do newslettera.' );
        header( 'Location: ' . $_SERVER['REQUEST_URI'] );
        exit;
      break;
    
      case 'newsletter-signout':
        if ( \front\factory\Newsletter::newsletter_signout( \S::get( 'email' ) ) )
          \S::alert( 'Twój adres email został usunięty z naszej bazy.' );
        header( 'Location: ' . $_SERVER['REQUEST_URI'] );
        exit;
      break;
    }
    
    if ( \S::get( 'lang' ) )
      \S::set_session( 'current-lang', \S::get( 'lang' ) );
    
    if ( file_exists( 'modules/actions.php' ) )
      include 'modules/actions.php';
  }
}
?>
