<?php
require_once('ajax_auth.inc.php');
$db = Database::getDatabase();

/* get vars */
$params			= json_decode($_REQUEST['value']);
$banned_term 	= trim(strtolower($params->group1->banned_term));
$notes	 		= $params->group1->notes;

$response = array();
$response['content'] 	= "";
$response['javascript']	= "";
$response['errors']		= array();
$response['success']	= 1;

/* validate submission */
$db = Database::getDatabase(true);
$row = $db->getRow('SELECT id FROM banned_words WHERE bannedTerm = '.$db->quote($banned_term));
if(is_array($row))
{
	$response['errors']['banned_term'] = array(t("banned_word_already_in_list"));
}

/* insert/update db */
if(COUNT($response['errors']) == 0)
{
	/* create the intial record */
	$dbInsert = new DBObject("banned_words", array("bannedTerm", "banNotes", "dateBanned"));
	$dbInsert->bannedTerm 		= $banned_term;
	$dbInsert->banNotes 		= $notes;
	$dbInsert->dateBanned	 	= sqlDateTime();
	if(!$dbInsert->insert())
	{
		$response['errors']['banned_term'] = array(t("error_problem_record"));
	}
}

if(COUNT($response['errors']) > 0)
{
	$response['success'] = 0;
}

echo json_encode($response);
?>