<?php
class ControllerModuleAuction extends Controller {
	protected function index($setting) {
		$this->language->load('module/auction');
 
		$this->load->model('catalog/auction');
		
		if ($this->config->get('config_auction_status_id') && is_array($this->config->get('config_auction_status_id'))) {
			$auction_status_ids = $this->config->get('config_auction_status_id');
		} else {
			$auction_status_ids = array('starting' => 1, 'relisting' => 2, 'inprogress' => 3, 'extended' => 4, 'won' => 5, 'expired' => 6, 'sold' => 7, 'voided' => 8);
		}

		$this->load->model('localisation/auction_status');

		$auction_statuses = $this->model_localisation_auction_status->getAuctionStatuses();

		$this->data['text_auction_statuses'] = array();
		$this->data['auction_status_ids'] = array();
		$this->data['auction_statuses'] = array();

		foreach ($auction_status_ids as $key => $value) {
			foreach ($auction_statuses as $auction_status) {
				if ($auction_status['auction_status_id'] == $value) {
					$this->data['text_auction_statuses'][$key] = $auction_status['name'];
					$this->data['auction_status_ids'][$key] = $value;
					$this->data['auction_statuses'][(string)$value] = $auction_status['name'];
				}
			}
		}

		if (!is_null($this->config->get('config_auction_quick_bidding'))) {
			$quick_bidding = (int)$this->config->get('config_auction_quick_bidding');
		} else {
			$quick_bidding = 1;
		}

		$this->load->model('tool/image');

		$this->data['position'] = $setting['position'];

      	$this->data['heading_title'] = $this->language->get('heading_title');
				
		$this->data['text_closing'] = $this->language->get('text_closing');
		$this->data['text_closed'] = $this->language->get('text_closed');
		$this->data['text_ended'] = $this->language->get('text_ended');

		$this->data['button_quick_bid'] = $this->customer->isLogged() ? $this->language->get('button_quick_bid') : $this->language->get('button_login');

		$this->data['symbol_left'] = $this->currency->getSymbolLeft();
		$this->data['symbol_right'] = $this->currency->getSymbolRight();
		$this->data['decimal_place'] = $this->currency->getDecimalPlace();
		$this->data['decimal_point'] = $this->language->get('decimal_point');
		$this->data['thousand_point'] = $this->language->get('thousand_point');

		$this->data['logged'] = (int)$this->customer->isLogged();
		$this->data['login_url'] = $this->url->link('account/login');

		$this->data['quick_bidding'] = $quick_bidding;

		$this->data['auctions'] = array();
		$this->data['auction_ids'] = array();

		$results = $this->model_catalog_auction->getLiveAuctions($auction_status_ids, $setting['limit']);
		
		foreach ($results as $result) {
			if ($result['product_image']) {
				$product_image = $this->model_tool_image->resize($result['product_image'], $setting['image_width'], $setting['image_height']);
			} else {
				$product_image = $this->model_tool_image->resize('no_image.jpg', $setting['image_width'], $setting['image_height']);
			}

			//if (strlen(stripslashes($result['product_name'])) > 30) {
			//	$product_name = substr(stripslashes($result['product_name']), 0, 30) . ' ...';
			//} else {
				$product_name = stripslashes($result['product_name']);
			//}

			if (!$result['total_no_of_bid']) {
				$current_bid_amount = (float)$result['starting_price'];
				$next_bid_amount = (float)$result['starting_price'];
			} elseif ((float)$result['buyout_price'] > 0 && !$result['buyout_made'] && (float)(string)((float)$result['highest_bid_amount'] + (float)$result['min_increment']) > (float)$result['buyout_price']) {
				$current_bid_amount = (float)$result['highest_bid_amount'];
				$next_bid_amount = (float)$result['buyout_price'];
			} else {
				$current_bid_amount = (float)$result['highest_bid_amount'];
				$next_bid_amount = (float)(string)((float)$result['highest_bid_amount'] + (float)$result['min_increment']);
			}

			$this->data['auctions'][] = array(
				'product_image'           => $product_image,
				'product_name'            => $product_name,
				'auction_href'            => $this->url->link('product/product', '&product_id=' . $result['product_id'] . '&auction_id=' . $result['auction_id']),
				'auction_id'              => (int)$result['auction_id'],
				'auction_name'            => stripslashes($result['auction_name']),
				'reserved_quantity'       => (int)$result['reserved_quantity'],
				'buyout_price'            => $this->currency->format((float)$result['buyout_price'], '', '', false, 'up'),
				'min_increment'           => $this->currency->format((float)$result['min_increment'], '', '', false, 'up'),
				'highest_bid_id'          => (int)$result['highest_bid_id'],
				'highest_bidder_masked'   => (int)$result['highest_bidder_id'] ? substr(preg_replace("/[^a-z0-9]/", '', strtolower(stripslashes($result['customer_email']))), 0, 3) . 'xxxxxxx' : '---',
				'current_bid_amount'      => $this->currency->format((float)$current_bid_amount, '', '', false, 'up'),
				'current_bid_amount_text' => $this->currency->format((float)$current_bid_amount, '', '', true, 'up'),
				'next_bid_amount'         => $this->currency->format((float)$next_bid_amount, '', '', false, 'up'),
				'next_bid_amount_text'    => $this->currency->format((float)$next_bid_amount, '', '', true, 'up'),
				'real_ending_time'        => (int)$result['real_ending_time']
			);

			$this->data['auction_ids'][] = (int)$result['auction_id'];
		}

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/auction.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/auction.tpl';
		} else {
			$this->template = 'default/template/module/auction.tpl';
		}

		$this->render();
	}
}
?>