<?php 
class ControllerSaleAuction extends Controller {
	private $error = array(); 
     
  	public function index() {
		$this->language->load('sale/auction');
    	
		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('sale/auction');
		
		$this->getList();
  	}
  
  	public function delete() {
    	$this->language->load('sale/auction');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('sale/auction');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $auction_id) {
				$this->model_sale_auction->deleteAuction($auction_id);
	  		}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['filter_auction_name'])) {
				$url .= '&filter_auction_name=' . urlencode(html_entity_decode($this->request->get['filter_auction_name'], ENT_QUOTES, 'UTF-8'));
			}
		
			if (isset($this->request->get['filter_starting_time'])) {
				$url .= '&filter_starting_time=' . urlencode(html_entity_decode($this->request->get['filter_starting_time'], ENT_QUOTES, 'UTF-8'));
			}
			
			if (isset($this->request->get['filter_highest_bid'])) {
				$url .= '&filter_highest_bid=' . $this->request->get['filter_highest_bid'];
			}
			
			if (isset($this->request->get['filter_total_no_of_bid'])) {
				$url .= '&filter_total_no_of_bid=' . $this->request->get['filter_total_no_of_bid'];
			}	
		
			if (isset($this->request->get['filter_auction_status_id'])) {
				$url .= '&filter_auction_status_id=' . $this->request->get['filter_auction_status_id'];
			}
					
			if (isset($this->request->get['filter_real_ending_time'])) {
				$url .= '&filter_real_ending_time=' . urlencode(html_entity_decode($this->request->get['filter_real_ending_time'], ENT_QUOTES, 'UTF-8'));
			}
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('sale/auction', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

    	$this->getList();
  	}

  	protected function getList() {				
		if (isset($this->request->get['filter_auction_name'])) {
			$filter_auction_name = $this->request->get['filter_auction_name'];
		} else {
			$filter_auction_name = null;
		}

		if (isset($this->request->get['filter_starting_time'])) {
			$filter_starting_time = $this->request->get['filter_starting_time'];
		} else {
			$filter_starting_time = null;
		}
		
		if (isset($this->request->get['filter_highest_bid'])) {
			$filter_highest_bid = $this->request->get['filter_highest_bid'];
		} else {
			$filter_highest_bid = null;
		}

		if (isset($this->request->get['filter_total_no_of_bid'])) {
			$filter_total_no_of_bid = $this->request->get['filter_total_no_of_bid'];
		} else {
			$filter_total_no_of_bid = null;
		}

		if (isset($this->request->get['filter_auction_status_id'])) {
			$filter_auction_status_id = $this->request->get['filter_auction_status_id'];
		} else {
			$filter_auction_status_id = null;
		}

		if (isset($this->request->get['filter_real_ending_time'])) {
			$filter_real_ending_time = $this->request->get['filter_real_ending_time'];
		} else {
			$filter_real_ending_time = null;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'a.real_ending_time';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
						
		$url = '';
						
		if (isset($this->request->get['filter_auction_name'])) {
			$url .= '&filter_auction_name=' . urlencode(html_entity_decode($this->request->get['filter_auction_name'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['filter_starting_time'])) {
			$url .= '&filter_starting_time=' . urlencode(html_entity_decode($this->request->get['filter_starting_time'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['filter_highest_bid'])) {
			$url .= '&filter_highest_bid=' . $this->request->get['filter_highest_bid'];
		}
		
		if (isset($this->request->get['filter_total_no_of_bid'])) {
			$url .= '&filter_total_no_of_bid=' . $this->request->get['filter_total_no_of_bid'];
		}		

		if (isset($this->request->get['filter_auction_status_id'])) {
			$url .= '&filter_auction_status_id=' . $this->request->get['filter_auction_status_id'];
		}
						
		if (isset($this->request->get['filter_real_ending_time'])) {
			$url .= '&filter_real_ending_time=' . urlencode(html_entity_decode($this->request->get['filter_real_ending_time'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

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

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . $url, 'SSL'),       		
      		'separator' => ' :: '
   		);
		
		$this->data['delete'] = $this->url->link('sale/auction/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
    	
		$this->data['auctions'] = array();

		$data = array(
			'filter_auction_name'      => $filter_auction_name, 
			'filter_starting_time'     => $filter_starting_time,
			'filter_highest_bid'       => $filter_highest_bid,
			'filter_total_no_of_bid'   => $filter_total_no_of_bid,
			'filter_auction_status_id' => $filter_auction_status_id,
			'filter_real_ending_time'  => $filter_real_ending_time,
			'sort'                     => $sort,
			'order'                    => $order,
			'start'                    => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'                    => $this->config->get('config_admin_limit')
		);
		
		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'];
				}
			}
		}

		$this->load->model('tool/image');
		
		$auction_total = $this->model_sale_auction->getTotalAuctions($data);
			
		$results = $this->model_sale_auction->getAuctions($data);
				    	
		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_view'),
				'href' => $this->url->link('sale/auction/info', 'token=' . $this->session->data['token'] . '&auction_id=' . $result['auction_id'] . $url, 'SSL')
			);
			
			if ($result['product_image'] && file_exists(DIR_IMAGE . $result['product_image'])) {
				$product_image = $this->model_tool_image->resize($result['product_image'], 30, 30);
			} else {
				$product_image = $this->model_tool_image->resize('no_image.jpg', 30, 30);
			}
	
			if ($result['auction_status_id'] == $auction_status_ids['starting'] || $result['auction_status_id'] == $auction_status_ids['relisting'] || $result['auction_status_id'] == $auction_status_ids['inprogress'] || $result['auction_status_id'] == $auction_status_ids['extended']) {
				$auction_active = true;
			} else {
				$auction_active = false;
			}

      		$this->data['auctions'][] = array(
				'auction_active'          => $auction_active,
				'auction_id'              => (int)$result['auction_id'],
				'auction_name'            => stripslashes($result['auction_name']),
				'reserved_quantity'       => (int)$result['reserved_quantity'],
				'starting_time'           => (int)$result['starting_time'],
				'starting_time_text'      => date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), (int)$result['starting_time']),
				'highest_bid_amount'      => $this->currency->format((float)$result['highest_bid_amount'], $this->config->get('config_currency'), '', false),
				'highest_bid_amount_text' => $this->currency->format((float)$result['highest_bid_amount'], $this->config->get('config_currency')),
				'total_no_of_bid'         => (int)$result['total_no_of_bid'],
				'real_ending_time'        => (int)$result['real_ending_time'],
				'real_ending_time_text'   => (int)$result['buyout_made'] ? date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), strtotime($result['date_closed'])) : date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), (int)$result['real_ending_time']),
				'auction_status_id'       => (int)$result['auction_status_id'],
				'auction_status'          => isset($this->data['auction_statuses'][(string)$result['auction_status_id']]) ? stripslashes($this->data['auction_statuses'][(string)$result['auction_status_id']]) : '',
				'selected'                => isset($this->request->post['selected']) && in_array($result['auction_id'], $this->request->post['selected']),
				'product_name'            => stripslashes($result['product_name']),
				'product_image'           => $product_image,
				'action'                  => $action
			);
    	}
		
		$this->data['heading_title'] = $this->language->get('heading_title');		
				
		$this->data['symbol_left'] = $this->currency->getSymbolLeft($this->config->get('config_currency'));
		$this->data['symbol_right'] = $this->currency->getSymbolRight($this->config->get('config_currency'));
		$this->data['decimal_place'] = $this->currency->getDecimalPlace($this->config->get('config_currency'));
		$this->data['decimal_point'] = $this->language->get('decimal_point');
		$this->data['thousand_point'] = $this->language->get('thousand_point');

		$this->data['text_day'] = $this->language->get('text_day');
		$this->data['text_hour'] = $this->language->get('text_hour');
		$this->data['text_minute'] = $this->language->get('text_minute');
		$this->data['text_second'] = $this->language->get('text_second');

		$this->data['text_getstarted'] = $this->language->get('text_getstarted');
		$this->data['text_goinggoing'] = $this->language->get('text_goinggoing');
		$this->data['text_no_results'] = $this->language->get('text_no_results');		
			
		$this->data['column_product'] = $this->language->get('column_product');		
		$this->data['column_auction_name'] = $this->language->get('column_auction_name');		
		$this->data['column_starting_time'] = $this->language->get('column_starting_time');		
		$this->data['column_highest_bid'] = $this->language->get('column_highest_bid');		
		$this->data['column_total_no_of_bid'] = $this->language->get('column_total_no_of_bid');		
		$this->data['column_time_left'] = $this->language->get('column_time_left');		
		$this->data['column_auction_status'] = $this->language->get('column_auction_status');		
		$this->data['column_action'] = $this->language->get('column_action');		
				
		$this->data['button_delete'] = $this->language->get('button_delete');		
		$this->data['button_filter'] = $this->language->get('button_filter');
		 
 		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['delete'])) {
			$this->data['error_delete'] = $this->error['delete'];
		} else {
			$this->data['error_delete'] = array();
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if (isset($this->request->get['filter_auction_name'])) {
			$url .= '&filter_auction_name=' . urlencode(html_entity_decode($this->request->get['filter_auction_name'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['filter_starting_time'])) {
			$url .= '&filter_starting_time=' . urlencode(html_entity_decode($this->request->get['filter_starting_time'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['filter_highest_bid'])) {
			$url .= '&filter_highest_bid=' . $this->request->get['filter_highest_bid'];
		}
		
		if (isset($this->request->get['filter_total_no_of_bid'])) {
			$url .= '&filter_total_no_of_bid=' . $this->request->get['filter_total_no_of_bid'];
		}
		
		if (isset($this->request->get['filter_auction_status_id'])) {
			$url .= '&filter_auction_status_id=' . $this->request->get['filter_auction_status_id'];
		}
								
		if (isset($this->request->get['filter_real_ending_time'])) {
			$url .= '&filter_real_ending_time=' . urlencode(html_entity_decode($this->request->get['filter_real_ending_time'], ENT_QUOTES, 'UTF-8'));
		}
		
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
					
		$this->data['sort_auction_name'] = $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . '&sort=pa.auction_name' . $url, 'SSL');
		$this->data['sort_starting_time'] = $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . '&sort=pa.starting_time' . $url, 'SSL');
		$this->data['sort_highest_bid'] = $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . '&sort=a.highest_bid_amount' . $url, 'SSL');
		$this->data['sort_total_no_of_bid'] = $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . '&sort=a.total_no_of_bid' . $url, 'SSL');
		$this->data['sort_auction_status'] = $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . '&sort=auction_status' . $url, 'SSL');
		$this->data['sort_time_left'] = $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . '&sort=a.real_ending_time' . $url, 'SSL');
		
		$url = '';

		if (isset($this->request->get['filter_auction_name'])) {
			$url .= '&filter_auction_name=' . urlencode(html_entity_decode($this->request->get['filter_auction_name'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['filter_starting_time'])) {
			$url .= '&filter_starting_time=' . urlencode(html_entity_decode($this->request->get['filter_starting_time'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['filter_highest_bid'])) {
			$url .= '&filter_highest_bid=' . $this->request->get['filter_highest_bid'];
		}
		
		if (isset($this->request->get['filter_total_no_of_bid'])) {
			$url .= '&filter_total_no_of_bid=' . $this->request->get['filter_total_no_of_bid'];
		}

		if (isset($this->request->get['filter_auction_status_id'])) {
			$url .= '&filter_auction_status_id=' . $this->request->get['filter_auction_status_id'];
		}

		if (isset($this->request->get['filter_real_ending_time'])) {
			$url .= '&filter_real_ending_time=' . urlencode(html_entity_decode($this->request->get['filter_real_ending_time'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		$pagination = new Pagination();
		$pagination->total = $auction_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();
	
		$this->data['filter_auction_name'] = $filter_auction_name;
		$this->data['filter_starting_time'] = $filter_starting_time;
		$this->data['filter_highest_bid'] = $filter_highest_bid;
		$this->data['filter_total_no_of_bid'] = $filter_total_no_of_bid;
		$this->data['filter_auction_status_id'] = $filter_auction_status_id;
		$this->data['filter_real_ending_time'] = $filter_real_ending_time;
		
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'sale/auction_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}

	public function info() {
		$this->load->model('sale/auction');

		if (isset($this->request->get['auction_id'])) {
			$auction_id = $this->request->get['auction_id'];
		} else {
			$auction_id = 0;
		}

		$auction_info = $this->model_sale_auction->getAuction($auction_id);

		if ($auction_info) {
			$this->language->load('sale/auction');

			$this->document->setTitle($this->language->get('heading_title'));

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

			$this->data['text_day'] = $this->language->get('text_day');
			$this->data['text_hour'] = $this->language->get('text_hour');
			$this->data['text_minute'] = $this->language->get('text_minute');
			$this->data['text_second'] = $this->language->get('text_second');
			$this->data['text_yes'] = $this->language->get('text_yes');
			$this->data['text_no'] = $this->language->get('text_no');

			$this->data['text_auction_id'] = $this->language->get('text_auction_id');
			$this->data['text_auction_name'] = $this->language->get('text_auction_name');
			$this->data['text_auction_status'] = $this->language->get('text_auction_status');
			$this->data['text_real_ending_time'] = $this->language->get('text_real_ending_time');		
			$this->data['text_time_left'] = $this->language->get('text_time_left');
			$this->data['text_actual_duration'] = $this->language->get('text_actual_duration');
			$this->data['text_highest_bid_amount'] = $this->language->get('text_highest_bid_amount');
			$this->data['text_highest_bidder'] = $this->language->get('text_highest_bidder');
			$this->data['text_highest_bidder_ip'] = $this->language->get('text_highest_bidder_ip');
			$this->data['text_last_bid_time'] = $this->language->get('text_last_bid_time');
			$this->data['text_total_no_of_bid'] = $this->language->get('text_total_no_of_bid');
			$this->data['text_total_auto_bid'] = $this->language->get('text_total_auto_bid');
			$this->data['text_reserve_met'] = $this->language->get('text_reserve_met');		
			$this->data['text_buyout_made'] = $this->language->get('text_buyout_made');
			$this->data['text_extension_count'] = $this->language->get('text_extension_count');
			$this->data['text_relisting_count'] = $this->language->get('text_relisting_count');
			$this->data['text_link_order_id'] = $this->language->get('text_link_order_id');
			$this->data['text_date_added'] = $this->language->get('text_date_added');
			$this->data['text_date_modified'] = $this->language->get('text_date_modified');
			$this->data['text_date_closed'] = $this->language->get('text_date_closed');

			$this->data['text_product_image'] = $this->language->get('text_product_image');
			$this->data['text_product_name'] = $this->language->get('text_product_name');
			$this->data['text_reserved_quantity'] = $this->language->get('text_reserved_quantity');			
			$this->data['text_starting_time'] = $this->language->get('text_starting_time');
			$this->data['text_ending_time'] = $this->language->get('text_ending_time');
			$this->data['text_starting_price'] = $this->language->get('text_starting_price');
			$this->data['text_reserve_price'] = $this->language->get('text_reserve_price');
			$this->data['text_buyout_price'] = $this->language->get('text_buyout_price');
			$this->data['text_min_increment'] = $this->language->get('text_min_increment');
			$this->data['text_allow_auto_bid'] = $this->language->get('text_allow_auto_bid');
			$this->data['text_fast_fwd_auto_bids'] = $this->language->get('text_fast_fwd_auto_bids');
			$this->data['text_allow_auto_extend'] = $this->language->get('text_allow_auto_extend');
			$this->data['text_extension_duration'] = $this->language->get('text_extension_duration');
			$this->data['text_extension_window'] = $this->language->get('text_extension_window');
			$this->data['text_getstarted'] = $this->language->get('text_getstarted');
			$this->data['text_goinggoing'] = $this->language->get('text_goinggoing');	

			$this->data['column_bid_winner'] = $this->language->get('column_bid_winner');
			$this->data['column_bid_amount'] = $this->language->get('column_bid_amount');
			$this->data['column_bid_type'] = $this->language->get('column_bid_type');
			$this->data['column_bidder'] = $this->language->get('column_bidder');
			$this->data['column_bid_time'] = $this->language->get('column_bid_time');
			$this->data['column_bid_ip_address'] = $this->language->get('column_bid_ip_address');
							
			$this->data['tab_auction_detail'] = $this->language->get('tab_auction_detail');
			$this->data['tab_auction_setting'] = $this->language->get('tab_auction_setting');
			$this->data['tab_bid_history'] = $this->language->get('tab_bid_history');
			$this->data['tab_similar_auction'] = $this->language->get('tab_similar_auction');
		
			$this->data['token'] = $this->session->data['token'];

			$url = '';

			if (isset($this->request->get['filter_auction_name'])) {
				$url .= '&filter_auction_name=' . urlencode(html_entity_decode($this->request->get['filter_auction_name'], ENT_QUOTES, 'UTF-8'));
			}
			
			if (isset($this->request->get['filter_starting_time'])) {
				$url .= '&filter_starting_time=' . urlencode(html_entity_decode($this->request->get['filter_starting_time'], ENT_QUOTES, 'UTF-8'));
			}
			
			if (isset($this->request->get['filter_highest_bid'])) {
				$url .= '&filter_highest_bid=' . $this->request->get['filter_highest_bid'];
			}
			
			if (isset($this->request->get['filter_total_no_of_bid'])) {
				$url .= '&filter_total_no_of_bid=' . $this->request->get['filter_total_no_of_bid'];
			}		

			if (isset($this->request->get['filter_auction_status_id'])) {
				$url .= '&filter_auction_status_id=' . $this->request->get['filter_auction_status_id'];
			}

			if (isset($this->request->get['filter_real_ending_time'])) {
				$url .= '&filter_real_ending_time=' . urlencode(html_entity_decode($this->request->get['filter_real_ending_time'], ENT_QUOTES, 'UTF-8'));
			}
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

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

			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
				'separator' => false
			);

			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('heading_title'),
				'href'      => $this->url->link('sale/auction', 'token=' . $this->session->data['token'] . $url, 'SSL'),				
				'separator' => ' :: '
			);

			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 ($auction_info['auction_status_id'] == $auction_status_ids['starting'] || $auction_info['auction_status_id'] == $auction_status_ids['relisting'] || $auction_info['auction_status_id'] == $auction_status_ids['inprogress'] || $auction_info['auction_status_id'] == $auction_status_ids['extended']) {
				$this->data['active'] = true;
			} else {
				$this->data['active'] = false;
			}

			$this->data['auction_id'] = (int)$this->request->get['auction_id'];
			$this->data['auction_name'] = stripslashes($auction_info['auction_name']);
			$this->data['auction_status'] = isset($this->data['auction_statuses'][(string)$auction_info['auction_status_id']]) ? stripslashes($this->data['auction_statuses'][(string)$auction_info['auction_status_id']]) : '';
			$this->data['real_ending_time_text'] = (int)$auction_info['buyout_made'] ? date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), strtotime($auction_info['date_closed'])) : date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), (int)$auction_info['real_ending_time']);

			$this->data['starting_time'] = (int)$auction_info['starting_time'];
			$this->data['real_ending_time'] = (int)$auction_info['real_ending_time'];
			$this->data['auction_status_id'] = (int)$auction_info['auction_status_id'];

			$real_ending_time = (int)$auction_info['buyout_made'] ? strtotime($auction_info['date_closed']) : (int)$auction_info['real_ending_time'];
			$actual_duration = $real_ending_time - (int)$auction_info['starting_time'];
			$this->data['actual_duration'] = floor($actual_duration / 60 / 60 / 24) . $this->language->get('text_day') . ' ' . substr('0' . (string)floor($actual_duration / 60 / 60 % 24), -2) . $this->language->get('text_hour') . ' ' . substr('0' . (string)floor($actual_duration / 60 % 60 % 24), -2) . $this->language->get('text_minute') . ' ' . substr('0' . (string)floor($actual_duration % 60 % 60 % 24), -2) . $this->language->get('text_second');

			$this->data['highest_bid_amount'] = $this->currency->format((float)$auction_info['highest_bid_amount'], $this->config->get('config_currency'), '', false);
			$this->data['highest_bid_amount_text'] = $this->currency->format((float)$auction_info['highest_bid_amount'], $this->config->get('config_currency'));

			if ($auction_info['highest_bidder_id']) {
				$this->data['customer_href'] = $this->url->link('sale/customer/update', 'token=' . $this->session->data['token'] . '&customer_id=' . $auction_info['highest_bidder_id'], 'SSL');
				$this->data['highest_bidder_masked'] = substr(preg_replace("/[^a-z0-9]/", '', strtolower(stripslashes($auction_info['customer_email']))), 0, 3) . 'xxxxxxx';
				$this->data['highest_bidder_ip'] = stripslashes($auction_info['ip_address']);
				$this->data['highest_bid_time'] = (int)$auction_info['highest_bid_time'];
				$this->data['last_bid_time_text'] = date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), (int)$auction_info['highest_bid_time']);
				$this->data['highest_bidder_id'] = (int)$auction_info['highest_bidder_id'];
			} else {
				$this->data['customer_href'] = $this->language->get('text_no_bid_yet');
				$this->data['highest_bidder_masked'] = $this->language->get('text_no_bid_yet');
				$this->data['highest_bidder_ip'] = $this->language->get('text_no_bid_yet');
				$this->data['highest_bid_time'] = 0;
				$this->data['last_bid_time_text'] = $this->language->get('text_no_bid_yet');
				$this->data['highest_bidder_id'] = 0;
			}

			$this->data['total_no_of_bid'] = (int)$auction_info['total_no_of_bid'];
			$this->data['total_auto_bid'] = (int)$auction_info['total_auto_bid'];
			$this->data['reserve_met'] = (int)$auction_info['reserve_met'];
			$this->data['buyout_made'] = (int)$auction_info['buyout_made'];
			$this->data['reserve_met_text'] = (int)$auction_info['reserve_met'] ? $this->language->get('text_yes') : $this->language->get('text_no');
			$this->data['buyout_made_text'] = (int)$auction_info['buyout_made'] ? $this->language->get('text_yes') : $this->language->get('text_no');
			$this->data['extension_count'] = (int)$auction_info['extension_count'];
			$this->data['relisting_count'] = (int)$auction_info['relisting_count'];

			if ($auction_info['order_auction_id']) {
				$this->data['order_href'] = $this->url->link('sale/order/info', 'token=' . $this->session->data['token'] . '&order_id=' . $auction_info['order_id'], 'SSL');
				$this->data['order_text'] = $this->language->get('text_order');
				$this->data['order_id'] = (int)$auction_info['order_id'];
			} else {
				$this->data['order_href'] = '';
				$this->data['order_text'] = $this->language->get('text_not_paid');
				$this->data['order_id'] = 0;
			}

			if (strtotime($auction_info['date_added']) && strtotime($auction_info['date_added']) > 0) {
				$this->data['date_added_text'] = date($this->language->get('date_format_long') . ', ' . $this->language->get('time_format'), strtotime($auction_info['date_added']));
			} else {
				$this->data['date_added_text'] = '';
			}

			if (strtotime($auction_info['date_modified']) && strtotime($auction_info['date_modified']) > 0) {
				$this->data['date_modified_text'] = date($this->language->get('date_format_long') . ', ' . $this->language->get('time_format'), strtotime($auction_info['date_modified']));
			} else {
				$this->data['date_modified_text'] = '';
			}

			if (strtotime($auction_info['date_closed']) && strtotime($auction_info['date_closed']) > 0) {
				$this->data['date_closed_text'] = date($this->language->get('date_format_long') . ', ' . $this->language->get('time_format'), strtotime($auction_info['date_closed']));
			} else {
				$this->data['date_closed_text'] = '';
			}

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

			if ($auction_info['product_image'] && file_exists(DIR_IMAGE . $auction_info['product_image'])) {
				$this->data['product_image'] = $this->model_tool_image->resize($auction_info['product_image'], 100, 100);
			} else {
				$this->data['product_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
			}

			$this->data['product_id'] = (int)$auction_info['product_id'];
			$this->data['product_href'] = $this->url->link('catalog/product/update', 'token=' . $this->session->data['token'] . '&product_id=' . $auction_info['product_id'], 'SSL');
			$this->data['product_name'] = stripslashes($auction_info['product_name']);

			$this->data['reserved_quantity'] = (int)$auction_info['reserved_quantity'];
			$this->data['starting_time_text'] = date($this->language->get('date_format_long') . ', ' . $this->language->get('time_format'), (int)$auction_info['starting_time']);
			$this->data['ending_time_text'] = date($this->language->get('date_format_long') . ', ' . $this->language->get('time_format'), (int)$auction_info['ending_time']);
			$this->data['starting_price_text'] = $this->currency->format((float)$auction_info['starting_price'], $this->config->get('config_currency'));
			$this->data['reserve_price_text'] = $this->currency->format((float)$auction_info['reserve_price'], $this->config->get('config_currency'));
			$this->data['buyout_price_text'] = $this->currency->format((float)$auction_info['buyout_price'], $this->config->get('config_currency'));
			$this->data['min_increment_text'] = $this->currency->format((float)$auction_info['min_increment'], $this->config->get('config_currency'));

			$this->data['allow_auto_bid_text'] = (int)$auction_info['allow_auto_bid'] ? $this->language->get('text_yes') : $this->language->get('text_no');
			$this->data['fast_fwd_auto_bids_text'] = (int)$auction_info['fast_fwd_auto_bids'] ? $this->language->get('text_yes') : $this->language->get('text_no');
			$this->data['allow_extension_text'] = (int)$auction_info['allow_auto_extend'] ? $this->language->get('text_yes') : $this->language->get('text_no');
			$this->data['extension_duration'] = (int)$auction_info['extension_duration'];
			$this->data['extension_window'] = (int)$auction_info['extension_window'];

			$this->template = 'sale/auction_info.tpl';
			$this->children = array(
				'common/header',
				'common/footer'
			);
			
			$this->response->setOutput($this->render());
		} else {
			$this->language->load('error/not_found');

			$this->document->setTitle($this->language->get('heading_title'));

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

			$this->data['text_not_found'] = $this->language->get('text_not_found');

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

			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
				'separator' => false
			);

			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('heading_title'),
				'href'      => $this->url->link('error/not_found', 'token=' . $this->session->data['token'], 'SSL'),
				'separator' => ' :: '
			);
		
			$this->template = 'error/not_found.tpl';
			$this->children = array(
				'common/header',
				'common/footer'
			);
		
			$this->response->setOutput($this->render());
		}	
	}
	
	public function history() {
    	$this->language->load('sale/auction');
		
		$this->load->model('sale/auction');
	
		if ($this->config->get('config_auction_bid_status_id') && is_array($this->config->get('config_auction_bid_status_id'))) {
			$auction_bid_status_ids = $this->config->get('config_auction_bid_status_id');
		} else {
			$auction_bid_status_ids = array('winning' => 1, 'outbid' => 2, 'buyout' => 3, 'voided' => 4);
		}

		$this->data['text_no_results'] = $this->language->get('text_no_results');
		
		$this->data['column_bid_amount'] = $this->language->get('column_bid_amount');
		$this->data['column_bid_type'] = $this->language->get('column_bid_type');
		$this->data['column_bidder'] = $this->language->get('column_bidder');
		$this->data['column_bid_time'] = $this->language->get('column_bid_time');
		$this->data['column_bid_ip_address'] = $this->language->get('column_bid_ip_address');

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}  
		
		$this->data['histories'] = array();
			
		$results = $this->model_sale_auction->getAuctionBids($this->request->get['auction_id'], ($page - 1) * 20, 20);
      		
		foreach ($results as $result) {

			if ($result['auction_bid_status_id'] == $auction_bid_status_ids['voided']) {
				$bid_type = $this->language->get('text_bid_type_voided');
			} elseif ($result['auction_bid_status_id'] == $auction_bid_status_ids['buyout']) {
				$bid_type = $this->language->get('text_bid_type_buyout');
			} elseif ($result['auction_bid_status_id'] == $auction_bid_status_ids['winning']) {
				$bid_type = $this->language->get('text_bid_type_winning');
			} elseif ($result['is_auto_bid']) {
				$bid_type = $this->language->get('text_bid_type_auto');
			} else {
				$bid_type = $this->language->get('text_bid_type_manual');
			}

			$this->data['histories'][] = array(
				'bid_amount_text' => $this->currency->format((float)$result['bid_amount'], $this->config->get('config_currency')),
				'bid_type'        => $bid_type,
				'customer_href'   => $this->url->link('sale/customer/update', 'token=' . $this->session->data['token'] . '&customer_id=' . $result['customer_id'], 'SSL'),
				'customer_name'   => stripslashes($result['customer_name']),
				'bid_time_text'   => date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), (int)$result['bid_time']),
				'ip_address'      => stripslashes($result['ip_address'])
        	);
      	}			
		
		$history_total = $this->model_sale_auction->getTotalAuctionBids($this->request->get['auction_id']);
			
		$pagination = new Pagination();
		$pagination->total = $history_total;
		$pagination->page = $page;
		$pagination->limit = 20; 
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('sale/auction/history', 'token=' . $this->session->data['token'] . '&auction_id=' . $this->request->get['auction_id'] . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();
		
		$this->template = 'sale/auction_history.tpl';		
		
		$this->response->setOutput($this->render());
  	}
	
	public function similar() {
    	$this->language->load('sale/auction');
		
		$this->load->model('sale/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->data['text_no_results'] = $this->language->get('text_no_results');
		
		$this->data['column_auction_name'] = $this->language->get('column_auction_name');
		$this->data['column_starting_time'] = $this->language->get('column_starting_time');
		$this->data['column_highest_bid'] = $this->language->get('column_highest_bid');
		$this->data['column_total_no_of_bid'] = $this->language->get('column_total_no_of_bid');
		$this->data['column_auction_status'] = $this->language->get('column_auction_status');

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}  
		
		$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'];
				}
			}
		}
		
		$this->data['similars'] = array();
			
		$results = $this->model_sale_auction->getAuctionSimilars($this->request->get['product_id'], ($page - 1) * 20, 20);
      		
		foreach ($results as $result) {
			if ($result['auction_id'] != $this->request->get['auction_id']) {
				$this->data['similars'][] = array(
					'auction_href'            => $this->url->link('sale/auction/info', 'token=' . $this->session->data['token'] . '&auction_id=' . $result['auction_id'], 'SSL'),
					'auction_name'            => stripslashes($result['auction_name']),
					'starting_time_text'      => date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), (int)$result['starting_time']),
					'highest_bid_amount_text' => $this->currency->format((float)$result['highest_bid_amount'], $this->config->get('config_currency')),
					'total_no_of_bid'         => (int)$result['total_no_of_bid'],
					'auction_status'          => isset($this->data['auction_statuses'][(string)$result['auction_status_id']]) ? stripslashes($this->data['auction_statuses'][(string)$result['auction_status_id']]) : ''
				);
			}
      	}
		
		$similar_total = $this->model_sale_auction->getTotalAuctionSimilars($this->request->get['product_id']);
			
		$pagination = new Pagination();
		$pagination->total = $similar_total;
		$pagination->page = $page;
		$pagination->limit = 20; 
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('sale/auction/similar', 'token=' . $this->session->data['token'] . '&product_id=' . $this->request->get['product_id'] . '&auction_id=' . $this->request->get['auction_id'] . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();
		
		$this->template = 'sale/auction_similar.tpl';		
		
		$this->response->setOutput($this->render());
  	}
	
  	protected function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'sale/auction')) {
      		$this->error['warning'] = $this->language->get('error_permission');  
    	}
		
		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('catalog/product');
		
		if (isset($this->request->post['selected'])) {
			foreach ($this->request->post['selected'] as $auction_id) {
				$total_auctions = (int)$this->model_catalog_product->getTotalProductStartedAuctions($auction_id, $auction_status_ids);
				if ($total_auctions > 0) {
					$this->error['warning'] = $this->language->get('error_delete');
					$this->error['delete'][$auction_id] = true;
				}
			}
		}

		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}
  	
	public function autocomplete() {
		$json = array();
		
		if (isset($this->request->get['filter_auction_name'])) {
			$this->load->model('sale/auction');
			
			$data = array(
				'filter_auction_name' => $this->request->get['filter_auction_name'],
				'start'               => 0,
				'limit'               => 20
			);
		
			$results = $this->model_sale_auction->getAuctions($data);
			
			foreach ($results as $result) {
				$json[] = array(
					'product_id'        => (int)$result['product_id'],
					'product_name'      => strip_tags(html_entity_decode($result['product_name'], ENT_QUOTES, 'UTF-8')),
					'auction_id'        => (int)$result['auction_id'], 
					'auction_name'      => strip_tags(html_entity_decode($result['auction_name'], ENT_QUOTES, 'UTF-8'))
				);					
			}
		}

		$sort_order = array();
	  
		foreach ($json as $key => $value) {
			$sort_order[$key] = $value['product_name'];
		}

		array_multisort($sort_order, SORT_ASC, $json);

		$this->response->setOutput(json_encode($json));
	}
}
?>