<?php
class ContactsController extends AppController {

	var $name = 'Contacts';
	var $helpers = array('Html', 'Form', 'Ajax');
	var $components = array('RequestHandler');
	var $paginate = array(
			'Contact' => array('order' => array('Contact.customer_id' => 'asc')),
			'Enquiry' => array('order' => array('Enquiry.id' => 'asc'))
			);	

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

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Contact.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('contact', $this->Contact->read(null, $id));
		//$enquiries = $this->Contact->Enquiry->findAllByContactId($id);
		$this->set('enquiries', $this->paginate('Enquiry', array('Enquiry.contact_id' => $id)));
		//$this->set('enquiries', $this->paginate($enquiries));
		$statuses = $this->Contact->Enquiry->Status->find('all');
		$status_list = array();
		foreach ($statuses as $status) {
			$status_list[] = array($status['Status']['id'], $status['Status']['name']);
		}
		$this->set('status_list', $status_list);
	
	}

	

	function add_to_customer($customerid = null) {
		
		if(isset($this->params['named']['customerid'])) {
			$customerid = $this->params['named']['customerid'];
		}
		if (!$customerid && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Customer ID', true));
			$this->redirect(array('controller'=>'Enquiries', 'action'=>'index'));
		}	
		if (!empty($this->data)) {
			
			$this->Contact->create();
			if ($this->Contact->save($this->data)) {
				$this->Session->setFlash(__('The Contact has been saved', true));
				$this->redirect(array('controller' => 'Customers', 'action'=>'view/'.$this->data['Contact']['customer_id']));
			} else {	
				$this->Session->setFlash(__('The Contact could not be saved. Please try again.', true));
			}
		}
		$contact_categories = $this->Contact->ContactCategory->find('list');
		$customer = $this->Contact->Customer->findById($customerid);
		$this->set(compact('customer', 'contact_categories'));
	}
	
	function add_another($increment) {
		$this->set('contact_categories', $this->Contact->ContactCategory->find('list'));
		$this->set('increment', $increment); 
		
	}
	function remove_another($increment) {
		$this->set('increment', $increment);
	}
	function add_one($customerid) {
		$contact_categories = $this->Contact->ContactCategory->find('list');
		$this->set('customerid', $customerid);
		$this->set('contact_categories', $contact_categories);
	}
	function remove_one($customerid) {
		$contacts = $this->Contact->find('all', array('conditions' => array('Contact.customer_id' => $customerid)));
		$contacts = Set::Combine($contacts, '{n}.Contact.id', array('{0} {1}', '{n}.Contact.first_name', '{n}.Contact.last_name'));
		$this->set('contacts', $contacts);
		$this->set('customerid', $customerid);
	}
	
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Contact', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
				$this->data['Contact']['name'] = $this->data['Contact']['first_name'].' '.$this->data['Contact']['last_name'];
			if ($this->Contact->save($this->data)) {
				$customerid = $this->data['Contact']['customer_id'];
				
				$this->Session->setFlash(__('The Contact has been saved', true));
				$this->redirect(array('controller' => 'Customers', 'action'=>'view/'.$customerid));
			} else {
				$this->Session->setFlash(__('The Contact could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Contact->read(null, $id);
		}
		$customers = $this->Contact->Customer->find('list');
		$contact_categories = $this->Contact->ContactCategory->find('list');
		$this->set(compact('customers', 'contact_categories'));
	}

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

}
?>
