<?php
namespace admin\factory;
class GlobelusCandidates
{
  public static function switch_account_type( $user_id ) {
    \R::exec( 'DELETE FROM globelus_candidates_adverts WHERE user_id = ?', [ (int)$user_id ] );
    \R::exec( 'DELETE FROM globelus_candidates_categories WHERE user_id = ?', [ (int)$user_id ] );
    \R::exec( 'DELETE FROM globelus_candidates_countries WHERE user_id = ?', [ (int)$user_id ] );
    \R::exec( 'DELETE FROM globelus_candidates_data WHERE user_id = ?', [ (int)$user_id ] );
    \R::exec( 'DELETE FROM globelus_candidates_languages WHERE user_id = ?', [ (int)$user_id ] );
    \R::exec( 'DELETE FROM globelus_candidates_positions WHERE user_id = ?', [ (int)$user_id ] );
    \R::exec( 'DELETE FROM globelus_candidates_visits WHERE candidate_id = ?', [ (int)$user_id ] );
    \R::exec( 'DELETE FROM globelus_firms_candidates WHERE candidate_id = ?', [ (int)$user_id ] );

    return \R::exec( 'UPDATE globelus_users SET type = 1 WHERE id = ?', [ (int)$user_id ] );
  }

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

    $mdb -> update( 'globelus_users', [ 'highlight' => 1, 'highlight_to' => date( 'Y-m-d', strtotime( '+14 days', time() ) ) ], [ 'id' => $user_id ] );

    $email   = \front\factory\GlobelusUser::get_email( $user_id );

    $text = $settings['newsletter_header'];
    $text .= \front\factory\Newsletter::get_template( '#profil-zostal-wyrozniony' );
    $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 );

    $send = \S::send_email( $email, 'Twój profil w serwisie GLOBELUS.PL  został właśnie wyróżniony!', $text );

    return true;
  }

  public static function candidate_details( $user_id )
  {
    global $mdb;

    $g_user = $mdb -> get( 'globelus_users', '*', [ 'id' => $user_id ] );
    $g_user_data  = $mdb -> get( 'globelus_candidates_data', '*', [ 'user_id' => $user_id ] );
    if ( is_array( $g_user_data ) )
    {
      $g_user['data_id'] = $g_user_data['id'];
      unset( $g_user_data['id'] );
      $g_user       = array_merge( $g_user, $g_user_data );
    }

    $g_user['countries']  = $mdb -> select( 'globelus_candidates_countries', [ '[><]globelus_countries' => [ 'country_id' => 'id' ] ], 'country_id', [ 'user_id' => $user_id ] );
    $g_user['voivodeships']  = $mdb -> select( 'globelus_candidates_voivodeships', [ '[><]globelus_voivodeships' => [ 'id_voivodeship' => 'id' ] ], 'id_voivodeship', [ 'id_user' => $user_id ] );
    $g_user['categories'] = $mdb -> select( 'globelus_candidates_categories', [ '[><]globelus_adverts_categories' => [ 'category_id' => 'id' ] ], 'category_id', [ 'user_id' => $user_id ] );
    $g_user['positions']  = $mdb -> select( 'globelus_candidates_positions', [ 'id', 'position', 'experience', 'id_position' ], [ 'user_id' => $user_id ] );
    $g_user['languages']  = $mdb -> select( 'globelus_candidates_languages', [ 'id', 'language', 'experience' ], [ 'user_id' => $user_id ] );
    $g_user['adverts']    = $mdb -> select( 'globelus_candidates_adverts', 'advert_id', [ 'user_id' => $user_id ] );
    $g_user['cv_hash']    = \front\factory\GlobelusCandidates::cv_hash( $user_id );

    return $g_user;
  }

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

    $email   = \front\factory\GlobelusUser::get_email( $user_id );

    $answers_cv = $mdb -> select( 'globelus_adverts_answers', 'cv', [ 'AND' => [ 'user_id' => $user_id, 'cv[!]' => null ] ] );
    if ( is_array( $answers_cv ) and count( $answers_cv ) ) foreach ( $answers_cv as $cv )
    {
      if ( '../' . file_exists( $cv ) )
        unlink( '../' . $cv );
    }

    $mdb -> delete( 'globelus_adverts', [ 'user_id' => $user_id ] );
    $mdb -> delete( 'globelus_adverts_answers', [ 'user_id' => $user_id ] );

    $cv_file = \front\factory\GlobelusCandidates::cv_url( $user_id );
    if ( file_exists( '../' . $cv_file ) )
        unlink( '../' . $cv_file );

    $avatar_img = \front\factory\GlobelusCandidates::avatar_url( $user_id );
    if ( file_exists( '../' . $avatar_img ) )
        unlink( '../' . $avatar_img );

    $logo_img = \front\factory\GlobelusFirms::logo_url( $user_id );
    if ( file_exists( '../' . $avatar_img ) )
        unlink( '../' . $avatar_img );

    $mdb -> delete( 'globelus_users', [ 'id' => $user_id ] );

    $text = $settings['newsletter_header'];
    $text .= \front\factory\Newsletter::get_template( '#globelus-usnieto-konto' );
    $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 );

    $send = \S::send_email( $email, 'Twoje konto w serwisie GLOBELUS.PL zostało usunięte!', $text );

    return true;
  }
}
