<?php
class ControllerAccountDownload extends Controller {
	function index() {
		$config   =& $this->locator->get('config');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');	
		
		if (!$customer->isLogged()) {
			$session->set('redirect', $url->ssl('account_download'));

			$response->redirect($url->ssl('account_login'));
		}
         		
		$language->load('controller/account_download.php');

		$template->set('title', $language->get('heading_title'));
		
		$results = $database->getRows($database->splitQuery("select o.order_id, o.date_added, od.order_download_id, od.name, od.filename, od.remaining from order_download od left join `order` o on od.order_id = o.order_id where o.customer_id = '" . (int)$customer->getId() . "' and o.order_status_id = '" . (int)$config->get('config_download_status') . "'", $session->get('account_download.page'), $config->get('config_max_rows')));
		
		if ($results) {
			$view = $this->locator->create('template');

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

			$view->set('text_order', $language->get('text_order'));
			$view->set('text_date_added', $language->get('text_date_added'));
			$view->set('text_name', $language->get('text_name'));
			$view->set('text_remaining', $language->get('text_remaining'));
			$view->set('text_size', $language->get('text_size'));
			$view->set('text_download', $language->get('text_download'));
			$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));

			$view->set('entry_page', $language->get('entry_page'));
			
			$view->set('button_continue', $language->get('button_continue'));
			
			$view->set('action', $url->href('account_download'));

			$download_data = array();

			foreach ($results as $result) {
				$size = filesize(DIR_DOWNLOAD . $result['filename']);

				$i = 0;

				$suffix = array(
					'B',
					'KB',
					'MB',
					'GB',
					'TB',
					'PB',
					'EB',
					'ZB',
					'YB'
				);

				while (($size / 1024) > 1) {
					$size = $size / 1024;
					$i++;
				}

				$download_data[] = array(
					'order_id'   => $result['order_id'],
					'date_added' => date($language->get('date_format_short'), strtotime($result['date_added'])),
					'name'       => $result['name'],
					'remaining'  => $result['remaining'],
					'size'       => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
					'href'       => $url->ssl('account_download', 'download', array('order_download_id' => $result['order_download_id']))
				);
			}

			$view->set('downloads', $download_data);

			$view->set('page', $session->get('account_download.page'));

			$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);

			$view->set('continue', $url->ssl('account'));

			$template->set('content', $view->fetch('content/account_download.tpl'));
		} else {
			$view = $this->locator->create('template');

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

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

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

			$view->set('continue', $url->ssl('account'));

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

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

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

	function download() {
		$config   =& $this->locator->get('config');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$download =& $this->locator->get('download');
		$request  =& $this->locator->get('request');

		if (!$customer->isLogged()) {
			$session->set('redirect', $url->ssl('account_download'));

			$response->redirect($url->ssl('account_login'));
		}
		
		$download_info = $database->getRow("select * from `order` o left join order_download od on o.order_id = od.order_id where o.customer_id = '" . (int)$customer->getId() . "' and o.order_status_id = '" . (int)$config->get('config_download_status') . "' and od.order_download_id = '" . (int)$request->get('order_download_id') . "' and od.remaining > 0");
	
		if ($download_info) {
			$download->setSource(DIR_DOWNLOAD . $download_info['filename']);
			$download->setFilename($download_info['mask']);
			$download->output();
			
			if (!$download->getError()) {
				$database->query("update order_download set remaining = (remaining - 1) where order_download_id = '" . (int)$request->get('order_download_id') . "'");
			}
		} else {
			$response->redirect($url->ssl('account_download'));
		}
	}
	
	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
						
		if ($request->has('page', 'post')) {
	  		$session->set('account_download.page', $request->get('page', 'post'));
		}
		
		$response->redirect($url->ssl('account_download'));
	}	
}
?>