<?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();


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

$projectId 		= $site->getSubmittedVar('project_id', 0);
$mode 				= $site->getSubmittedVar('mode', '');


function createMainList(&$container) {
	global $site;

	$dbManipulator = &$site->getDBManipulator();
	$contentsManager = &$site->getContentsManager();

	$query = "SELECT distinct(`id`) FROM `easy_cms_content_type_mailing_list`;";
	$result = $dbManipulator->sendQuery($query);

	$vPos = 2;

	while($row = $dbManipulator->fetchArray($result)){
		$mailingListId = $row['id'];
		$mailingList = &$contentsManager->getContent($mailingListId, "mailing_list", 1);

    $linkId = "lnk_".$mailingListId;

		$mailingListName = $mailingList->getContentFieldValue(1, 'name');

		$link = &new egLink($linkId, $mailingListName->getValue(), 10, $vPos, 230, 18, "setCurrentProject(\'".$mailingListId."\', \'".$mailingListName->getValue()."\');document.getElementById(\'sbw_1\').src=\'".$_SERVER['PHP_SELF']."?project_id=".$mailingListId."&pane=newsletters\';", "interface/icons/icon_ml.png");

		$container->addObject($link);

	  $vPos += 20;
	}

}


function createFrame(&$container, $projectId) {
	global $site;

	$dbManipulator = &$site->getDBManipulator();
	$contentsManager = &$site->getContentsManager();

	$query = "SELECT `id`, `sent` FROM `easy_cms_content_type_mailing_message` WHERE `lang_id`='1' and `mailing_list_id` = '$projectId' and `deleted`= '0';";
	$result = $dbManipulator->sendQuery($query);

	$vPos = 2;

	while($row = $dbManipulator->fetchArray($result)){
		$newsletterId = $row['id'];
		if($newsletter = &$contentsManager->getContent($newsletterId, "mailing_message", false, false, 1)) {

	    $linkId = "lnk_".$newsletterId;

			$newsletterIcon = "interface/icons/icon_newsletter.png";

			if($row['sent'] == 1) {
				$newsletterIcon = "interface/icons/icon_newsletter_sent.png";
			}

			$newsletterName = $newsletter->getContentFieldValue(1, 'subject');

			$link = &new egLink($linkId, $newsletterName->getValue(), 10, $vPos, 350, 18, "sendResponse(\'".$newsletterId."\', \'".$newsletterName->getValue()."\')", $newsletterIcon);

			$container->addObject($link);

		  $vPos += 20;
		}
	}
}


if(isset($_GET['project_id']) && isset($_GET['pane']) && $_GET['pane'] == 'newsletters'){
	// display the ubscribers of the selected project
	$manager = &egManager::getInstance();

	$main = &new egContainer("main_ctn", 0, 0, 0, 0);

	createFrame($main, $_GET['project_id']);

	$manager->addContainer($main);

	$scripts = new egScript();
	$line = "function sendResponse(newsletterId, newsletterName) {
	window.parent.setSelectedNewsletter(newsletterId, newsletterName);
}
";
	$scripts->addLine($line);

	$manager->addScripts($scripts);

	echo $manager->getHTML();
}
else {
	// display the project list
	$manager = &egManager::getInstance();
	$main = &new egContainer("main_ctn", 0, 0, 0, 0);

	$subCtn = &new egContainer("sub_ctn", 10, 10, 248, 348);

	createMainList($subCtn);

	$frame1 = &new egFrame("fra_1", "frame 1", 10, 10, 250, 350);
	$frame2 = &new egFrame("fra_2", "frame 2", 270, 10, 400, 350);

	$subWindow = &new egSubWindow("sbw_1", "select_newsletter.php?project_id=$projectId&pane=newsletters", 271, 11, 398, 348);

	$lPos = 10;

	$mlNameStatic = &new egStatic("project_name_static", LANG_MAILING_LIST_MENU_SELECTED." : ", 10, 375, 170, 0, true);
	$mlNameValue  = &new egStatic("project_name_value", "", 180, 375, 350, 0, false);

	$newsletterNameStatic = &new egStatic("newsletter_name_static", LANG_NEWSLETTER_MENU_SELECTED." : ", 10, 395, 170, 0, true);
	$newsletterNameValue  = &new egStatic("newsletter_name_value", "", 180, 395, 350, 0, false);


	if($mode == 'admin') {
		$linkAdd = &new egLink("lnk_add", LANG_NEWSLETTER_MENU_ADD, 126, 420, 200, 18, "launchNewsletterCreationWindow()", "interface/icons/add.png");
		$linkEdit = &new egLink("lnk_edit", LANG_NEWSLETTER_MENU_EDIT, 126, 440, 200, 18, "launchNewsletterEditionWindow()", "interface/icons/edit.png");
		$linkRemove = &new egLink("lnk_remove", LANG_NEWSLETTER_MENU_REMOVE, 126, 460, 200, 18, "launchNewsletterDeletionWindow()", "interface/icons/remove.png");
		$linkSend = &new egLink("lnk_send", LANG_NEWSLETTER_MENU_SEND, 126, 500, 200, 18, "launchNewsletterSending()", "interface/icons/icon_newsletter_send.png");
		$main->addObject($linkAdd);
		$main->addObject($linkEdit);
		$main->addObject($linkRemove);
		$main->addObject($linkSend);
	}
	else {
		$okButton = &new egPressbutton("btn_ok", "Ok", 505, 385, 80, null, "clickOKButton();");
		$cancelButton = &new egPressbutton("btn_cancel", "Cancel", 595, 385, 80, null, "clickCancelButton();");
		$main->addObject($okButton);
		$main->addObject($cancelButton);
	}

	$main->addObject($frame1);

	$main->addObject($mlNameStatic);
	$main->addObject($mlNameValue);

	$main->addObject($newsletterNameStatic);
	$main->addObject($newsletterNameValue);

	$main->addObject($frame2);
	$main->addObject($subWindow);

	$manager->addContainer($main);
	$manager->addContainer($subCtn);


	$txtObjName  = &new egStatic("st_obname", "", 10, 5, 200, 0, true);
	$main->addObject($txtObjName);

	$scripts = new egScript();


	$line = "
function clickOKButton() {
		sendResponse();
}
var selectedNewsletter = 0;
var currentProject = 0;
function clickCancelButton() {
	window.close();
}
function launchNewsletterCreationWindow() {
	if(currentProject > 0) {
		document.getElementById('sbw_1').contentWindow.openWindow('../index.php?'+'display=edit_content&mode=ask_modif_content'+'&content_id=0'+'&type_name=mailing_message&mailing_list_id='+currentProject,540, 600, 0, 0);
	}
	else {
		alert('no mailing list selected');
	}
}
function launchNewsletterEditionWindow() {
	if(selectedNewsletter > 0) {
		document.getElementById('sbw_1').contentWindow.openWindow('../index.php?'+'display=edit_content&mode=ask_modif_content'+'&content_id='+selectedNewsletter+'&type_name=mailing_message',540, 600, 0, 0);
	}
	else {
		alert('no newsletter selected');
	}
}
function launchNewsletterDeletionWindow() {
	if(selectedNewsletter > 0) {
		document.getElementById('sbw_1').contentWindow.openWindow('../index.php?'+'display=edit_content&mode=ask_delete_content'+'&content_id='+selectedNewsletter+'&type_name=mailing_message',380, 280, 0, 0);
		setSelectedNewsletter(0, '');
	}
	else {
		alert('no newsletter selected');
	}
}
function setCurrentProject(projectId, projectName) {
	currentProject = projectId;
	document.getElementById('project_name_value').innerHTML = projectName;
	setSelectedNewsletter(0, '');
}
function setSelectedNewsletter(newsletterId, newsletterName) {
	selectedNewsletter = newsletterId;
	document.getElementById('newsletter_name_value').innerHTML = newsletterName;
}
function launchNewsletterSending() {
	if(selectedNewsletter > 0) {
		document.getElementById('sbw_1').contentWindow.openWindow('send_newsletter.php?'+'mode=ask_send_newsletter'+'&newsletter_id='+selectedNewsletter,340, 200, 0, 0);
	}
	else {
		alert('no newsletter selected');
	}
}
function refreshFrame() {
	var temp = document.getElementById('sbw_1').src;
	document.getElementById('sbw_1').src = '';
	document.getElementById('sbw_1').src = temp;
}
";
	$scripts->addLine($line);


	$manager->addScripts($scripts);


	echo $manager->getHTML();
}
?>