<?php
App::uses('CakeEmail', 'Network/Email');
class PagesController extends AppController {

	public function index($page_alias = null){
		if( is_null($page_alias) ){
			throw new NotFoundException('Такой страницы нет...');
		}
		$page = $this->Page->findByAlias($page_alias);
		if( !$page ){
			throw new NotFoundException('Такой страницы нет...');
		}
		$this->set(compact('page_alias', 'page'));
	}

	public function sendmail(){
		if( !empty($this->request->data) ){
			$email = new CakeEmail();
			// $email->config('default');
			$email->from(array('admin@cake.loc' => 'Administration of cake.loc'));
			$email->to('test@mail.com');
			$email->subject($this->request->data['Page']['subject']);
			$text = "E-mail: {$this->request->data['Page']['email']}/n";
			$text .= "Сообщение: {$this->request->data['Page']['text']}";
			if( $email->send($text) ){
				$this->Session->setFlash('Письмо успешно отправлено', 'default', array('class' => 'ok'));
				return $this->redirect($this->referer());
			}else{
				$this->Session->setFlash('Ошибка!', 'default', array('class' => 'error'));
				return $this->redirect($this->referer());
			}
		}
	}

}