<?php

class ProgramistaIt_Appointment_Request_Widget extends WP_Widget {

    protected $widget_id = 'appointment_request_widget';
    // attributes with default values
    protected $attributes = array('title' => '', 'email' => '');
    protected $labels = array('title' => 'Title', 'email' => 'Your Email');

    // constructor
    function ProgramistaIt_Appointment_Request_Widget() {
        $this->attributes['email'] = get_settings('admin_email');
        $options = array('classname' => $this->widget_id, 'description' => 'Show appointment form');
        $this->WP_Widget($this->widget_id, 'Appointment Request Widget', $options);
        add_action('admin_head', array($this, 'addToAdminHead'));
        add_action('wp_head', array($this, 'addToWpHead'));
        add_action('wp_footer', array($this, 'addToWpFooter'));
        add_action('init', array($this, 'wpInit'));
    }

    // add jquery to the site
    function wpInit() {
        // add jquery
        wp_enqueue_script('jquery');
        if (isset($_GET['appointment_request_widget_footer_iframe'])) {
            $emailStatus = -1;
            require dirname(__FILE__) . '/recaptcha/loader.php';
            $recaptcha = new Recaptcha();
            if (isset($_POST['Form'])) {
                $captcha_status = $recaptcha->isValid();
                $emailStatus = $this->sendEmail($captcha_status);
            }
            require dirname(__FILE__) . '/tpl/appointment.html.php';
            exit;
        }
    }

    function sendEmail($captcha_status) {
        if ($captcha_status) {
            $msg = '';
            $_POST['Form']['Comments'] = mb_substr($_POST['Form']['Comments'], 0, 250);
            foreach ($_POST['Form'] as $key => $val) {
                $msg .= '<p><b>' . $key . '</b>: ' . $val . '</p>';
            }

            $settings = $this->get_settings();
            $email = $settings[$this->number]['email'];

            add_filter('wp_mail_content_type', create_function('', 'return "text/html";'));
            return wp_mail($email, 'Appointment Request', $msg) ? 1 : 0;
        }
        return -1;
    }

    // widget content
    function widget($args, $instance) {
        extract($args, EXTR_SKIP);
        $params = array();
        foreach ($this->attributes as $key => $chunk) {
            $params[$key] = apply_filters('widget_' . $key, trim($instance[$key]));
        }
        require dirname(__FILE__) . '/tpl/widget.html.php';
    }

    // update widget options
    function update($new_instance, $instance) {
        foreach ($this->attributes as $key => $value) {
            $instance[$key] = trim(strip_tags($new_instance[$key]));
        }
        return $instance;
    }

    // widget admin configuration
    function form($instance) {
        $instance = wp_parse_args((array) $instance, $this->attributes);
        require dirname(__FILE__) . '/tpl/form.html.php';
    }

    // add code to admin head
    function addToAdminHead() {
        require dirname(__FILE__) . '/tpl/admin-head.html.php';
    }

    function addToWpFooter() {
        require dirname(__FILE__) . '/tpl/wp-footer.html.php';
    }

    // add code to wp head
    function addToWpHead() {
        require dirname(__FILE__) . '/tpl/wp-head.html.php';
    }

}

register_widget('ProgramistaIt_Appointment_Request_Widget');
