<?php
class ControllerUserGroup extends Controller {
	var $error = array();
 
	function index() {
		$response =& $this->locator->get('response');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template'); 
		$module   =& $this->locator->get('module');

		$language->load('controller/user_group.php');

		$template->set('title', $language->get('heading_title'));
 
		$template->set('content', $this->getList());

		$template->set($module->fetch());
 
		$response->set($template->fetch('layout.tpl'));
	}

	function insert() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');

		$language->load('controller/user_group.php');

		$template->set('title', $language->get('heading_title'));

		if (($request->isPost()) && ($this->validateForm())) {
			$permission = array(
				'access' => $request->get('access', 'post'),
				'modify' => $request->get('modify', 'post')
			);

			$sql ="insert into user_group set name = '?', permission = '?'";
			$database->query($database->parse($sql, $request->get('name', 'post'), serialize($permission)));
			
			$session->set('message', $language->get('text_message'));

			$response->redirect($url->ssl('user_group'));
		}

		$template->set('content', $this->getForm());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function update() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');

		$language->load('controller/user_group.php');

		$template->set('title', $language->get('heading_title'));

		if (($request->isPost()) && ($this->validateForm())) {
			$permission = array(
				'access' => $request->get('access', 'post'),
				'modify' => $request->get('modify', 'post')
			);

			$sql ="update user_group set name = '?', permission = '?' where user_group_id = '?'";
			$database->query($database->parse($sql, $request->get('name', 'post'), serialize($permission), $request->get('user_group_id')));
			
			$session->set('message', $language->get('text_message'));
			
			$response->redirect($url->ssl('user_group'));
		}

		$template->set('content', $this->getForm());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function delete() { 
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');

		$language->load('controller/user_group.php');

		$template->set('title', $language->get('heading_title'));

		if (($request->get('user_group_id')) && ($this->validateDelete())) {
			$database->query("delete from user_group where user_group_id = '" . (int)$request->get('user_group_id') . "'");
			
			$session->set('message', $language->get('text_message'));
			
			$response->redirect($url->ssl('user_group'));
		}

		$template->set('content', $this->getList());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function getList() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$config   =& $this->locator->get('config');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$session  =& $this->locator->get('session');

		$cols = array();

		$cols[] = array(
			'name'  => $language->get('column_name'),
			'sort'  => 'name',
			'align' => 'left'
		);

    	$cols[] = array(
      		'name'  => $language->get('column_action'),
      		'align' => 'right'
    	);
		
		if (!$session->get('user_group.search')) {
			$sql = "select user_group_id, name from user_group";
		} else {
			$sql = "select user_group_id, name from user_group where name like '?'";
		}

		if (in_array($session->get('user_group.sort'), array('name'))) {
			$sql .= " order by " . $session->get('user_group.sort') . " " . (($session->get('user_group.order') == 'desc') ? 'desc' : 'asc');
		} else {
			$sql .= " order by name asc";
		}

		$results = $database->getRows($database->splitQuery($database->parse($sql, '%' . $session->get('user_group.search') . '%'), $session->get('user_group.page'), $config->get('config_max_rows')));

		$rows = array();

		foreach ($results as $result) {
			$cell = array();

			$cell[] = array(
				'value' => $result['name'],
				'align' => 'left'
			);
			
			$action = array();
      		
			$action[] = array(
        		'icon' => 'update.png',
				'text' => $language->get('button_update'),
				'href' => $url->ssl('user_group', 'update', array('user_group_id' => $result['user_group_id']))
      		);
						
			$action[] = array(
        		'icon' => 'delete.png',
				'text' => $language->get('button_delete'),
				'href' => $url->ssl('user_group', 'delete', array('user_group_id' => $result['user_group_id']))
      		);

      		$cell[] = array(
        		'action' => $action,
        		'align'  => 'right'
      		);
			
			$rows[] = array('cell' => $cell);
		}

		$view = $this->locator->create('template');

		$view->set('heading_title', $language->get('heading_title'));
		$view->set('heading_description', $language->get('heading_description'));

		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));

		$view->set('entry_page', $language->get('entry_page'));
		$view->set('entry_search', $language->get('entry_search'));

		$view->set('button_list', $language->get('button_list'));
		$view->set('button_insert', $language->get('button_insert'));
		$view->set('button_update', $language->get('button_update'));
		$view->set('button_delete', $language->get('button_delete'));
		$view->set('button_save', $language->get('button_save'));
		$view->set('button_cancel', $language->get('button_cancel'));

		$view->set('error', @$this->error['message']);
		
		$view->set('message', $session->get('message'));
		
		$session->delete('message');
		
		$view->set('action', $url->ssl('user_group', 'page'));
 
		$view->set('search', $session->get('user_group.search'));
		$view->set('sort', $session->get('user_group.sort'));
		$view->set('order', $session->get('user_group.order'));
		$view->set('page', $session->get('user_group.page'));

		$view->set('cols', $cols);
		$view->set('rows', $rows);

		$view->set('list', $url->ssl('user_group'));

		$view->set('insert', $url->ssl('user_group', 'insert'));

		$page_data = array();

		for ($i = 1; $i <= $database->getPages(); $i++) {
			$page_data[] = array(
				'text'  => $language->get('text_pages', $i, $database->getPages()),
				'value' => $i
			);
		}

		$view->set('pages', $page_data);

		return $view->fetch('content/list.tpl');
	}

	function getForm() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');

		$view = $this->locator->create('template');

		$view->set('heading_title', $language->get('heading_title'));
		$view->set('heading_description', $language->get('heading_description'));

		$view->set('entry_name', $language->get('entry_name'));
		$view->set('entry_access', $language->get('entry_access'));
		$view->set('entry_modify', $language->get('entry_modify'));

		$view->set('button_list', $language->get('button_list'));
		$view->set('button_insert', $language->get('button_insert'));
		$view->set('button_update', $language->get('button_update'));
		$view->set('button_delete', $language->get('button_delete'));
		$view->set('button_save', $language->get('button_save'));
		$view->set('button_cancel', $language->get('button_cancel'));

		$view->set('tab_general', $language->get('tab_general'));

		$view->set('error', @$this->error['message']);
		$view->set('error_name', @$this->error['name']);

		$view->set('action', $url->ssl('user_group', $request->get('action'), array('user_group_id' => $request->get('user_group_id'))));

		$view->set('list', $url->ssl('user_group'));

		$view->set('insert', $url->ssl('user_group', 'insert'));

		if ($request->get('user_group_id')) {
			$view->set('update', $url->ssl('user_group', 'update', array('user_group_id' => $request->get('user_group_id'))));
			$view->set('delete', $url->ssl('user_group', 'delete', array('user_group_id' => $request->get('user_group_id'))));
		}

		$view->set('cancel', $url->ssl('user_group'));

		if (($request->get('user_group_id')) && (! $request->isPost())) {
			$user_group_info = $database->getRow("select distinct * from user_group where user_group_id = '" . (int)$request->get('user_group_id') . "'");
		}

		if ($request->has('name', 'post')) {
			$view->set('name', $request->get('name', 'post'));
		} else {
			$view->set('name', @$user_group_info['name']);
		}

		if (isset($user_group_info['permission'])) {
			$permission = unserialize($user_group_info['permission']);
		}

		if ($request->has('access', 'post')) {
			$permission['access'] = $request->get('access', 'post');
		}
		
		if (!isset($permission['access'])) { 
			$permission['access'] = array();
		}

		if ($request->has('modify', 'post')) {
			$permission['modify'] = $request->get('modify', 'post');
		}

		if (!isset($permission['modify'])) {
			$permission['modify'] = array();
		}

		$permission_data = array();

		foreach (glob(DIR_CONTROLLER . '*') as $controller) {
			$permission_data[] = array(
				'name'   => basename($controller, '.php'),
				'access' => in_array(basename($controller, '.php'), $permission['access']),
				'modify' => in_array(basename($controller, '.php'), $permission['modify'])
			);
		}

		$view->set('permissions', $permission_data);

		return $view->fetch('content/user_group.tpl');
	}

	function validateForm() {
		$request  =& $this->locator->get('request');
		$user     =& $this->locator->get('user');
		$language =& $this->locator->get('language');

		if (!$user->hasPermission('modify', 'user_group')) {
			$this->error['message'] = $language->get('error_permission');
		}

		if ((strlen($request->get('name', 'post')) < 3) || (strlen($request->get('name', 'post')) > 64)) {
			$this->error['name'] = $language->get('error_name');
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function validateDelete() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$user     =& $this->locator->get('user');
		$language =& $this->locator->get('language');

		if (!$user->hasPermission('modify', 'user_group')) {
			$this->error['message'] = $language->get('error_permission');
		}

		$user_info = $database->getRow("select count(*) as total from user where user_group_id = '" . (int)$request->get('user_group_id') . "'");

		if ($user_info['total']) {
			$this->error['message'] = $language->get('error_user', $user_info['total']);
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$url      =& $this->locator->get('url');
		$session  =& $this->locator->get('session');

		if ($request->has('search', 'post')) {
			$session->set('user_group.search', $request->get('search', 'post'));
		}

		if (($request->has('page', 'post')) || ($request->has('search', 'post'))) {
			$session->set('user_group.page', $request->get('page', 'post'));
		}

		if ($request->has('sort', 'post')) {
			$session->set('user_group.order', (($session->get('user_group.sort') == $request->get('sort', 'post')) && ($session->get('user_group.order') == 'asc')) ? 'desc' : 'asc');
		}

		if ($request->has('sort', 'post')) {
			$session->set('user_group.sort', $request->get('sort', 'post'));
		}

		$response->redirect($url->ssl('user_group'));
	}	
}
?>