<?php
class controller_contact extends controller
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_index()
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/contact.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('contact');
                return;
            }
            
            $to = setting::get('email', 'site');
#            $to = $to['value'];
            $email = new email();
            $email->to($to)
                  ->from($form->email)
                  ->subject('Our Website Contact Form')
                  ->body("Name: " . $form->name . "<br><br>" .
                         "Phone: " . $form->phone . "<br><br>" .
                         "Street: " . $form->street . "<br><br>" .
                         "State: " . $form->state . "<br><br>" .
                         "Zipcode: " . $form->zipcode . "<br><br>" .
                         "Email: " . $form->email . "<br><br>" .
                         "Message: " . str_replace("\n", '<br>', $form->message))
                  ->send();
            
            $this->session->flash('sent', true);
            $this->redirect('contact');
        }
        else
        {
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/contact.php');
            }
            
            $data['form'] = $form;
            $data['sent'] = isset($this->session->sent);
            unset($this->session->sent);
            $data['type'] = 'contact';
            $data['type_id'] = '1';
            $this->render('index.html', $data);
        }
    }
}
