<?php

/**
 * User: wernerpe
 * Date: 19.01.2015
 * Time: 10:43
 */
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->libdir . '/formslib.php');

class contact_form extends moodleform {

    function definition() {

        global $CFG, $DB, $USER;
        $mform = & $this->_form;

        $mform->addElement('header', 'personaldetails', get_string('personaldetails', 'local_hsh'));

        $mform->addElement('text', 'firstname', get_string('firstname', 'local_hsh'), 'maxlength="254" size="34"');
        $mform->addRule('firstname', get_string('firstname_mis', 'local_hsh'), 'required', null, 'client');
        $mform->setType('firstname', PARAM_TEXT);

        $mform->addElement('text', 'lastname', get_string('lastname', 'local_hsh'), 'maxlength="254" size="34"');
        $mform->addRule('lastname', get_string('lastname_mis', 'local_hsh'), 'required', null, 'client');
        $mform->setType('lastname', PARAM_TEXT);

        $mform->addElement('text', 'email', get_string('email', 'local_hsh'), 'maxlength="254" size="34"');
        $mform->addRule('email', get_string('email_mis', 'local_hsh'), 'required', null, 'client');
        $mform->addRule('email', get_string('email_mis', 'local_hsh'), 'email', null, 'client');
        $mform->setType('email', PARAM_EMAIL);

        $mform->addElement('text', 'telephone', get_string('telephone', 'local_hsh'), 'maxlength="254" size="34"');

        $mform->setType('telephone', PARAM_TEXT);

        $mform->addElement('header', 'messageheader', get_string('message', 'local_hsh'));

        $mform->addElement('text', 'subject', get_string('subject', 'local_hsh'), ' style="width: 20em;"');
        $mform->setType('subject', PARAM_TEXT);
        $mform->addElement('textarea', 'message', get_string('message', 'local_hsh'), 'style="width: 20em;height: 20em"');
        $mform->addRule('message', get_string('message_mis', 'local_hsh'), 'required', null, 'client');

        if (isloggedin()) {
            $mform->setDefault('firstname', $USER->firstname);
            $mform->setDefault('lastname', $USER->lastname);
            $mform->setDefault('email', $USER->email);
            $mform->setDefault('telephone', $USER->phone1);
        }
//        if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) {
//			$mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'), 'style="margin: 10em"');
//
//        }

        $this->add_action_buttons(true, get_string('submit', 'local_hsh'));
    }

    function validation($data, $files) {
        global $CFG;
        if(empty($data["lastname"])){
            $errors['lastname'] = get_string('missinglastname');
        }
        if(empty($data["firstname"])){
            $errors['firstname'] = get_string('missingfirstname');            
        }  
        if(empty($data["message"])){
            $errors['message'] = get_string('missingdescription');           
        }
        if (! validate_email($data['email'])) {
            $errors['email'] = get_string('invalidemail');
        }
//        if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) {
//                $recaptcha_element = $this->_form->getElement('recaptcha_element');
//                if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
//                        $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
//                        $response_field = $this->_form->_submitValues['recaptcha_response_field'];
//                        if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
//                                $errors['recaptcha_element'] = $result;
//                        }
//                } else {
//                        $errors['recaptcha_element'] = get_string('missingrecaptchachallengefield');
//                }
//        }
        return $errors;
    }

}