<?php
namespace admin\factory;
class GlobelusFirms
{
  public static function order_fvat_switch( $order_id )
  {
    global $mdb;

    if ( $mdb -> get( 'globelus_orders', 'fvat', [ 'id' => $order_id ] ) )
      $mdb -> update( 'globelus_orders', [ 'fvat' => 0 ], [ 'id' => $order_id ] );
    else
      $mdb -> update( 'globelus_orders', [ 'fvat' => 1 ], [ 'id' => $order_id ] );
  }

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

    $mdb -> update( 'globelus_users', [ 'status' => 1 ], [ 'id' => $user_id ] );
    $email = $mdb -> get( 'globelus_users', 'email', [ 'id' => $user_id ] );

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

    // $send = \S::send_email( $email, 'Potwierdzenie aktywacji konta w portalu  GLOBELUS.PL', $text );
    return true;
  }

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

    $g_user       = $mdb -> get( 'globelus_users', '*', [ 'id' => $user_id ] );
    $g_user_data  = $mdb -> get( 'globelus_firms_data', '*', [ 'user_id' => $user_id ] );

    $g_user_data['country'] = $mdb -> get( 'globelus_countries', 'name', [ 'id' => $g_user_data['country_id'] ] );
    if ( is_array( $g_user_data ) )
    {
      unset( $g_user_data['id'] );
      $g_user       = array_merge( $g_user, $g_user_data );
    }

    $g_user['categories'] = $mdb -> select( 'globelus_firms_categories', 'category_id', [ 'user_id' => $user_id ] );
    $g_user['candidates'] = $mdb -> select( 'globelus_firms_candidates', 'candidate_id', [ 'user_id' => $user_id ] );

    return $g_user;
  }

  public static function firm_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;
  }
}
