<?php
/**
* @version      $Id: admin_customer_group_editor.php
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       Richard Kammermayer <rk@ta-edv.de>
* Easyshop is a web shop system
*/

include_once './core/admin_role.class.php';

class admin_admin_role_editor {
	
	private $base_object;
	private $config;
	private $layout_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config_object;
		$this->layout_object = $layout_object;
	}

	function run() {
		if(isset($_GET['id']) && $_GET['id']) {
			$has_id = true;
		} else if(isset($_POST['id']) && $_POST['id']) {
			$has_id = true;
		} else {
			$has_id = false;
		}
		
		if(isset($_POST['submit'])) {
			$is_submitted = true;
		} else {
			$is_submitted = false;
		}
	
		// decide what CRUD method to call and call it in ListAndEdit
		// create
		$admin_role_object = new Admin_role($this->base_object);
		$admin_modules = $admin_role_object->get_all_admin_modules();
		$modules = array();
		foreach ($admin_modules as $modul) {
			if (isset($this->base_object->i18n->shopadmin_translation['admin_interface']['menu'][$modul['name']])) {
				$modul['name'] = $this->base_object->i18n->shopadmin_translation['admin_interface']['menu'][$modul['name']];
			}
			$modules[] = $modul;
		}
		$this->layout_object->assign('admin_modules', $modules);
		if($is_submitted && !$has_id) {
			$form_data = $admin_role_object->data_filter($_POST['form_field']);
			$form_data['acl_show'] = $admin_role_object->acl_2_string($_POST['acl_show']);
			$admin_role_object->create($form_data);
			$data = $admin_role_object->get_data();
			$this->layout_object->assign('form_data', $data);
		}// read
		elseif($has_id && !$is_submitted && $_GET['mod'] != 'delete') {
			// template variable name
			$admin_role_object->id = $_GET['id'];
			$data = $admin_role_object->get_data();
			$this->layout_object->assign('form_data', $data);
		} // update
		elseif($has_id && $is_submitted && $_GET['mod'] != 'delete') {
			$admin_role_object->id = $_POST['id'];
			$form_data = $admin_role_object->data_filter($_POST['form_field']);
			$form_data['acl_show'] = $admin_role_object->acl_2_string($_POST['acl_show']);
			$admin_role_object->update($form_data);
			$data = $admin_role_object->get_data();
			$this->layout_object->assign('form_data', $data);
		} // delete
		elseif($has_id && isset($_GET['mod']) && $_GET['mod'] == 'delete'){
			$admin_role_object->id = $_GET['id'];
			$admin_role_object->delete();
		}
		else {

		}
		return $this->layout_object->fetch('admin_admin_role_editor.tpl');
	}
	
	
}

?>