<?php
################################################################################################
#  Customer Import / Export from CSV for Opencart 1.5.x From HostJars opencart.hostjars.com  #
################################################################################################

class ModelToolCustomers extends Model {

	
	public function addCustomer($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int)$this->config->get('config_store_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (int)$data['newsletter'] . "', customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "', status = '1', date_added = NOW()");
		 
		$customer_id = $this->db->getLastId();
			
		$this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', company = '" . $this->db->escape($data['company']) . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', city = '" . $this->db->escape($data['city']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', country_id = '" . (int)$data['country_id'] . "', zone_id = '" . (int)$data['zone_id'] . "'");
	
		$address_id = $this->db->getLastId();
	
		$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
	
		if (!$this->config->get('config_customer_approval')) {
			$this->db->query("UPDATE " . DB_PREFIX . "customer SET approved = '1' WHERE customer_id = '" . (int)$customer_id . "'");
		}
	}
	
	public function getCustomers() {
		$sql = "SELECT * FROM " . DB_PREFIX . "customer c LEFT JOIN " . DB_PREFIX . "address a ON (c.customer_id = a.customer_id)";
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getCountryIdByName($country_name) {
		$query = $this->db->query('SELECT country_id FROM ' . DB_PREFIX . "country WHERE name = '" . ucfirst($this->db->escape($country_name)) . "'");
		if ($query->num_rows) {
			return $query->row['country_id'];
		} else {
			return 0;
		}
	}
	
	public function getCountry($country_id) {
		$query = $this->db->query("SELECT name FROM " . DB_PREFIX . "country WHERE country_id = '" . (int)$country_id . "'");
		if ($query->num_rows){
			return $query->row['name'];
		} else {
			return '';
		}
	}
	
	public function getZoneIdByName($zone_name) {
		$query = $this->db->query('SELECT zone_id FROM ' . DB_PREFIX . "zone WHERE name = '" . ucfirst($this->db->escape($zone_name)) . "' AND status = 1");
		if ($query->num_rows) {
			return $query->row['zone_id'];
		} else {
			return 0;
		}
	}
	
	public function getZone($zone_id, $country_id) {
		$sql = "SELECT name FROM " . DB_PREFIX . "zone WHERE zone_id = '" . (int)$zone_id . "'";
		$query = $this->db->query($sql);
		if ($query->num_rows){
			return $query->row['name'];
		} else {
			return '';
		}
	}
	
	public function export($fields) {
		
		$customers = $this->getCustomers();
		
		
		//headings
		$output = implode(",", $fields['export']) . "\n";
		
		foreach ($customers as $customer) {
			$customer_output = array();
			//load extra stuff into customer array
			if ($customer['country_id']) {
				$customer['country_id'] = $this->getCountry($customer['country_id']);
				if ($customer['zone_id']) {
					$customer['zone_id'] = $this->getZone($customer['zone_id'], $customer['country_id']);
				}
			}
			//loop over desired fields and print each one
			$customer_data = array();
			foreach ($fields['export'] as $field) {
				if (isset($customer[$field])) {
					$customer_output[] = '"' . $customer[$field] . '"';
				} else {
					$customer_output[] = '';
				}
			}
			$output .= implode(",", $customer_output) . "\n";
			
		}
		$output .= "\n";
		

		return $output;	
	}
}
?>