<?php

class PostsController extends AppController {

	var $helpers = array ('Html','Form');
	var $name = 'Posts';

	function index() {
		$this->paginate['Post'] = array(
			'contain' => array('Person'));
		$this->set('posts', $this->paginate('Post'));
	}

	function view($id = null) {
		$this->Post->id = $id;
		$this->set('post', $this->Post->read());
	}
	
	function add() {
		if (!empty($this->data)) {
			# add the user id of the logged in user
            $person = $this->Session->read('Auth.Person');
            $this->data['Post']['person_id'] = $person['id'];
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash('Your post has been saved.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('Unable to add your post.');
			}
		}
	}
	
	function delete($id) {
		if ($this->Post->delete($id)) {
			$this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
			$this->redirect(array('action' => 'index'));
		}
	}
	
	function edit($id = null) {
		$this->Post->id = $id;
		if (empty($this->data)) {
			$this->data = $this->Post->read();
		} else {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash('Your post has been updated.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('Unable to update your post.');
			}
		}
	}

}