<?php
chdir('..');
require_once('view/View.php');
$view = new View();
$comment = new stdClass();

$comment->approved = 0;
$comment->relation = 'pages';
$comment->name = $view->request->post('name', 'string');
$comment->email = $view->request->post('email');
$comment->text = $view->request->post('message');
$comment->relation_id = $view->request->post('relation_id', 'integer');


$result = new stdClass;
$result->text = null;
$result->type = 'error';

// Проверяем обязательные поля
if(empty($comment->name) || empty($comment->text))
    $result->text = "required_fields";

// Recaptcha
if($view->custom->is_bot())
    $result->text = "captcha_failed";

if(!$result->text)
{
    // Выводим сообщение об успехе
    $result->type = 'success';

    // Добавляем комментарий в базу
    $comment_id = $view->comments->add_comment($comment);
    $view->notify->email_comment_admin($comment_id);
}

$view->request->set_header('json');
print json_encode($result);
