<?php 

	/**
	 * Kundengruppen Modul
	 */
	class wpsg_mod_customergroup extends wpsg_mod_basic 
	{
		
		var $id = 3090;		
		var $lizenz = 1;
		var $version = "9.9.9";
		var $hilfeURL = 'http://wpshopgermany.maennchen1.de/?p=13706';
		
		var $customerGroupPerPage = 25;
				
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Kundengruppen', 'wpsg');
			$this->group = __('Sonstiges', 'wpsg');
			$this->desc = __('Ist dieses Modul aktiv, so lassen sich die Kunden in Kundengruppen verwalten. Mit den entsprechenden Modul "Rabatt" ist es möglich einen Rabatt je Kundengruppe zu definieren.', 'wpsg');
						
		} // public function __construct()		
		
		public function install()
		{
			
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
			
			/** Kundentabelle erweitern */
			$sql = "CREATE TABLE ".WPSG_TBL_KU." (		   		
				group_id INT(11) NOT NULL	   		
		   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
			 
			dbDelta($sql);
			
			/** Kundengruppentabelle anlegen */
			$sql = "CREATE TABLE ".WPSG_TBL_KG." (
   				id mediumint(9) NOT NULL AUTO_INCREMENT,
   				name VARCHAR(255) NOT NULL,
   				calculation INT(11) DEFAULT '-1' NOT NULL,
   				PRIMARY KEY  (id)
   			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
				
			dbDelta($sql);
						
		} // public function install()
		
		public function init()
		{
				
			// Modell einbinden
			require_once(WPSG_PATH_MOD.'mod_customergroup/wpsg_customergroup.php');
			
		} // public function init()
		
		public function wpsg_add_pages()
		{
		
			add_submenu_page('wpsg-Admin', __('Kundengruppenverwaltung', 'wpsg'), __('Kundengruppen', 'wpsg'), 'wpsg_order', 'wpsg-Customergroup', array($this, 'be_dispatch'));
				
		} // public function wpsg_add_pages()
		
		public function settings_edit()
		{
			
			$this->shop->view['arCustomergroup'] = wpsg_array_merge(array('0' => __('Unzugeordnet', 'wpsg')), $this->db->fetchAssocField("
				SELECT
					KG.`id`, CONCAT(KG.`name`, ' (ID:', KG.`id`, ')') AS `name`
				FROM
					`".WPSG_TBL_KG."` AS KG
				ORDER BY
					KG.`name` ASC
			", "id", "name"));
			
			$this->shop->render(WPSG_PATH_VIEW.'mods/mod_customergroup/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_page_mod_kundenverwaltung_group_checkout', $_REQUEST['wpsg_page_mod_kundenverwaltung_group_checkout']);
			$this->shop->update_option('wpsg_page_mod_kundenverwaltung_group_register', $_REQUEST['wpsg_page_mod_kundenverwaltung_group_register']);
							
		} // public function settings_save()
				
		/* Modulfunktionen */

		public function be_dispatch()
		{
				
			if (wpsg_isSizedString($_REQUEST['action'], 'add'))
			{
					
				$this->addAction();
					
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'edit'))
			{
		
				$this->editAction();
		
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'save'))
			{
					
				$this->saveAction();
					
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'del'))
			{
		
				$this->delAction();
		
			}
			else
			{
					
				$this->indexAction();
					
			}
				
		} // public function be_dispatch()
		 
		/**
		 * Soll eine Kundengruppe bearbeiten
		 */
		public function editAction()
		{
				
			if (!wpsg_isSizedInt($_REQUEST['edit_id'])) $this->shop->throwErrorCode('3090_1');
				
			$this->shop->view['data'] = new wpsg_customergroup();
			$this->shop->view['data']->load($_REQUEST['edit_id']);
			$this->shop->view['edit_id'] = $_REQUEST['edit_id'];
			$this->shop->view['mode'] = 'edit';
				
			$this->shop->render(WPSG_PATH_VIEW.'mods/mod_customergroup/addedit.phtml');
				
		} // public function editAction()
		
		/**
		 * Soll eine Kundengruppe löschen
		 */
		public function delAction()
		{
				
			$bOK = wpsg_customergroup::delete($_REQUEST['edit_id']);
				
			if ($bOK !== true) $this->shop->addBackendError(__('Kundengruppe konnte nicht erfolgreich gelöscht werden', 'wpsg'));
			else $this->shop->addBackendMessage(__('Kundengruppe wurde erfolgreich gelöscht', 'wpsg'));
				
			$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Customergroup');
		
		} // public function delAction()
		
		/**
		 * Generiert das Formular zum Anlegen einer neuen Kundengruppe
		 */
		public function addAction()
		{
				
			$this->shop->view['mode'] = 'add';
			$this->shop->view['edit_id'] = 0;
			$this->shop->view['data'] = false;
				
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_customergroup/addedit.phtml');
				
		} // public function addAction()
		
		/**
		 * Speichert eine Kundengruppe im Backend
		 */
		public function saveAction()
		{
				
			if (wpsg_isSizedInt($_REQUEST['edit_id']))
			{
					
				// Update
				$this->db->UpdateQuery(WPSG_TBL_KG, array(
						'name' => wpsg_q($_REQUEST['wpsg_mod_kundenverwaltung']['name']),
						'calculation' => wpsg_q($_REQUEST['wpsg_mod_kundenverwaltung']['calculation'])
				), " `id` = '".wpsg_q($_REQUEST['edit_id'])."' ");
					
				$this->shop->addBackendMessage(__('Kundengruppe erfolgreich gespeichert', 'wpsg'));
					
			}
			else
			{
					
				// Insert
				$_REQUEST['edit_id'] = $this->db->ImportQuery(WPSG_TBL_KG, array(
						'name' => wpsg_q($_REQUEST['wpsg_mod_kundenverwaltung']['name']),
						'calculation' => wpsg_q($_REQUEST['wpsg_mod_kundenverwaltung']['calculation'])
				));
					
				$this->shop->addBackendMessage(__('Kundengruppe erfolgreich angelegt', 'wpsg'));
					
			}
				
			$this->shop->callMods('customergroup_save', array(&$_REQUEST['edit_id']));
		
			if (isset($_REQUEST['submit_index']))
			{
					
				$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Customergroup');
					
			}
			else
			{
					
				$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Customergroup&action=edit&edit_id='.$_REQUEST['edit_id']);
					
			}
				
		} // public function saveAction()
		
		/**
		 * Generiert die Übersicht der Kundengruppen im Backend
		 */
		public function indexAction()
		{
		
			$arFilter = array();
				
			if (wpsg_isSizedArray($_REQUEST['filter']))
			{
		
				if (wpsg_isSizedString($_REQUEST['filter']['s']))
				{
		
					$arFilter['name'] = $_REQUEST['filter']['s'];
		
				}
		
				$arFilter['order'] = $_REQUEST['filter']['order'];
				$arFilter['ascdesc'] = $_REQUEST['filter']['ascdesc'];
		
				$this->shop->view['filter'] = $_REQUEST['filter'];
		
			}
			else
			{
		
				$arFilter['order'] = 'id';
				$arFilter['ascdesc'] = 'desc';
		
				$this->shop->view['filter'] = $arFilter;
		
			}
				
			$this->shop->view['data'] = wpsg_customergroup::find($arFilter);
				
			$this->shop->view['count'] = sizeof($this->shop->view['data']);
			if (isset($_REQUEST['seite'])) $this->shop->view['seite'] = $_REQUEST['seite']; else $this->shop->view['seite'] = 1;
			$this->shop->view['pages'] = ceil($this->shop->view['count'] / $this->customerGroupPerPage);
			if ($this->shop->view['seite'] <= 0 || $this->shop->view['seite'] > $this->shop->view['pages']) $this->shop->view['seite'] = 1;
				
			$this->shop->view['data'] = array_slice($this->shop->view['data'], ($this->shop->view['seite'] - 1) * $this->customerGroupPerPage, $this->customerGroupPerPage);
				
			$this->shop->render(WPSG_PATH_VIEW.'mods/mod_customergroup/index.phtml');
				
		} // public function indexAction()
				
	} // class wpsg_mod_customergroup extends wpsg_mod_basic
	
?>