<?php

class UsersController extends AppController {

    var $name = 'Users';
    var $helpers = array('Html', 'Form', 'Javascript', 'Text');

    var $paginate = array(
	    'limit' => 500,
	    'Users' => array('order' => array('User.archived' => 'ASC', 'User.id' => 'asc'),
	    ),
		'Enquiry' => array('order' => array('Enquiry.id' => 'desc'), 'limit' => 250),
    );




    function index($type = 'user') {
	$this->User->recursive = 0;

	$users = $this->paginate(array('User.type'=> $type));

	$this->set('users', $users);
    }

    /**
     * View a User.
     *
     * Depending on the type of user. Fetch the appropriate data and render the appropriate template.
     *
     * @param int $id
     */
    function view($id = null) {
	if (!$id) {
	    $this->Session->setFlash(__('Invalid User.', true));
	    $this->redirect(array('action' => 'index'));
	}

	//$user = $this->User->read(null, $id);

	$userFields = array('User.id', 'User.principle_id', 'User.customer_id', 'User.type',
		'User.access_level', 'User.username', 'User.first_name', 'User.last_name',
		'User.email', 'User.job_title', 'User.phone', 'User.mobile', 'User.fax', 'User.phone_extension', 'User.direct_phone',
		'User.notes', 'User.by_vault', 'User.blacklisted'
	);

	$user = $this->User->find('first', array('conditions' => array('User.id' => $id), 'fields' => $userFields, 'recursive' => 0));

	$this->set('user', $user);


	/** $this->render() is a better approach than I used in View Document. Keeps the views out of Elements and in the right directory.
	 *
	    Will refactor at some point in the future. Maybe.
	 *
	 */

	switch ($user['User']['type']) {
	    case 'contact':

		if (isset($user['User']['customer_id'])) {
		    $this->set('customer', $this->User->Customer->find('first', array('conditions' => array('Customer.id' => $user['User']['customer_id']), 'recursive' => 0)));
		}
		$this->render('viewContact');
		break;

	    case 'principle':

		$this->render('viewPrinciple');
		break;

	    case 'user':
		$this->set('enquiries', $this->paginate('Enquiry', array('Enquiry.user_id' => $id)));
		$status_list = $this->User->Enquiry->Status->getJSON();
		$this->set('status_list', $status_list);
		$classNames = $this->User->Enquiry->Status->getClassNamesJSON();
		$this->set('class_names', $classNames);

		$this->render('viewUser');
		break;

	    default:
		break;
	}
    }

    function add_edit() {
	Configure::write('debug', 0);

	if (isset($this->params['named']['type'])) {
	    $this->set('type', $this->params['named']['type']);
	}

	if (isset($this->params['named']['principle_id'])) {
	    $this->set('principle_id', $this->params['named']['principle_id']);
	}

	if (isset($this->params['named']['customer_id'])) {
	    $this->set('customer_id', $this->params['named']['customer_id']);
	}

	if (isset($this->params['named']['user_id'])) {
	    $userID = $this->params['named']['user_id'];
	    $this->set('user_id', $userID);
	}

	if (isset($this->params['named']['action'])) {
	    $action = $this->params['named']['action'];
	    $this->set('action', $action);
	}

	if (isset($action) && isset($userID)) {
	    $this->data = $this->User->read(null, $userID);
	}
    }

    function add_user() {

	Configure::write('debug', 0);
	$this->layout = 'ajax';
	if (!empty($this->data)) {
	    $this->User->create();
	    $this->User->set($this->data['User']);

	    if ($this->User->save($this->data)) {
		$message = __('The User has been saved.', true);
		$data = $this->data;
		$this->set('status', array('status'=>'success'));
	    } else {
		$message = __('The User could not be saved.', true);
		$this->set('status', array('status'=>'failure'));
	    }
	}
    }


    /**
     * the default generated add() method. Used for system users, rather than contacts & principle contacts.
     */
    function add() {

	if (!empty($this->data)) {
	    $this->User->create();
	    if ($this->User->save($this->data)) {
		$this->Session->setFlash(__('The User has been saved', true));
		$this->redirect(array('action' => 'index'));
	    } else {
		$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
	    }
	}
    }

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

}

?>
