<?php 

	class wpsg_mod_kundenkontakt extends wpsg_mod_basic 
	{
		
		var $lizenz = 1;
		var $id = 1000;
		var $hilfeURL = 'http://wpshopgermany.de/?p=3886';
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name 	= __('Kundenkontakt', 'wpsg');
			$this->group 	= __('Sonstiges', 'wpsg');
			$this->desc 	= __('Erlaubt es in der Bestellverwaltung dem Kunden E-Mails zu senden.', 'wpsg');
						
		} // public function __construct()
		
		/**
		 * zeigt das Formular zum Senden in der Bestellverwaltung
		 */
		public function order_view_content($order_id)
		{
			
			$path = $this->shop->getRessourcePath('mods/mod_kundenkontakt/templates/');

			$this->shop->view['arTemplates'] = array();
			
			$arrFiles = scandir($path);
			
			foreach ($arrFiles as $file)
			{
				
				if (is_file($path.$file) && preg_match('/(.*)\.phtml/', $file) && !preg_match('/(.*)_html\.phtml/', $file))
				{
					
					$template_name = str_replace('.phtml', '', $file);
					
					$this->shop->view['arTemplates'][$file]['filename'] = $file;
					$this->shop->view['arTemplates'][$file]['name'] = ucfirst($template_name);
					
				}
				
			}
			
			$this->shop->render(WPSG_PATH_VIEW.'mods/mod_kundenkontakt/order_view.phtml');
			
		} // public function order_view_content($order_id)
		
		public function order_ajax()
		{
			
			if ($_REQUEST['do'] == 'kk_switchTemplate')
			{
				
				$this->switchTemplate();
				
			}

			if ($_REQUEST['do'] == 'kk_sendMail')
			{
				
				$this->send();
				
			}
			
		}
		
		public function admin_emailconf()
		{
			
			echo wpsg_drawEMailConfig(
				'customercontact', 
				__('E-Mail, die vom Kundenkontakt Modul versendet wird.', 'wpsg'), 
				__('Diese Mail kann in der Bestellverwaltung an den Kunden versendet werden.', 'wpsg')); 
						
		} // public function admin_emailconf()
		
		public function admin_emailconf_save() 
		{
			
			wpsg_saveEMailConfig("customercontact");
			 			
		} // public function admin_emailconf_save()
		
		/* Modulfunktionen */
		
		/**
		 * 
		 * ermöglichr die Auswahl eines Templates zum versenden als Kundenkontakt
		 */
		private function switchTemplate()
		{
			
			if ($_REQUEST['template_file'] != '-1')
			{
				 
				$path = $this->shop->getRessourcePath('mods/mod_kundenkontakt/templates/');
				
				$basket = new wpsg_basket();
				$basket->initFromDB(wpsg_q($_REQUEST['edit_id']));

				if ($this->shop->get_option('wpsg_htmlMail') === '1')
				{
					
					$this->shop->htmlMail = true;
					
				}
				
				$this->shop->view['basket'] = $basket->toArray();
				$this->shop->view['order'] = $this->shop->cache->loadOrder($_REQUEST['edit_id']);
				$this->shop->view['kunde'] = $this->shop->cache->loadKunden($this->shop->view['order']['k_id']);
				
				$order_data = $this->shop->cache->loadOrder($_REQUEST['edit_id']);
				if (trim($order_data['language']) != '') $this->shop->setTempLocale($order_data['language']);
				
				$this->shop->view['datum'] = time();
				
				// Namen und Betreff des Templates ermitteln
				$template_name = "";
				$template_betreff = ""; 
				
            	global $template_name, $template_betreff;
				
				$template = $path.wpsg_q($_REQUEST['template_file']);
				
				if ($this->shop->get_option('wpsg_htmlmail') === '1')
				{
					
					$template_html = preg_replace('/\.phtml$/i', '_html.phtml', $template);
					
					if (file_exists($template_html)) 
					{
						
						$template = $template_html;
												
					}
					
				}
								
				$content = $this->shop->render($template, false);				
				$content = $this->shop->replaceUniversalPlatzhalter($content, $_REQUEST['edit_id']);
				$template_betreff = $this->shop->replaceUniversalPlatzhalter($template_betreff, $_REQUEST['edit_id']);
				 				
				$this->shop->restoreTempLocale();
								
				$arData = array(
					'subject' => $template_betreff,
					'content' => $content						
				);
				
				header('Content-Type: application/json');
				
				die(json_encode($arData));
				
			}

		}
		
		/**
		 * 
		 * Enter description here ...
		 */
		private function send()
		{ 
			
			$mail_text = $this->shop->replaceUniversalPlatzhalter($_REQUEST['text'], $_REQUEST['edit_id']);
			$to = $_REQUEST['empfaenger'];
			$subject = $this->shop->replaceUniversalPlatzhalter($_REQUEST['subject'], $_REQUEST['edit_id']);
			
			// HTML Mail
			if ($this->shop->get_option('wpsg_htmlmail') == '1')
			{
				
				$mail_html = $this->shop->replaceUniversalPlatzhalter($_REQUEST['text'], $_REQUEST['edit_id']);
				$mail_text = strip_tags($mail_html);
				
			} else $mail_html = false;
			 
			$this->shop->sendMail($mail_text, $to, 'customercontact', array(), $_REQUEST['edit_id'], false, $mail_html, $subject);
			
			$this->shop->addBackendMessage(__('Eine Nachricht über den Kundenkontakt wurde versendet.', 'wpsg'));
			
			$this->db->ImportQuery(WPSG_TBL_OL, array(
				'cdate' => 'NOW()',
				'o_id' => wpsg_q($_REQUEST['edit_id']),
				'title' => wpsg_q(wpsg_translate(__('Kundenkontakt "#1#"', 'wpsg'), $subject)),
				'mailtext' => wpsg_q($mail_text)					
			));
			 			
			die('1');
			
		}
	
	} // class wpsg_mod_kundenkontakt extends wpsg_mod_basic 
	
?>