<?php
class ControllerLeaveMessage extends Controller
{
    public function index()
    {   	
    	 
        $customer =& $this->locator->get('customer');
        $language =& $this->locator->get('language');
        $module   =& $this->locator->get('module');
        $response =& $this->locator->get('response');
        $request  =& $this->locator->get('request');
        $template =& $this->locator->get('template');
        $url      =& $this->locator->get('url');
        $config   =& $this->locator->get('config');
        $database =& $this->locator->get('database');
        if ($request->isPost()) {
        	
            $name = $request->get('name', 'POST');
            $email = $request->get('email', 'POST');
            $telephone_country = $request->get('telephone_country', 'POST');
            $telephone_number = $request->get('telephone_number', 'POST');
            $with_order_number = $request->get('with_order_number', 'POST');
            $order_number = $request->get('order_number', 'POST');
            $comment = $request->get('comment', 'POST');
            $date = now();
            $reg  = '/[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$/';
             
            if (empty($name)) {
                $msg = 'The name is empty!';
            } elseif (empty($email)) {
                $msg = 'The email address is empty!';
            } elseif (!preg_match($reg, $email)) {
                $msg = 'The email address is invaild!';
            } elseif (empty($comment)) {
                $msg = 'The comment is empty!';
            } else {
                $database->query("INSERT INTO `leave_message` (`name`, `email`, `iso_code_2`, `telephone_number`, `order_number`, `message`, `date_added`) VALUES ('{$name}', '{$email}', '{$telephone_country}', '{$telephone_number}', '{$order_number}', '{$comment}'), '{$date}')");
                $msg  = "Leave us a message sucessfully, please be patient to wait our reply!";
            }
        } else {
            $msg  = "error";
        }
        // insert data to 'leave_message" table

        echo "<script>alert('" . $msg . "'); window.history.back(-1);</script>";
    }
}
