<?php
class PrincipleAddressesController extends AppController {

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

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

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

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

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Principle Address', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->PrincipleAddress->save($this->data)) {
				$principleid = $this->data['PrincipleAddress']['principle_id'];
				$this->Session->setFlash(__('The Principle Address has been saved', true));
				$this->redirect(array('controller' => 'Principles', 'action'=>'view/'.$principleid));
			} else {
				$this->Session->setFlash(__('The Principle Address could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->PrincipleAddress->read(null, $id);
		}
		$principle = $this->PrincipleAddress->Principle->findById($id);
		$countries = $this->PrincipleAddress->Country->find('list');
		$this->set(compact('principle','countries'));
	}

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

}
?>
