<?php
namespace admin\controls;

class Newsletter
{
  public static function emails_import()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );
    
    $values   = \S::json_to_array( \S::get( 'values' ) );

    if ( \admin\factory\Newsletter::emails_import( $values['emails'] ) )
      \S::alert( 'Emaile zostały zaimportowane.' );
    
    echo json_encode( [ 'status' => 'ok', 'msg' => 'Emaile zostały zaimportowane.' ] );
    exit;
  }
  
  public static function import()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );
    
    return \Tpl::view( 'newsletter/import' );
  }
  
  public static function emails_list()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
             return \S::alert('Nie masz uprawnień');
    
    return \admin\view\Newsletter::emails_list();
   }
  
  public static function send()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );
    
    if ( \admin\factory\Newsletter::send( \S::get( 'dates' ), \S::get( 'template' ), \S::get( 'only_once' ) ) )
      \S::alert( 'Newsletter został dodany do kolejki wysyłania.' );
    
    header( 'Location: /admin/newsletter/prepare/' );
    exit;
  }
  
  public static function prepare()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
      return \S::alert('Nie masz uprawnień');
    
    return \admin\view\Newsletter::prepare(
              \admin\factory\Newsletter::templates_list()
            );
  }
  
  public static function settings_save()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );
    
    $values   = \S::json_to_array( \S::get( 'values' ) );
    
    \admin\factory\Settings::settings_update( 'newsletter_footer_1', $values['newsletter_footer_1'] );
    \admin\factory\Settings::settings_update( 'newsletter_footer_2', $values['newsletter_footer_2'] );
    \admin\factory\Settings::settings_update( 'newsletter_header', $values['newsletter_header'] );
    
    \S::alert( 'Ustawienia zostały zapisane.' );

    echo json_encode( [ 'status' => 'ok', 'msg' => 'Ustawienia zostały zapisane.' ] );
    exit;
  }
  
  public static function settings()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id']))
             return \S::alert('Nie masz uprawnień');
    
    return \admin\view\Newsletter::settings(
              \admin\factory\Settings::settings_details()
            );
  }
  
  public static function email_templates_user()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
      return \S::alert('Nie masz uprawnień');
    
    return \admin\view\Newsletter::email_templates_user();
  }
  
  public static function email_templates_admin()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
      return \S::alert('Nie masz uprawnień');
    
    return \admin\view\Newsletter::email_templates_admin();
  }
  
  public static function email_template_delete()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );
    
    $is_admin = \admin\factory\Newsletter::is_admin_template( \S::get( 'id' ) );
    
    if ( !$is_admin and \admin\factory\Newsletter::newsletter_template_delete( \S::get( 'id' ) ) )
      \S::alert( 'Szablon newslettera został usunięty.' );
    
    if ( $is_admin )
      header( 'Location: /admin/newsletter/email_templates_admin/' );
    else
      header( 'Location: /admin/newsletter/email_templates_user/' );
    exit;
  }
  
  public static function email_template_edit()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id']))
      return \S::alert('Nie masz uprawnień');
    
    return \admin\view\Newsletter::email_template_edit( 
              \admin\factory\Newsletter::email_template_detalis(
                \S::get( 'id' )
                )
            );
  }
  
  public static function template_save()
  {
    global $user;
    if ( !\admin\factory\Users::check_privileges( 'newsletter_administration', $user['id']))
      return \S::alert('Nie masz uprawnień');
    
    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania wystąpił błąd. Proszę spróbować ponownie.' ];
    $values   = \S::json_to_array( \S::get( 'values' ) );
    
    if ( $id = \admin\factory\Newsletter::template_save( $values['id'], $values['name'], $values['text'] ) )
      $response = [ 'status' => 'ok', 'msg' => 'Zmiany zostały zapisane.', 'id' => $id ];

    echo json_encode( $response );
    exit;
  }
}
