<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Ajax extends \Ciext\Controller\AbstractJsonController {

    public function add()
    {
        $data = [];
        $this->load->model('rtform/rtform_tmodel');
        $this->load->library('form_hash');
        $form = $this->rtform_tmodel->fetch((int) $this->input->post('rtform_id'));

        if (!empty($form)) {
            $form_class = $this->load->library($form['form_class']);
            if ($form_class->is_valid() && $this->form_hash->check_form_hash()) {
                $this->load->model('rtform/rtform_request_tmodel');
                $this->rtform_request_tmodel->add_data($form['id_rtform'], $form_class->get_value());
                $data['success'] = $form['success_message'];
            } else {
                $data['error'] = $this->form_validation->error_string();
                if (!$this->form_hash->check_form_hash()) {
                    $data['error_message'] .= '<span class="error">CSRF</span>';
                }
            }
        }

        $data['hash'] = $this->form_hash->set_form_hash();

        return $this->response($data);
    }

}
