<?php
namespace admin\factory;

class Newsletter
{
  public static function emails_import( $emails )
  {
    global $mdb;
    
    $emails = explode( PHP_EOL, $emails );
    if ( is_array( $emails ) ) foreach ( $emails as $email )
    {
      if ( trim( $email ) and !$mdb -> count( 'pp_newsletter', [ 'email' => trim( $email ) ] ) )
        $mdb -> insert( 'pp_newsletter', [ 
                  'email' => trim( $email ),
                  'hash' => md5( $email . time() ),
                  'status' => 1
                ] );
    }
    return true;
  }
  
  public static function is_admin_template( $template_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_newsletter_templates', 'is_admin', [ 'id' => (int)$template_id ] );
  }
  
  public static function newsletter_template_delete( $template_id )
  {
    global $mdb;
    return $mdb -> delete( 'pp_newsletter_templates', [ 'id' => (int)$template_id ] );
  }
  
  public static function send( $dates, $template, $only_once )
  {
    global $mdb;
    
    $results = $mdb -> select( 'pp_newsletter', 'email', [ 'status' => 1 ] );
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      if ( $template and $only_once )
      {
        if ( !$mdb -> count( 'pp_newsletter_send', [ 'AND' => [ 'id_template' => $template, 'email' => $row ] ] ) )
          $mdb -> insert( 'pp_newsletter_send', [
                    'email'       => $row,
                    'dates'       => $dates,
                    'id_template' => $template ? $template : null,
                    'only_once'   => ( $only_once == 'on' and $template ) ? 1 : 0
                  ] );
      }
      else
        $mdb -> insert( 'pp_newsletter_send', [
                  'email'       => $row,
                  'dates'       => $dates,
                  'id_template' => $template ? $template : null,
                  'only_once'   => ( $only_once == 'on' and $template ) ? 1 : 0
                ] );
    }
    return true;    
  }
  
  public static function email_template_detalis ($id_template)
  {
    global $mdb;
    
    $result = $mdb -> get ('pp_newsletter_templates', '*', [ 'id' => (int)$id_template ]  );
    return $result;
  }
  
  public static function template_save($id, $name, $text)
  {
    global $mdb;
      if ( !$id )
        {
        if ( $mdb -> insert( 'pp_newsletter_templates', [
                'name' => $name,
                'text' => $text
        ] ) )
        {
          \S::delete_cache();
          return $mdb -> id();
        }
      }
      else
      {   
        $mdb -> update( 'pp_newsletter_templates', [
                  'name' => $name,
                  'text' => $text

          ], [
                  'id' => (int)$id
          ] );

        \S::delete_cache();
        return $id;
      }
    }
  
  public static function templates_list()
  {
    global $mdb;
    return $mdb -> select( 'pp_newsletter_templates', '*', [ 'is_admin' => 0, 'ORDER' => [ 'name' => 'ASC' ] ] );
  }
}
