<?php
class UsersController extends AppController {

    var $components = array(
	    'Filter' => array(
	        'index' => array(
	            'username' => array(
        	        'form_options' => array('label' => 'Username'),
	            	'filter_options' => array('fields' => array('username'), 'operator' => 'like^'),
	            ),
	            'user_level_id' => array(
        	        'form_options' => array('label' => 'Level', 'options' => 'UserLevels'),
	            	'filter_options' => array(),
	            ),
	        )
        ),
	);

	function  beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allowedActions = array('login', 'logout', 'update_perms');
		$this->Auth->mapActions(array('create_perms' => 'update'));
	}

	function login() {
        $this->layout = 'login';

        if ($this->Session->check('Auth.User.id')) {
            if (empty($this->data['User'])) {
                $this->Session->setFlash(__('You are already logged in!', true), 'default', array('class' => 'notification'));
                $this->redirect($this->referer(), null, true);
            } else {
                $this->User->save(array('id' => $this->Auth->user('id'), 'last_login' => date('Y-m-d H:i:s'), 'modified' => false, 'modified_by' => false));
                $this->redirect($this->Auth->redirect(), null, true);
            }
        }
    }

    function logout() {
        $this->redirect($this->Auth->logout());
    }

	function index() {
	//$this->update_perms();
		$this->User->recursive = 0;
		$this->set('items', $this->paginate());
	}

	function view($id = null) {
		if (!$id  || !($item = $this->User->read(null, $id))) {
			$this->Session->setFlash(__('Invalid User', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('item', $item);
	}

	function add() {

		if (!empty($this->data)) {

            if (isset($this->data['User']['password'])) unset($this->data['User']['password']);
            if (!empty($this->data['User']['password1'])) {
                $this->data['User']['password'] = $this->Auth->password($this->data['User']['password1']);
                unset($this->data['User']['password1']);
            }

		    $this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$this->set('userLevels', $this->_faUserLevels($this->me['UserLevel']['id']));
	}

	function edit($id = null) {
		// force regular users to edit their own account
		if (!$this->me['is_admin']) {
			if ($id != $this->me['User']['id']) {
				$this->Session->setFlash(__('You can not change other accounts', true), 'default', array('class' => 'error'));
			}
			$id = $this->me['User']['id'];
		}

	    if ((!$id && empty($this->data)) || ($id && !($item = $this->User->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid User', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {

            if (isset($this->data['User']['password'])) unset($this->data['User']['password']);
            if (!empty($this->data['User']['password1'])) {
                $this->data['User']['password'] = $this->Auth->password($this->data['User']['password1']);
                unset($this->data['User']['password1']);
            }

    	    if (!$this->me['is_admin']) {
    	        if (isset($this->data['User']['user_level_id'])) unset($this->data['User']['user_level_id']);
    	        if (isset($this->data['User']['username'])) unset($this->data['User']['username']);
    	    }

			$this->data['User']['id'] = $id;

			$this->User->data = null;	// unset model data so modified field can be updated automagically
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
		$this->set('userLevels', $this->_faUserLevels($this->me['UserLevel']['id']));
	}

	function delete($id = null) {

	    if ($id == $this->me['User']['id']) {
			$this->Session->setFlash(__('You can\'t delete your own account', true), 'default', array('class' => 'error'));
			$this->redirect(array('action'=>'index'), null, true);
	    }

	    if (!$id || !$this->User->read(null, $id)) {
			$this->Session->setFlash(__('Invalid User', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true), 'default', array('class' => 'success'));
		} else {
			$this->Session->setFlash(__('User delete failed. Please, try again.', true), 'default', array('class' => 'error'));
		}

		$this->redirect(array('action' => 'index'));
	}




	function update_perms() {
		$admin_level = array('model' => 'UserLevel', 'foreign_key' => 10);
		$gazda_level = array('model' => 'UserLevel', 'foreign_key' => 20);
		$konob_level = array('model' => 'UserLevel', 'foreign_key' => 30);

		$this->Acl->allow($admin_level, 'Pages');
		$this->Acl->allow($admin_level, 'Articles');
		$this->Acl->allow($admin_level, 'Users');
		$this->Acl->allow($admin_level, 'UserLevels');
		$this->Acl->allow($admin_level, 'Suppliers');
		$this->Acl->allow($admin_level, 'Receivings');
		$this->Acl->allow($admin_level, 'ReceivingOrders');
		$this->Acl->allow($admin_level, 'Products');
		$this->Acl->allow($admin_level, 'Customers');
		$this->Acl->allow($admin_level, 'Tables');
		$this->Acl->allow($admin_level, 'Sales');
		$this->Acl->allow($admin_level, 'Notes');
		$this->Acl->allow($admin_level, 'ExtraGroups');
		$this->Acl->allow($admin_level, 'Reports');
		$this->Acl->allow($admin_level, 'Settings');
		$this->Acl->allow($admin_level, 'TaxGroups');
		$this->Acl->allow($admin_level, 'Printers');

		$this->Acl->allow($gazda_level, 'Pages', array('read'));
		$this->Acl->allow($gazda_level, 'Articles');
		$this->Acl->allow($gazda_level, 'Users');
		$this->Acl->allow($gazda_level, 'UserLevels');
		$this->Acl->allow($gazda_level, 'Suppliers');
		$this->Acl->allow($gazda_level, 'Receivings');
		$this->Acl->allow($gazda_level, 'ReceivingOrders');
		$this->Acl->allow($gazda_level, 'Products');
		$this->Acl->allow($gazda_level, 'Customers');
		$this->Acl->allow($gazda_level, 'Tables');
		$this->Acl->allow($gazda_level, 'Sales');
		$this->Acl->allow($gazda_level, 'Notes');
		$this->Acl->allow($gazda_level, 'ExtraGroups');
		$this->Acl->allow($gazda_level, 'Reports');
		$this->Acl->allow($gazda_level, 'Settings');
		$this->Acl->allow($gazda_level, 'Printers');

		$this->Acl->allow($konob_level, 'Pages', array('read'));
		$this->Acl->allow($konob_level, 'Articles');
		$this->Acl->allow($konob_level, 'Users', array('update'));
		$this->Acl->deny($konob_level, 'UserLevels');
		$this->Acl->allow($konob_level, 'Suppliers');
		$this->Acl->allow($konob_level, 'Receivings');
		$this->Acl->allow($konob_level, 'ReceivingOrders');
		$this->Acl->allow($konob_level, 'Products');
		$this->Acl->allow($konob_level, 'Customers');
		$this->Acl->deny($konob_level, 'Tables');
		$this->Acl->deny($konob_level, 'Sales');
		$this->Acl->deny($konob_level, 'Notes');
		$this->Acl->deny($konob_level, 'ExtraGroups');
		$this->Acl->deny($konob_level, 'Reports');
		$this->Acl->allow($konob_level, 'Settings');

		$this->autoRender = false;
		echo 'finished';
	}

}
