<?php

$this->addScript('/admin/js/ckeditor/ckeditor.js');
$this->addScript('/admin/js/ckeditor/adapters/jquery.js');
$objPage = new Page($this->objLanguage);
$objForm = new Form($this->objUrl);
$objValidation = new Validation($this->objLanguage);
$expected = array('name', 'content', 'meta_title', 'meta_description', 'meta_keywords', 'identity');
$required = array('name', 'content', 'meta_title', 'meta_description', 'meta_keywords', 'identity');
if (isset($_POST['name'])) {
    $array = $objForm->post2Array($expected, 'content', array('identity' => 'sanitise'));
    if (array_key_exists('identity', $array) && !empty($array['identity']) && $objPage->duplicate($array['identity'])) {
        $objValidation->add2Errors('identity', 'identity_taken');
    }
    if ($objValidation->isValid($array, $required)) {
        if ($objPage->add($array)) {
            Helper::redirect($this->objUrl->getCurrent(array('a', 'id')) . '/a/index');
        }
    }
}
require_once 'header.php';
?>

<h1><?php 
echo $this->objLanguage->labels[9];
?>
</h1>

<form method="post">
	<table class="tbl_insert">
		<tr>
<?php

require_once '../inc/autoload.php';
if (isset($_POST['full_name'])) {
    $objForm = new Form();
    $objValidation = new Validation();
    $objValidation->wrap = true;
    $expected = array('full_name', 'email', 'enquiry');
    $required = array('full_name', 'email', 'enquiry');
    $array = $objForm->post2Array($expected);
    if ($objValidation->isValid($array, $required)) {
        $objEmail = new Email();
        if ($objEmail->send(1, array($array['email'] => $array['full_name']), array(EMAIL_CONTACT => 'SSD Tutorials'), $array)) {
            echo json_encode(array('error' => false, 'message' => 'successful'));
        } else {
            echo json_encode(array('error' => false, 'message' => 'unsuccessful'));
        }
    } else {
        echo json_encode(array('error' => true, 'validation' => $objValidation->errors));
    }
} else {
    echo json_encode(array('error' => true));
}