<?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';

#[\AllowDynamicProperties]
class Admin_role extends Main {

	public $list_table_config = array (
		'title' => 'Verwaltungs Rollen',
		'db_table' => 'admin_roles',
		'list_fields' => array(
			array(
					'db_field' => 'name',
					'name' => 'Rollenname',
					'sortable' => 1
			),
			array(
					'db_field' => 'description',
					'name' => 'Beschreibung',
					'sortable' => 1
			)
		),
		'search_fields' => array('name', 'description'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_admin_role_editor&id=',
		'toolbar' => array(
			'delete' => '1',
			'new' => 'index.php?admin_modul=admin_admin_role_editor',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		)
	);

	private $object_fields = array(
		'name' => 'text',
		'acl_show' => 'text',
		'description' => 'text'
		);
		protected $db;
		protected $base;
		private $config;

		public function __construct($base_object) {
			parent::__construct($base_object);
			$this->config_object = $base_object->config;
			$this->db = $base_object->db;
			$this->base = $base_object;
		}

		public function get_data() {
			$sql = "SELECT * FROM admin_roles WHERE id = ".$this->id;
			$result = $this->db->query($sql);
			$obj = $result->fetch_object();
			$acl_show_string = $obj->acl_show;
			$acl_show = explode(';', $acl_show_string);
			$acl_permission = array();
			foreach ($acl_show as $modul) {
				$acl_permission[$modul] = 1;
			}
			$obj->acl_show = $acl_permission;

			return $obj;
		} // end get_data

		public function get_all() {
			// get all groups from database
			$sql = "
				SELECT
				*
				FROM
					admin_roles
			";
			$result = $this->db->query($sql);
			$data = array();
			while ($obj = $result->fetch_object()) {
				$data[] = $obj;
			}
			return $data;
		}

		public function delete() {
			$sql = "
				DELETE FROM
					admin_roles
				WHERE id=".$this->id;
			$this->db->query($sql);
			return;
		}

		public function data_filter($request) {
			$data = array();
			foreach ($this->object_fields as $field_name => $var_type) {
				if (isset($request[$field_name])) {
					if ($var_type == 'text') {
						$data[$field_name] = $request[$field_name];
					} else {
						$data[$field_name] = (int)$request[$field_name];
					}
				}
			}
			return $data;
		}

		public function create($data) {
			if ($data) {
				$sql = "INSERT INTO admin_roles SET ";
				foreach ($data as $var_name => $value) {
					$value = $this->db->real_escape_string($value);
					if ($this->object_fields[$var_name] == 'integer') {
						$sql .= $var_name.' = '.$value.', ';
					} else {
						$sql .= $var_name.' = "'.$value.'", ';
					}
				}
				$sql = substr($sql, 0, -2);
				$this->db->query($sql);
				$this->id = $this->db->insert_id;
			}
			return;
		}

		public function update($data) {
			if ($data) {
				$sql = "
					UPDATE
						admin_roles
					SET 
				";
				foreach ($data as $var_name => $value) {
					$value = $this->db->real_escape_string($value);
					if ($this->object_fields[$var_name] == 'integer') {
						$sql .= $var_name.'='.$value.', ';
					} else {
						$sql .= $var_name.'="'.$value.'", ';
					}
				}
				$sql = substr($sql, 0, -2);
				$sql .= ' WHERE id='.$this->id;
				$this->db->query($sql);
			}
			return;
		}

		public function get_all_paginated($items , $page, $order = false) {
			$page = ($page - 1) * $items;

			$sql = "SELECT *
					FROM admin_roles
					LIMIT $items OFFSET $page";

			return parent::get_all_paginated($items, $page, $sql);
		} // end get_all_paginated

		public function get_number_of_pages($items) {
			$sql = "SELECT COUNT(id) FROM admin_roles";

			return parent::get_number_of_pages($items, $sql);
		}

		public function get_pagination_array($items, $page) {
			$sql = "SELECT COUNT(id) FROM admin_roles";

			return parent::get_pagination_array($items, $page, $sql);
		}

		public function delete_by_id($id) {
			$rs = $this->db->query("DELETE FROM admin_roles WHERE id=$id;");
		}

		public function get_all_admin_modules() {
			include('./core/system_registration.inc.php');
			return $admin_modul_registration;
		}

		public function acl_2_string($acl_array) {
			include('./core/system_registration.inc.php');
			$acl = array();
			foreach ($admin_modul_registration as $modul) {
				if (isset($acl_array[$modul['modul']]) && $acl_array[$modul['modul']] == 1) {
					$acl[] = $modul['modul'];
				}
			}
			return implode(';', $acl);
		}

		function get_all_names() {
				$data = array();
				$result = $this->db->query('SELECT id, name FROM admin_roles');
				if ($result) {
					while ($obj = $result->fetch_object()) {
						$data[$obj->id] = $obj->name;
					}
				}
				return $data;
		}

}

?>