<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

	require "../_container/container_top.php";
	require_once ('business/MailMgr.php');
	require_once("config/Tagger.php");

	$mailMgr = new MailMgr();
	$tagger = new Tagger();

	$action = getPost('action');
	$tabid = getPost('tabid');

	$selectDescription = getPost('selectDescription');
	$selectEnable = getPost('selectEnable');
	$selectTo = getPost('selectTo');
	$selectCc = getPost('selectCc');
	$selectBcc = getPost('selectBcc');
	$selectSubject = getPost('selectSubject');
	$selectContent = getPost('selectContent');

	if (!empty($action)) {
		if ($action == "save"){
			$mail = array();
			$mail['tabid'] = str_replace("mail_", "", $tabid);
			$mail['description'] = $selectDescription;
			$mail['enable'] = $selectEnable;
			$mail['to'] = $selectTo;
			$mail['cc'] = $selectCc;
			$mail['bcc'] = $selectBcc;
			$mail['subject'] = $selectSubject;
			$mail['content'] = ($selectContent);
			$mail['tabid'] = str_replace("mail_", "", $tabid);

			if ((!empty($selectEnable)) && (empty($selectTo))){
				$msgType = "error";
				$msg = $tr->translate("msgErrorToMissing");
			}else{
				$res = $mailMgr->saveMail($mail);
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
			}

		}
		if ($action == "add"){
			$mail = array();

			$mail['tabid'] = str_replace("mail_", "", $tabid);
			$mail['description'] = $selectDescription;
			$mail['enable'] = $selectEnable;
			$mail['to'] = $selectTo;
			$mail['cc'] = $selectCc;
			$mail['bcc'] = $selectBcc;
			$mail['subject'] = $selectSubject;
			$mail['content'] = $selectContent;
			if ((!empty($selectEnable)) && (empty($selectTo))){
				$msgType = "error";
				$msg = $tr->translate("msgErrorToMissing");
			}else{
				$res = $mailMgr->addMail($mail);
				if ($res > 0){
					$msgType = "success";
					$msg = $tr->translate("msgSuccess");
				}else{
					$msgType = "error";
					$msg = $tr->translate("msgError");
				}
			}
		}
		if ($action == "test"){
			$mail = $mailMgr->getMail(str_replace("mail_", "", $tabid));

			$tagger->setUserId($session->userDisplayName);
			Logger::debug(' --> mail before tagging: ' . print_r($mail, true));
			$mail[0]->from = $config['renter_email'];
			$mail[0]->to = $session->userEmail;

			Logger::debug('Mail CC before: ' . $mail[0]->cc);
			$mail[0]->cc = $tagger->tag($mail[0]->cc);
			Logger::debug('Mail CC after: ' . $mail[0]->cc);

			$mail[0]->bcc = $tagger->tag($mail[0]->bcc);
			$mail[0]->subject = $tagger->tag($mail[0]->subject);
			$mail[0]->content = $tagger->tag($mail[0]->content,"preview");
			Logger::debug(' --> mail after tagging: ' . print_r($mail, true));
			if ((!empty($selectEnable)) && (empty($selectTo))){
				$msgType = "error";
				$msg = $tr->translate("msgErrorToMissing");
			}else{
				include_once ('config/Mailer.php');
				$m = new Mailer($config);
				$r = $m->sendMail($mail);
				if (!$r) {
					$msgType = "error";
					$msg = $tr->translate("msgMailError");
				}else{
					$msgType = "success";
					$msg = $tr->translate("msgSuccess");
				}
			}
		}
		echo '<div class="alert alert-' . $msgType . '">
			<strong>' . $msg . '</strong>
			</div>';
	}

	if (empty($tabid)){
		$tabid = "mail_1";
	}

	$arrMail = $mailMgr->getMail(str_replace("mail_", "", $tabid));
	if (empty($arrMail)) {
		$arrMail[] = new MailVo();
	}else{
		$mailEmpty = true;
	}

?>

	<div>
		<ul class="breadcrumb">
			<li><a href="./"><?php echo $tr->translate("menuHome"); ?></a> <span class="divider">/</span></li>
			<li class="active"><?php echo $tr->translate("menuMailing"); ?></li>
		</ul>
	</div>
	<div class="sortable row-fluid">
		<a data-rel="tooltip" title="<?php echo $tr->translate('AddReserv'); ?>" class="well span2 top-block btn-action" href="#" name="newReservation" id="newReservation" _page="reservationDetail.php" _action="Add">
			<i class="icon-red icon-user"></i>
			<div><?php echo $tr->translate("AddReserv"); ?></div>
			<div></div>
		</a>
		<a data-rel="tooltip" title="<?php echo $tr->translate("AddPayment"); ?>" class="well span2 top-block btn-action" href="#" name="newPayment" id="newPayment" _page="paymentDetail.php" _action="Add">
			<i class="icon-cog"></i>
			<div><?php echo $tr->translate("AddPayment"); ?></div>
			<div></div>
		</a>
	</div>

	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-picture"></i> <?php echo $tr->translate("thTagList"); ?></h2>
				<div class="box-icon">
					<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-down"></i></a>
				</div>
			</div>
			<div class="box-content" style="display:none;">
				<div class="tabbable">
					<ul class="nav nav-tabs">
						<?php $arrTags = Tagger::getTagList(); foreach ($arrTags as $tag) { ?>
							<li class="<?php if (reset($arrTags) == $tag) echo "active"; ?>"><a href="#tag_<?php echo $tag; ?>" data-toggle="tab"><?php echo $tr->translate('tagName_'.$tag); ?></a></li>
						<?php } ?>
					</ul>
					<div class="tab-content">
					<?php $arrTags = Tagger::getTagList(); foreach ($arrTags as $tag) { ?>
						<div class="tab-pane <?php if (reset($arrTags) == $tag) echo "active"; ?>" id="tag_<?php echo $tag; ?>">
							<table id="tableMailingTagList" name="tableMailingTagList" class="table table-bordered table-striped table-condensed">
								<thead>
									<tr>
										<th style="width:55px;"><?php echo $tr->translate("thTagName"); ?></th>
										<th><?php echo $tr->translate("thTagDescription"); ?></th>
									</tr>
								</thead>
								<tbody>
								<?php foreach(Tagger::getTag($tag) as $tagName){ ?>
									<tr><td><code><?php echo htmlentities($tagName); ?></code></td>
										<td><?php echo $tr->translate($tagName); ?></td></tr>
								<?php } ?>
								</tbody>
							</table>
						</div>
					<?php } ?>
					</div>
				</div>
			</div>
		</div><!--/span-->
	</div><!--/row-->

	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-picture"></i> <?php echo $tr->translate("thMailing"); ?></h2>
			</div>
			<div class="box-content">
				<?php if(empty($mailEmpty)) {
						$msgType = "error";
						$msg = $tr->translate("msgErrorMailNotExist");
						echo '<div class="alert alert-' . $msgType . '">
							<strong>' . $msg . '</strong>
							</div>';
						}
					?>
			<form class="form-horizontal" method="post" name="mailingForm" id="mailingForm" action="mailing.php">
				<input type="hidden" name="action" id="action" value="" />
				<input type="hidden" name="tabid" id="tabid" value="<?php echo $tabid; ?>" />
				
				<div class="tabbable">
					<ul class="nav nav-tabs nav-tabs-action">
						<li class="<?php if ($tabid == "mail_1") echo "active"; ?>" id="mail_1"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_1"); ?></a></li>
						<li class="<?php if ($tabid == "mail_10") echo "active"; ?>" id="mail_10"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_10"); ?></a></li>
						<li class="<?php if ($tabid == "mail_2") echo "active"; ?>" id="mail_2"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_2"); ?></a></li>
						<li class="<?php if ($tabid == "mail_9") echo "active"; ?>" id="mail_9"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_9"); ?></a></li>
						<li class="<?php if ($tabid == "mail_3") echo "active"; ?>" id="mail_3"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_3"); ?></a></li>
						<li class="<?php if ($tabid == "mail_4") echo "active"; ?>" id="mail_4"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_4"); ?></a></li>
						<li class="<?php if ($tabid == "mail_8") echo "active"; ?>" id="mail_8"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_8"); ?></a></li>
						<li class="<?php if ($tabid == "mail_5") echo "active"; ?>" id="mail_5"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_5"); ?></a></li>
						<li class="<?php if ($tabid == "mail_11") echo "active"; ?>" id="mail_11"><a href="#" data-toggle="tab"><?php echo $tr->translate("mailName_11"); ?></a></li>
					</ul>
					<div class="tab-content">
						<div class="tab-pane active">
							<div class="control-group">
								<label class="control-label" for="selectDescription"><?php echo $tr->translate("thMailingActive"); ?></label>
								<div class="controls">
									<input id="selectEnable" name="selectEnable" type="checkbox"<?php if (!empty($arrMail[0]->enable)) echo " checked"; ?>>
								</div>
							</div>
								<div class="control-group">
									<label class="control-label" for="selectDescription"><?php echo $tr->translate("thMailingDescription"); ?></label>
									<div class="controls">
										<textarea id="selectDescription" name="selectDescription" class="span6" rows="2"><?php echo $arrMail[0]->description; ?></textarea>
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="selectTo"><?php echo $tr->translate("thMailingTo"); ?></label>
									<div class="controls">
										<input id="selectTo" name="selectTo" type="text" class="tags span6" value="<?php echo $arrMail[0]->to; ?>">
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="selectCc"><?php echo $tr->translate("thMailingCc"); ?></label>
									<div class="controls">
										<input id="selectCc" name="selectCc" type="text" class="tags span6" value="<?php echo $arrMail[0]->cc; ?>">
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="selectBcc"><?php echo $tr->translate("thMailingBcc"); ?></label>
									<div class="controls">
										<input id="selectBcc" name="selectBcc" type="text" class="tags span6" value="<?php echo $arrMail[0]->bcc; ?>">
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="selectSubject"><?php echo $tr->translate("thMailingSubject"); ?></label>
									<div class="controls">
										<input class="span6" type="text" id="selectSubject" name="selectSubject" value="<?php echo $arrMail[0]->subject; ?>">
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="selectContent"><?php echo $tr->translate("thMailingContent"); ?></label>
									<div class="controls">
										<textarea id="selectContent" name="selectContent" class="span6 mceEditor" rows="10"><?php echo htmlspecialchars($arrMail[0]->content); ?></textarea>
									</div>
								</div>
						<div class="form-actions">
						<?php if (empty($mailEmpty)){ ?>
							<button type="btn" name="add" id="add" class="btn btn-primary"><?php echo $tr->translate("btnMailingAdd"); ?></button>
						<?php }else{ ?>
							<button type="btn" name="save" id="save" class="btn btn-primary"><?php echo $tr->translate("btnMailingSave"); ?></button>
							<button type="button" name="test" id="test" class="btn btn-success"><?php echo $tr->translate("btnMailingTest"); ?></button>
						<?php } ?>
						</div>
					</div>
				</div>
            </div>
			</form>
			</div>
		</div><!--/span-->

	</div><!--/row-->

<?php require "../_container/container_bottom.php"; ?>
<script>

$(".nav-tabs-action li").on("click", function(e){
	blockUI();
	$().redirect(
		$('#mailingForm').attr('action'),{
			'tabid': $(this).attr('id')
		},
		'POST'
	);
});
$("#save").click(function(e){
	e.preventDefault();
	$("#action").val('save');
	blockUI();
	mailingForm.submit();
});
$('#add').click(function(e){
	$('#action').val('add');
	blockUI();
});
$('#test').click(function(e){
	e.preventDefault();
	$('#action').val('test');
	blockUI();
	mailingForm.submit();
});

</script>