<?php

	class wpsg_mod_spconditions extends wpsg_mod_basic
	{
		
		var $lizenz = 2;
		var $id = 3000;				
		var $inline = true;
		
		//var $hilfeURL = '';
		
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Versand- und Zahlungsbedingungen', 'wpsg');
			$this->group = __('Sonstiges', 'wpsg');
			$this->desc = __('Ermöglicht die Erstellung von Bedingungen für Versand- und Zahlungsarten.', 'wpsg');
			
			if (defined('WPSG_TBL_SPCONDITIONS')) define('WPSG_TBL_SPCONDITIONS', $this->shop->prefix.'wpsg_spconditions');
			
		} // public function __construct()
		
		public function install()
		{
		
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
				
			/**
			 * Tabelle für die Bedingungen
			 */
			$sql = "CREATE TABLE ".WPSG_TBL_SPCONDITIONS." (
		   		id mediumint(9) NOT NULL AUTO_INCREMENT,		
	  			cdate datetime NOT NULL,
				payment_id varchar(11),
				shipping_id varchar(11),
				pos int(11),
				activ int(11),
				min_price DOUBLE(10,2),
				max_price DOUBLE(10,2),
				PRIMARY KEY  (id)
		   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
			 
			dbDelta($sql);
			
		} // public function install()

		public function settings_edit()
		{
			
			$this->shop->view['conditionList'] = $this->conditionList();
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_spconditions/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
		} // public function settings_save()
		
		public function be_ajax()
		{
			
			if ($_REQUEST['do'] == 'conditionList')
			{
				
				die($this->conditionList());
				
			}
			else if ($_REQUEST['do'] == 'add')
			{
				
				die($this->be_ajax_add());
				
			}
			
		} // public function be_ajax()
		
		/* Modulfunktionen */
		
		/**
		 * Gibt die Liste aller derzeit definierten Bedingungen zurück
		 */
		private function conditionList()
		{
			
			$this->shop->view['data'] = $this->db->fetchAssoc("SELECT * FROM `".WPSG_TBL_SPCONDITIONS."` ORDER BY `pos` ASC ");
			
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_spconditions/conditionList.phtml', false);
			
		} // private function conditionList()
		
		/**
		 * Wird per AJAX aufgerufen und stellt eine neue Bedingung in die Datenbank ein
		 */
		private function be_ajax_add()
		{
			
			$nMaxPos = $this->db->fetchOne("SELECT MAX(`pos`) FROM `".WPSG_TBL_SPCONDITIONS."` ");
			
			$this->db->ImportQuery(WPSG_TBL_SPCONDITIONS, array(
				'cdate' => 'NOW()',
				'payment_id' => 0,
				'shipping_id' => 0,
				'pos' => wpsg_q($nMaxPos + 1),
				'activ' => '1',
				'min_price' => '0',
				'max_price' => '0'
			));
			
			return $this->conditionList();
			
		} // private function be_ajax_add()
		
	} // class wpsg_mod_spconditions extends wpsg_mod_basic

?>