<?php 
class ControllerAccountMail extends Controller {
	public function index() {
	
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
	
		$subject = $this->request->post['subject'];
		if($this->request->post['customer']==''){
			$message = $this->request->post['reporter'] . ' (Designer #'. $this->request->post['reporter_id'] .') has reported the design #'. $this->request->post['sketchid'] .' with following message: <br> ---------------<br>';
		}else{
			$message = $this->customer->getFirstName() . ' ' . $this->customer->getLastName() . ' (Customer #'. $this->request->post['customer']  .') has reported the design #'. $this->request->post['did'] .' with following message: <br> ---------------<br>';
        }
		$message = $message . $this->request->post['body']  ;
        $message = $message . '<br>---------------';
		
		$mail = new Mail();
		$mail->protocol = $this->config->get('config_mail_protocol');
		$mail->parameter = $this->config->get('config_mail_parameter');
		$mail->hostname = $this->config->get('config_smtp_host');
		$mail->username = $this->config->get('config_smtp_username');
		$mail->password = $this->config->get('config_smtp_password');
		$mail->port = $this->config->get('config_smtp_port');
		$mail->timeout = $this->config->get('config_smtp_timeout');				
		$mail->setTo($this->config->get('config_email'));
		$mail->setFrom($this->config->get('config_email'));
  		$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  		$mail->setHtml((html_entity_decode($message, ENT_QUOTES, 'UTF-8')));
		$mail->setSender($this->config->get('config_name'));
		$mail->setSubject($subject);
		$mail->setText($message);
		$mail->send();
				
		$this->db->query("UPDATE contest_sketch_assignment SET reported = 'y'  WHERE sketch_assignment_id = '" . $this->request->post['sketch_id'] . "'");

		
		$this->redirect($this->url->link('account/mail', '', 'SSL'));
		}
		
		$this->data['action'] = $this->url->link('account/mail', '', 'SSL');
		if(isset($this->request->get['did']))$this->data['did'] = $this->request->get['did'];
		if(isset($this->request->get['dname']))$this->data['dname'] = $this->request->get['dname'];
		if(isset($this->request->get['sketch']))$this->data['sketch_id'] = $this->request->get['sketch'];
		
		$this->data['customer'] = '';
		$this->data['reporter'] = '';
		$this->data['sketchid'] = '';
		$this->data['reporter_id'] = '';
		
		if(isset($this->request->get['customer']))$this->data['customer'] = $this->request->get['customer'];
		
		if(isset($this->request->get['reporter']))$this->data['reporter'] = $this->request->get['reporter'];
		if(isset($this->request->get['sketchid']))$this->data['sketchid'] = $this->request->get['sketchid'];
		if(isset($this->request->get['reporter_id']))$this->data['reporter_id'] = $this->request->get['reporter_id'];
	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/mail.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/mail.tpl';
		} else {
			$this->template = 'default/template/common/mail.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
						
		$this->response->setOutput($this->render());	
  	}
}
?>