<?php

/**
 * Snippet to allow submission of comments.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS\module\comments
 * @copyright Copyright (c) 2013 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/* @var $tplEngine cfTemplateEngine */
$controller = $tplEngine->controller;

$module = isset($module) ? $module : $controller->pageData['module'];
$id = isset($id) ? $id : $controller->pageData['contentid'];
$ratings = isset($ratings) && $ratings;
$redirectTo = isset($redirectTo) ? $redirectTo : 0;
$redirectUri = isset($redirectUri) ? $redirectUri : null;
$verification = isset($verification) ? $verification : 0;
$prefix = isset($prefix) ? $prefix : '';

$form = new flForm(clearFusionCMS::getRequest(), $prefix . 'submitcomment', $controller->getRequest()->getSelf(), true);

$name = new flFormElementText($form, $prefix . 'cmntname');
$name->setLabel(__('Your Name', 'comments'))
	->setRequired()
	->addValidator(new flFormValidatorRequired())
	->addValidator(new flFormValidatorMaxLength(255));

$email = new flFormElementEmail($form, $prefix . 'cmntemail');
$email->setLabel(__('Email', 'comments'))
	->setRequired()
	->addValidator(new flFormValidatorRequired())
	->addValidator(new flFormValidatorMaxLength(255));

if($ratings) {
	$rating = new flFormElementSelect($form, $prefix . 'cmntrating', 3, array(
		1 => __('1 - Worst', 'comments'),
		2 => __('2', 'comments'),
		3 => __('3', 'comments'),
		4 => __('4', 'comments'),
		5 => __('5 - Best', 'comments')
	));
	$rating->setLabel(__('Rating', 'comments'));
}

$ctnt = new flFormElementTextArea($form, $prefix . 'cmntcontent');
$ctnt->setLabel(__('Comment', 'comments'))
	->setRequired()
	->addValidator(new flFormValidatorRequired())
	->addValidator(new flFormValidatorMaxLength(2097152))
	->setRows(10);

if($verification) {
	$ns = clearFusionCMS::getSession()->getNamespace('cfComment.Verify');
	$v1 = $ns->get('v1', intval(rand(1, 5)));
	$v2 = $ns->get('v2', intval(rand(1, 5)));
	$ns->v1 = $v1;
	$ns->v2 = $v2;

	$number = new flFormElementInteger($form, $prefix . 'cmntverify');
	$number->setLabel(sprintf(__('What is %d + %d?', 'comments'), $v1, $v2))
		->setRequired()
		->addValidator(new flFormValidatorRequired(__('Please add the two numbers and enter the answer.', 'comments')))
		->addValidator(new flFormValidatorEquals($v1 + $v2, __('Please add the two numbers and enter the answer.', 'comments')));
}

$submit = new flFormElementSubmit($form, $prefix . 'cmntsubmit');
$submit->setCaption(__('Submit', 'comments'))->setCSS('primary');

$showForm = true;
if($form->isSubmitted() && $form->isValid()) {
	// Create comment
	$now = new flDate();
	if(clearFusionCMS::getDb()->insert('cf_comments_' . clearFusionCMS::getSite(), $d = array(
			'commentdate' => $now->toString(),
			'rating' => $ratings ? $rating->getValue() : 3,
			'published' => 0,
			'contentid' => $id,
			'modulename' => $module,
			'author' => $name->getValue(),
			'email' => $email->getValue(),
			'content' => $ctnt->getCleanedValue()
		))) {

		if($verification)
			unset($ns->v1, $ns->v2);

		$cfg = clearFusionCMS::getSiteConfig('cfComments');
		if(!empty($cfg->notifyEmail)) {
			$mailer = clearFusionCMS::getMailer();
			$mailer->addRecipient($cfg->notifyEmail)
				->setSubject(sprintf(__('New comment submitted on %s', 'comments'), clearFusionCMS::getSiteName()))
				->setTextBody(__("A new comment has been submitted to your website.\n\nYou will need to login to your site manager to review and publish it.\n\n", 'comments'));

			if($mailer->connect()) {
				$mailer->send();
				$mailer->close();
			}
		}

		$event = new cfEventNotify(
			clearFusionCMS::getSiteConfig('commentsSystemNotify')->get('sub', 3),
			__('New Comment Submitted', 'comments'),
			sprintf(__('A new comment has been submitted by %s', 'comments'), $d['author'])
		);
		$event->mention(clearFusionCMS::getSiteConfig('commentsSystemNotify')->get('mentions', ''))
			->addField(__('URL', 'comments'), $tplEngine->controller->getRequest()->getSelf(true))
			->addField(__('Email', 'comments'), $d['email'])
			->addField(__('Rating', 'comments'), $d['rating'])
			->addField(__('Comment', 'comments'), $d['content'])
			->trigger();

		$showForm = false;
		if($redirectTo)
			clearFusionCMS::getResponse()->redirectTo($controller->content->getPageURI($redirectTo, true));
		else if($redirectUri !== null)
			clearFusionCMS::getResponse()->redirectTo($redirectUri);
		else
			echo __('Thank you for your submission, once it has been approved it will appear on the website.', 'comments');
	}
}

if($showForm)
	echo $form->render($tplEngine->getCodeBuffer());
