<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// define the path context
chdir('..');
define('EASY_GUI_APP_DIR', '../');


include_once('config.inc.php');
include_once('classes/core/Site.class.php');


include_once('interface/egScript.class.php');
include_once('interface/egManager.class.php');
include_once('interface/egContainer.class.php');
include_once('interface/egMenuItem.class.php');
include_once('interface/egFrame.class.php');
include_once('interface/egToolTip.class.php');
include_once('interface/egRichText.class.php');
include_once('interface/egLink.class.php');
include_once('interface/egStatic.class.php');
include_once('interface/egEdit.class.php');
include_once('interface/egCheckBox.class.php');
include_once('interface/egRadioButton.class.php');
include_once('interface/egPressButton.class.php');
include_once('interface/egDropDownListBox.class.php');
include_once('interface/egPropertyList.class.php');
include_once('interface/egPropertySheet.class.php');
include_once('interface/egPropertyPage.class.php');
include_once('interface/egTreeNode.class.php');
include_once('interface/egTreeList.class.php');
include_once('interface/egSubWindow.class.php');


$site = &Site::getInstance(); // constructor checks the $_GET, $_POST and $_SESSION arrays

//security check : user must be identified to upload some file
$user = $site->getCurrentUser();
if(!$user || $user->getUserId() == USER_ID_ANONYMOUS) {
	die("access denied");
	exit();
}


$mode 					= $site->getSubmittedVar('mode', ASK_SEND_NEWSLETTER);
$newsletterId	 	= $site->getSubmittedVar('newsletter_id', 0);

if(!$newsletterId) {
	die('newsletter_id is missing');
}

switch ($mode) {
	case ASK_SEND_NEWSLETTER :
    $dbManipulator = &$site->getDBManipulator();

		$query = "SELECT * FROM `easy_cms_content_type_mailing_message` WHERE `id` = '$newsletterId' AND `sent` = '0' AND `deleted` = '0';";
		$result = $dbManipulator->sendQuery($query);
		if($row = $dbManipulator->fetchArray($result)){
			$mailingListId = $row['mailing_list_id'];
		}
		else {
			die('newsletter not found or already sent');
		}

		$recipientsCount = 0;
		$query = "SELECT count(*) as `count` FROM `easy_cms_content_type_mailing_subscriber` WHERE `mailing_list_id` = '$mailingListId' AND `deleted` = '0';";
		$result = $dbManipulator->sendQuery($query);
		if($row = $dbManipulator->fetchArray($result)){
			$recipientsCount = $row['count'];
		}


		$manager = &egManager::getInstance(LANG_FILE_RENAME_TITLE);
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);
		$manager->addContainer($main);


		$warningTxt = &new egStatic("warning_txt_1", LANG_MAILING_LIST_MENU_SENDING_WARNING.'<b>'.$recipientsCount.'</b>'.LANG_MAILING_LIST_MENU_SENDING_MEMBERS, 10, 15, 350, 0, false);
		$confirmTxt = &new egStatic("confirm_txt_1", LANG_MAILING_LIST_MENU_SENDING_CONFIRM, 10, 40, 350, 0, false);


		$okButton = &new egPressbutton("btn_ok", LANG_OK_BUTTON, 150, 130, 80, null, "clickOKButton();");
		$cancelButton = &new egPressbutton("btn_cancel", LANG_CANCEL_BUTTON, 235, 130, 80, null, "clickCancelButton();");

		$hiddenValuesContainer = &new egContainer("ctn_hidden_values", 0, 0, 1, 1);

		$objMode = &new egEdit("hidden_control_mode", SEND_NEWSLETTER, 0, 0, 0, 0, "mode");
		$objId = &new egEdit("hidden_control_newsletter_id", $newsletterId, 0, 0, 0, 0, "newsletter_id");

		$hiddenValuesContainer->addObject($objMode);
		$hiddenValuesContainer->addObject($objId);

		$manager->addContainer($hiddenValuesContainer);

		$main->addObject($okButton);
		$main->addObject($cancelButton);

		$main->addObject($warningTxt);
		$main->addObject($confirmTxt);

		$scripts = &new egScript();
		$line = "function clickOKButton() {
document.forms[0].submit();
}
function clickCancelButton() {
window.close();
}
";
		$scripts->addLine($line);
		$manager->addScripts($scripts);

		print($manager->getHTML());

		break;
	case SEND_NEWSLETTER :
    $dbManipulator = &$site->getDBManipulator();

		$query = "SELECT * FROM `easy_cms_content_type_mailing_message` WHERE `id` = '$newsletterId' AND `sent` = '0' AND `deleted` = '0';";
		$result = $dbManipulator->sendQuery($query);
		if($row = $dbManipulator->fetchArray($result)){
			$mailingListId = $row['mailing_list_id'];
		}
		else {
			die('newsletter not found or already sent');
		}

		$query = "SELECT distinct(`id`) FROM `easy_cms_content_type_mailing_subscriber` WHERE `mailing_list_id` = '$mailingListId' AND `deleted` = '0';";
		$result = $dbManipulator->sendQuery($query);
		while($row = $dbManipulator->fetchArray($result)){
			$subscriberId = $row['id'];
			$dateIn = date('Y-m-d');
			// insert sending query in queue
			$query = "INSERT INTO `easy_cms_mailing_queue` (`id`,`message_id`,`subscriber_id`,`date_in`,`date_out`) VALUES ('', '$newsletterId', '$subscriberId', '$dateIn', '0000-00-00');";
			$dbManipulator->sendQuery($query);
		}

		$query = "UPDATE `easy_cms_content_type_mailing_message` SET `sent` = '1' WHERE `id`= '$newsletterId';";
		$dbManipulator->sendQuery($query);


		$manager = &egManager::getInstance();
		$main = &new egContainer("main_ctn", 0, 0, 0, 0);
		$manager->addContainer($main);

		$scripts = new egScript();
		$line = "window.opener.location.reload();window.close();";
		$scripts->addLine($line);

		$manager->addScripts($scripts);
		print($manager->getHTML());

		break;
}
?>