<?php

	if (!isset($gCms)) exit;

	if (isset($params['cancelcomment']))
	{
		$this->RedirectContent($returnid);
	}

	$errormsg = "";

	$author = "";
	if (isset($params["author"]))
	{
		$author = $params["author"];
	}
	$content = "";
	if (isset($params["content"]))
	{
		$content = $params["content"];
	}

	$validinfo = true;

	if (isset($params['submitcomment']))
	{
		if ($author == "")
		{
			$validinfo = false;
			$errormsg .= "<li>".$this->Lang('errorenterauthor')."</li>";
		}
	
		if ($content == "")
		{
			$validinfo = false;
			$errormsg .= "<li>".$this->Lang('errorentercomment')."</li>";
		}

		if ($validinfo)
		{
			$db = $this->cms->db;
			$new_id = $db->GenID(cms_db_prefix()."module_comments_seq");
			$query = "INSERT INTO ".cms_db_prefix()."module_comments (comment_id, page_id, comment_author, comment_data, comment_date, create_date) VALUES ($new_id, $returnid, ".$db->qstr($author).", ".$db->qstr($content).",'".$db->DBTimeStamp(time())."','".$db->DBTimeStamp(time())."')";
			$dbresult = $db->Execute($query);
			
			if(isset($params['notify'])){
				$cmsmailer = $this->GetModuleInstance('CMSMailer');
				if( $cmsmailer ) {
					$pageaddr = $gCms->config['root_url'] . "/index.php?page=" . $returnid;
					$notifysubj=$this->Lang('notifysubj', array($pageaddr));
					$notifymsg= $this->Lang('notifymsg',  array($pageaddr));
					$cmsmailer->AddAddress($params['notify']);
					$cmsmailer->SetSubject($notifysubj);
					$cmsmailer->SetBody($notifymsg);  
					$cmsmailer->IsHTML(false); // we're not sending an html mail
					$cmsmailer->Send();
				}
			}
			$this->RedirectContent($returnid);
		}
	}

	if (strlen($errormsg) > 0)
	{
		echo "<p>".$this->Lang('error').":</p><ul>".$errormsg."</ul>";
	}

	echo $this->CreateFormStart($id, 'addcomment', $returnid);
	
    ?>
    <h3>Add Comment</h3>

    <?php echo $this->CreateInputHidden($id, 'notify', $params['notify'])?>
    <table>
        <tr>
            <td><?php echo $this->Lang('yourname')?>:</td>
            <td><?php echo $this->CreateInputText($id, 'author', $author)?></td>
        </tr>
        <tr>
            <td><?php echo $this->Lang('comment')?>:</td>
            <td><?php echo $this->CreateTextArea(false, $id, $content, 'content', $id)?></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <?php echo $this->CreateInputSubmit($id, 'submitcomment', $this->Lang('submit')) ?>
                <?php echo $this->CreateInputSubmit($id, 'cancelcomment', $this->Lang('cancel')) ?>
            </td>
        </tr>
    </table>
    <?php

    echo $this->CreateFormEnd();


# vim:ts=4 sw=4 noet
?>
