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

class Manufacturer extends Main {
	protected $base_object;
	protected $db;

	public $list_table_config = array (
		'title' => 'Hersteller',
		'db_table' => 'manufacturers',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Herstellername',
				'sortable' => 1
			),
			array(
				'db_field' => 'email',
				'name' => 'E-Mail',
				'sortable' => 1
			),
			array(
				'db_field' => 'website',
				'name' => 'Webseite',
				'sortable' => 1
			),
			array(
				'db_field' => 'phone',
				'name' => 'Telefon',
				'sortable' => 1
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'rewrite_function' => 'state_text',
				'sortable' => 1
			)
		),
		'default_sort_item' => 'name',
		'default_sort_direction' => 'up',
		'search_fields' => array('name', 'email', 'website'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=Manufacturer&object_id=',
		'toolbar' => array(
			'delete' => '1',
			'new' => 'index.php?admin_modul=admin_object_edit&object=Manufacturer',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Hersteller',
		'edit_fields' => array (
			array(
				'name' => 'Stammdaten',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'values' => 'state_text',
				'type' => 'int'
			),
			array(
				'db_field' => 'name',
				'name' => 'Name',
				'type' => 'text'
			),
			array(
				'db_field' => 'description',
				'name' => 'Beschreibung',
				'type' => 'multitext'
			),
			array(
				'db_field' => 'short_uri',
				'name' => 'Kurz URL',
				'type' => 'text',
				'auto_value' => array(
					'function' => 'short_uri',
					'func_param' => array('default' => 'name')
				)
			),
			array(
				'name' => 'Kontakt',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'phone',
				'name' => 'Telefon',
				'type' => 'text'
			),
			array(
				'db_field' => 'email',
				'name' => 'E-Mail',
				'type' => 'text'
			),
			array(
				'db_field' => 'website',
				'name' => 'Webseite',
				'type' => 'text'
			),
			array(
				'name' => 'Herstellerlogo',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'logo',
				'name' => 'Logo',
				'type' => 'image',
				'file_directory' => '',
				'max_image_size' => array('height' => '70', 'width' => '70')
			)
		),
		'edit_mandatory_fields' => array('name'),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_edit&object=Manufacturer',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'delete' => 1
		)
	);

	public $id; // so i don't break old code

	private $config;
	private $path;
	private $url;
	private $error;
	private static $logo_size = array('height' => '70', 'width' => '70');

	public function __construct($base_object) {
		global $config_object;
		$this->base_object = $base_object;
		$this->config = $base_object->config;
		$this->db = $base_object->db;
		$this->path = ROOT_DIR.'web/'.SHOP_SYSTEM.'/images/manufacturers/';
		$this->url = './web/'.SHOP_SYSTEM.'/images/manufacturers/';
		$this->list_table_config['edit_fields'][10]['file_directory'] = $this->url;
		$this->id = false;
		$this->error = '';
	} // end __construct

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


	public function set_id($id) {
		$this->id = $id;
	}

	public function get_id() {
		return $this->id;
	}

	public function get_error() {
		return $this->error;
	}

	public function get_all() {
		$sql = "SELECT * FROM manufacturers ORDER BY name";

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

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

		return $data;
	} // end get_all


	public function get_all_names() {
		$sql = "SELECT id, name FROM manufacturers ORDER BY name";

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

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

		return $data;
	} // end get_all

	public function get_all_active() {
		$sql = "SELECT * FROM manufacturers WHERE active=1 ORDER BY name";

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

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

		return $data;
	} // end get_all_active

	public function get_by_id($id) {
		$sql = "SELECT * FROM manufacturers WHERE id = $id";

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

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

		return false;
	} // end get_by_id



	public function get_where_name_like($name) {
		$sql = "SELECT id FROM manufacturers WHERE name LIKE '%".$name."%'";

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

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

		return $data;
	} // end get_where_name_like

	public function get_active() {
		$sql = "SELECT id, name FROM manufacturers WHERE active = 1 ORDER BY name";

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

		return $data;
	} // end get_active

	public function get_data($id = false) {
		$sql = "SELECT * FROM manufacturers";

		if ($id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($id);
		} else if ($this->id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($this->id);
		} else {
			$this->error = 'no id';

			return false;
		}

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

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}
		$this->error = 'no data';

		return false;
	} // end get_data



	public function get_name_by_id($id) {
		$sql = "SELECT name FROM manufacturers where id=$id";

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

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

		return false;
	} // end get_name_by_id

	public function set_logo($id, $name) {
		$sql = "UPDATE manufacturers SET logo='$name' WHERE id=$id";
		return $this->db->query($sql);
	}

	public function delete_logo($id) {
		// get filename
		$sql = "SELECT logo	FROM manufacturers where id=$id";
		$result = $this->db->query($sql);
		if ($result->num_rows > 0) {
			$file_name = $result->fetch_object()->logo;
			unlink($this->path.$file_name);
			// update db
			$this->set_logo($id,'');
			return 1;
		}
		return 0;
	}

	public function save_logo($id) {
		$file_info = getimagesize($_FILES['Filedata']['tmp_name']);
		$image_type = array('', 'gif', 'jpg', 'png');
		$file_name = 'manufacturer_logo_'.$id.'.'.$image_type[$file_info[2]];
		if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $this->path.$file_name)) {
			$imaginator = new phImaginator();
			$imaginator->add($this->path.$file_name);
			$imaginator->Resize_fitBoth($this->config->shopConfiguration['manufacturer_image_size'], $this->config->shopConfiguration['manufacturer_image_size']);
			$imaginator->save();

			$this->set_logo($id, $file_name);
			//return
			return $this->url.$file_name;
		} else {
			return false;
		}
	}

	public function get_image_url() {
		return $this->url;
	}

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

		$sql = "SELECT id, name
				FROM manufacturers";

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

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				$return[$obj->id] = $obj->name;
			}

			return $return;
		}

		return false;
	}

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

		$sql = "SELECT name FROM manufacturers where id=$id";

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

		return $result['name'];
	}

	public static function get_names_by_structure_id($id) {
		$db = Registry::get('base')->db;
		$manufacturers = Manufacturer::get_name_id_pair();

		$sql = "SELECT DISTINCT manufacturer_id
				FROM items 
				WHERE 
					manufacturer_id != 0
				AND (
					structure_id = $id
					OR structure_id_2 = $id
					OR structure_id_3 = $id
					OR structure_id_4 = $id
					OR structure_id_5 = $id
				)
		";
		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				$return[$obj->manufacturer_id] = $manufacturers[$obj->manufacturer_id];
			}
			asort($return);
			return $return;
		}

		return false;
	}


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

		$sql = "
			SELECT DISTINCT items.manufacturer_id, manufacturers.*
			FROM items
			LEFT JOIN manufacturers ON items.manufacturer_id=manufacturers.id
			WHERE structure_id = $id
			OR structure_id_2 = $id
			OR structure_id_3 = $id
		";

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

		return $data;
	}



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

		$sql = "SELECT name, short_uri, logo
				FROM manufacturers
				WHERE active = 1 ORDER BY name";

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

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				$return[] = $obj;
			}

			return $return;
		}

		return false;
	}


	public function get_by_suri($suri) {
		$sql = "SELECT *
				FROM manufacturers
				WHERE short_uri LIKE '%".$suri."'";

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

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

		return false;
	} // end get_by_id

	public function set_active($id, $active) {
		$sql = "UPDATE manufacturers
				SET active=".$this->db->real_escape_string($active).
				" WHERE id=".$this->db->real_escape_string($id);

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

		return $result;
	}

} // end Manufacturer

?>