<?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 './core/main.class.php';

class Import extends Main {

	public $list_table_config = array (
		'title' => 'Import',
		'db_table' => 'import',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Name',
				'sortable' => 1
			),
			array(
				'db_field' => 'count_imported_items',
				'name' => 'Importierte Datensätze',
				'sortable' => 1
			),
			array(
				'db_field' => 'last_import_date',
				'name' => 'Letzter Import',
				'sortable' => 1
			)
		),
		'search_fields' => array('name'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_import_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;

	function __construct($base_object) {
		parent::__construct($base_object);
		$this->db = $base_object->db;
		$this->base_object = $base_object;
	} // end __construct

	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 import WHERE id = ".$this->id;

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

			$obj = $result->fetch_object();

			return $obj;
		}

		return false;
	} // end get_data

	public function save_file($id, $temp_file = null) {
		// get import modul data
		$this->id = $id;
		$data = $this->get_data();
		$import_modul = $data->import_filter;

		// Verwende relativen Pfad statt ROOT_DIR
		$import_dir = './tmp/import/';

		// Prüfe ob tmp/import Ordner existiert
		if (!is_dir($import_dir)) {
			// Versuche auch tmp/ zu erstellen falls nicht vorhanden
			if (!is_dir('./tmp/')) {
				if (!mkdir('./tmp/', 0777, true)) {
					error_log("Konnte tmp-Verzeichnis nicht erstellen: ./tmp/");
					return false;
				}
			}
			if (!mkdir($import_dir, 0777, true)) {
				error_log("Konnte Import-Verzeichnis nicht erstellen: " . $import_dir);
				return false;
			}
		}

		// Bestimme Quell- und Ziel-Datei
		if ($temp_file && file_exists($temp_file)) {
			// Neuer Weg: temp_file direkt übergeben
			$source_file = $temp_file;
			$target_file = $import_dir . basename($_FILES['Filedata']['name']);
		} else {
			// Legacy-Weg: Aus $_FILES array
			$source_file = $_FILES['Filedata']['tmp_name'];
			$target_file = $import_dir . basename($_FILES['Filedata']['name']);
		}

		// save file
		if (move_uploaded_file($source_file, $target_file)) {
			$log_object = Logger::get_instance();
			if ($log_object) {
				$log_object->info('import', "Datei gespeichert: " . $target_file);
			}

			// Legacy: load import modul (wird nicht mehr verwendet mit Python)
			if (file_exists('./core/import_filter/' . $import_modul)) {
				include_once './core/import_filter/' . $import_modul;
			}

			return true;
		} else {
			error_log("move_uploaded_file fehlgeschlagen: $source_file -> $target_file");
			error_log("Current dir: " . getcwd());
			error_log("File exists: " . (file_exists($source_file) ? 'yes' : 'no'));
			error_log("Target dir writable: " . (is_writable(dirname($target_file)) ? 'yes' : 'no'));
			return false;
		}
	}

} // end Export

?>