<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

include_once 'main.class.php';
include_once 'item.class.php';

class Export extends Main {

	public $list_table_config = array (
		'title' => 'Preissuchmaschinen',
		'db_table' => 'export',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Name',
				'sortable' => 1
			),
			array(
				'db_field' => 'count_exported_items',
				'name' => 'Exportierte Artikel',
				'sortable' => 1
			),
			array(
				'db_field' => 'last_export_date',
				'name' => 'Letzter Export',
				'sortable' => 1
			)
		),
		'search_fields' => array('name'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_price_search_engine_editor&id=',
		'toolbar' => array(
			'delete' => '0',
			'new' => '',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		)
	);

	private $object_fields = array(
		'name' => 'text',
		'customer_group_id' =>'integer',
		'all_item_groups' => 'integer',
		'export_filter' => 'text',
		'count_exported_items' => 'integer'
	);

	protected $base_object;
	protected $db;
	private $customer_number;
	private $seperator;

	function __construct($base_object) {
		parent::__construct($base_object);
		$this->db = $base_object->db;
		$this->base_object = $base_object;
		$this->customer_number = '';
		$this->seperator = "|";
	} // end __construct

	public function set_customer_number($customer_number) {
		$this->customer_number = $customer_number;
	} // end set_customer_number

	public function set_seperator($seperator) {
		$this->seperator = $seperator;
	} // end set_seperator

	public function get_all() {
		$sql = "SELECT * FROM export";

		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = $obj;
		}

		return $data;
	} // end get_all

	public function get_data() {
		if ($this->id) {
			$sql = "SELECT * FROM export_item_groups WHERE export_id = ".$this->id;

			$result = $this->db->query($sql);
			$structure_ids = array();
			while ($obj = $result->fetch_object()) {
				$structure_ids[$obj->structure_id] = 1;
			}
			$sql = "SELECT * FROM export WHERE id = ".$this->id;

			$result = $this->db->query($sql);
			$obj = $result->fetch_object();
			$obj->structure_ids = $structure_ids;

			return $obj;
		}

		return false;
	} // end get_data

	public function delete() {
		$sql = "DELETE FROM export WHERE id=".$this->id;

		$this->db->query($sql);

		return;
	}// end delete

	public function data_filter($request) {
		$data = array();

		foreach ($this->object_fields as $field_name => $var_type) {
			if (isset($request[$field_name])) {
				if ($var_type == 'text') {
					$data[$field_name] = $request[$field_name];
				} else {
					$data[$field_name] = (int)$request[$field_name];
				}
			}
		}

		return $data;
	} // end data_filter

	public function create($data) {
		if ($data) {
			$sql = "INSERT INTO export SET ";

			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);
				if ($this->object_fields[$var_name] == 'integer') {
					$sql .= $var_name.' = '.$value.', ';
				} else {
					$sql .= $var_name.' = "'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$this->db->query($sql);
			$this->id = $this->db->insert_id;
		}

		return;
	} // end create

	public function update($data) {
		if ($data) {
			$sql = "UPDATE export SET ";

			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);
				if ($this->object_fields[$var_name] == 'integer') {
					$sql .= $var_name.'='.$value.', ';
				} else {
					$sql .= $var_name.'="'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$sql .= ' WHERE id='.$this->id;
			$this->db->query($sql);
		}

		return;
	} // end update


	public function update_selected_structure($data) {

		$sql = "DELETE FROM export_item_groups WHERE export_id=".$this->id;
		$this->db->query($sql);
		if ($data) {
			foreach ($data as $value) {
				$sql = "INSERT INTO export_item_groups SET ";
				$value = $this->db->real_escape_string($value);
				$sql .= 'export_id="'.$this->id.'", ';
				$sql .= 'structure_id="'.$value.'"';
				$this->db->query($sql);
			}
		}

		return;
	} // end update


	public function get_all_paginated($items , $page, $order = false) {
		$page = ($page - 1) * $items;

		$sql = "SELECT * FROM export LIMIT $items OFFSET $page";

		$result = $this->db->query($sql);
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}

		return $data;
	} // end get_all_paginated


	public function delete_by_id($id) {
		$rs = $this->db->query("DELETE FROM export
				WHERE id=$id;");
	} // end delete_by_id

	public static function get_name_by_id($id) {
		$db = Registry::get('base')->db;

		$sql = "SELECT
					name
				FROM
					export
				WHERE id = $id";

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			$return = $result->fetch_object();

			return $return->name;
		}

		return false;
	} // end get_name_by_id


	public function get_export_csv($export_name) {
		$sql = "SELECT * FROM export WHERE name = '".$this->db->real_escape_string($export_name)."'";

		$result = $this->db->query($sql);

		if ($result->num_rows > 0) {
			$obj = $result->fetch_object();
			// get items

			$item_count = 0;
			if ($obj->set_customer_group_id) {
				$this->base_object->customer_group->id = $obj->customer_group_id;
			}

			$item_object = new Item($this->base_object);
			if ($obj->id == 5) {
				$items = $item_object->get_flat_items();
			} else {
				$items = $item_object->get_all_items_list();
			}
			$data_array = array();

			// load filter
			include_once './core/export_filter/'.$obj->export_filter;

			// make csv file
			$csv = '';
			foreach ($data_array as $row) {
				//$csv .= implode(";",$row)."\r\n";
				$csv .= implode($this->seperator,$row)."\r\n";
				//$csv .= implode("|",$row)."<br>";
				$item_count++;
			}

			// update
			$sql = "
				UPDATE export
				SET last_export_date = current_timestamp,
					count_exported_items = $item_count
				WHERE id = ".$obj->id."
			";
			$this->db->query($sql);

			return $csv;
		}

		return false;
	}

} // end Export

?>