<?php
namespace front\factory;
class GlobelusCron
{
  static public function fill_proposed_candidates()
  {
    global $mdb;

    // usuń starych proponowanych kandydatów
    $mdb -> delete( 'globelus_proposed_candidates', [ 'date_add[<=]' => date( 'Y-m-d', strtotime( '-60 days', time() ) ) ] );

    // pobierz wszystkie aktywne ogłoszenia z przypisanymi kandydatami i przypisanem krajem
    $adverts = $mdb -> query( 'SELECT ga.id, id_position, country_id, title FROM globelus_adverts AS ga WHERE visible = 1 AND id_position IS NOT NULL AND country_id IS NOT NULL AND ( ga.proposed_candidates_date IS NULL OR ga.proposed_candidates_date <= \'' . date( 'Y-m-d H:i:s', strtotime( '-1 days', time() ) ) . '\' ) ORDER BY proposed_candidates_date ASC, id ASC LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );

    if ( is_array( $adverts ) ) foreach ( $adverts as $advert )
    {
      $candidates_ids = $mdb -> select( 'globelus_candidates_positions', 'user_id', [ 'id_position' => $advert['id_position'] ] );

      $candidates_ids = $mdb -> query( 'SELECT gu.id FROM globelus_users AS gu WHERE type = 0 AND status = 1 AND visible = 1 AND active_to >= \'' . date( "Y-m-d" ) . '\' AND gu.id IN ( ' . implode( ',', array_merge( [0], $candidates_ids ) ) . ')' ) -> fetchAll( \PDO::FETCH_ASSOC );
      if ( is_array( $candidates_ids ) ) foreach ( $candidates_ids as $candidate )
      {
        if ( !$mdb -> count( 'globelus_proposed_candidates', [ 'AND' => [ 'advert_id' => $advert['id'], 'candidate_id' => $candidate['id'] ] ] ) )
        {
          if ( $mdb -> count( 'globelus_candidates_countries', [ 'AND' => [ 'user_id' => $candidate['id'], 'country_id' => $advert['country_id'] ] ] ) )
            $mdb -> insert( 'globelus_proposed_candidates', [ 'advert_id' => $advert['id'], 'candidate_id' => $candidate['id'], 'date_add' => date( 'Y-m-d H:i:s' ) ] );
        }
      }

      $mdb -> update( 'globelus_adverts', [ 'proposed_candidates_date' => date( 'Y-m-d H:i:s' ) ], [ 'id' => $advert['id'] ] );

      echo( '<p>Wyszukuję proponowanych kandydatów dla ogłoszenia: <b>' . $advert['title'] . '</b> #' . $advert['id'] . '</p>' );
    }
    exit;
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'gu.id '
                                . 'FROM '
                                  . 'globelus_users AS gu '
                                . 'WHERE '
                                    . 'gu.cv_access_date < \'' . date( 'Y-m-d' ) . '\' '
                                . 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_users', [
                'cv_access' => 0,
                'cv_access_date' => null
              ], [
                'id' => $row['id']
              ] );
      $mdb -> delete( 'globelus_firms_categories', [ 'user_id' => $row['id'] ] );
      echo( '<p>Wyłączam dostęp do CV.</p>' );
    }
  }

  public static function nopl()
  {
    global $mdb;

    $results = $mdb -> query( 'SELECT id, title FROM globelus_adverts WHERE title_nopl IS NULL' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_adverts', [ 'title_nopl' => \S::seo( $row['title'], true ) ], [ 'id' => $row['id'] ] );
      echo( '<p>Generuje wartość nopl - ogłoszenia.</p>' );
    }

    $results = $mdb -> query( 'SELECT id, firm_name_profile FROM globelus_firms_data WHERE firm_name_profile_nopl IS NULL' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_firms_data', [ 'firm_name_profile_nopl' => \S::seo( $row['firm_name_profile'], true ) ], [ 'id' => $row['id'] ] );
      echo( '<p>Generuje wartość nopl - firmy.</p>' );
    }

    $results = $mdb -> query( 'SELECT id, text FROM globelus_adverts WHERE text_nopl IS NULL' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_adverts', [ 'text_nopl' => \S::seo( $row['text'], true ) ], [ 'id' => $row['id'] ] );
      echo( '<p>Generuje wartość nopl - ogłoszenia.</p>' );
    }

    $results = $mdb -> query( 'SELECT id, name FROM globelus_countries WHERE name_nopl IS NULL' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_countries', [ 'name_nopl' => \S::seo( $row['name'], true ) ], [ 'id' => $row['id'] ] );
      echo( '<p>Generuje wartość nopl - kraje.</p>' );
    }

    $results = $mdb -> query( 'SELECT id, city FROM globelus_adverts WHERE city_nopl IS NULL' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_adverts', [ 'city_nopl' => \S::seo( $row['city'], true ) ], [ 'id' => $row['id'] ] );
      echo( '<p>Generuje wartość nopl - miasta.</p>' );
    }

    $results = $mdb -> query( 'SELECT id, name FROM globelus_adverts_categories WHERE name_seo IS NULL' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_adverts_categories', [ 'name_seo' => \S::seo( $row['name'], true ) ], [ 'id' => $row['id'] ] );
      echo( '<p>Generuje wartość nopl - kategorie.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'ga.id, gu.email, ga.title, ga.date_add '
                                . 'FROM '
                                  . 'globelus_adverts AS ga '
                                  . 'INNER JOIN globelus_users AS gu ON gu.id = ga.user_id '
                                . 'WHERE '
                                    . 'ga.active_to < \'' . date( "Y-m-d" ) . '\' '
                                  . 'AND '
                                    . '( old = 0 OR ga.visible = 1 )' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_adverts', [
                'visible' => 0,
                'active_to' => null,
                'highlight' => 0,
                'highlight_to' => null,
                'main_page' => 0,
                'main_page_to' => null,
                'old' => 1,
                'last_refresh' => null
              ], [
                'id' => $row['id']
              ] );
      echo( '<p>Wyłączam stare ogłoszenia.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'ga.id, gu.email, ga.title '
                                . 'FROM '
                                  . 'globelus_adverts AS ga '
                                  . 'INNER JOIN globelus_users AS gu ON gu.id = ga.user_id '
                                . 'WHERE '
                                    . 'ga.main_page_to < \'' . date( 'Y-m-d' ) . '\' '
                                . 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $days = \S::date_diff( date( 'Y-m-d' ), $row['active_to'], 60 * 60 * 24 );

      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#koniec-polecania-ogloszenia' );

      $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( '[X]', $days, $text );
      $text   = str_replace( '[TYTUL]', $row['title'], $text );

      //$send = \S::send_email( $row['email'], str_replace( '[TYTUL]', $row['title'], $lang['mail-ogloszenie-koniec-polecania'] ), $text );

      $mdb -> update( 'globelus_adverts', [ 'main_page' => 0, 'main_page_to' => null ], [ 'id' => $row['id'] ] );

      echo( '<p>Wysyłam powiadomienie o końcu polecania ogłoszenia.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'ga.id, gu.email, ga.title '
                                . 'FROM '
                                  . 'globelus_adverts AS ga '
                                  . 'INNER JOIN globelus_users AS gu ON gu.id = ga.user_id '
                                . 'WHERE '
                                    . 'ga.highlight_to < \'' . date( 'Y-m-d' ) . '\' '
                                . 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $days = \S::date_diff( date( 'Y-m-d' ), $row['active_to'], 60 * 60 * 24 );

      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#koniec-wyroznienia-ogloszenia' );

      $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( '[X]', $days, $text );
      $text   = str_replace( '[TYTUL]', $row['title'], $text );

      //$send = \S::send_email( $row['email'], str_replace( '[TYTUL]', $row['title'], $lang['mail-ogloszenie-koniec-wyroznienia'] ), $text );

      $mdb -> update( 'globelus_adverts', [ 'highlight' => 0, 'highlight_to' => null ], [ 'id' => $row['id'] ] );

      echo( '<p>Wysyłam powiadomienie o końcu wyróżnienia ogłoszenia.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'gu.id, gu.email '
                                . 'FROM '
                                  . 'globelus_users AS gu '
                                . 'WHERE '
                                    . 'gu.highlight_to < \'' . date( 'Y-m-d' ) . '\' '
                                . 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $mdb -> update( 'globelus_users', [ 'highlight' => 0, 'highlight_to' => null ], [ 'id' => $row['id'] ] );

      echo( '<p>Wyłączam wyróżnione profile.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'ga.id, ga.active_to, gu.email, ga.title '
                                . 'FROM '
                                  . 'globelus_adverts AS ga '
                                  . 'INNER JOIN globelus_users AS gu ON gu.id = ga.user_id '
                                . 'WHERE '
                                    . 'ga.visible = 1 '
                                  . 'AND '
                                    . 'ga.active_to IS NOT NULL '
                                  . 'AND '
                                    . 'ga.active_to < \'' . date( 'Y-m-d' ) . '\' '
                                  . 'AND '
                                    . 'ga.mail_expired = 0 '
                                . 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $days = \S::date_diff( date( 'Y-m-d' ), $row['active_to'], 60 * 60 * 24 );

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

      $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( '[X]', $days, $text );
      $text   = str_replace( '[TYTUL]', $row['title'], $text );

//      if ( $globelus_settings['maile-wysylanie'] )
//        $send = \S::send_email( $row['email'], str_replace( '[TYTUL]', $row['title'], $lang['mail-tytul-ogloszenie-wygaslo'] ), $text );

      $mdb -> update( 'globelus_adverts', [ 'visible' => 0, 'mail_expired' => 1, 'highlight' => 0, 'highlight_to' => null, 'main_page' => 0, 'main_page_to' => null ], [ 'id' => $row['id'] ] );

      echo( '<p>Wysyłam powiadomienie o wygaśnięciu ogłoszenia.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'gu.id, gu.email '
                                . 'FROM '
                                  . 'globelus_users AS gu '
                                . 'WHERE '
                                    . 'gu.type = 0 '
                                  . 'AND '
                                    . 'gu.mail_delete_date < \'' . date( 'Y-m-d', strtotime( '-14 days', time() ) ) . '\' '
                                  . 'AND '
                                    . 'gu.mail_delete = 1 '
                                . 'LIMIT 1' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#kandydat-usuniecie-konta-kandydata' );

      $text .= $settings['newsletter_footer_1'];

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

      $text = str_replace( '[LINK]', $base . "://" . $_SERVER['SERVER_NAME'] . "/login-as/" . $row['hash'] . "?return_url=" . urlencode( '/panel-pracodawcy/ogloszenia' ), $text );

      $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( '[TYTUL]', $row['title'], $text );

      if ( $globelus_settings['maile-wysylanie'] )
        $send = \S::send_email( $row['email'], $lang['mail-tytul-konto-kandydata-usuniete'] . $row['title'], $text );

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

      echo( '<p>Usuwanie nieaktywnych kont.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'ga.id, ga.active_to, gu.email, ga.title, gu.hash '
                                . 'FROM '
                                  . 'globelus_adverts AS ga '
                                  . 'INNER JOIN globelus_users AS gu ON gu.id = ga.user_id '
                                . 'WHERE '
                                    . 'ga.visible = 1 '
                                  . 'AND '
                                    . 'ga.active_to IS NOT NULL '
                                  . 'AND '
                                    . 'ga.active_to <= \'' . date( 'Y-m-d', strtotime( '+3 days', time() ) ) . '\' '
                                  . 'AND '
                                    . 'ga.mail_expiration = 0 '
                                . 'LIMIT 1' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $days = \S::date_diff( date( 'Y-m-d' ), $row['active_to'], 60 * 60 * 24 );

      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#przedluz-swoje-ogloszenie' );

      $text .= $settings['newsletter_footer_1'];

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

      $text = str_replace( '[LINK]', $base . "://" . $_SERVER['SERVER_NAME'] . "/login-as/" . $row['hash'] . "?return_url=" . urlencode( '/panel-pracodawcy/ogloszenia' ), $text );

      $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( '[X]', $days, $text );
      $text = str_replace( '[TYTUL]', $row['title'], $text );

      if ( $globelus_settings['maile-wysylanie'] )
        $send = \S::send_email( $row['email'], $lang['mail-tytul-ogloszenie-wygasa'] . $row['title'] . '!', $text );

      $mdb -> update( 'globelus_adverts', [ 'mail_expiration' => 1 ], [ 'id' => $row['id'] ] );

      echo( '<p>Wysyłam powiadomienie o wygasaniu ogłoszenia.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'gu.id, gu.email, active_to '
                                . 'FROM '
                                  . 'globelus_users AS gu '
                                . 'WHERE '
                                    . 'type = 0 '
                                  . 'AND '
                                    . 'DATE_ADD( active_to, INTERVAL 180 day ) < \'' . date( 'Y-m-d' ) . '\' '
                                  . 'AND '
                                    . 'mail_delete = 0 '
                                . 'LIMIT 1' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $days = \S::date_diff( date( 'Y-m-d' ), $row['active_to'], 60 * 60 * 24 );

      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#kandydat-powiadomienie-o-usunieciu-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( '[X]', $days, $text );

      if ( $globelus_settings['maile-wysylanie'] )
      {
        $send = \S::send_email( $row['email'], $lang['mail-tytul-profil-usuniety'], $text );
      }

      $mdb -> update( 'globelus_users', [ 'mail_delete' => 1, 'mail_delete_date' => date( 'Y-m-d H:i:s' ) ], [ 'id' => $row['id'] ] );

      echo( '<p>Wysyłam powiadomienie o usuniętym profilu.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'gu.id, gu.email, active_to '
                                . 'FROM '
                                  . 'globelus_users AS gu '
                                . 'WHERE '
                                    . 'type = 0 '
                                  . 'AND '
                                    . 'active_to < \'' . date( 'Y-m-d' ) . '\' '
                                  . 'AND '
                                    . 'mail_expired = 0 '
                                . 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $days = \S::date_diff( date( 'Y-m-d' ), $row['active_to'], 60 * 60 * 24 );

      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#kandydat-powiadomienie-o-wygasnieciu-profilu' );

      $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( '[X]', $days, $text );

      if ( $globelus_settings['maile-wysylanie'] )
        $send = \S::send_email( $row['email'], $lang['mail-tytul-profil-wygasl'], $text );

      $mdb -> update( 'globelus_users', [ 'mail_expired' => 1 ], [ 'id' => $row['id'] ] );

      echo( '<p>Wysyłam powiadomienie o wygaśniętym profilu.</p>' );
    }
  }

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

    $results = $mdb -> query( 'SELECT '
                                  . 'gu.id, gu.email, active_to '
                                . 'FROM '
                                  . 'globelus_users AS gu '
                                . 'WHERE '
                                    . 'type = 0 '
                                  . 'AND '
                                    . 'active_to <= \'' . date( 'Y-m-d', strtotime( '+7 days', time() ) ) . '\' '
                                  . 'AND '
                                    . 'mail_expiration = 0 '
                                . 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $days = \S::date_diff( date( 'Y-m-d' ), $row['active_to'], 60 * 60 * 24 );

      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#kandydat-powiadomienie-o-wygasaniu-profilu' );

      $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( '[X]', $days, $text );

      if ( $globelus_settings['maile-wysylanie'] )
        $send = \S::send_email( $row['email'], $lang['mail-tytul-profil-niedlugo-wygasnie'], $text );

      $mdb -> update( 'globelus_users', [ 'mail_expiration' => 1 ], [ 'id' => $row['id'] ] );

      echo( '<p>Wysyłam powiadomienie o wygasaniu profilu.</p>' );
    }
  }
}
