<?php
namespace admin\factory;
class GlobelusAdverts
{
  public static function disabled_by_admin( $advert_id, $disabled_by_admin ) {
    global $mdb;
    return $mdb -> update( 'globelus_adverts', [ 'disabled_by_admin' => $disabled_by_admin ], [ 'id' => $advert_id ] );
  }
  
  public static function update_active_to() {
    global $mdb;
    $results = $mdb -> select( 'globelus_adverts', [ 'id', 'date_add' ], [ 'active_to' => null ] );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
      $mdb -> update( 'globelus_adverts', [ 'active_to' => date( 'Y-m-d', strtotime( '+6 months', strtotime( $row['date_add'] ) ) ) ], [ 'id' => $row['id'] ] );
  }
  
  public static function firms_list()
  {
    global $mdb;
    $results = $mdb -> query( 'SELECT '
                                    . 'gu.id, gfd.firm_name '
                                  . 'FROM '
                                    . 'globelus_users AS gu '
                                    . 'INNER JOIN globelus_firms_data AS gfd ON gfd.user_id = gu.id '
                                  . 'WHERE '
                                    . 'type = 1' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and count( $results ) ) foreach ( $results as $row )
      $firms[ $row['id'] ] = $row['firm_name'];
    return $firms;
  }
  
  public static function answers_list( $advert_id )
  {
    global $mdb;
    return $mdb -> query( 'SELECT '
                              . 'id, name, surname, email, phone, text, cv, cv_extension, displayed, date_add, user_id '
                            . 'FROM '
                              . 'globelus_adverts_answers AS gaa '
                            . 'WHERE '
                              . 'advert_id = ' . (int)$advert_id . ' AND deleted = 0 '
                            . 'ORDER BY '
                              . 'date_add DESC, id DESC' ) -> fetchAll();
  }
  
  public static function advert_details( $advert_id )
  {
    global $mdb;
    return $mdb -> query( 'SELECT '
                              . 'ga.id, title, ga.user_id, category_id, ga.country_id, ga.city, ga.region, text, contact_person, email, ga.phone, firm_name, highlight, highlight_to, main_page, main_page_to, '
                              . 'gac.name AS category_name, gc.name AS country_name, gfd.firm_name_profile, date_add, ga.clauses, salary, work_type, without_language, without_experience, for_couples, from_now,'
                              . 'disabled_by_admin, accommodation, accommodation_cost, overtime, overtime_quantity, travel_refund, outside_ue, id_voivodeship '
                            . 'FROM '
                              . 'globelus_adverts AS ga '
                              . 'LEFT JOIN globelus_adverts_categories AS gac ON gac.id = ga.category_id '
                              . 'LEFT JOIN globelus_countries AS gc ON gc.id = ga.country_id '
                              . 'INNER JOIN globelus_firms_data AS gfd ON gfd.user_id = ga.user_id '
                            . 'WHERE '
                              . 'ga.id = ' . (int)$advert_id ) -> fetch( \PDO::FETCH_ASSOC );
  }
  
  public static function advert_delete( $advert_id )
  {
    global $mdb, $settings;
    
    $answers_cv = $mdb -> select( 'globelus_adverts_answers', 'cv', [ 'AND' => [ 'advert_id' => $advert_id, 'cv[!]' => null ] ] );
    if ( is_array( $answers_cv ) and count( $answers_cv ) ) foreach ( $answers_cv as $cv )
    {
      if ( strpos( $cv, 'files/cv_tmp/' ) and file_exists( '../' . $cv ) )
        unlink( '../' . $cv );
    }
    
    return $mdb -> delete( 'globelus_adverts', [ 'id' => $advert_id ] );
  }
}
