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

	protected $base_object;

	public $list_table_config = array (
		'title' => 'Newsletter',
		'db_table' => 'newsletter',
		'list_fields' => array(
			array(
				'db_field' => 'subject',
				'name' => 'Betreff',
				'sortable' => 1
			),
			array(
				'db_field' => 'create_time',
				'name' => 'Erstelldatum',
				'sortable' => 1
			),
			array(
				'db_field' => 'send_time',
				'name' => 'Versand-Datum',
				'sortable' => 1
			),
			array(
				'db_field' => 'create_user_id',
				'name' => 'Mitarbeiter',
				'sortable' => 1
			)
		),
		'search_fields' => array('subject'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=Newsletter&object_id=',
		'toolbar' => array(
			'delete' => 'index.php?admin_modul=admin_newsletter_editor&object_id=',
			'new' => 'index.php?admin_modul=admin_object_edit&object=Newsletter',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Newsletter',
		'edit_fields' => array (
			array(
				'name' => 'Stammdaten',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'mail_from',
				'name' => 'Absender',
				'type' => 'text',
				'values' => 'mail_sender_values',
			),
			array(
				'name' => 'Vorlage',
				'values' => 'email_template_values',
				'id' => 'email_template_selection',
				'type' => 'tmp_function'
			),
			array(
				'db_field' => 'subject',
				'name' => 'Betreff',
				'type' => 'text'
			),
			array(
				'db_field' => 'message',
				'name' => 'Nachricht',
				'type' => 'formatedtext'
			),
			/*array(
				'name' => 'Artikel hinzubuchen',
				'type' => 'form_title'
			),
			array(
				'name' => 'Artikelsuche',
				'type' => 'livesearch',
				'oject' => 'Item'
			),*/
			array(
				'name' => 'Empfängerauswahl',
				'type' => 'form_title'
			),
			array(
				/*'name' => 'Kundengruppe,Land,PLZ,Einzelsuche,letzter einkauf vor 1 bis 5 Monaten, Umsatz in Euro von bis in den letzten 2 monaten',*/
				'name' => 'Kundengruppe',
				'values' => 'customer_group_values',
				'id' => 'customer_group',
				'type' => 'text',
				'function' => 'newsletter_send',
				'function_name' => 'Senden'
			),
			array(
				'name' => 'Empfänger',
				'type' => 'form_title'
			),
		),
		'edit_js_file' => 'admin_newsletter_edit.js',
		'edit_mandatory_fields' => array('email'),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_list&object=Newsletter',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'send' => 0,
			'send_test' => 1,
			'delete' => 1
		)
	);

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


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

	function email_template_values() {
		include_once './core/email_template.class.php';
		$email_template_object = New Email_template($this->base_object);
		return $email_template_object->get_all_selectable();
	}

	function mail_sender_values() {
		return array(
			$this->base_object->config->shopConfiguration['system_email'] => $this->base_object->config->shopConfiguration['system_email']
		);
	}

	function customer_group_values() {
		include_once './core/customer_group.class.php';
		$customer_group_object = New Customer_group($this->base_object);
		return $customer_group_object->get_all_selectable();
	}

	public function test_email($id, $email) {
		// get newsletter data
		$data = $this->load($id);

		// send newsletter
		include_once './core/mail.class.php';
		$mailer = new mail_tools($this->base_object);
		$result = $mailer->send_mail($data['subject'], $data['message'], $email, 'extern', false, false, false, false, false);
		if (!$result) {
			$log_object = Logger::get_instance();
			$log_object->info('Error in /core/newsletter.php->email_test()', $result);
		}
		return 1;
	}

	public function send_email($id, $customer_group) {
		// get newsletter data
		$data = $this->load($id);

		// get recipients
		$recipients = $this->base_object->customer->get_all_newsletter_subscriber($customer_group);

		// send newsletter
		include_once './core/mail.class.php';
		$mailer = new mail_tools($this->base_object);
		foreach ($recipients as $subscriber) {
			$email = $subscriber->email;
			$result = $mailer->send_mail($data['subject'], $data['message'], $email, 'extern', false, false, false, false, false);
			if (!$result) {
				$log_object = Logger::get_instance();
				$log_object->info('Error in /core/newsletter.php->send_email()', $result);
			}
		}

		// set timestamp for sendet newsletter
		$sql = "UPDATE newsletter SET send_time=CURRENT_TIMESTAMP WHERE id=".$this->db->real_escape_string($id);
		$log_object = Logger::get_instance();
		$result = $this->db->query($sql);
		if (!$result) {
			$log_object = Logger::get_instance();
			$log_object->info('Error in /core/newsletter.php->send_email()', $this->db->error.', sql:'.$sql);
		}

		return 1;
	}

}