<?php
class PrincipleContactsController extends AppController {

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

	function index() {
		$this->PrincipleContact->recursive = 0;
		$this->set('principleContacts', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid PrincipleContact.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('principleContact', $this->PrincipleContact->read(null, $id));
	}

	function add() {
		
		if(isset($this->params['named']['principleid'])) {
			$principleid = $this->params['named']['principleid'];
		}
		
		if (!(isset($principleid)) && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Principle ID', true));
			$this->redirect(array('controller'=>'Principles', 'action'=>'index'));
		}
		
		
		if (!empty($this->data)) {
			$this->PrincipleContact->create();
			if ($this->PrincipleContact->save($this->data)) {
				$this->Session->setFlash(__('The Contact has been added to the Principle', true));
				$principleid = $this->data['PrincipleContact']['principle_id'];
				$this->redirect(array('controller'=>'Principles', 'action'=>'view/'.$principleid));
			} else {
				$this->Session->setFlash(__('The PrincipleContact could not be saved. Please, try again.', true));
			}
		}
		$principle = $this->PrincipleContact->Principle->findById($principleid);
		$this->set(compact('principle'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid PrincipleContact', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->PrincipleContact->save($this->data)) {
				$this->Session->setFlash(__('The PrincipleContact has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The PrincipleContact could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->PrincipleContact->read(null, $id);
		}
		$principles = $this->PrincipleContact->Principle->find('list');
		$this->set(compact('principles'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for PrincipleContact', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->PrincipleContact->del($id)) {
			$this->Session->setFlash(__('PrincipleContact deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
