<?php

if (!isset($PathPrefix)) {
	$PathPrefix='../';
}

include($PathPrefix . 'includes/session.php');
$Title=_('Preset Message Maintenance Page');
include('includes/header.php');

$page_limit = 10;
$page_list = array();
$user = $_SESSION['UserID'];
$sql = "SELECT id, url, label, message FROM lms_page WHERE url <> '' AND userid='".$user."' and active = 1 and approved = 1";
$result = DB_query($sql);
while($myrow = DB_fetch_array($result))
{
	$page_list[$myrow['id']] = array("url" => $myrow['url'], "label" => $myrow['label'], "message" => $myrow['message']);
}

if(isset($_POST['request_add']))
{
	for($i=1; $i <= ($page_limit - count($page_list)); $i++)
	{
		$id = $_POST['id'][$i];

		if(!empty($_POST['url'][$i]) && !empty($_POST['label'][$i]))
		{
			$url = htmlspecialchars_decode($_POST['url'][$i], ENT_QUOTES);
		
			$url = filter_var($url, FILTER_SANITIZE_URL);
			if(filter_var($url, FILTER_VALIDATE_URL))
			{
				$contact_card_url = getNameCardLink($user);
				if($url == $contact_card_url)
				{
					prnMsg('You can only have one contact card URL link. Link will not be processed.','warn');
				}
				else
				{
					$label = htmlspecialchars_decode($_POST['label'][$i], ENT_QUOTES);
					$message = htmlspecialchars_decode($_POST['message'][$i], ENT_QUOTES);

					$sql = "SELECT id FROM lms_page WHERE url = '".DB_escape_string($url)."' AND userid = '".$user."'";
					$result = DB_query($sql);

					if (DB_num_rows($result) > 0)
					{
						$myrow = DB_fetch_array($result);
						$page_id = $myrow['id'];
					
						$sql = "UPDATE lms_page SET label='".DB_escape_string($label)."',
													message='".DB_escape_string($message)."',
													active = 0,
													approved = 0
								WHERE id = '".$page_id."'";
						$result = DB_query($sql);
					}
					else
					{
						$sql = "INSERT INTO lms_page (url, label, message, userid, active, approved)
								VALUES ('".DB_escape_string($url)."', '".DB_escape_string($label)."', '".DB_escape_string($message)."', '".$user."', 0, 0)";
						$result = DB_query($sql);
					}

					// $sql = "INSERT INTO lms_page (url, label, message, userid, active, approved)
					// 		VALUES ('".DB_escape_string($url)."', '".DB_escape_string($label)."', '".DB_escape_string($message)."', '".$user."', 0, 0)
					// 		ON DUPLICATE KEY UPDATE label='".DB_escape_string($label)."', message='".DB_escape_string($message)."', active = 0, approved = 0";
					// $result = DB_query($sql);
				}
			}
			else
			{
				prnMsg('Invalid URL: ' . $url,'error');
			}
		}
		else if(!empty($id))
		{
			$sql = "UPDATE lms_page SET active = 0, approved = -1 WHERE id = '".$id."'";
			$result = DB_query($sql);
		}
	}

	prnMsg('Your request has been successfully updated. Kindly wait for the evaluation and review process to complete. Thank you!','success');
}

$sql = "SELECT id, url, label, message FROM lms_page WHERE url <> '' AND userid='".$user."' and approved = 0";
$result = DB_query($sql);
while($myrow = DB_fetch_array($result))
{
	$approval_list[$myrow['id']] = array("url" => $myrow['url'], "label" => $myrow['label'], "message" => $myrow['message']);
}

?>

<script>
function setRequired(field, i)
{
	if(field.value=='')
	{
		$("[name='label[" + i + "]']").attr("required", false);
	}
	else
	{
		$("[name='label[" + i + "]']").attr("required", true);
	}
	
}	
</script>

<div class="row">
	<div class="col-sm-4"></div>
	<div class="col-sm-4">
		<h2>Request to Add Link(s)</h2>
		<br>
	</div>
	<div class="col-sm-4"></div>
</div>

<div class="row">
	<div class="col-sm-12">
		<div class="well well-sm">
			<h4>Note:</h4>
			<ol>
				<li>A processing fee will be charged for each request submitted</li>
				<li>Submitted URLs will be subjected to evaluation and review for approval</li>
				<li>You can only have up to a maximum of <?php echo $page_limit; ?> active links at any one time</li>
			</ol>
		</div>
	</div>
</div>

<?php

if(count($page_list) < $page_limit || count($approval_list) > 0)
{
?>

<form method="post" onsubmit="return confirm('Are you sure you wish to submit the request?');">
	<table class="table">
		<thead>
			<tr>
				<th>#</th>
				<th>Label</th>
				<th>Message</th>
				<th>URL</th>
			</tr>
		</thead>
		<tbody>
		
<?php

$i = 1;
$page_count = count($page_list);

foreach($approval_list as $id => $page)
{
	echo '<tr>
			<td><input type="hidden" name="id['.$i.']" value="'.$id.'" />'.$i.'</td>
			<td><input type="text" class="form-control" name="label['.$i.']" maxlength="255" placeholder="Label" value="'.htmlspecialchars($page['label'], ENT_QUOTES).'"></td>
			<td><input type="text" class="form-control" name="message['.$i.']" placeholder="Message" value="'.htmlspecialchars($page['message'], ENT_QUOTES).'"></td>
			<td><input onchange="setRequired(this, '.$i.')" type="text" class="form-control" name="url['.$i.']" maxlength="500" placeholder="URL" value="'.$page['url'].'"></td>
		</tr>';
	
	$i++;
	$page_count++;
}

while($page_count < $page_limit)
{
	echo '<tr>
			<td><input type="hidden" name="id['.$i.']" value="" />'.$i.'</td>
			<td><input type="text" class="form-control" name="label['.$i.']" maxlength="255" placeholder="Label" value=""></td>
			<td><input type="text" class="form-control" name="message['.$i.']" placeholder="Message" value=""></td>
			<td><input onchange="setRequired(this, '.$i.')" type="text" class="form-control" name="url['.$i.']" maxlength="500" placeholder="URL" value=""></td>
		</tr>';

	$i++;
	$page_count++;
}

echo '</tbody>
</table>
<input type="hidden" name="FormID" value="'.$_SESSION['FormID'].'" />
<input type="hidden" name="user" value="'.$user.'" />
<button type="submit" name="request_add" class="btn btn-danger">Submit Request <span class="glyphicon glyphicon-send"></span></button>
</form>';
}
else
{
	echo '<div class="alert alert-warning">
			<strong>Oops!</strong> You do not have any additional slots for new links. Remove active links to free up more slots.
		</div>';
}

include('includes/footer.php');
?>