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

	public function admin_index(){
		$pages = $this->Page->find('all',
			array('fields' => array('id', 'title'))
		);
		$this->set(compact('pages'));
	}

	public function admin_add(){
		if($this->request->is('post')){
			$this->Page->create();
			if($this->Page->save($this->request->data)){
				$this->Session->setFlash('Страница добавлена', 'default', array('class' => 'ok'));
				return $this->redirect($this->referer());
			}else{
				$this->Session->setFlash('Ошибка!', 'default', array('class' => 'error'));
			}
		}
	}

	public function admin_edit($page_id = null){
		if( is_null($page_id) || !(int)$page_id ){
			throw new NotFoundException('Такой странцы нет...');
		}
		$page = $this->Page->findById($page_id);
		if( !$page ){
			throw new NotFoundException('Такой странцы нет...');	
		}
		
		if($this->request->is(array('post', 'put'))){
			$this->Page->id = $page_id;
			if($this->Page->save($this->request->data)){
				$this->Session->setFlash('Сохранено', 'default', array('class' => 'ok'));
				return $this->redirect($this->referer());
			}else{
				$this->Session->setFlash('Ошибка!', 'default', array('class' => 'error'));
			}
		}

		if(!$this->request->data){
			$this->request->data = $page;
		}
		$this->set(compact('page'));
	}

	public function admin_delete($id){
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
		if($this->Page->delete($id)){
			$this->Session->setFlash('Страница удалена', 'default', array('class' => 'ok'));
		}else{
			$this->Session->setFlash('Ошибка', 'default', array('class' => 'error'));
		}
		return $this->redirect($this->referer());
	}

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

	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());
			}
		}
	}

}