<?php
namespace front\controls;
class GlobelusCandidates
{
  public static function new_candidate()
  {
    if ( \S::get( 'name', true ) and \S::get( 'surname', true ) and \S::get( 'email', true ) )
    {
      $temp_data['p'] = $_POST;
      \S::set_session( 'td', $temp_data );

      $results = \front\factory\GlobelusCandidates::register_candidate(
                    \S::get( 'email' ), \S::get( 'name' ), \S::get( 'surname' ), \S::get( 'phone' ), \S::get( 'date_of_birth' ), \S::get( 'sex' ), \S::get( 'positions' ), \S::get( 'positions_experience' ),
                    \S::get( 'inexperience' ), \S::get( 'categories' ), \S::get( 'languages' ), \S::get( 'languages_experience' ), \S::get( 'countries' ), \S::get( 'voivodeships' ), \S::get( 'status_of_availability' ),
                    \S::get( 'date_of_availability' ), \S::get( 'experience_abroad' ), \S::get( 'accommodation' ), \S::get( 'driving_license' ), \S::get( 'own_car' ), \S::get( 'other_information' ),
                    $_FILES['cv'], $_FILES['avatar']
                  );

      \S::set_alert_prompt( 'Informacja', $results['msg'] );

      if ( $results['status'] == 'ok' )
        header( 'Location: /panel-kandydata/profil' );
      else
        header( 'Location: /jakiej-pracy-szukasz' );
      exit;
    }
  }

  public static function cv_tmp_download()
  {
    return \front\factory\GlobelusCandidates::cv_tmp_download( \S::get( 'answer_id' ), \S::get( 'token' ) );
  }

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

    return \front\view\GlobelusCandidates::adverts_list(
              \front\factory\GlobelusCandidates::adverts_list( $g_user['id'] )
            );
  }

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

    return \front\view\GlobelusCandidates::answers_list(
              \front\factory\GlobelusCandidates::answers_list( $g_user['id'] )
            );
  }

  public static function avatar()
  {
    return \front\factory\GlobelusCandidates::avatar( \S::get( 'user_id' ), \S::get( 'token' ) );
  }

  public static function cv_download()
  {
    return \front\factory\GlobelusCandidates::cv_download( \S::get( 'user_id' ), \S::get( 'token' ) );
  }

  public static function profile_disable()
  {
    global $lang;

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

    if ( \front\factory\GlobelusCandidates::profile_disable( $g_user['id'] ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang[ 'konto-zostalo-wylaczone' ] );
      \S::set_session( 'g_user', \front\factory\GlobelusUser::user_details( $g_user['id'] ) );
    }

    header( 'Location: /panel-kandydata/profil' );
    exit;
  }

  public static function profile_enable()
  {
    global $lang;

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

    if ( \front\factory\GlobelusCandidates::profile_enable( $g_user['id'], $g_user['email'] ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang[ 'konto-zostalo-opublikowane' ] );
      \S::set_session( 'g_user', \front\factory\GlobelusUser::user_details( $g_user['id'] ) );
    }

    header( 'Location: /panel-kandydata/profil' );
    exit;
  }

  public static function profile_extend()
  {
    global $lang;

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

    if ( !$g_user or $g_user['type'] )
      return false;

    if ( \front\factory\GlobelusCandidates::profile_extend( $g_user['id'], $g_user['active_to'] ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang[ 'konto-zostalo-przedluzone' ] );
      \S::set_session( 'g_user', \front\factory\GlobelusUser::user_details( $g_user['id'] ) );
    }

    header( 'Location: /panel-kandydata/profil' );
    exit;
  }

  public static function profile_refresh()
  {
    global $lang;

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

    if ( \front\factory\GlobelusCandidates::profile_refresh( $g_user['id'], $g_user['last_refresh'] ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang[ 'konto-zostalo-odzwiezone' ] );
      \S::set_session( 'g_user', \front\factory\GlobelusUser::user_details( $g_user['id'] ) );
    }

    header( 'Location: /panel-kandydata/profil' );
    exit;
  }

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

    \front\factory\GlobelusUser::user_change_password( $g_user['id'], \S::get( 'current_password' ), \S::get( 'new_password' ), \S::get( 'repeat_new_password' ) );

    header( 'Location: /panel-kandydata/ustawienia-konta' );
    exit;
  }

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

    return \front\view\GlobelusCandidates::profile_settings( $g_user );
  }

  public static function data_save()
  {
    global $lang;

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

    if ( \front\factory\GlobelusCandidates::datą_save(
            $g_user['id'], \S::get( 'name' ), \S::get( 'surname' ), \S::get( 'date_of_birth' ), \S::get( 'sex' ), \S::get( 'phone' ), \S::get( 'experience_abroad' ), \S::get( 'status_of_availability' ),
            \S::get( 'date_of_availability' ), \S::get( 'accommodation' ), \S::get( 'driving_license' ), \S::get( 'own_car' ), \S::get( 'positions' ), \S::get( 'positions_experience' ),
            \S::get( 'inexperience' ), \S::get( 'countries' ), \S::get( 'voivodeships' ), \S::get( 'languages' ), \S::get( 'languages_experience' ), \S::get( 'other_information' ), \S::get( 'categories' ), $_FILES['cv'], $_FILES['avatar']
          ) )
    {
      if ( $g_user['visible'] )
        \S::set_alert_prompt( 'Informacja', $lang['dane-zostaly-zaktualizowane'] );
      else
        \S::set_alert_prompt( 'Informacja', $lang['dane-zostaly-zaktualizowane-nieopublikowany-profil'] );
      \S::set_session( 'g_user', \front\factory\GlobelusUser::user_details( $g_user['id'] ) );
      header( 'Location: /panel-kandydata/profil' );
    }
    else
      header( 'Location: /panel-kandydata/edycja-profilu' );
    exit;
  }

  static public function profile_edit()
  {
    if ( !\front\factory\Globelus::permissions_check( $g_user = \S::get_session( 'g_user' ), __CLASS__ . "\\" . __FUNCTION__ ) )
      return \Tpl::view( 'globelus/candidates/alerts/profile-edit' );

    return \Tpl::view( 'globelus/candidates/profile-edit', [
      'g_user' => $g_user,
      'categories' => \front\factory\GlobelusAdverts::categories(),
      'positions' => \front\factory\Globelus::get_positions_list()
    ] );
  }

  public static function profile_preview()
  {
    if ( !\front\factory\Globelus::permissions_check( $g_user = \S::get_session( 'g_user' ), __CLASS__ . "\\" . __FUNCTION__ ) )
      return \Tpl::view( 'globelus/candidates/alerts/profile-edit' );

    return \front\view\GlobelusCandidates::profile_preview( $g_user );
  }
}
