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

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

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

    function choose_audience() {
      global $HTTP_GET_VARS, $languages_id;

      $products_array = array();
      $products_query = tep_db_query("select pd.products_id, pd.products_name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where pd.language_id = '" . $languages_id . "' and pd.products_id = p.products_id and p.products_status = '1' order by pd.products_name");
      while ($products = tep_db_fetch_array($products_query)) {
        $products_array[] = array('id' => $products['products_id'],
                                  'text' => $products['products_name']);
      }

	$choose_audience_string = '<script language="javascript"><!--
	function mover(move) {
	  if (move == \'remove\') {
	    for (x=0; x<(document.notifications.products.length); x++) {
	      if (document.notifications.products.options[x].selected) {
	        with(document.notifications.elements[\'chosen[]\']) {
	          options[options.length] = new Option(document.notifications.products.options[x].text,document.notifications.products.options[x].value);
	        }
	        document.notifications.products.options[x] = null;
	        x = -1;
	      }
	    }
	  }
	  if (move == \'add\') {
	    for (x=0; x<(document.notifications.elements[\'chosen[]\'].length); x++) {
	      if (document.notifications.elements[\'chosen[]\'].options[x].selected) {
	        with(document.notifications.products) {
	          options[options.length] = new Option(document.notifications.elements[\'chosen[]\'].options[x].text,document.notifications.elements[\'chosen[]\'].options[x].value);
	        }
	        document.notifications.elements[\'chosen[]\'].options[x] = null;
	        x = -1;
	      }
	    }
	  }
	  return true;
	}

	function selectAll(FormName, SelectBox) {
	  temp = "document." + FormName + ".elements[\'" + SelectBox + "\']";
	  Source = eval(temp);

	  for (x=0; x<(Source.length); x++) {
	    Source.options[x].selected = "true";
	  }

	  if (x<1) {
	    alert(\'' . JS_PLEASE_SELECT_PRODUCTS . '\');
	    return false;
	  } else {
	    return true;
	  }
	}
	//--></script>';

      $global_button = '<script language="javascript"><!--' . "\n" .
                       'document.write(\'<input type="button" value="' . BUTTON_GLOBAL . '"  style="font-size:10px; font-face:Tahoma; color:#ffffff; background-color:#000000;" onclick="document.location=\\\'' . tep_href_link(FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID'] . '&action=confirm&global=true') . '\\\'">\');' . "\n" .
                       '//--></script><noscript><a href="' . tep_href_link(FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID'] . '&action=confirm&global=true') . '">[ ' . BUTTON_GLOBAL . ' ]</a></noscript>';

      $cancel_button = '<script language="javascript"><!--' . "\n" .
                       'document.write(\'<input type="button" value="' . BUTTON_CANCEL . '"  style="font-size:10px; font-face:Tahoma; color:#ffffff; background-color:#000000;" onclick="document.location=\\\'' . tep_href_link(FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID']) . '\\\'">\');' . "\n" .
                       '//--></script><noscript><a href="' . tep_href_link(FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID']) . '">[ ' . BUTTON_CANCEL . ' ]</a></noscript>';

      $choose_audience_string .= '<form name="notifications" action="' . tep_href_link(FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID'] . '&action=confirm') . '" method="post" onSubmit="return selectAll(\'notifications\', \'chosen[]\')"><table border="0" width="100%" cellspacing="0" cellpadding="2">' . "\n" .
                                 '  <tr>' . "\n" .
                                 '    <td align="center" class="main"><b>' . TEXT_PRODUCTS . '</b><br>' . tep_draw_pull_down_menu('products', $products_array, '', 'size="20" style="width: 20em;" multiple') . '</td>' . "\n" .
                                 '    <td align="center" class="main">&nbsp;<br>' . $global_button . '<br><br><br><input type="button" value="' . BUTTON_SELECT . '"  style="font-size:10px; font-face:Tahoma; color:#ffffff; background-color:#000000;" onClick="mover(\'remove\');"><br><br><input type="button" value="' . BUTTON_UNSELECT . '"  style="font-size:10px; font-face:Tahoma; color:#ffffff; background-color:#000000;" onClick="mover(\'add\');"><br><br><br><input type="submit" value="' . BUTTON_SUBMIT . '"  style="font-size:10px; font-face:Tahoma; color:#ffffff; background-color:#000000;"><br><br>' . $cancel_button . '</td>' . "\n" .
                                 '    <td align="center" class="main"><b>' . TEXT_SELECTED_PRODUCTS . '</b><br>' . tep_draw_pull_down_menu('chosen[]', array(), '', 'size="20" style="width: 20em;" multiple') . '</td>' . "\n" .
                                 '  </tr>' . "\n" .
                                 '</table></form>';

      return $choose_audience_string;
    }

    function confirm() {
      global $HTTP_GET_VARS, $HTTP_POST_VARS;

      $audience = array();

      if (isset($HTTP_GET_VARS['global']) && ($HTTP_GET_VARS['global'] == 'true')) {
        $products_query = tep_db_query("select distinct customers_id from " . TABLE_PRODUCTS_NOTIFICATIONS);
        while ($products = tep_db_fetch_array($products_query)) {
          $audience[$products['customers_id']] = '1';
        }

        $customers_query = tep_db_query("select customers_info_id from " . TABLE_CUSTOMERS_INFO . " where global_product_notifications = '1'");
        while ($customers = tep_db_fetch_array($customers_query)) {
          $audience[$customers['customers_info_id']] = '1';
        }
      } else {
        $chosen = $HTTP_POST_VARS['chosen'];

        $ids = implode(',', $chosen);

        $products_query = tep_db_query("select distinct customers_id from " . TABLE_PRODUCTS_NOTIFICATIONS . " where products_id in (" . $ids . ")");
        while ($products = tep_db_fetch_array($products_query)) {
          $audience[$products['customers_id']] = '1';
        }

        $customers_query = tep_db_query("select customers_info_id from " . TABLE_CUSTOMERS_INFO . " where global_product_notifications = '1'");
        while ($customers = tep_db_fetch_array($customers_query)) {
          $audience[$customers['customers_info_id']] = '1';
        }
      }

      $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>' . tep_draw_form('confirm', FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID'] . '&action=confirm_send') .
										'<td align="left" style="padding-top:15px">';
									      if (sizeof($audience) > 0) {
									        if (isset($HTTP_GET_VARS['global']) && ($HTTP_GET_VARS['global'] == 'true')) {
									          $confirm_string .= tep_draw_hidden_field('global', 'true');
									        } else {
									          for ($i = 0, $n = sizeof($chosen); $i < $n; $i++) {
									            $confirm_string .= tep_draw_hidden_field('chosen[]', $chosen[$i]);
									          }
									        }
									        $confirm_string .= tep_image_submit('button_send.gif', IMAGE_SEND) . ' ';
									      } else {
										    $confirm_string .= '<span class="main" style="color:#ff0000;font-size:18px">Brak subskrybentów dla wybranych produktów, nie można wysłać biuletynu !!!</span><br><br>';
										  }
									      $confirm_string .= '<a href="' . tep_href_link(FILENAME_NEWSLETTERS, 'page=' . $HTTP_GET_VARS['page'] . '&nID=' . $HTTP_GET_VARS['nID'] . '&action=send') . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</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 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']."'");
	   //
	   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';		 
	  }
    }
  }
?>
