<?php
/*
###################################################################################
  Bigware Shop 2.3
  Release Datum: 23.08.2015
  
  Bigware Shop
  http://www.bigware.de

  Copyright (c) 2015 Bigware LTD
  (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
  (c) 2015	Bigware LTD

  Copyrightvermerke duerfen nicht entfernt werden.
  ------------------------------------------------------------------------
  Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
  der GNU General Public License, wie von der Free Software Foundation
  veroeffentlicht, weitergeben und/oder modifizieren, entweder gemaess Version 2 
  der Lizenz oder (nach Ihrer Option) jeder spaeteren Version.
  Die Veroeffentlichung dieses Programms erfolgt in der Hoffnung, dass es Ihnen
  von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die
  implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FUER EINEN
  BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
  
  Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
  Programm erhalten haben. Falls nicht, schreiben Sie an die Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

  Infos:
  ------------------------------------------------------------------------
  Der Bigware Shop wurde vor vielen Jahren bereits aus dem bekannten Shopsystem osCommerce
  weiter- und neuentwickelt.
  Der Bigware Shop legt im hohen Masse Wert auf Bedienerfreundlichkeit, beinhaltet eine leichte
  Installation, viele neue professionelle Werkzeuge und zeichnet sich aus durch eine grosse 
  Community, die bei Problemen weiterhelfen kann.
  
  Der Bigware Shop ist auf jedem System lauffaehig, welches eine PHP Umgebung
  (ab PHP 4.1.3) und mySQL zur Verfuegung stellt und auf Linux basiert.
 
  Hilfe erhalten Sie im Forum auf www.bigware.de 
  
  -----------------------------------------------------------------------
  
 ##################################################################################




*/
?>
<?php
  class item_inform {
    var $show_choose_audience, $title, $content;
    function item_inform($title, $content) {
      $this->show_choose_audience = true;
      $this->title = $title;
      $this->content = $content;
    }
    function choose_audience() {
      global $_GET, $languages_id;
      $items_array = array();
      $items_query = go_db_query("select pd.items_id, pd.items_name from " . DB_TBL_ITEMS . " p, " . DB_TBL_ITEMS_DESCRIPTION . " pd where pd.language_id = '" . $languages_id . "' and pd.items_id = p.items_id and p.items_status = '1' order by pd.items_name");
      while ($items = go_db_fetch_array($items_query)) {
        $items_array[] = array('id' => $items['items_id'],
                                  'text' => $items['items_name']);
      }
$choose_audience_string = '<script language="javascript"><!--
function mover(move) {
  if (move == \'remove\') {
    for (x=0; x<(document.informs.items.length); x++) {
      if (document.informs.items.options[x].selected) {
        with(document.informs.elements[\'chosen[]\']) {
          options[options.length] = new Option(document.informs.items.options[x].text,document.informs.items.options[x].value);
        }
        document.informs.items.options[x] = null;
        x = -1;
      }
    }
  }
  if (move == \'add\') {
    for (x=0; x<(document.informs.elements[\'chosen[]\'].length); x++) {
      if (document.informs.elements[\'chosen[]\'].options[x].selected) {
        with(document.informs.items) {
          options[options.length] = new Option(document.informs.elements[\'chosen[]\'].options[x].text,document.informs.elements[\'chosen[]\'].options[x].value);
        }
        document.informs.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_ITEMS . '\');
    return false;
  } else {
    return true;
  }
}
//--></script>';
      $global_button = '<script language="javascript"><!--' . "\n" .
                       'document.write(\'<input type="button" value="' . BUTTON_GLOBAL . '" style="width: 8em;" onclick="document.location=\\\'' . go_href_link(NAME_OF_FILE_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=confirm&global=true') . '\\\'">\');' . "\n" .
                       '//--></script><noscript><a href="' . go_href_link(NAME_OF_FILE_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=confirm&global=true') . '">[ ' . BUTTON_GLOBAL . ' ]</a></noscript>';
      $cancel_button = '<script language="javascript"><!--' . "\n" .
                       'document.write(\'<input type="button" value="' . BUTTON_CANCEL . '" style="width: 8em;" onclick="document.location=\\\'' . go_href_link(NAME_OF_FILE_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID']) . '\\\'">\');' . "\n" .
                       '//--></script><noscript><a href="' . go_href_link(NAME_OF_FILE_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID']) . '">[ ' . BUTTON_CANCEL . ' ]</a></noscript>';
      $choose_audience_string .= '<form name="informs" action="' . go_href_link(NAME_OF_FILE_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=confirm') . '" method="post" onSubmit="return selectAll(\'informs\', \'chosen[]\')"><table border="0" width="100%" cellspacing="0" cellpadding="2">' . "\n" .
                                 '  <tr>' . "\n" .
                                 '    <td align="center" class="main"><b>' . TEXT_ITEMS . '</b><br>' . go_fetch_pull_down_menu('items', $items_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="width: 8em;" onClick="mover(\'remove\');"><br><br><input type="button" value="' . BUTTON_UNSELECT . '" style="width: 8em;" onClick="mover(\'add\');"><br><br><br><input type="submit" value="' . BUTTON_SUBMIT . '" style="width: 8em;"><br><br>' . $cancel_button . '</td>' . "\n" .
                                 '    <td align="center" class="main"><b>' . TEXT_SELECTED_ITEMS . '</b><br>' . go_fetch_pull_down_menu('chosen[]', array(), '', 'size="20" style="width: 20em;" multiple') . '</td>' . "\n" .
                                 '  </tr>' . "\n" .
                                 '</table></form>';
      return $choose_audience_string;
    }
    function confirm() {
      global $_GET, $_POST;
      $audience = array();
      if (isset($_GET['global']) && ($_GET['global'] == 'true')) {
        $items_query = go_db_query("select distinct attendees_id from " . DB_TBL_ITEMS_INFORMS);
        while ($items = go_db_fetch_array($items_query)) {
          $audience[$items['attendees_id']] = '1';
        }
        $attendees_query = go_db_query("select attendees_info_id from " . DB_TBL_ATTENDEES_INFO . " where global_item_informs = '1'");
        while ($attendees = go_db_fetch_array($attendees_query)) {
          $audience[$attendees['attendees_info_id']] = '1';
        }
      } else {
        $chosen = $_POST['chosen'];
        $ids = implode(',', $chosen);
        $items_query = go_db_query("select distinct attendees_id from " . DB_TBL_ITEMS_INFORMS . " where items_id in (" . $ids . ")");
        while ($items = go_db_fetch_array($items_query)) {
          $audience[$items['attendees_id']] = '1';
        }
        $attendees_query = go_db_query("select attendees_info_id from " . DB_TBL_ATTENDEES_INFO . " where global_item_informs = '1'");
        while ($attendees = go_db_fetch_array($attendees_query)) {
          $audience[$attendees['attendees_info_id']] = '1';
        }
      }
      $confirm_string = '<table border="0" cellspacing="0" cellpadding="2">' . "\n" .
                        '  <tr>' . "\n" .
                        '    <td class="main"><font color="#ff0000"><b>' . sprintf(TEXT_COUNT_ATTENDEES, sizeof($audience)) . '</b></font></td>' . "\n" .
                        '  </tr>' . "\n" .
                        '  <tr>' . "\n" .
                        '    <td>' . go_fetch_dividing_up('tranparentes.gif', '1', '10') . '</td>' . "\n" .
                        '  </tr>' . "\n" .
                        '  <tr>' . "\n" .
                        '    <td class="main"><b>' . $this->title . '</b></td>' . "\n" .
                        '  </tr>' . "\n" .
                        '  <tr>' . "\n" .
                        '    <td>' . go_fetch_dividing_up('tranparentes.gif', '1', '10') . '</td>' . "\n" .
                        '  </tr>' . "\n" .
                        '  <tr>' . "\n" .
                        '    <td class="main"><tt>' . nl2br($this->content) . '</tt></td>' . "\n" .
                        '  </tr>' . "\n" .
                        '  <tr>' . "\n" .
                        '    <td>' . go_fetch_dividing_up('tranparentes.gif', '1', '10') . '</td>' . "\n" .
                        '  </tr>' . "\n" .
                        '  <tr>' . go_fetch_form('confirm', NAME_OF_FILE_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=confirm_send') . "\n" .
                        '    <td align="right">';
      if (sizeof($audience) > 0) {
        if (isset($_GET['global']) && ($_GET['global'] == 'true')) {
          $confirm_string .= go_fetch_hidden_field('global', 'true');
        } else {
          for ($i = 0, $n = sizeof($chosen); $i < $n; $i++) {
            $confirm_string .= go_fetch_hidden_field('chosen[]', $chosen[$i]);
          }
        }
        $confirm_string .= go_picture_submit('button_send.gif', PICTURE_SEND) . ' ';
      }
      $confirm_string .= '<a href="' . go_href_link(NAME_OF_FILE_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID'] . '&action=send') . '">' . go_picture_button('button_back.gif', PICTURE_BACK) . '</a> <a href="' . go_href_link(NAME_OF_FILE_NEWSLETTERS, 'page=' . $_GET['page'] . '&nID=' . $_GET['nID']) . '">' . go_picture_button('button_cancel.gif', PICTURE_CANCEL) . '</a></td>' . "\n" .
                         '  </tr>' . "\n" .
                         '</table>';
      return $confirm_string;
    }
    function send($newsletter_id) {
      global $_POST;
      $audience = array();
      if (isset($_POST['global']) && ($_POST['global'] == 'true')) {
        $items_query = go_db_query("select distinct pn.attendees_id, c.attendees_firstname, c.attendees_lastname, c.attendees_email_address from " . DB_TBL_ATTENDEES . " c, " . DB_TBL_ITEMS_INFORMS . " pn where c.attendees_id = pn.attendees_id");
        while ($items = go_db_fetch_array($items_query)) {
          $audience[$items['attendees_id']] = array('firstname' => $items['attendees_firstname'],
                                                       'lastname' => $items['attendees_lastname'],
                                                       'email_address' => $items['attendees_email_address']);
        }
        $attendees_query = go_db_query("select c.attendees_id, c.attendees_firstname, c.attendees_lastname, c.attendees_email_address from " . DB_TBL_ATTENDEES . " c, " . DB_TBL_ATTENDEES_INFO . " ci where c.attendees_id = ci.attendees_info_id and ci.global_item_informs = '1'");
        while ($attendees = go_db_fetch_array($attendees_query)) {
          $audience[$attendees['attendees_id']] = array('firstname' => $attendees['attendees_firstname'],
                                                        'lastname' => $attendees['attendees_lastname'],
                                                        'email_address' => $attendees['attendees_email_address']);
        }
      } else {
        $chosen = $_POST['chosen'];
        $ids = implode(',', $chosen);
        $items_query = go_db_query("select distinct pn.attendees_id, c.attendees_firstname, c.attendees_lastname, c.attendees_email_address from " . DB_TBL_ATTENDEES . " c, " . DB_TBL_ITEMS_INFORMS . " pn where c.attendees_id = pn.attendees_id and pn.items_id in (" . $ids . ")");
        while ($items = go_db_fetch_array($items_query)) {
          $audience[$items['attendees_id']] = array('firstname' => $items['attendees_firstname'],
                                                       'lastname' => $items['attendees_lastname'],
                                                       'email_address' => $items['attendees_email_address']);
        }
        $attendees_query = go_db_query("select c.attendees_id, c.attendees_firstname, c.attendees_lastname, c.attendees_email_address from " . DB_TBL_ATTENDEES . " c, " . DB_TBL_ATTENDEES_INFO . " ci where c.attendees_id = ci.attendees_info_id and ci.global_item_informs = '1'");
        while ($attendees = go_db_fetch_array($attendees_query)) {
          $audience[$attendees['attendees_id']] = array('firstname' => $attendees['attendees_firstname'],
                                                        'lastname' => $attendees['attendees_lastname'],
                                                        'email_address' => $attendees['attendees_email_address']);
        }
      }
      $mimemessage = new email(array('X-Mailer: bigware bulk mailer')); 

      $mimemessage->add_html($this->content); 

      $mimemessage->build_message();
      reset($audience);
      while (list($key, $value) = each ($audience)) {
        $mimemessage->send($value['firstname'] . ' ' . $value['lastname'], $value['email_address'], '', EMAIL_FROM, $this->title);
      }
      $newsletter_id = go_db_producing_input($newsletter_id);
      go_db_query("update " . DB_TBL_NEWSLETTERS . " set date_sent = now(), status = '1' where newsletters_id = '" . go_db_input($newsletter_id) . "'");
    }
  }
?>
