<?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 Standard_message extends Main {

	public $list_table_config = array (
		'title' => 'Standard Benachrichtigungen',
		'db_table' => 'standard_messages',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Bezeichnung',
				'sortable' => 1
			),
			array(
				'db_field' => 'title',
				'name' => 'Subject',
				'sortable' => 1
			),
			array(
				'db_field' => 'event_type',
				'name' => 'Event',
				'rewrite_function' => 'get_events',
				'sortable' => 1
			),
			array(
				'db_field' => 'recipient',
				'name' => 'Empfänger',
				'rewrite_function' => 'recipient_text',
				'sortable' => 1
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'rewrite_function' => 'state_text',
				'sortable' => 1
			)
		),
		'search_fields' => array('name', 'title', 'message'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_standard_message_editor&id=',
		'toolbar' => array(
			'delete' => '1',
			'new' => 'index.php?admin_modul=admin_standard_message_editor',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		)
	);

	protected $base_object;
	protected $db;

	private $object_fields = array(
		'name' => 'text',
		'recipient' => 'integer',
		'event_type' => 'integer',
		'title' => 'text',
		'active' => 'integer',
		'customer_group_id' => 'integer',
		'message' => 'text'
	);

	public $events = array(
		'1' => array('type' => '1', 'name' => 'Neues Kontaktformular'),
		'2' => array('type' => '2', 'name' => 'Neue Kundenregistrierung (normal)'),
		'13' => array('type' => '13', 'name' => 'Neue Kundenregistrierung (Firma)'),
		'3' => array('type' => '3', 'name' => 'Neue Bestellung'),
		'4' => array('type' => '4', 'name' => 'Neuer Bestellstatus'),
		'5' => array('type' => '5', 'name' => 'Bestellstatus gesetzt: Bestätigt'),
		'6' => array('type' => '6', 'name' => 'Bestellstatus gesetzt: Versandt'),
		'7' => array('type' => '7', 'name' => 'Bestellstatus gesetzt: Rückgabe'),
		'8' => array('type' => '8', 'name' => 'Bestellstatus gesetzt: Storniert'),
		'9' => array('type' => '9', 'name' => 'Zahlungseingang - Vorkasse'),
		'10' => array('type' => '10', 'name' => 'Neues Passwort (Lost Password)'),
		'11' => array('type' => '11', 'name' => 'Rechnung an Kunden senden'),
		'12' => array('type' => '12', 'name' => 'Kundenfreischaltung'),
		'14' => array('type' => '14', 'name' => 'Bestellstatus gesetzt: Prüfung'),
		'15' => array('type' => '15', 'name' => 'Kundendaten im Kundencenter neu gespeichert.'),
		'16' => array('type' => '16', 'name' => 'Kunde bekommt neues Passwort (In der Kundenbearbeitung gesetzt).')
	);

	function __construct($base_object) {
		$this->base_object = $base_object;
		$this->db = $base_object->db;
	}

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

	public function recipient_text() {
		return array (
			'0' => '-',
			'1' => 'Kunde',
			'2' => 'Shopadmin'
		);
	}

	public function get_events() {
		$events = array();
		foreach ($this->events as $event) {
			$events[$event['type']] = $event['name'];
		}
		return $events;
	}

	public function get_all() {
		$sql = "SELECT * FROM standard_messages";
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}

		return $data;
	}


	public function get_data() {
		$sql = "SELECT * FROM standard_messages WHERE id = ".$this->id;
		$result = $this->db->query($sql);
		$obj = $result->fetch_object();

		$obj->files = $this->get_file_list();
		return $obj;
	} // end get_data


	public function get_messages_by_event($event_type) {
		$sql = "SELECT * FROM standard_messages WHERE event_type = ".$event_type;
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}

		return $data;
	}


	public function delete() {
		$sql = "DELETE FROM standard_messages WHERE id=".$this->id;
		$this->db->query($sql);
		$sql = "DELETE FROM standard_message_attachments WHERE standard_message_id=".$this->id;
		$this->db->query($sql);
		return;
	}

	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;
	}

	public function create($data) {
		if ($data) {
			$sql = "INSERT INTO standard_messages 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;
	}

	public function update($data) {
		if ($data) {
			$sql = "UPDATE standard_messages 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;
	}

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

	public function add_uploaded_file($file_data) {
		$tmpname = $file_data['Filedata']['tmp_name'];
		$name = $file_data['Filedata']['name'];
		$type = $file_data['Filedata']['type'];
		$hndFile = fopen($tmpname, "r");
		$data = fread($hndFile, filesize($tmpname));
		$data = base64_encode($data);

		if ($data) {
			$sql = "INSERT INTO standard_message_attachments 
					(standard_message_id, file_name, mime_type, content) VALUES
					(".$this->id.", '".$name."', '".$type."','".$data."')" ;

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

	public function get_file_list() {
		$sql = "SELECT * FROM standard_message_attachments WHERE standard_message_id=".$this->id;
		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
			$obj->content = base64_decode($obj->content);
		}
		return $data;
	}

	public function get_file($file_id) {
		$sql = "SELECT * FROM standard_message_attachments WHERE id=".$file_id;
		$result = $this->db->query($sql);
		$obj = $result->fetch_object();
		$obj->content = base64_decode($obj->content);
		return $obj;
	}

	public function delete_file($file_id) {
		$sql = "DELETE FROM standard_message_attachments
				WHERE id=".$this->db->real_escape_string($file_id);
		$result = $this->db->query($sql);
		return;
	}

}