<?php
class ControllerUrlAlias 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/url_alias.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');
		$cache    =& $this->locator->get('cache');

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

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

		if (($request->isPost()) && ($this->validateForm())) {
			$sql = "insert into url_alias set query = '?', alias = '?'";
			$database->query($database->parse($sql, $request->get('query', 'post'), $request->get('alias', 'post')));

			$cache->delete('url');

			$session->set('message', $language->get('text_message'));
			
			$response->redirect($url->ssl('url_alias'));
		}

		$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');
		$cache    =& $this->locator->get('cache');

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

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

		if (($request->isPost()) && ($this->validateForm())) {
			$sql = "update url_alias set query = '?', alias = '?' where url_alias_id = '?'";
			$database->query($database->parse($sql, $request->get('query', 'post'), $request->get('alias', 'post'), $request->get('url_alias_id')));

			$cache->delete('url');
			
			$session->set('message', $language->get('text_message'));
			
			$response->redirect($url->ssl('url_alias'));
		}

		$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');
		$cache    =& $this->locator->get('cache');

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

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

		if (($request->get('url_alias_id')) && ($this->validateDelete())) {
			$database->query("delete from url_alias where url_alias_id = '" . (int)$request->get('url_alias_id') . "'");
 
			$cache->delete('url');

			$session->set('message', $language->get('text_message'));
			
			$response->redirect($url->ssl('url_alias'));
		}

		$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_query'),
			'sort'  => 'query',
			'align' => 'left'
		);

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

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

		$sort = array(
			'query',
			'alias'
		);

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

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

		$rows = array();

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

			$cell[] = array(
				'value' => $result['query'],
				'align' => 'left'
			);

			$cell[] = array(
				'value' => $result['alias'],
				'align' => 'left'
			);
			
			$action = array();
      		
			$action[] = array(
        		'icon' => 'update.png',
				'text' => $language->get('button_update'),
				'href' => $url->ssl('url_alias', 'update', array('url_alias_id' => $result['url_alias_id']))
      		);
						
			$action[] = array(
        		'icon' => 'delete.png',
				'text' => $language->get('button_delete'),
				'href' => $url->ssl('url_alias', 'delete', array('url_alias_id' => $result['url_alias_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('url_alias', 'page'));
 
		$view->set('search', $session->get('url_alias.search'));
		$view->set('sort', $session->get('url_alias.sort'));
		$view->set('order', $session->get('url_alias.order'));
		$view->set('page', $session->get('url_alias.page'));

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

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

		$view->set('insert', $url->ssl('url_alias', '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_query', $language->get('entry_query'));
		$view->set('entry_alias', $language->get('entry_alias'));

		$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_query', @$this->error['query']);

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

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

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

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

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

		if (($request->get('url_alias_id')) && (!$request->isPost())) {
			$url_alias_info = $database->getRow("select distinct query, alias from url_alias where url_alias_id = '" . (int)$request->get('url_alias_id') . "'");
		}

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

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

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

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

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

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

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

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

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

		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('url_alias.search', $request->get('search', 'post'));
		}

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

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

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

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