<?php 
class ControllerHome extends Controller {
	function index() {
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$image    =& $this->locator->get('image');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$tax      =& $this->locator->get('tax');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		
		$language->load('controller/home.php');

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

		$view = $this->locator->create('template');
		
		$view->set('heading_title', $language->get('heading_title', $config->get('config_store')));

		if (!$customer->isLogged()) {
			$view->set('text_greeting', $language->get('text_greeting', $url->href('account_login'), $url->href('account_create')));
		} else {
			$view->set('text_greeting', $language->get('text_logged', $customer->getFirstName()));
		}
 
		$view->set('text_latest', $language->get('text_latest'));

		$product_data = array();

		$results = $database->getRows("select * from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where p.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' order by p.date_added limit 6");

		foreach ($results as $result) {
			$product_data[] = array(
				'name'  => $result['name'],
				'href'  => $url->href('product', FALSE, array('product_id' => $result['product_id'])),
				'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
				'price' => $currency->format($tax->calculate($result['price'], $result['tax_class_id']))
			);
		}

		$view->set('products', $product_data);

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

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

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