<?php
require_once('_header.inc.php');

/* do any deletions */
if($r = (int)$_REQUEST['r'])
{
	$db->query("DELETE FROM banned_words WHERE id = ".$r." LIMIT 1");
}

?>
	
<p><?php echo t("banned_terms_intro"); ?></p>

<div id="cellediting" class="yuiTable"></div>

<ul class="navLink" style="padding-top: 10px;">
	<li><a href="#" onClick="displayBannedWordsPopup(); return false;"><?php echo t("add_banned_term"); ?></a></li>
</ul>

<?php
/* load in config options from db */
$rows = $db->getRows("SELECT * FROM banned_words ORDER BY bannedTerm");
if(COUNT($rows) > 0)
{
?>
	<script>
	mfScripts.bannedWordsTable = {};

	mfScripts.bannedWordsTable.Data = {
		configData: [
			<?php
				$formattedRow = array();
				foreach($rows AS $row)
				{
					$banNotes = $row['banNotes']?$row['banNotes']:"-";
					$banNotes = str_replace(array("\n", "\r"), "<br/>", $banNotes);
					$formattedRow[] = "{id:\"".$row['id']."\", banned_term:\"".addslashes($row['bannedTerm'])."\", date_banned:\"".dater($row['dateBanned'])."\", ban_notes:\"".addslashes($banNotes)."\", ban_action:\"<a href='#' onClick='return removeBannedWord(".$row['id'].");'>".t("remove")."</a>\"}";
				}
				echo implode(",", $formattedRow);
			?>
		]
	};

	YAHOO.util.Event.addListener(window, "load", function()
	{
		mfScripts.bannedWordsTable.InlineCellEditing = function()
		{
			var myColumnDefs = [
				{key:"id", hidden:true},
				{key:"banned_term", label:t("banned_term"), sortable:true},
				{key:"date_banned", label:t("date_banned"), sortable:true},
				{key:"ban_notes", label:t("ban_notes"), sortable:true},
				{key:"ban_action", label:t("action"), sortable:false}
			];

			var myDataSource = new YAHOO.util.DataSource(mfScripts.bannedWordsTable.Data.configData);
			myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
			myDataSource.responseSchema = {
				fields: ["id","banned_term","date_banned","ban_notes","ban_action"]
			};

			var myDataTable = new YAHOO.widget.DataTable("cellediting", myColumnDefs, myDataSource, {selectionMode:"single"});

			myDataTable.subscribe("rowMouseoverEvent", myDataTable.onEventHighlightRow);
			myDataTable.subscribe("rowMouseoutEvent", myDataTable.onEventUnhighlightRow);
			myDataTable.subscribe("rowClickEvent", myDataTable.onEventSelectRow);
			
			return {
				oDS: myDataSource,
				oDT: myDataTable
			};
		}();
		
	});
	</script>
<?php
}
?>
	<script>
	/* popup widget */
	YAHOO.util.Event.addListener(window, "load", function(){
		mfScripts.newBannedWordPopup = new YAHOO.widget.Dialog("newBannedWordPopup", 
			{
				width : "400px",
				modal : true,
				zindex : 4,
				fixedcenter : true,
				visible : false, 
				constraintoviewport : true
			});

		mfScripts.newBannedWordPopup.render(document.body);

		new YAHOO.inputEx.Form({
			parentEl: 'myFormContainer',
			fields: [
					{type:'group',inputParams:
							{legend:'Banned Word Details', name:'group1', className:'inputEx-Group-Custom', fields:
								[
									{inputParams: {label: t("banned_term"), name: 'banned_term', required: true } }, 
									{type: 'text', inputParams: {label: t("ban_notes"), name: 'notes' } }
								]
							}
					}
					],
			buttons: [{type: 'submit', value: 'Add Banned Word'}],
			ajax: {
				method: 'POST',
				uri: 'ajax/addNewBannedWord.ajax.php',
				callback: {
					success: function(o)
					{
						var responseObj = YAHOO.lang.JSON.parse(o.responseText);
						if(responseObj.success == 0)
						{
							/* error */
							var errorStr = "Error:\n";
							for(e in responseObj.errors)
							{
								errorStr += responseObj.errors[e]+"\n";
							}
							alert(errorStr);
						}
						else
						{
							/* success */
							window.location = "banned_words.php";
						}
					},
					failure: function(o)
					{
						alert(t("error_submitting_form"));
					}
				},
				showMask: true
			}
		});
	});
		 
	function displayBannedWordsPopup()
	{
		mfScripts.newBannedWordPopup.show();
	}
	</script>

<?php
require_once('_footer.inc.php');
?>

<div id="newBannedWordPopup" class="yui-pe-content">
	<div class="hd"><?php echo t("enter_banned_term_details"); ?></div>
	<div class="bd">
		<div class='demoContainer' id='myFormContainer'></div>
	</div>
</div>