<?php

session_start();

require 'authenticate.user.php';



require 'configuration.php';

require_once 'includes/common.functions.php';

require_once ('classes/database.php');

require_once ('classes/functions.php');

$sqlObj = new mysqlClass;

$funcObj = new funcClass;

$table = "##addressbook";

$curFile = basename(__FILE__);

$ACTION				= 'add';



$alphaCharSearch = '';

if (!empty($_GET['alpha']) && isset($_GET['alpha']))

{

	$alpha =ord($_GET['alpha']);

	

	if ($alpha >= 65 && $alpha <= 90)

	{

		$alphaCharSearch = " WHERE NAME RLIKE '^" . $_GET['alpha'] . "'";

	}

	else

	{

		$alphaCharSearch = " WHERE NAME RLIKE '^A'";

	}

}

if ($_SESSION['IS_USER_CLIENT'])

{

	die ("Sorry, you are not authorized to access this page");

}



$id = intval($_GET['id']);

if (empty($id))

	die("Required parameter is missing");





$query = "SELECT CAMPAIGN_NAME FROM ##campaigns WHERE ID = '{$id}'";

$resultSet = $funcObj->CustomQuery($query);

$campaignName = $resultSet[0]->CAMPAIGN_NAME;



if (($_POST['formaction'] == 'edit') && (empty($error)))

{

	$id = intval($_POST['id']);

	$query = "DELETE FROM ##campaign_send_stats WHERE CAMPAIGN_ID = '$id' AND SESSION_ID='".session_id()."'";

	$funcObj->CustomDelete($query);

}

//ADD RECORD

if ($_POST['formaction'] == 'add' || $_POST['formaction'] == 'edit' && empty($error))

{

	$id = intval($_POST['id']);

	$query = "DELETE FROM ##campaign_send_stats WHERE CAMPAIGN_ID = '$id' AND SESSION_ID='".session_id()."'";

	$funcObj->CustomDelete($query);

	

	$chkAddressBooks = $_POST['chkAddressBooks'];

	foreach ($chkAddressBooks as $addrID)

	{ 

		$query = "INSERT INTO ##campaign_send_stats SET CAMPAIGN_ID='$id', ADDR_ID='$addrID', SESSION_ID='".session_id()."'";

		$funcObj->insertQuery($query);

	}

	header ("location: send2.php?id=$id");

	exit;

}



## if edit is EDIT

# then load values into the form

if ($_GET['action'] == 'edit')

{

	$ID = $_GET['id'];

	$query = "SELECT ADDR_ID FROM ##campaign_send_stats WHERE CAMPAIGN_ID = '{$id}' AND SESSION_ID = '".session_id()."'";

	$editResultSet = array();

	$editResultSet = $funcObj->CustomQueryArray($query);

	$chkBoxes = array();

	foreach ($editResultSet as $row => $val)

	{

		$chkBoxes[] = $val['ADDR_ID'];

	}

	//print_r($chkBoxes);

	$ACTION				= 'edit';

}



//$searchTxt = trim($funcObj->AppendSlashes($_REQUEST['searchbox']));

$searchTxt = trim('J%');

$searchArray = 'NAME';



if($_GET['action']=='del')

{

	$whereField = "ID";

	$whereValue = $ID;

	$funcObj->DeleteRecord($table,$whereField,$whereValue);

	$message = "Campaign(s) have been deleted successfully";

}



$query = "SELECT COUNT(NAME) FROM ##addressbook " . $alphaCharSearch;

$resultSet = $funcObj->CustomQueryCount($query);





$records	= $funcObj->fetchParameterValue('SHOW_RECORDS_PER_PAGE');

if($records > 0)

{

	$limit = intval($records);

}

else

{

	$limit = $configAdminPaging;

}



if($_REQUEST['start'])

	$start = $_REQUEST['start'];

else

	$start = 0;

	





$proCount = $resultSet;



$link = $curFile."?id=$id";



require_once('classes/paging.class.php');

$pageObj = new Paging( $proCount, $start , $limit);



/* ACTIVATE THE TAB */

$activeTab = 'Campaigns';

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Segnant Mailer - Manage Campaigns</title>

<link rel="stylesheet" type="text/css" href="includes/css/paging.css"  />

<link rel="stylesheet" type="text/css" href="includes/css/segnantmailer.css" />

<link rel="stylesheet" href="includes/css/themes/blue/style.css" type="text/css"  media="print, projection, screen" />

<script type="text/javascript" src="includes/js/jquery-1.3.2.min.js"></script>

<script type="text/javascript" src="includes/js/jquery.tablesorter.js"></script>

<script type="text/javascript" src="includes/js/common.jquery.js"></script>

<script type="text/javascript" src="includes/js/send.jquery.js"></script>

<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />

</head>

<body>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

  <tr>

    <td height="79" class="headerbg"><? require_once 'includes/inc.header.php' ?>    

    </td>

  </tr>



  <tr>

    <td class="canvas" height="300" valign="top"><table width="100%" border="0" cellspacing="10" cellpadding="4">

      <tr>

        <td width="100%" height="50" valign="top" class="heading">Send Campaign</td>

        </tr>

      <tr>

        <td><table width="100%" border="0" cellspacing="1" cellpadding="1">

              <?php if (!empty($message))

					  echo "

                      <tr>

                        <td class='message'>$message</td>

                      </tr>

					  ";

					  ?>

              <tr>

                <td><form name="frmListing" id="frmListing" method="post" style="margin:0; padding: 0" action="">

                    <?

					  $query = "SELECT * FROM ##addressbook $alphaCharSearch ORDER BY NAME DESC LIMIT {$pageObj->start}, {$pageObj->limit}";

					  $resultSet = $funcObj->CustomQuery($query);

					  ?>

                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="activeTable">

                      <tr class="activeTab">

                        <td width="30" align="center"><img src="images/11.png" hspace="8" alt="" border="0" /></td>

                        <td width="90%"><span><strong>Campaign:</strong> select data sources for &quot;

                          <?=$campaignName?>

                        &quot;</span></td>

                        <td width="10%">&nbsp;</td>

                      </tr>

                      <tr>

                        <td colspan="3" height="40" style="padding: 8px;">Please select one or more address books to send this campaign.</td>

                      </tr>

                      <tr>

                        <td colspan="2" height="40" style="padding: 8px;"><strong>Address books (<?= count($resultSet) ?>)</strong> &nbsp;&nbsp;Show Address book [<a href='send.php?&id=<?php echo $id;?>'>ALL</a>] 

                          <?php

                          for($alpha = 65; $alpha <= 90; $alpha++) 

						  {

					      	echo '[<a href="send.php?&id='.$id.'&alpha='.chr($alpha).'">' . chr($alpha) . '</a>]&nbsp;';

						  }

						  ?>

                          </td>

                          <td align="right" style="padding: 5px;"><input type="submit" onclick="return checkSel()" value="Goto Step 2" class="button" /></td>

                      </tr>

                      <tr>

                        <td colspan="3"><?php

					  if(count($resultSet) > 15)

					  {

					  	echo "<div style='overflow: scroll; height: 300px'>";

						$overflow = 1;

					  }

					  ?>

                          <table cellspacing="1" class="tablesorter">

                        <thead>

                            <tr>

                              <th width="30" align="center"><input type="checkbox" name="chkAll" id="chkAll" onclick="checkAll()" value="1" /></th>

                              <th align="left" width="110"> Date updated</th>

                              <th align="left" width="30%"> Address book name</th>

                              <th align="right" width="14%"> Unique Contacts &nbsp;&nbsp;&nbsp;&nbsp;</th>

                              <th align="left">Address book description</th>

                            </tr>

                            </thead>

                            <tbody>

                            <?php

					$i=1;

					if(count($resultSet)!=0)

					{

					foreach ($resultSet as $row)

					{

						$query = "SELECT count(DISTINCT(EMAIL)) FROM ##contacts WHERE ADDR_ID = $row->ID AND IS_DELETED = 0";

						$countContacts = $funcObj->CustomQueryCount($query);

						$i++;

						echo "<tr>";

						

						if ($_GET['action'] == 'edit')

							$checked = (in_array($row->ID, $chkBoxes) ? 'checked="checked"' : '');

							

						echo "

						<td align='center'><input type='checkbox' name='chkAddressBooks[]' value='{$row->ID}' $checked /></td>

						<td align='left'>";

						echo date("M j, Y", strtotime($row->LAST_UPDATE));

						echo "</td>

						<td align='left'>{$row->NAME}</td>

						<td align='right'>{$countContacts}</td>

						<td align='left'>{$row->DESCRIPTION}</td>

						</tr>

						";

						}

					}

					else

					echo "<tr><td colspan='5' align='center' height='30' valign='middle' class='row0'>No Record Found... </td></tr>";

					

					?>

                          </tbody>

                          </table>

                          <?

					  if ($overflow == 1) echo "</div>";

					  ?>

                        </td>

                      </tr>

                    </table>

                    <div style="height: 50px; border: 0px solid #000; vertical-align: middle; padding: 5px; text-align: right">

                      <table width="100%">

                      <tr>

                      <td class="pagingTD"><table cellspacing='0' cellpadding='0'  width='100%'>

                          <tr>

                            <td style="text-align:left" class="form_base_header"><span class="pagenav">

                              <?=$pageObj->ShowPageInfo()?>

                              </span></td>

                            <td style="text-align:right" class="form_base_header"><span class="pagenav">

                              <?=$pageObj->Show($link)?>

                              </span></td>

                          </tr>

                        </table></td>

                    </tr>

                    </table>

                    </div>

                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="inactiveTable" style="margin-top: 3px;">

                      <tr class="inactiveTab">

                        <td width="30" align="center"><img src="images/20.png" alt="" border="0" hspace="8" /></td>

                        <td width="90%"><span><strong>Campaign:</strong> summary for &quot;

                          <?=$campaignName?>

                        &quot;</span></td>

                        <td width="10%">&nbsp;</td>

                      </tr>

                    </table>

                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="inactiveTable" style="margin-top: 3px;">

                      <tr class="inactiveTab">

                        <td width="30" align="center"><img src="images/30.png" alt="" border="0" hspace="8" /></td>

                        <td width="90%"><span><strong>Campaign:</strong> live preview for &quot;

                          <?=$campaignName?>

                        &quot;</span></td>

                        <td width="10%">&nbsp;</td>

                      </tr>

                    </table>

                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="inactiveTable" style="margin-top: 3px;">

                      <tr class="inactiveTab">

                        <td width="30" align="center"><img src="images/40.png" alt="" hspace="8" border="0" /></td>

                        <td width="90%"><span><strong>Campaign:</strong> send</span></td>

                        <td width="10%">&nbsp;</td>

                      </tr>

                    </table>

                    <input type="hidden" name="id" value="<?php echo $_GET['id']?>" />

                    <input type="hidden" name="formaction" value="<?php echo $ACTION?>" />

                  </form></td>

              </tr>

            </table></td>

      </tr>

    </table></td>

  </tr>

<? require_once 'includes/inc.footer.php' ?>

</table>

</body>

</html>

