<?php
namespace front\controls;
class GlobelusUser
{
  public static function confirm_account_delete()
  {
    global $lang;

    if ( \front\factory\GlobelusUser::confirm_account_delete( \S::get( 'hash' ) ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang[ 'konto-zostalo-usuniete' ] );
      unset( $_SESSION['g_user'] );
      unset( $g_user );
    }

    header( 'Location: /' );
    exit;
  }

  public static function delete_account()
  {
    global $lang, $g_user;

    if ( !\front\factory\Globelus::permissions_check( $g_user = \S::get_session( 'g_user' ), __CLASS__ . "\\" . __FUNCTION__ ) )
      return false;

    if ( \front\factory\GlobelusUser::delete_account( $g_user['id'] ) )
      \S::set_alert_prompt( 'Informacja', $lang[ 'usuwanie-konta-email' ] );

    if ( $g_user['type'] )
      header( 'Location: /panel-pracodawcy/profil' );
    else
      header( 'Location: /panel-kandydata/profil' );

    exit;
  }

  static public function save_new_password() {
    global $lang;

    if ( \front\factory\GlobelusUser::save_new_password( \S::get( 'hash' ), \S::get( 'password' ), \S::get( 'password_retype' ) ) )
      \S::set_alert_prompt( 'Informacja', $lang['haslo-zostalo-zmienione'] );

    header( 'Location: /logowanie' );
    exit;
  }

  public static function new_password()
  {
    global $lang;

    return \Tpl::view( 'globelus/user/new-password', array(
      'hash' => \S::get( 'hash' )
    ) );

    if ( \front\factory\GlobelusUser::new_password( \S::get( 'hash' ) ) )
      \S::set_alert_prompt( 'Informacja', $lang['nowe-haslo-zostalo-wyslane-na-twoj-adres-email'] );

    header( 'Location: /logowanie' );
    exit;
  }

  public static function send_email_password_recovery()
  {
    global $lang;

    if ( !\front\factory\GlobelusUser::email_user_exists( \S::get( 'email' ) ) )
    {
      \S::set_alert_prompt( 'Informacja', 'Podany adres email nie znajduje się w naszej bazie danych.' );
      header( 'Location: /logowanie' );
      exit;
    }

    if ( \front\factory\GlobelusUser::send_email_password_recovery( \S::get( 'email' ) ) )
      \S::set_alert_prompt( 'Informacja', $lang['odzyskiwanie-hasla-link-komunikat'] );
    else
      \S::set_alert_prompt( 'Informacja', $lang['odzyskiwanie-hasla-blad'] );
    header( 'Location: /logowanie' );
    exit;
  }

  public static function recover_password()
  {
    return \front\view\GlobelusUser::recover_password();
  }

  public static function confirm()
  {
    global $lang;

    if ( \front\factory\GlobelusUser::register_confirm( \S::get( 'hash' ) ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang['rejestracja-potwierdzenie'] );

      \front\factory\GlobelusUser::signin( null, null, null, null, \S::get( 'hash' ) );

      $g_user = \S::get_session( 'g_user' );

      if ( $g_user['type']  and !$g_user['profile_completed'] )
        \S::set_alert_prompt( 'Informacja', $lang['pracodawca-niewypelniony-profil-komunikat'] );

      if ( !$g_user['type'] )
        header( 'Location: /panel-kandydata/profil' );
      else
        header( 'Location: /panel-pracodawcy/profil' );
    }
    exit;
  }

  public static function resend_activation_mail()
  {
    global $lang;

    if ( \front\factory\GlobelusUser::resend_activation_mail( \S::get( 'hash' ) ) )
      \S::set_alert_prompt( 'Informacja', $lang['wyslano-link-do-aktywacji-konta'] );

    header( 'Location: /logowanie' );
    exit;
  }

  public static function login_as()
  {
    global $lang;


    if ( !\front\factory\GlobelusUser::signin( null, null, null, null, \S::get( 'hash' ) ) )
      header( 'Location: /logowanie' );
    else
    {
      $g_user = \S::get_session( 'g_user' );

      if ( $g_user['type']  and !$g_user['profile_completed'] )
        \S::set_alert_prompt( 'Informacja', $lang['pracodawca-niewypelniony-profil-komunikat'] );

      if ( \S::get( 'return_url' ) )
      {
        header( 'Location: ' . \S::get( 'return_url' ) );
        exit;
      }

      if ( !$g_user['type'] )
        header( 'Location: /panel-kandydata/profil' );
      else
        header( 'Location: /panel-pracodawcy/profil' );
    }
    exit;
  }

  public static function signin()
  {
    global $lang;

    if ( !\front\factory\GlobelusUser::signin( \S::get( 'email' ), \S::get( 'password' ) ) )
      header( 'Location: /logowanie' );
    else
    {
      $g_user = \S::get_session( 'g_user' );

      if ( $g_user['type']  and !$g_user['profile_completed'] )
        \S::set_alert_prompt( 'Informacja', $lang['pracodawca-niewypelniony-profil-komunikat'] );

      if ( !$g_user['type'] )
        header( 'Location: /panel-kandydata/profil' );
      else
      {
        if ( !$g_user['profile_completed'] )
          header( 'Location: /panel-pracodawcy/profil' );
        else
          header( 'Location: /panel-pracodawcy/ogloszenia' );
      }
    }
    exit;
  }

  public static function signup()
  {
    $result = \front\factory\GlobelusUser::signup( \S::get( 'email' ), \S::get( 'password' ), \S::get( 'type' ), \S::get( 'agremment_profile' ), \S::get( 'agremment_marketing' ) );
    echo json_encode( $result );
    exit;
  }

  public static function register()
  {
    if ( $g_user = \S::get_session( 'g_user' ) ) {
      if ( $g_user['type'] == '1' ) {
        header( 'Location: /panel-pracodawcy/profil' );
        exit;
      }
    }

    return \front\view\GlobelusUser::register();
  }

  public static function login()
  {
    return \front\view\GlobelusUser::login();
  }

  public static function logout()
  {
    session_destroy();
    header( 'Location: /' );
    exit;
  }
}