cmc-sales/controllers/users_controller.php

193 lines
4.7 KiB
PHP
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Form');
var $components = array('Acl','Auth', 'Cookie');
var $paginate = array(
'Users' => array('order' => array('User.id' => 'desc'),
'limit' => 200
),
'Enquiry' => array('order' => array('Enquiry.id' => 'desc'), 'limit' => 250)
);
function beforeFilter() {
$this->Auth->allow('add');
$this->set('currentuser', $this->Auth->user());
$this->Auth->autoRedirect = false;
//$this->login();
}
function login() { //Provided by the authComponent
$this->pageTitle = ': Login';
//$this->Session->setFlash(__('Please enter your Username and Password to continue', true));
/* Auth Cookie code from http://www.webdevelopment2.com/cakephp-auth-component-tutorial-3/ */
// code inside this function will execute only when autoRedirect was set to false (i.e. in a beforeFilter).
if ($this->Auth->user()) {
if (!empty($this->data) && $this->data['User']['remember_me']) {
$cookie = array();
$cookie['username'] = $this->data['User']['username'];
$cookie['password'] = $this->data['User']['password'];
$this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
unset($this->data['User']['remember_me']);
}
$this->redirect($this->Auth->redirect());
}
if (empty($this->data)) {
$cookie = $this->Cookie->read('Auth.User');
if (!is_null($cookie)) {
if ($this->Auth->login($cookie)) {
// Clear auth message, just in case we use it.
// $this->Session->setFlash(__('Welcome back '.$cookie['username']), true);
$this->Session->del('Message.auth');
$this->redirect($this->Auth->redirect());
} else { // Delete invalid Cookie
$this->Cookie->del('Auth.User');
}
}
}
}
function logout() {
$this->redirect($this->Auth->logout());
}
function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
/**
* 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);
$this->set('user', $user);
switch($user['User']['type']) {
case 'contact':
$this->render('viewContact');
break;
case 'principle':
$this->render('viewPrinciple');
break;
case 'user':
$this->set('enquiries', $this->paginate('Enquiry', array('Enquiry.user_id' => $id)));
$statuses = $this->User->Enquiry->Status->find('all', array('recursive'=>0));
$status_list = array();
foreach ($statuses as $status) {
$statusid = $status['Status']['id'];
$status_list[$statusid] = $status['Status']['name'];
}
$this->set('status_list', $status_list);
$this->render('viewUser');
break;
default:
break;
}
}
function add_contact() {
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));
}
}
}
/**
* 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);
$this->set('groups', $this->User->Group->find('list'));
}
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for User', true));
$this->redirect(array('action'=>'index'));
}
if ($this->User->del($id)) {
$this->Session->setFlash(__('User deleted', true));
$this->redirect(array('action'=>'index'));
}
}
}
?>