<?php
namespace front\controls;
class GlobelusFirms
{
  // usuwanie logo
  static public function delete_logo()
  {
    global $mdb;

    $logo_hash = \front\factory\GlobelusFirms::logo_hash( \S::get( 'user_id' ) );
    $file_name = 'files/logos/' . $logo_hash{0} . '/' . $logo_hash{1} . '/' . $logo_hash;

    if ( file_exists( $file_name ) )
    {
      unlink($file_name);
      $mdb -> update ('globelus_firms_data', ['logo_extension' => NULL]);
    }
    echo json_encode( [ 'result' => 'ok' ] );
    exit;
  }
  //usuwanie zdjęcia górnego
  static public function delete_img_header()
  {
    global $mdb;

    $img_header_hash = \front\factory\GlobelusFirms::img_header_hash( \S::get( 'user_id' ) );
    $file_name = 'files/img_header/' . $img_header_hash{0} . '/' . $img_header_hash{1} . '/' . $img_header_hash;

    if ( file_exists( $file_name ) )
    {
      unlink($file_name);
      $mdb -> update ('globelus_firms_data', ['img_header_extension' => NULL]);
    }
    echo json_encode( [ 'result' => 'ok' ] );
    exit;
  }

  //usuwanie zdjęcia dolnego
  static public function delete_img_bottom()
  {
    global $mdb;

    $img_bottom_hash = \front\factory\GlobelusFirms::img_bottom_hash( \S::get( 'user_id' ) );
    $file_name = 'files/img_bottom/' . $img_bottom_hash{0} . '/' . $img_bottom_hash{1} . '/' . $img_bottom_hash;

    if ( file_exists( $file_name ) )
    {
      unlink($file_name);
      $mdb -> update ('globelus_firms_data', ['img_bottom_extension' => NULL]);
    }
    echo json_encode( [ 'result' => 'ok' ] );
    exit;
  }

    //usuwanie zdjęcia górnego edycja
    static public function delete_advert_img_top()
    {
      global $mdb;

      $advert_add_img_top_hash = \front\factory\GlobelusFirms::advert_add_img_top_hash( \S::get( 'id' ) );
      $file_name = 'files/advert_add_img_top/' . $advert_add_img_top_hash{0} . '/' . $advert_add_img_top_hash{1} . '/' . $advert_add_img_top_hash;

      if ( file_exists( $file_name ) )
      {
        unlink($file_name);
        $mdb -> update ('globelus_adverts', ['advert_add_img_top' => NULL]);
      }
      echo json_encode( [ 'result' => 'ok' ] );
      exit;
    }

       //usuwanie zdjęcia dolnego edycja
       static public function delete_advert_img_bottom()
       {
         global $mdb;

         $advert_add_img_bottom_hash = \front\factory\GlobelusFirms::advert_add_img_bottom_hash( \S::get( 'id' ) );
         $file_name = 'files/advert_add_img_bottom/' . $advert_add_img_bottom_hash{0} . '/' . $advert_add_img_bottom_hash{1} . '/' . $advert_add_img_bottom_hash;

         if ( file_exists( $file_name ) )
         {
           unlink($file_name);
           $mdb -> update ('globelus_adverts', ['advert_add_img_bottom' => NULL]);
         }
         echo json_encode( [ 'result' => 'ok' ] );
         exit;
       }


  public static function set_answer_as_displayed()
  {
    global $mdb;
    $mdb -> update( 'globelus_adverts_answers', [ 'displayed' => 1 ], [ 'id' => \S::get( 'answer_id' ) ] );
    exit;
  }

  public static function payment_error()
  {
    global $page, $globelus_settings;

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

    $page['language']['meta_title'] = 'Zamówienie nieopłacone ● Globelus';

    return \Tpl::view( 'globelus/firms/payment-error', [
              'user' => $g_user
            ] );
  }

  public static function payment_success()
  {
    global $page, $globelus_settings;

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

    $page['language']['meta_title'] = 'Zamówienie opłacone ● Globelus';

    return \Tpl::view( 'globelus/firms/payment-success', [
              'user' => $g_user
            ] );
  }

  public static function set_order_status()
  {
    global $mdb, $settings;

    if ( \S::get( 'tr_status' ) == 'TRUE' and \S::get( 'tr_crc' ) )
    {
      $order = $mdb -> get( 'globelus_orders', [ 'user_id', 'points', 'price' ], [ 'hash' => \S::get( 'tr_crc' ) ] );
      $mdb -> update( 'globelus_orders', [ 'status' => 1 ], [ 'hash' => \S::get( 'tr_crc' ) ] );
      $mdb -> update( 'globelus_users', [ 'points[+]' => $order['points'] ], [ 'id' => $order['user_id'] ] );

      $user_tmp = \front\factory\GlobelusUser::user_details( $order['user_id'] );

      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#doladowanie-konta' );
      $text .= $settings['newsletter_footer_1'];

      $settings['ssl'] ? $base = 'https' : $base = 'http';

      $regex  = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $regex  = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $text = str_replace( '[KWOTA]', $order['price'], $text );
      $text = str_replace( '[SALDO]', $user_tmp['points'], $text );

      if ( $globelus_settings['maile-wysylanie'] )
        $send = \S::send_email( $user_tmp['email'], 'Doładowanie salda - GLOBELUS.PL', $text );

      echo 'true';
      exit;
    }

    echo 'false';
    exit;
  }

  public static function create_order()
  {
    global $page, $globelus_settings;

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

    $page['language']['meta_title'] = 'Zamówienie ● Globelus';

    $package = \S::get( 'package' );
    if ( $package > 5)
      $package = 5;

    $hash = \front\factory\GlobelusFirms::generate_order(
                                            $g_user['id'],
                                            $package,
                                            $globelus_settings['pkt-pakiet-' . $package . '-ilosc'],
                                            str_replace( ',', '.', $globelus_settings['pkt-pakiet-' . $package . '-cena'] )
                                          );

    return \Tpl::view( 'globelus/firms/create-order', [
              'user' => $g_user,
              'gsettings' => $globelus_settings,
              'package' => $package,
              'hash' => $hash
            ] );
  }

  static public function multi_delete()
  {
    $g_user = \S::get_session( 'g_user' );

    if ( is_array( \S::get( 'ids' ) ) ) foreach ( \S::get( 'ids' ) as $id )
      \front\factory\GlobelusFirms::advert_delete( $g_user['id'], $id );

    exit;
  }

  static public function multi_refresh() {
    $g_user = \S::get_session( 'g_user' );

    if ( is_array( \S::get( 'ids' ) ) ) foreach ( \S::get( 'ids' ) as $id )
      \front\factory\GlobelusFirms::advert_refresh( $id, $g_user['id'] );

    exit;
  }

  public static function multi_disable()
  {
    $g_user = \S::get_session( 'g_user' );

    if ( is_array( \S::get( 'ids' ) ) ) foreach ( \S::get( 'ids' ) as $id )
      \front\factory\GlobelusFirms::advert_disable( $g_user['id'], $id );

    exit;
  }

  public static function alist_set_cp() {
    \S::set_session( 'advert_list_cp', \S::get( 'cp' ) );
    exit;
  }

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

    \front\factory\GlobelusFirms::answer_delete(
      (int)$g_user['id'], (int)\S::get( 'advert_id' ), (int)\S::get( 'answer_id' )
    );

    if ( \S::get( 'advert_id' ) )
      header( 'Location: /panel-pracodawcy/odpowiedzi/'. \S::get( 'advert_id' ) );
    else
      header( 'Location: /panel-pracodawcy/odpowiedzi-na-ogloszenia' );
    exit;
  }

  public static function firm_profile()
  {
    global $page;

    $firm_profile = \front\factory\GlobelusFirms::firm_profile( \S::get( 'user_id' ) );
    $page['language']['meta_title'] = 'Praca w ' . $firm_profile['firm_name_profile'] . ' - oferta pracy | Globelus';

    return \Tpl::view( 'globelus/firms/firm-profile', [
              'firm' => $firm_profile,
              'adverts' => \front\factory\GlobelusFirms::firm_adverts( \S::get( 'user_id' ) ),
              'g_user' => \S::get_session( 'g_user' )
          ] );
  }

  public static function firms_list()
  {
    return \front\view\GlobelusFirms::firms_list(
              \front\factory\GlobelusFirms::firms_list( (int)\S::get( 'cp' ), \S::get( 'letter' ), \front\factory\GlobelusFirms::firm_list_limit ),
              \front\factory\GlobelusFirms::firms_count( \S::get( 'letter' ) ),
              \S::get( 'letter' ),
              \S::get( 'cp' )
            );
  }

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

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

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

  public static function advert_add_img_top()
  {
    return \front\factory\GlobelusFirms::advert_add_img_top( \S::get( 'advert_id' ), \S::get( 'token' ) );
  }

  public static function advert_add_img_bottom()
  {
    return \front\factory\GlobelusFirms::advert_add_img_bottom( \S::get( 'advert_id' ), \S::get( 'token' ) );
  }

  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-pracodawcy/ustawienia-konta' );
    exit;
  }

  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\GlobelusFirms::datą_save(
            $g_user['id'], \S::get( 'firm_name' ), \S::get( 'firm_name_profile' ), \S::get( 'nip_vat' ), \S::get( 'phone' ), \S::get( 'www' ), \S::get( 'country_id' ), \S::get( 'region' ), \S::get( 'city' ),
            \S::get( 'postal_code' ), \S::get( 'street' ), \S::get( 'apartment_number' ), \S::get( 'other_information' ), $_FILES['logo'], \S::get( 'contact_person_1' ), \S::get( 'email_1' ),
            \S::get( 'phone_1' ), \S::get( 'contact_person_2' ), \S::get( 'email_2' ), \S::get( 'phone_2' ), \S::get( 'clauses' ), $_FILES['img-header'], $_FILES['img-bottom']
          ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang['dane-zostaly-zaktualizowane'] );
      \S::set_session( 'g_user', \front\factory\GlobelusUser::user_details( $g_user['id'] ) );
      header( 'Location: /panel-pracodawcy/profil' );
    }
    else
      header( 'Location: /panel-pracodawcy/edycja-profilu' );
    exit;
  }

  public static function candidate_remove_from_favorite()
  {
    global $lang;

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

    if ( \front\factory\GlobelusFirms::candidate_remove_from_favorite( \S::get( 'candidate_id' ), $g_user['id'] ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang['kandydat-zostal-usuniety-obserwowanych'] );
      \S::set_session( 'g_user', \front\factory\GlobelusUser::user_details( $g_user['id'] ) );
    }
    header( 'Location: ' . \S::get( 'redirect_url' ) );
    exit;
  }

  public static function candidate_add_to_favorite()
  {
    global $lang;

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

    if ( \front\factory\GlobelusFirms::candidate_add_to_favorite( \S::get( 'candidate_id' ), $g_user['id'] ) )
    {
      \S::set_alert_prompt( 'Informacja', $lang['kandydat-zostal-dodany-do-obserwowanych'] );
      \S::set_session( 'g_user', \front\factory\GlobelusUser::user_details( $g_user['id'] ) );
    }
    header( 'Location: ' . \S::get( 'redirect_url' ) );
    exit;
  }

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

    return \Tpl::view( 'globelus/firms/followed-candidates', [
              'user' => $g_user,
              'candidates' => \front\factory\GlobelusFirms::followed_candidates( $g_user['id'] )
            ] );
  }

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

    $url_components = parse_url( $_SERVER['REQUEST_URI'] );
    parse_str( $url_components['query'], $params );

    if ( \front\factory\GlobelusFirms::is_user_advert( $g_user['id'], $params['aid'] ) )
      $advert_id = $params['aid'];

    $adverts = \front\factory\GlobelusFirms::active_adverts_list( $g_user['id'] );

    return \Tpl::view( 'globelus/firms/proposed-candidates', [
      'user' => $g_user,
      'positions_experience' => \front\factory\Globelus::positions_experience(),
      'candidates' => array_unique( \front\factory\GlobelusFirms::proposed_candidates( $advert_id, $adverts ) ),
      'adverts' => $adverts,
      'current_advert' => $advert_id
    ] );
  }

  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\GlobelusFirms::profile_settings( $g_user );
  }

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

    return \front\view\GlobelusFirms::profile_edit( $g_user );
  }

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

    return \Tpl::view( 'globelus/firms/profile-preview', [
              'g_user' => $g_user
          ] );
  }

  public static function advert_refresh()
  {
    global $lang;

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

    \front\factory\GlobelusFirms::advert_refresh( \S::get( 'advert_id'), $g_user['id'] );

    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

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

    \front\factory\GlobelusFirms::advert_additional_promotion( $g_user['id'], \S::get( 'advert-id' ) );

    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

  public static function advert_extend()
  {
    global $lang;

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

    if ( \front\factory\GlobelusFirms::advert_extend( \S::get( 'advert_id'), $g_user['id'] ) )
      \S::set_alert_prompt( 'Informacja', $lang[ 'ogloszenie-zostalo-przedluzone' ] );

    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

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

    \front\factory\GlobelusFirms::advert_main_page_enable( $g_user['id'], \S::get( 'advert-id' ), $g_user['email'] );

    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

  public static function advert_highlight_enable()
  {
    global $lang;

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

    \front\factory\GlobelusFirms::advert_highlight_enable( $g_user['id'], \S::get( 'advert-id' ), $g_user['email'] );

    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

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

    $result = \front\factory\GlobelusFirms::advert_disable( $g_user['id'], \S::get( 'advert-id' ) );

    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

  public static function advert_enable()
  {
    global $lang;

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

    $result = \front\factory\GlobelusFirms::advert_enable( $g_user['id'], \S::get( 'advert-id' ), $g_user['email'] );
    // \S::set_alert_prompt( 'Informacja', $result['msg'] );

    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

  public static function advert_delete()
  {
    global $lang;

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

    $result = \front\factory\GlobelusFirms::advert_delete( $g_user['id'], \S::get( 'advert-id' ) );

    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

  // dodanie nowego ogłoszenia
  static public function advert_save()
  {
    global $lang, $mdb;

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

    $old = $mdb -> get( 'globelus_adverts', 'old', [ 'AND' => [ 'id' => \S::get( 'id' ), 'user_id' => $g_user['id'] ] ] );

    if ( $advert_id = \front\factory\GlobelusFirms::advert_save( [
                        'user_id' => $g_user['id'],
                        'advert_id' => \S::get( 'id' ),
                        'title' => \S::get( 'title' ),
                        'category' => \S::get( 'category' ),
                        'country' => \S::get( 'country' ),
                        'city' => \S::get( 'city' ),
                        'region' => \S::get( 'region' ),
                        'text' => \S::get( 'text' ),
                        'contact_person' => \S::get( 'contact_person' ),
                        'email' => \S::get( 'email' ),
                        'phone' => \S::get( 'phone' ),
                        'salary' => \S::get( 'salary' ),
                        'work_type' => \S::get( 'work_type' ),
                        'without_language' => \S::get( 'without_language' ),
                        'without_experience' => \S::get( 'without_experience' ),
                        'for_couples' => \S::get( 'for_couples' ),
                        'from_now' => 0,
                        'accommodation' => \S::get( 'accommodation' ),
                        'accommodation_cost' => \S::get( 'accommodation_cost' ),
                        'overtime' => \S::get( 'overtime' ),
                        'overtime_quantity' => \S::get( 'overtime_quantity' ),
                        'travel_refund' => \S::get( 'travel_refund' ),
                        'outside_ue' => \S::get( 'outside_ue' ),
                        'without_driving_license' => \S::get( 'without_driving_license' ),
                      ],
                      \S::get( 'id_voivodeship' ), \S::get( 'aplication_link' ), $_FILES['advert-img-bottom'], $_FILES[ 'advert-img-top'], \S::get( 'position' ) ) )
    {
      if ( !\S::get( 'id' ) or $old )
        \S::set_alert_prompt( 'Ogłoszenie zostało dodane', 'Zanim będzie się ono wyświetlać kandydatom należy je jeszcze opublikować.' );
      else
      {
        if ( !\front\factory\GlobelusFirms::is_advert_visible( $advert_id ) )
          \S::set_alert_prompt( 'Ogłoszenie zostało zmodyfikowane', 'Zanim będzie się ono wyświetlać kandydatom należy je jeszcze opublikować.' );
        else
          \S::set_alert_prompt( 'Ogłoszenie zostało zmodyfikowane', '', 5 );
      }
    }
    header( 'Location: /panel-pracodawcy/ogloszenia' );
    exit;
  }

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

    return \Tpl::view( 'globelus/firms/answers', [
              'answers' => \front\factory\GlobelusFirms::answers( (int)$g_user['id'] ),
              'g_user' => $g_user
            ] );
  }

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

    $advert = \front\factory\GlobelusAdverts::advert_details( \S::get( 'advert_id' ) );

    if ( $advert['user_id'] != $g_user['id'] )
      return false;

    return \front\view\GlobelusFirms::advert_answers(
              $g_user,
              $advert,
              \front\factory\GlobelusFirms::advert_answers( \S::get( 'advert_id' ) )
          );
  }

  public static function advert_edit()
  {
    global $globelus_settings;

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

    $advert = \front\factory\GlobelusAdverts::advert_details( \S::get( 'advert_id' ) );

    if ( $advert['user_id'] != $g_user['id'] )
      return false;

    return \Tpl::view( 'globelus/firms/advert-add', [
            'gsettings' => $globelus_settings,
            'user' => $g_user,
            'adverts_count' => \front\factory\GlobelusFirms::adverts_count( $g_user['id'] ),
            'user_points' => \front\factory\GlobelusFirms::get_firm_points( $g_user['id'] ),
            'advert' => $advert,
            'voivodeships' => \front\factory\Globelus::get_voivodeships_list(),
            'positions' => \front\factory\Globelus::get_positions_list()
          ] );
  }

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

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

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

    return \Tpl::view( 'globelus/firms/advert-add', [
            'gsettings' => $globelus_settings,
            'user' => $g_user,
            'adverts_count' => \front\factory\GlobelusFirms::adverts_count( $g_user['id'] ),
            'user_points' => \front\factory\GlobelusFirms::get_firm_points( $g_user['id'] ),
            'voivodeships' => \front\factory\Globelus::get_voivodeships_list(),
            'positions' => \front\factory\Globelus::get_positions_list()
          ] );
  }

  public static function adverts_list()
  {
    $g_user = \S::get_session( 'g_user' );

    if ( !$g_user['id'] )
      return \Tpl::view( 'globelus/firms/alerts/advert-edit' );

    if ( \S::get( 'alert_promotion' ) == 'true' )
    {
      \S::set_alert_prompt( 'Dodatkowa promocja', 'Wybierz z listy ogłoszeń ofertę pracy, którą chcesz, abyśmy dla Ciebie wypromowali oraz kliknij przycisk "dodatkowa promocja" znajdujący się przy ogłoszeniu.' );
      header( 'Location: /panel-pracodawcy/ogloszenia' );
      exit;
    }

    return \Tpl::view( 'globelus/firms/adverts-list', [
            'user' => $g_user,
            'adverts' => \front\factory\GlobelusFirms::adverts_list( $g_user['id'] ),
            'highlight_left' => \front\factory\GlobelusFirms::highlight_left( $g_user['id'] ),
            'main_page_left' => \front\factory\GlobelusFirms::main_page_left( $g_user['id'] ),
            'adverts_count' => \front\factory\GlobelusFirms::adverts_count( $g_user['id'] ),
            'firm_points' => \front\factory\GlobelusFirms::get_firm_points( $g_user['id'] ),
            'user_adverts_count' => \front\factory\GlobelusFirms::user_adverts_count( $g_user['id'] )
          ] );
  }

  public static function buy_points()
  {
    global $page, $globelus_settings;

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

    $page['language']['meta_title'] = 'Zakup punktów ● Globelus';

    return \Tpl::view( 'globelus/firms/buy-points', [
              'user' => $g_user,
              'points' => \front\factory\GlobelusFirms::get_firm_points( $g_user['id'] ),
              'gsettings' => $globelus_settings
            ] );
  }
}
