<?php if (!defined('HTTP_SERVER')) die('You can not access this file directly!');
/**
  @licence GPL 2005-2011  The osCSS developers - osCSS Open Source E-commerce
  @portion code Copyright (c) 2002 osCommerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.1.0
  @date  22/03/11, 16:14
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8
  @class mail
*/

class mail
  implements ExtModPage,  InterfaceModule {

  const FILENAME =FILENAME_MAIL;

  public static $code;

  public static $action;

  public static $list=array();

  /**
    class constructor
  */
  public function __construct() {
    self::$code=__CLASS__;
  }


  public function check_action($actions){
    global $messageStack, $oscss, $language;

    self::$action=$actions;

    if (!_cst_bool('SEND_EMAILS')) $messageStack->add(ERR_TXT_EMAIL_OFF, 'warning');
    if (!_cst_bool('EMAIL_USE_HTML'))  $messageStack->add(ERR_TXT_EMAIL_TXT_STRICT, 'warning');
    if ( (self::$action == 'preview') && !isset($_POST['customers_email_address']) ) $messageStack->add_session(ERROR_NO_CUSTOMER_SELECTED, 'error');


    switch (self::$action) {
      case 'send_email_to_user':

	$err=array();

	switch ($_POST['customers_email_address']) {
	  case '***':
	    $mail_query = tep_db_query("select * from " . TABLE_CUSTOMERS);
	    $mail_sent_to = TEXT_ALL_CUSTOMERS;
	    break;
	  case '**D':
	    $mail_query = tep_db_query("select * from " . TABLE_CUSTOMERS . " where customers_newsletter = '1'");
	    $mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
	    break;
	  default:
	    $customers_email_address = tep_db_prepare_input($_POST['customers_email_address']);

	    $mail_query = tep_db_query("select *  from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($customers_email_address) . "'");
	    $mail_sent_to = $_POST['customers_email_address'];
	    break;
	}

	$from = tep_db_prepare_input($_POST['from']);
	$subject = tep_db_prepare_input($_POST['subject']);
	$message = tep_db_prepare_input($_POST['message']);



	/* Convertion */
// 	$html_reg['license']=HTML_REG_LICENSE;
// 	$html_reg['desinscription']=HTML_REG_DESINSCRIPTION;
// 	$html_reg['signature']=HTML_REG_SIGN;
// 	$text_reg['license']=TEXT_REG_LICENSE;
// 	$text_reg['desinscription']=TEXT_REG_DESINSCRIPTION;
// 	$text_reg['signature']=TEXT_REG_SIGN;


	while ($mail = tep_db_fetch_array($mail_query)) {

	    $mymessage = array(
		      'to_name'=>$mail['customers_firstname'].' '.$mail['customers_lastname'],
		      'to_email_address'=>$mail['customers_email_address'],
		      'email_subject'=>$subject,
		      'email'=>$message,
		      'from_email_name'=>$from,
		      'from_email_address'=>$mail_sent_to
		      );

	    $html_reg['numero_client']=$mail['customers_id'];
	    $text_reg['numero_client']=$mail['customers_id'];
	    $html_reg['genre']=$mail['customers_gender'];
	    $text_reg['genre']=$mail['customers_gender'];
	    $html_reg['firstname']=$mail['customers_firstname'];
	    $text_reg['firstname']=$mail['customers_firstname'];
	    $html_reg['lastname']=$mail['customers_lastname'];
	    $text_reg['lastname']=$mail['customers_lastname'];
	    $html_reg['email_address']=$mail['customers_email_address'];
	    $text_reg['email_address']=$mail['customers_email_address'];



	      $content=($_POST['p_format'] == 'HTML') ? tep_replace_for_mail($message,$subject,$html_reg,$text_reg,'HTML') : tep_replace_for_mail(strip_tags($message),$subject,$html_reg,$text_reg,'TXT');
	      $myarray = array(
			'email_use_html'=>(($_POST['p_format'] == 'HTML')?true:false),
			'mail_sent_to'=>$mail_sent_to,
			'from'=>$from,
			'subject'=>$subject,
			'message'=>$content
			);

	      $message_final=emailUtility::post_prepare_email("mail.tpl",$language,$myarray);



	      if ( tep_mail($mymessage['to_name'], $mymessage['to_email_address'], $mymessage['email_subject'], $message_final, $mymessage['from_email_name'], @$mymessage['from_email_address']) !=false) $err[]=true;
	      else $err[$mymessage['to_email_address']]=false;

	}


	if(!in_array(false,$err) ) {
	  $messageStack->add_session(sprintf(__('notice email sent to %s'), $mail_sent_to), 'success');
	  tep_redirect(tep_href_link(FILENAME_MAIL));

	}else {
	  $messageStack->add_session(sprintf(__('warning email not sent to %s'), $mail_sent_to), 'error');
	  tep_redirect(tep_href_link(FILENAME_MAIL, 'action=edit'));
	}

        break;
    }



    $oscss->_add_ext(BO_WISIWYG_SELECTED,'editeur/');
    echo $oscss->_call(BO_WISIWYG_SELECTED,'_top','message_form','600','400');


    return self::$action;
  }

  public function get_header(){ return false; }

  public static function load_db_values($ID){ return false; }

  public function display_view(){
    return tep_get_include_contents(self::$code.'.page');
  }

/** Specific module */

   public static function get_list_customers(){

    if(isset($_GET['customer']))  return tep_draw_input_field('customers_email_address','customers_email_address',$_GET['customer']);
    if(isset($_POST['customers_email_address']))  return tep_draw_input_field('customers_email_address','customers_email_address',$_POST['customers_email_address']).' '.self::sendto();
    else {
      $DB=Database::getInstance();
      $customers = array();
      $customers[] = array('id' => '', 'text' => __('text select customer'));
      $customers[] = array('id' => '***', 'text' => __('text all customers'));
      $customers[] = array('id' => '**D', 'text' => __('text newsletter customers'));
      $mail_query = $DB->query("select customers_email_address, customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " order by customers_lastname");
      while($customers_values = $mail_query->fetchAssoc() ) {
	$customers[] = array('id' => $customers_values['customers_email_address'],
			    'text' => $customers_values['customers_lastname'] . ', ' . $customers_values['customers_firstname'] . ' (' . $customers_values['customers_email_address'] . ')');
      }

      return tep_draw_pull_down_menu('customers_email_address', $customers, (isset($_GET['customer']) ? $_GET['customer'] : ''));
    }
  }

  public static function sendto(){
    switch ($_POST['customers_email_address']) {
      case '***':
        return __('text all customers');
        break;
      case '**D':
        return __('text newsletter customers');
        break;
      default:
        return $_POST['customers_email_address'];
    }
  }


  /**
    Re-Post all POST'ed variables
  */
  public static function repost(){
    $str='';
    reset($_POST);
    while (list($key, $value) = each($_POST))
      if (!is_array($_POST[$key])){
	if($key =='message_form') $str .= tep_draw_hidden_field('message', '', htmlspecialchars(stripslashes($value)));
	else $str .= tep_draw_hidden_field($key, '', htmlspecialchars(stripslashes($value)));
      }
    return $str;
  }

/** Interface module */

  function check() { return false; }
  function install() { return false; }
  function remove() { return false;  }
  function keys() { return false; }
}

?>