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

class Vendor extends Main {

	protected $base_object;
	protected $db;

	public $list_table_config = array (
		'title' => 'Lieferanten',
		'db_table' => 'herstellerinformationen',
		'list_fields' => array(
			array(
				'db_field' => 'marke_name',
				'name' => 'Marke',
				'sortable' => 1
			),
			array(
				'db_field' => 'hersteller_name',
				'name' => 'Hersteller',
				'sortable' => 1
			),
			array(
				'db_field' => 'ort',
				'name' => 'Ort',
				'sortable' => 1
			),
			array(
				'db_field' => 'land',
				'name' => 'Land',
				'sortable' => 1
			),
			array(
				'db_field' => 'email',
				'name' => 'E-Mail',
				'sortable' => 1
			),
			array(
				'db_field' => 'homepage',
				'name' => 'Website',
				'sortable' => 1
			)
		),
		'default_sort_item' => 'hersteller_name',
		'default_sort_direction' => 'up',
		'search_fields' => array('marke_name', 'hersteller_name', 'email', 'ort'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_vendor_editor&object_id=',
		'close' => 'index.php?admin_modul=admin_object_list&object=Vendor',
		'delete_link' => 'index.php?admin_modul=admin_vendor_editor&action=delete',
		'add_link' => 'index.php?admin_modul=admin_vendor_editor'
	);


	public function __construct($base_object) {
		global $config_object;
		$this->config = $base_object->config;
		$this->db = $base_object->db;
		$this->base_object = $base_object;
	}

	public function state_text() {
		return array (
			'1' => 'aktiv',
			'0' => 'inaktiv'
		);
	}


	public function country_name() {
		$country_object = new Country($this->base_object);
		return $country_object->get_all_names();
	}


	public function get_all() {
		$sql = "SELECT * FROM herstellerinformationen ORDER BY marke_name ASC";
		$result = $this->db->query($sql);

		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}

		return $data;
	}

	public function get_all_names() {
		$result = $this->db->query("SELECT id, hersteller_name FROM herstellerinformationen ORDER BY marke_name ASC");

		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->id] = $obj->hersteller_name;
		}

		return $data;
	}

	public function get_by_id($id) {
		$sql = "SELECT * FROM herstellerinformationen WHERE id = " . (int)$id;
		$result = $this->db->query($sql);
		return $result->fetch_object();
	}

	public function set_by_id($data, $id) {
		$data = Database::clean($data);
		$sql = Database::update('herstellerinformationen', $data, ' WHERE id = '.(int)$id);
		return $this->db->query($sql);
	}

	public function delete_by_id($id) {
		$sql = "DELETE FROM herstellerinformationen WHERE id = " . (int)$id;
		return $this->db->query($sql);
	}

	public function get_active() {
		$sql = "SELECT id, hersteller_name FROM herstellerinformationen ORDER BY marke_name ASC";
		$result = $this->db->query($sql);

		$data = array();
		while ($row = $result->fetch_object()) {
			$data[$row->id] = $row->hersteller_name;
		}

		return $data;
	}

	public function get_dropdown_data() {
		$sql = "SELECT * FROM herstellerinformationen ORDER BY marke_name ASC";
		$result = $this->db->query($sql);

		$data = array();
		while ($obj = $result->fetch_object()) {
			$vendor = new stdClass();
			$vendor->id = $obj->id;
			$vendor->hersteller_id = $obj->id;  // Wichtig für das Template
			if (stripos($obj->hersteller_name, $obj->marke_name) === 0) {
				$vendor->name = $obj->hersteller_name;
			} else {
				$vendor->name = $obj->marke_name . ' - ' . $obj->hersteller_name;
			}
			$data[] = $vendor;
		}

		return $data;
	}

}