<?php
namespace front\factory;

class Newsletter
{
  public static function newsletter_unsubscribe( $hash )
  {
    global $mdb;
    return $mdb -> update( 'pp_newsletter', [ 'status' => 0 ], [ 'hash' => $hash ] );
  }
  
  public static function newsletter_confirm( $hash )
  {
    global $mdb;
    if ( !$id = $mdb -> get( 'pp_newsletter', 'id', [ 'AND' => [ 'hash' => $hash, 'status' => 0 ] ] ) )
      return false;
    else
      $mdb -> update( 'pp_newsletter', [ 'status' => 1 ], [ 'id' => $id ] );
    return true;
  }
  
  public static function newsletter_send( $limit = 5 )
  {
    global $mdb, $settings, $lang;
    
    $results = $mdb -> query( 'SELECT * FROM pp_newsletter_send WHERE mailed = 0 ORDER BY id ASC LIMIT ' . $limit ) -> fetchAll();
    if ( is_array( $results ) and !empty( $results ) ) 
    {
      foreach ( $results as $row )
      {
        $dates = explode( ' - ', $row['dates'] );
     
        $text = \admin\view\Newsletter::preview(
                  \admin\factory\Articles::articles_by_date_add( $dates[0], $dates[1] ),
                  \admin\factory\Settings::settings_details(),
                  \admin\factory\Newsletter::email_template_detalis($row['id_template'])
                );
        
        if ( $settings['ssl'] ) $base = 'https'; else $base = 'http';     
        
        $link = $base . "://" . $_SERVER['SERVER_NAME'] . '/newsletter/unsubscribe/hash=' . \front\factory\Newsletter::get_hash( $row['email'] );
        $text = str_replace( '[WYPISZ_SIE]', $link, $text );
        
        $regex  = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|http(|s)://).)*)(['\"][^>]*>)-i";
        $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );
        
        $regex  = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|http(|s)://).)*)(['\"][^>]*>)-i";
        $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );
      
        \S::send_email( $row['email'], 'Newsletter ze strony: ' . $_SERVER['SERVER_NAME'], $text );
        
        if ( $row['only_once'] )
          $mdb -> update( 'pp_newsletter_send', [ 'mailed' => 1 ], [ 'id' => $row['id'] ] );
        else
          $mdb -> delete( 'pp_newsletter_send', [ 'id' => $row['id'] ] );
      }
      return true;
    }
    return false;
  }
  
  public static function get_hash( $email )
  {
    global $mdb;
    return $mdb -> get( 'pp_newsletter', 'hash', [ 'email' => $email ] );
  }
  
  public static function newsletter_signin( $email )
  {
    global $mdb, $lang, $settings;
    
    if ( !\S::email_check( $email ) )
      return false;
    
    if ( !$mdb -> get( 'pp_newsletter', 'id', [ 'email' => $email ] ) )
    {
      $hash = md5( time() . $email );
      
      $text = $settings['newsletter_header'];
      $text .= \front\factory\Newsletter::get_template( '#potwierdzenie-zapisu-do-newslettera' );
      $text .= $settings['newsletter_footer_1'];
      
      $settings['ssl'] ? $base = 'https' : $base = 'http';
      
      $regex  = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );

      $regex  = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i";
      $text   = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );
      
      $link = '/newsletter/confirm/hash=' . $hash;
      
      $text = str_replace( '[LINK]', $link, $text );
      
      $send = \S::send_email( $email, $lang['potwierdz-zapisanie-sie-do-newslettera'], $text );  
      
      $mdb -> insert( 'pp_newsletter', [ 'email' => $email, 'hash' => $hash, 'status' => 0 ] );
      
      return true;
    }
    return false;
  }
  
  public static function get_template( $template_name )
  {
    global $mdb;
    return $mdb -> get( 'pp_newsletter_templates', 'text', [ 'name' => $template_name ] );
  }
  
  public static function newsletter_signout( $email )
  {
    global $mdb;
    
    if ( $mdb -> get( 'pp_newsletter', 'id', [ 'email' => $email ] ) )
      return $mdb -> delete( 'pp_newsletter', [ 'email' => $email ] );
    return false;
  }
}