<?php
namespace controls;

class Page {

  public function checkUrlParams()
  {
    switch ( \S::get( 'rw' ) )
    {
      case 'download':
        \S::downloadFile( \S::get( 'file' ) );
        header( 'Location: /' );
        exit;
      break;

      case 'user_logout':
        session_destroy();
        header( 'Location: /' );
        exit;
      break;

      case 'user_login':
        \factory\User::logon( \S::get( 'login' ) , \S::get( 'password' ) );
        header( 'Location: /' );
        exit;
      break;

      case 'db_edit_save':
        $db_edit_table        = \S::get_session( 'db_edit_table' );
        $db_edit_key          = \S::get_session( 'db_edit_key' );
        $db_edit_val          = \S::get_session( 'db_edit_val' );
        $db_edit_pols         = \S::get_session( 'db_edit_pols' );
        $db_edit_pass         = \S::get_session( 'db_edit_pass' );
        $db_edit_pols_t       = \S::get_session( 'db_edit_pols_type' );
        $db_edit_pols_hidden  = \S::get_session( 'db_edit_pols_hidden' );
        \DataBase::SaveData( $db_edit_table , $db_edit_key , $db_edit_val , $db_edit_pols , $db_edit_pass , $db_edit_pols_t , $db_edit_pols_hidden );
      break;

      case 'db_edit_add_new':
        $db_edit_table        	= \S::get_session( 'db_edit_table' );
        $db_edit_pols         	= \S::get_session( 'db_edit_pols' );
        $db_edit_pass         	= \S::get_session( 'db_edit_pass' );
        $db_edit_pols_hidden    = \S::get_session( 'db_edit_pols_hidden' );
        $db_edit_pols_t       	= \S::get_session( 'db_edit_pols_type' );
        $time                 	= \S::getHash( 'time' );
        \DataBase::AddData( $db_edit_table , $db_edit_pols , $db_edit_pols_hidden , $db_edit_pols_t , $time , $db_edit_pass );
      break;
    }
  }

  public function getContent()
  {
    global $user;

    $class = '\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() );

    if ( \S::get( 'p' ) == 'cron' )
      return \controls\Cron::route();

    if ( \S::get( 'p' ) )
      \S::set_session( 'page-type' , \S::get( 'p' ) );

    if ( $user['type'] == 'client' or $user['type'] == 'worker' )
    {
      switch ( \S::get_session( 'page-type' ) )
      {
        case 'client_sites':
        default:
          return \controls\RankerClients::main_view();
        break;

        case 'client_summary':
          if ( $user['type'] == 'client' )
            return \controls\RankerClients::getSummary();
        break;

        case 'client_reports':
          if ( $user['type'] == 'client' )
            return \controls\RankerClients::getReportsContent();
        break;
      }
    }

    if ( $user['type'] == 'reseller' )
    {
      switch ( \S::get_session( 'page-type' ) )
      {
        case 'client_sites':
        default:
          return \controls\RankerReseller::getSitesContent();
        break;

        case 'reseller_clients':
          return \controls\RankerReseller::getClientsContent();
        break;

        case 'reseller_summary':
          return \controls\RankerReseller::getSummary();
        break;

        case 'reseller_reports':
          return \controls\RankerReseller::getReportsContent();
        break;
      }
    }

    if ( $user['type'] == 'admin' )
    {
      switch ( \S::get_session( 'page-type' ) )
      {
        case 'cron':
          return \controls\Cron::route();
        break;

        case 'ranker_reports':
          return \controls\Ranker::getReportsContent();
        break;

        case 'ranker_summary':
          return \controls\Ranker::getSummaryContent();
        break;

        case 'ranker_clients':
          return \controls\Ranker::getClientsContent();
        break;

        case 'ranker_sites':
          return \controls\Ranker::getSitesContent();
        break;

        case 'settings':
          return \controls\Settings::getContent();
        break;

        case 'proxy_php':
          return \controls\Proxy::getContentProxyPhp();
        break;

        case 'statistics_general':
        default:
          return \controls\Statistics::route_general();
        break;
      }
    }
  }
}
?>
