<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/

  class newsletter {
    var $show_choose_audience, $newsletters_id, $module_subscribers, $title, $header, $content;

    function newsletter($newsletters_id, $module_subscribers, $title, $header, $content) {
      $this->show_choose_audience = false;
      $this->newsletters_id = $newsletters_id;
      $this->module = $module_subscribers;
      $this->title = $title;
	  $this->header = $header;
      $this->content = $content;    					
    }

    function choose_audience() {
      return false;
    }

    function confirm() {
      global $HTTP_GET_VARS;

      $mail_query = tep_db_query("select count(*) as count from " . TABLE_CUSTOMERS . " where customers_newsletter = '1'");
      $mail = tep_db_fetch_array($mail_query);

      $confirm_string = '<table border="0" cellspacing="0" cellpadding="2" width="100%">
						 <tr>
							<td align="center">
								<table border="0" width="95%" cellspacing="0" cellpadding="5">
								  <tr>
							        <td class="main" style="color:#ff0000;font-size:14px"><b>'.TEXT_TITRE_INFO.'</b></font></td>
							      </tr>
							      <tr>
							        <td class="main">'.TEXT_BULLETIN_NUMB . '&nbsp;<font color="#0000ff">' . $this->newsletters_id .'</font></td>
							      </tr>
							      <tr>
							        <td class="main">'.TEXT_MODULE . '&nbsp;<font color="#0000ff">' . zamien_news($this->module) . '</font></td>
							      </tr>
							      <tr>
							        <td class="main">'.TEXT_TITRE_MAIL . '&nbsp;<font color="#0000ff">' . $this->title . '</font></td>
							      </tr>
							      <tr>
							        <td class="main" style="color:#ff0000;font-size:14px"><b>'. TEXT_TITRE_VIEW .'</b></font></td>
							       </tr>											
							       <tr>
							         <td class="main" style="border:1px solid #474747;padding:10px;background:#fff">'. $this->content.'</td>
							       </tr>
								   <tr>
		                             <td align="left" style="padding-top:15px"><a href="' . tep_href_link(FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID'] . '&action=confirm_send') . '">' . tep_image_button('button_send.gif', IMAGE_SEND) . '</a> <a href="' . tep_href_link(FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID']) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a></td>
		                           </tr>								   
								 </table>
							</td>
						 </tr>
                        </table>';

      return $confirm_string;
    }

    function send($newsletter_id) {
	  global $HTTP_GET_VARS;
      $mail_query = tep_db_query("select subs_id, firstname, lastname, email from " . TABLE_NEWSLETTERS_TMP . " limit ".MAX_LIMIT_MAIL);
	  //
	  $email = str_replace('src="/','src="'.HTTP_SERVER.DIR_WS_CATALOG,$this->content);
	  if (EMAIL_USE_HTML == 'true') {
		  $ArrayLNTargets = array("\r\n", "\n\r", "\n", "\r", "\t"); 
	      $email = str_replace($ArrayLNTargets, '', $email);
      }		  
	  //
      $nadawca = MakeUTF(STORE_NAME);
	  if (isset($_GET['licz'])) { $licznik = $_GET['licz']; } else { $licznik = 0; }
      while ($mail = tep_db_fetch_array($mail_query)) {
	   $adresat = MakeUTF($mail['firstname'] . ' ' . $mail['lastname'] );
	   tep_db_query("delete from " . TABLE_NEWSLETTERS_TMP . " where firstname = '".$mail['firstname']."' and lastname = '".$mail['lastname']."' and email = '".$mail['email']."' and subs_id = '".$mail['subs_id']."'");
	   //
	   tep_mail($adresat, $mail['email'], MakeUTF($this->title), $email, $nadawca, STORE_OWNER_EMAIL_ADDRESS);
	   $licznik++;
      }
	  $tmp_query = tep_db_query("select firstname, lastname, email from " . TABLE_NEWSLETTERS_TMP);
	  if ((int)tep_db_num_rows($tmp_query) > 0) {
		 echo "<script type=\"text/javascript\" language=\"javascript\">document.location='newsletters.php?".(isset($_GET['page']) ? 'page=' . $_GET['page'] . '&' : '') . 'nID=' . $newsletter_id."&action=confirm_send&ciag=tak&licz=".$licznik."';</script>";
		 } else {
		 echo 'Wysłano: '.$licznik.' wiadomości';		 
	  }	
	}
  }
?>
