controllers/aumanager.php
author viric@mandarina
Thu, 25 Dec 2008 11:25:58 +0000
changeset 0 3773f4677c40
permissions -rw-r--r--
Opiniarbo - versikontrolita.

<?php
/*
 * Manage users, users group and role
 */
class Aumanager extends Controller
{
	private $data=NULL;
	
	function Aumanager()
	{
		parent::Controller();
        $this->load->library('aulib');
    	if ( !$this->aulib->isRole('admin') ) $this->aulib->denyAccess(); 
		$this->config->load('auconfig');
		if ($this->config->item('au_language') != '') 
			$curLang = $this->config->item('au_language');
		else
			$curLang = $this->config->item('language');    
		$this->lang->load('au', $curLang); 
		$this->load->helper(array('form', 'url'));
		$this->load->library('validation');
		$this->load->library('parser');
		$this->load->model('aumodel');
	}
	
	/*
	 * Start point for manager
	 */
	function index()
	{
		$this->load->library('table');
		$tmpl = array ( 'table_open'  => '<table width="96%" border="1" cellpadding="2" cellspacing="1"' );
		$this->table->set_template($tmpl);
		
		$view_table = $this->uri->segment(3, 'user'); 
		switch($view_table)
		{
			case 'user': 
						$field_caption = explode("; ", $this->lang->line('field_name_user'));
						$table_caption = $this->lang->line('table_name_user');
						$this->data['add_new'] = '';
						$sql_str = sprintf("SELECT
											CONCAT('<a href=\"/aumanager/edit/user/',auser.id,'\">',auser.name,'</a>') as _edit, 
											email, 
											created, 
											last_visit, 
											aurole.name as arole, 
											augroup.name as agroup,
								 			CONCAT('<a href=\"#\" 
								 						onClick=\"if(confirm(&quot;%s&quot;)) location=\'/aumanager/delete/user/',
								 						auser.id,
								 						'\';return false;\">%s</a>') as _delete
								 			FROM auser, aurole, augroup
								 	WHERE (auser.role_id = aurole.id) and (auser.group_id = augroup.id)", 
									$this->lang->line('request_confirm_delete'),
									$this->lang->line('field_delete'));
						break;
		  case 'group': $field_caption = explode("; ", $this->lang->line('field_name_group'));
		  				$table_caption = $this->lang->line('table_name_group');
		  				$this->data['add_new'] = sprintf("<a href='/aumanager/append/group'>%s</a>", 
														$this->lang->line('add_new_link'));
		  				$sql_str = sprintf("select
		  							CONCAT('<a href=\"/aumanager/edit/group/',augroup.id,'\">',augroup.name,'</a>') as _edit,
								 	CONCAT('<a href=\"#\" 
								 			onClick=\"if(confirm(&quot;%s&quot;)) location=\'/aumanager/delete/group/',
								 			augroup.id,
								 			'\';return false;\">%s</a>') as _delete
		  							from augroup", 
		  							$this->lang->line('request_confirm_delete'),
									$this->lang->line('field_delete'));
						break;	
		  case 'role': $field_caption = explode("; ", $this->lang->line('field_name_role'));
		  				$table_caption = $this->lang->line('table_name_role');
		  				$this->data['add_new'] = sprintf("<a href='/aumanager/append/role'>%s</a>", 
														$this->lang->line('add_new_link'));
		  				$sql_str = sprintf("select
		  							CONCAT('<a href=\"/aumanager/edit/role/',aurole.id,'\">',aurole.name,'</a>') as _edit,
								 	CONCAT('<a href=\"#\" 
								 			onClick=\"if(confirm(&quot;%s&quot;)) location=\'/aumanager/delete/role/',
								 			aurole.id,
								 			'\';return false;\">%s</a>') as _delete
		  							from aurole", 
		  							$this->lang->line('request_confirm_delete'),
									$this->lang->line('field_delete'));
						break;
			default: redirect('aumanager'); break;
		}
		$this->table->set_caption("<h2>".$table_caption.'</h2>');
		$this->table->set_heading($field_caption);
		$this->table->set_empty("---");
		$sql = $this->db->query($sql_str);

		$this->data['table'] = $this->table->generate($sql);
		$this->data['table_name_user'] = $this->lang->line('table_name_user');
		$this->data['table_name_group'] = $this->lang->line('table_name_group');
		$this->data['table_name_role'] = $this->lang->line('table_name_role');
		$this->_show_view('au/manager'); 
	}
	
	/*
	 * Edit page
	 */
	function edit()
	{
		$mode = $this->uri->segment(3, 'user');
		switch($mode)
		{
			case 'user' : $this->showUserEdit(); break;
			case 'group': $this->showRefEdit(); break;
			case 'role' : $this->showRefEdit(); break;
			default: redirect('aumanager'); break;
		}
	}

	/*
	 * Delete page
	 */
	function delete()
	{
		if(FALSE === $this->uri->segment(3)) return;
		$mode = $this->uri->segment(3);
		if(FALSE === $this->uri->segment(4)) return;
		$id = $this->uri->segment(4);
		switch($mode)
		{
			case 'user' : 
				$this->aumodel->delete('auser', $id); 
				redirect('aumanager/index/user'); 
				break;
			case 'group':
				$this->aumodel->updateUsersRef('group', $id, 1); 
				$this->aumodel->delete('augroup', $id);
				redirect('aumanager/index/group');
				break;
			case 'role' :
				$this->aumodel->updateUsersRef('role', $id, 1); 
				$this->aumodel->delete('aurole', $id); 
				redirect('aumanager/index/role'); 
				break;
			default: redirect('aumanager'); break;
		}
	}

	/*
	 * Add new 
	 */
	function append()
	{
		if(FALSE === $this->uri->segment(3)) return;
		$mode = $this->uri->segment(3);
		switch($mode)
		{
			case 'group': $this->aumodel->append('augroup');redirect('aumanager/index/group');break;
			case 'role' : $this->aumodel->append('aurole'); redirect('aumanager/index/role'); break;
			default: redirect('aumanager'); break;
		}
	}
/*
 *	Service function
 */
	function showRefEdit()
	{
		$ref = $this->uri->segment(3);
		$id = $this->uri->segment(4);
		$rules['name'] = "trim|required|min_length[4]|xss_clean";
		$this->validation->set_rules($rules);
		if($ref == 'group')
			$fields['name']	= '"'.$this->lang->line('form_group').'"';
		else
			$fields['name']	= '"'.$this->lang->line('form_role').'"';
		$this->validation->set_fields($fields);
		
		if ($this->validation->run() == FALSE)
		{
			$name = $this->aumodel->getRefName('au'.$ref,$id);
			$this->data['name_label'] = $this->lang->line('form_'.$ref);
			$this->data['form_title'] = $this->lang->line('table_name_'.$ref);
			$this->data['name_value'] = ($this->input->post('name_value') ? $this->validation->name : $name);
			$this->data['name_error'] = $this->validation->name_error;
			$this->data['submit_label'] = $this->lang->line('form_ok');
			$this->data['id'] = $ref.'/'.$id;
			$this->_show_view('au/group_role_edit.php');
		}
		else
		{
		 	$this->aumodel->setRefName('au'.$ref, $id, $this->input->post('name'));
		 	redirect('aumanager/index/'.$ref);
		}
	}
	function showUserEdit()
	{
		$id = $this->uri->segment(4);
		$rules['username'] = "trim|required|min_length[4]|max_length[12]|xss_clean";
		$rules['email'] = "trim|required|valid_email";
		$this->validation->set_rules($rules);
		
		$fields['username']	= '"'.$this->lang->line('form_username').'"';
		$fields['email']	= '"'.$this->lang->line('form_email').'"';
		$this->validation->set_fields($fields);
		
		if ($this->validation->run() == FALSE)
		{
			$user = $this->aumodel->userFullInfo($id);
			$this->data['form_title'] = $this->lang->line('table_name_user');
			$this->data['username_label'] = $this->lang->line('form_username');
			$this->data['username_value'] = ($this->input->post('username_value') ? $this->validation->username : $user['name']);
			$this->data['username_error'] = $this->validation->username_error;
			$this->data['email_label'] = $this->lang->line('form_email');
			$this->data['email_value'] = ($this->input->post('email_value') ? $this->validation->email : $user['email']);
			$this->data['email_error'] = $this->validation->email_error;
			$this->data['group_label'] = $this->lang->line('form_group');
			$group_list = $this->aumodel->getRefList('augroup'); 
			$this->data['group_value'] = form_dropdown('group_value', $group_list, ($this->input->post('group_value') ? $this->input->post('group_value') : $user['group_id']));
			$this->data['role_label'] = $this->lang->line('form_role');
			$role_list = $this->aumodel->getRefList('aurole');
			$this->data['role_value'] = form_dropdown('role_value', $role_list, ($this->input->post('role_value') ? $this->input->post('role_value') : $user['role_id']));
			
			$this->data['submit_label'] = $this->lang->line('form_ok');
			$this->data['id'] = $id;
			$this->_show_view('au/user_edit.php');
		}			
 		else
		 {
		 	$newdata['name'] = $this->input->post('username');
		 	$newdata['email'] = $this->input->post('email');
		 	$newdata['group_id'] = $this->input->post('group_value');
		 	$newdata['role_id'] = $this->input->post('role_value');
		 	$this->aumodel->change($id, $newdata);
		 	redirect('aumanager/index/user');
		 }
	}

	/*
	 * All view's call from this point
	 * You may customize it as you need
	 */
	function _show_view($view_name)
	{
		$this->parser->parse($view_name, $this->data);
	}
}
?>