<?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 ORDER BY label";
$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['update_list']))
{
	$page_count = count($page_list);
	for($i=1; $i <= $page_count; $i++)
	{
		if(isset($_POST['delete'][$i])) // remove url
		{
			// de-activate old url 
			unset($page_list[$_POST['id'][$i]]);
			$sql = "UPDATE lms_page SET active = 0 WHERE id = '".$_POST['id'][$i]."'";
			$result = DB_query($sql);

			prnMsg('Preset URL: ' . $url . ' has been removed successfully','success');
		}
		else if(!empty($_POST['label'][$i]))
		{

			$url = htmlspecialchars_decode($_POST['url'][$i], ENT_QUOTES);
			$label = htmlspecialchars_decode($_POST['label'][$i], ENT_QUOTES);
			$message = htmlspecialchars_decode($_POST['message'][$i], ENT_QUOTES);

			$id = $_POST['id'][$i];

			if(!empty($id)) // possible edit made
			{
				if($label != $page_list[$id]['label'] || $message != $page_list[$id]['message']) // editted label and/or message
				{
					$sql = 'UPDATE lms_page SET label = "'.DB_escape_string($label).'", message = "'.DB_escape_string($message).'" WHERE id = "'.$id.'"';
					$result = DB_query($sql);

					$page_list[$id]['label'] = $label;
					$page_list[$id]['message'] = $message;

					prnMsg('Preset Message and/or Label for URL: ' . $url . ' has been updated successfully','success');
				}
			}
		}
	}
}

?>

<div class="row">
	<div class="col-sm-4"></div>
	<div class="col-sm-4">
		<h2>Link Settings</h2>
		<br>
	</div>
	<div class="col-sm-4"></div>
</div>

<?php

if(isset($page_list))
{
	echo '<form method="post">
			<table class="table">
				<thead>
					<tr>
						<th>#</th>
						<th>Label</th>
						<th>Message</th>
						<th>URL</th>
						<th>Preview</th>
						<th>Remove</th>
					</tr>
				</thead>
				<tbody>';

	$page_count = 0;
	$contact_card_url = getNameCardLink($user);
	foreach($page_list as $id => $page)
	{
		$page_count++;

		if($page['url'] == $contact_card_url) // omit contact card url
		{
			echo '<tr class="info">
					<td><input type="hidden" name="id['.$page_count.']" value="'.$id.'" />'.$page_count.'</td>
					<td><input type="text" class="form-control" name="label['.$page_count.']" maxlength="255" placeholder="Label" value="'.htmlspecialchars($page['label'], ENT_QUOTES).'" required></td>
					<td><input type="text" class="form-control" name="message['.$page_count.']" placeholder="Message" value="'.htmlspecialchars($page['message'], ENT_QUOTES).'"></td>
					<td><input type="text" class="form-control" name="url['.$page_count.']" maxlength="500" placeholder="URL" value="'.$page['url'].'" readonly></td>
					<td><a href="'.$page['url'].'" target="_blank"><span class="glyphicon glyphicon-share"></span></a></td>
					<td></td>
				</tr>';
		}
		else
		{
			echo '<tr>
					<td><input type="hidden" name="id['.$page_count.']" value="'.$id.'" />'.$page_count.'</td>
					<td><input type="text" class="form-control" name="label['.$page_count.']" maxlength="255" placeholder="Label" value="'.htmlspecialchars($page['label'], ENT_QUOTES).'" required></td>
					<td><input type="text" class="form-control" name="message['.$page_count.']" placeholder="Message" value="'.htmlspecialchars($page['message'], ENT_QUOTES).'"></td>
					<td><input type="text" class="form-control" name="url['.$page_count.']" maxlength="500" placeholder="URL" value="'.$page['url'].'" readonly></td>
					<td><a href="'.$page['url'].'" target="_blank"><span class="glyphicon glyphicon-share"></span></a></td>
					<td>
						<label><input name="delete['.$page_count.']" type="checkbox"></label>
					</td>
				</tr>';
		}
	}

	echo '</tbody>
		</table>
		<input type="hidden" name="FormID" value="'.$_SESSION['FormID'].'" />
		<input type="hidden" name="user" value="'.$user.'" />
		<button type="submit" name="update_list" class="btn btn-info">Save Changes <span class="glyphicon glyphicon-save"></span></button>
		<a href="lms_AddLinkRequest.php" class="btn btn-primary">Add Link <span class="glyphicon glyphicon-plus"></span></a>
	</form>';
}

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