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

    if ( !\front\factory\Globelus::permissions_check( $g_user = \S::get_session( 'g_user' ), __CLASS__ . "\\" . __FUNCTION__ ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    return \Tpl::view( 'globelus/cv/candidate-profile', [
      'g_user' => $g_user,
      'candidate' => \front\factory\GlobelusUser::user_details( \S::get( 'user_id' ) ),
    ] );
  }

  public static function candidate_profile()
  {
    global $lang;

    if ( !\front\factory\Globelus::permissions_check( $g_user = \S::get_session( 'g_user' ), __CLASS__ . "\\" . __FUNCTION__ ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    if ( !is_array( $g_user['categories'] ) or !count( $g_user['categories'] ) )
      return \Tpl::view( 'globelus/cv/alerts/cv-access' );

    $next_profile = \front\factory\GlobelusCv::next_profile(
                        \S::get( 'user_id' ),
                        $g_user['categories'],
                        \S::get_session( 'cv-filtr-keyword' ),
                        \S::get_session( 'cv-filtr-countries' ),
                        \S::get_session( 'cv-filtr-availability' ),
                        \S::get_session( 'cv-filtr-categories' ),
                        \S::get_session( 'cv-filtr-voivodeships' ),
                        \S::get_session( 'cv-id_position' )
                      );

    $prev_profile = \front\factory\GlobelusCv::prev_profile(
                      \S::get( 'user_id' ),
                      $g_user['categories'],
                      \S::get_session( 'cv-filtr-keyword' ),
                      \S::get_session( 'cv-filtr-countries' ),
                      \S::get_session( 'cv-filtr-availability' ),
                      \S::get_session( 'cv-filtr-categories' ),
                      \S::get_session( 'cv-filtr-voivodeships' ),
                      \S::get_session( 'cv-id_position' )
                    );

    return \Tpl::view( 'globelus/cv/candidate-profile', [
      'g_user' => $g_user,
      'candidate' => \front\factory\GlobelusUser::user_details( \S::get( 'user_id' ) ),
      'next_profile' => $next_profile,
      'prev_profile' => $prev_profile
    ] );
  }

  public static function activate_access()
  {
    global $lang, $mdb, $globelus_settings;

    if ( !\front\factory\Globelus::permissions_check( $g_user = \S::get_session( 'g_user' ), __CLASS__ . "\\" . __FUNCTION__ ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    $points = \front\factory\GlobelusFirms::get_firm_points( $g_user['id'] );

    if ( $points < $globelus_settings['pkt-dostep-baza-kandydatow'] )
    {
      //\S::set_alert_prompt( 'Nie posiadasz wystarczającej ilości punktów aby wykupić dostęp do bazy kandydatów.' );
      header( 'Location: /baza-cv' );
      exit;
    }

    $mdb -> update( 'globelus_users', [ 'points[-]' => $globelus_settings['pkt-dostep-baza-kandydatow'], 'cv_access' => 1, 'cv_access_date' => date( 'Y-m-d', strtotime( '+30 days', time() ) ) ], [ 'id' => $g_user['id'] ] );
    $mdb -> insert( 'globelus_points_history', [
              'user_id' => $g_user['id'],
              'points' => $globelus_settings['pkt-dostep-baza-kandydatow'],
              'event_type' => 5
            ] );

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

    $mdb -> delete( 'globelus_firms_categories', [ 'user_id' => $g_user['id'] ] );
    if ( is_array( $categories ) and !empty ( $categories ) ) foreach ( $categories as $category )
    {
      if ( ++$i <= 3 )
        $mdb -> insert( 'globelus_firms_categories', [
                  'user_id' => $g_user['id'],
                  'category_id' => $category
                ] );
    }

    $g_user = \front\factory\GlobelusUser::user_details( $g_user['id'] );
    \S::set_session( 'g_user', $g_user );

    //\S::set_alert_prompt( 'Dostęp do bazy kandydatów został aktywowany.' );
    header( 'Location: /baza-cv' );
    exit;
  }

  public static function cv_list()
  {
    global $lang, $globelus_settings;

    if ( !\front\factory\Globelus::permissions_check( $g_user = \S::get_session( 'g_user' ), __CLASS__ . "\\" . __FUNCTION__ ) )
    {
      header( 'Location: /logowanie' );
      exit;
    }

    if ( \S::get( 'filtr' ) )
    {
      \S::set_session( 'cv-filtr-keyword', \S::get( 'k' ) );
      \S::set_session( 'cv-filtr-countries', \S::get( 'c1' ) );
      \S::set_session( 'cv-filtr-availability', \S::get( 'a' ) );
      \S::set_session( 'cv-filtr-categories', \S::get( 'c2' ) );
      \S::set_session( 'cv-filtr-voivodeships', \S::get( 'v' ) );
      \S::set_session( 'cv-id_position', \S::get( 'id_position' ) );
    }

    $cv_list = \front\factory\GlobelusCv::cv_list(
                  (int)\S::get( 'cp' ),
                  \front\factory\GlobelusCv::cv_list_limit,
                  $g_user['categories'],
                  \S::get_session( 'cv-filtr-keyword' ),
                  \S::get_session( 'cv-filtr-countries' ),
                  \S::get_session( 'cv-filtr-availability' ),
                  \S::get_session( 'cv-filtr-categories' ),
                  \S::get_session( 'cv-filtr-voivodeships' ),
                  \S::get_session( 'cv-id_position' )
                );
    $cv_count = \front\factory\GlobelusCv::cv_count(
                    $g_user['categories'],
                    \S::get_session( 'cv-filtr-keyword' ),
                    \S::get_session( 'cv-filtr-countries' ),
                    \S::get_session( 'cv-filtr-availability' ),
                    \S::get_session( 'cv-filtr-categories' ),
                    \S::get_session( 'cv-filtr-voivodeships' ),
                    \S::get_session( 'cv-id_position' )
                  );

    return \Tpl::view( 'globelus/cv/cv-list', [
        'cv_list' => $cv_list,
        'cv_count' => $cv_count,
        'current_page' => \S::get( 'cp' ),
        'keyword' => \S::get_session( 'cv-filtr-keyword' ),
        'countries' => \front\factory\Globelus::countries(),
        'countries_selected' => \S::get_session( 'cv-filtr-countries' ),
        'categories' => \front\factory\GlobelusAdverts::categories( $g_user['categories'] ),
        'categories_selected' => \S::get_session( 'cv-filtr-categories' ),
        'availability_selected' => \S::get_session( 'cv-filtr-availability' ),
        'g_user' => $g_user,
        'gsettings' => $globelus_settings,
        'voivodeships_selected' =>  \S::get_session( 'cv-filtr-voivodeships' ),
        'voivodeships' => \front\factory\Globelus::get_voivodeships_list(),
        'positions' => \front\factory\Globelus::get_positions_list(),
        'position_selected' => \S::get_session( 'cv-id_position' )
      ] );
  }
}
