<?php
class ControllerReportOnline extends Controller {
	function index() { 
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$response =& $this->locator->get('response');
		$template =& $this->locator->get('template');
		$module   =& $this->locator->get('module');
 
		$language->load('controller/report_online.php');

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

		$results = $database->getRows("select value, ip, time, url from session");

		$rows = array();

		foreach ($results as $result) {
			$value = array();
 
			$a = preg_split("/(\w+)\|/", $result['value'], - 1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

			for ($i = 0; $i < count($a); $i = $i + 2) {
				$value[$a[$i]] = unserialize($a[$i + 1]);
			}

			if (isset($value['user_id'])) {
				$user_info = $database->getRow("select username from user where user_id = '" . (int)$value['user_id'] . "'");

				$name = $language->get('text_admin', $user_info['username']);
			} elseif (isset($value['customer_id'])) {
				$customer_info = $database->getRow("select concat(firstname,' ',lastname) as name from customer where customer_id = '" . (int)$value['customer_id'] . "'");

				$name = $customer_info['name'];
			} else {
				$name = $language->get('text_guest');
			}

			$rows[] = array(
				'name'  => $name,
				'time'  => date('dS F Y h:i:s A', strtotime($result['time'])),
				'ip'    => $result['ip'],
				'url'   => $result['url'],
				'total' => (isset($value['products']) ? count($value['products']) : 0)
			);
		}

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

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

		$view->set('column_name', $language->get('column_name'));
		$view->set('column_time', $language->get('column_time'));
		$view->set('column_ip', $language->get('column_ip'));
		$view->set('column_url', $language->get('column_url'));
		$view->set('column_total', $language->get('column_total'));

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

		$template->set('content', $view->fetch('content/report_online.tpl'));

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

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