<?php
namespace front\controls;

class Site
{
  static public function page_title()
  {
    $class = '\front\controls\\';

    $results = explode( '_', \S::get( 'module' ) );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $class .= ucfirst( $row );

    $property = \S::get( 'action' );
    if ( class_exists( $class ) and property_exists( new $class, 'page_title' ) )
      return $class::$title[$property];
  }

  static public function title()
  {
    global $settings;

    $class = '\front\controls\\';

    $results = explode( '_', \S::get( 'module' ) );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $class .= ucfirst( $row );

    $property = \S::get( 'action' );
    if ( class_exists( $class ) and property_exists( new $class, 'title' ) )
      return $class::$title[$property] . ' | ' . $settings['firm_name'];
  }

  public static function route( $product = '', $category = '' )
  {
    global $page, $lang_id, $settings;

    if ( \S::get( 'article' ) )
      return \front\view\Articles::full_article( \S::get( 'article' ), $lang_id );

    // wyświetlenie pojedynczego produktu
    if ( $product )
    {
      \shop\Product::add_visit( $product -> id );
      return \Tpl::view( 'shop-product/product', [
                'product' => $product,
                'settings' => $settings,
                'lang_id' => $lang_id,
                'settings' => $settings
              ] );
    }

    if ( $category )
      return \front\view\ShopCategory::category_view( $category, $lang_id, \S::get( 'bs' ) );

    // stare klasy
    $class = '\front\controls\\';

    $results = explode( '_', \S::get( 'module' ) );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $class .= ucfirst( $row );

    $action = \S::get( 'action' );

    if ( class_exists( $class ) and method_exists( new $class, $action ) )
      return call_user_func_array( array( $class, $action ), array() );

    // klasy sklepowe
    $class = '\shop\\';

    $results = explode( '_', \S::get( 'module' ) );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $class .= ucfirst( $row );

    $action = \S::get( 'action' );

    if ( class_exists( $class ) and method_exists( new $class, $action ) )
      return call_user_func_array( array( $class, $action ), array() );

    if ( $page['id'] )
    {
      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;
      }
    }
  }

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

    if ( \S::get( 'lang' ) )
      \S::set_session( 'current-lang', \S::get( 'lang' ) );

    if ( file_exists( 'modules/actions.php' ) )
      include 'modules/actions.php';
  }
}
?>
