<?php
/**
 * $Id$
 * przypominarka
 *
 */
class MailingController extends ModuleController implements ControllerInterface {
	const REDIRECT_URL = "mailing.frmx";
	/**
	 * Domyslna metoda
	 *
	 */
	public function IndexAction($param) {

		//Utils::ArrayDisplay($param);

		//$arrayAddress = MailingDAL::GetMailingAddressList(1);
		//PEJDZOWANIE

		$countLimit = 10;
		if(isset($param['strona']) && $param['strona'] > 0 ){
			$offset = ($param['strona']-1) * $countLimit;
			$this->smarty->assign('currentPage', ($param['strona']-1)* $countLimit);

		}
		else{
			$offset = 0;
			$this->smarty->assign('currentPage', 0);
		}

		$this->smarty->assign('PageCount', ceil(MailingDAL::GetCountAll()/$countLimit));
		$this->RunShared('Pagination', $param);
		//AKCJE DOMY�LNE
		$arrayObjMailing = MailingDAL::GetAll($offset, $countLimit);
		//Utils::ArrayDisplay($_SERVER['DOCUMENT_ROOT']);


		$this->smarty->assign('arrayObjMailing', $arrayObjMailing);

	}



	/**
	 * Wysy�ka metoda
	 *
	 */
	public function SendAction($param) {


		if (isset($_POST['sendEmail'])) {
			
			//Utils::ArrayDisplay($_POST);

			$objMailing = new Mailing('', $_POST['subjet'], $_POST['content'], 0, '', 2, 0);
			MailingDAL::Save($objMailing);
			$this->smarty->assign('sendInfo', "Email zosta� przekazany do wysy�i");
			Utils::Redirect(URL_MAIN.'/index,'.self::REDIRECT_URL);

		}

		include("plugins/fckeditor/fckeditor_php5.php");
		$oFCKeditor = new FCKeditor('content');
		$oFCKeditor->BasePath = 'plugins/fckeditor/';
		$oFCKeditor->Value = '';
		$oFCKeditor->ToolbarSet = 'Formix';
		$oFCKeditor->Height = 400;
		$this->smarty->assign('fck', $oFCKeditor);

	}

	/**
	 * Podgl�d
	 *
	 */
	public function ShowAction($param) {

		//$arrayAddress = MailingDAL::GetMailingAddressList();
		//Utils::ArrayDisplay($arrayAddress);
		//Utils::ArrayDisplay($param);
		if (isset($param['id'])) {

			$objMailing = MailingDAL::GetMailingById($param['id']);
			$this->smarty->assign('objMailing', $objMailing);
		}

		//		if (!isset($objMailing)) {
		//			die(Header('Location: mailing'.APPLICATION_FILE_TYPE));
		//		}



	}



	public function GetSendAction() {
		$logger = LoggerManager::getLogger(__CLASS__);
		$objMailing = MailingDAL::GetMailingToSend();
		//while (($objMailing = MailingDAL::GetMailingToSend())) {

		//		}
		if (is_object($objMailing)) {
			$logger = LoggerManager::getLogger(__CLASS__);
			$limit = $objMailing->GetLastSend() + 50;
			$mailList = array();
			$logger->info("limit: $limit ".$objMailing->GetLastSend());
			$mailList = MailingDAL::GetMailingAddressList(2, $objMailing->GetLastSend(), 50);
			MailingDAL::UpdateLastSend($objMailing->GetId(), $limit);
			//Utils::ArrayDisplay($mailList);

			// pobiera z tabeli user (jezeli taka istnije w tym przypadku nie potrzebna)
			//			if (!is_array($mailList) || count($mailList) < 50 && $objMailing->GetType() == 1) {
			//				MailingDAL::UpdateLastSend($objMailing->GetId(), 0);
			//				MailingDAL::UpdateType($objMailing->GetId());
			//
			//			}

			if (!is_array($mailList) || count($mailList) < 50 && $objMailing->GetType() == 2) {
				MailingDAL::UpdateLastSend($objMailing->GetId(), 0);
				MailingDAL::SetExecuted($objMailing->GetId());
			}


			//$this->smarty->assign('content', $objMailing->GetText());
			//$txtmessage = $this->smarty->fetch('MailTxt.tpl');
			//Utils::ArrayDisplay($mailList);

			foreach ($mailList as $key => $arrayPackage) {
				foreach ($arrayPackage as $keyPackege => $value) {
					$subjet = " DB Druk - ".$objMailing->GetTitle();
					$mail = new PHPMailer();
					$mail -> CharSet = "utf-8";
					$mail -> Subject = $subjet;
					$mail -> SMTPAuth = true;
					$mail -> Mailer = 'smtp';
					$mail -> Host = "dbdruk.formix.eu";
					$mail -> Port = 25;
					$mail -> Priority = 3;
					$mail ->IsHTML(true);
					//$mail -> ConfirmReadingTo = "wino@wino.formix.eu";
					$mail ->From = "smtp@dbdruk.formix.eu";
					$mail -> FromName = "DB Druk";
					$mail -> Username = "smtp@dbdruk.formix.eu";
					$mail -> Password = "smtp1234";
					//$mail->AddAddress('maciek@formix.pl', 'Maciej Kloch');
					$mail->AddAddress($value['email'], $value['name']);

					$mail -> AddBCC("maciek@formix.pl", "Mailing  - dbdruk.pl ".$value['email']."- ".$value['name']." ".$key."-".$keyPackege);
					$mail -> Body = $objMailing->GetText();
					$mail -> Send();
				}
				//sleep(2);
			}
			//sleep(25);
		}
		Utils::Redirect(URL_MAIN.'/index,'.self::REDIRECT_URL);
	}

	public function ListAction($param) {
		//PEJDZOWANIE

		$countLimit = 10;
		if(isset($param['strona']) && $param['strona'] > 0 ){
			$offset = ($param['strona']-1) * $countLimit;
			$this->smarty->assign('currentPage', ($param['strona']-1)* $countLimit);

		}
		else{
			$offset = 0;
			$this->smarty->assign('currentPage', 0);
		}

		$this->smarty->assign('PageCount', ceil(MailingDAL::GetCountAllEmail()/$countLimit));
		$this->RunShared('Pagination', $param);
		//AKCJE DOMY�LNE
		$arrayEmail = MailingDAL::GetAllEmail($offset, $countLimit);
		//Utils::ArrayDisplay($arrayEmail);


		$this->smarty->assign('arrayEmail', $arrayEmail);

	}

	/**
	 * usuwanie emial z list mailingowej
	 *
	 */
	public function DeleteAction($param) {

		$logger = LoggerManager::getLogger(__CLASS__);

		//$idCustomer = SessionProxy::GetValue(EnumSessionValue::IDCUSTOMER);

		if(isset($param['id'])) {
			$id=$param['id'];
		} else{
			$id = null;
		}

		if($id) {
			MailingDAL::Delete($id);
			

			die(Header('Location: list,mailing'.APPLICATION_FILE_TYPE));
			if (Pat) {

			}
		}



	}


	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch($param) {
		//Utils::ArrayDisplay($param);
		$this->Run($param);
		$this->RunShared('Mailing');
		$this->smarty->assign('titleAdmin', 'Mailing');
		//$this->smarty->assign('showIcon', false);
		if($this->IsUser()==false) {
			$this->AddRedirect(URL_MAIN.'/Login.frmx', 0);
		}

	}
	
	public function postDispatch($param) {

	}
}
?>