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

class Statistic extends Main {

	public $list_table_config = array (
		'title' => 'Statistiken',
		'db_table' => 'statistics',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Bezeichnung',
				'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_statistic_editor&id=',
		'toolbar' => array(
			'delete' => '0',
			'new' => 0,
			'copy' => 0,
			'select_all' => 0,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		)
	);

	private $theme_fields = array(
		'name' => 'text',
		'description' => 'text',
		'modul' => 'text',
		'settings' => 'text'
	);

	public $id;

	protected $base_object;
	protected $db;
	private $config;
	private $path;
	private $error;

	public function __construct($base_object) {
		global $config_object;
		$this->base_object = $base_object;
		$this->config = $base_object->config;
		$this->db = $base_object->db;
		$this->id = false;
		$this->error = '';
	} // end __construct

	public function set_id($id) {
		$this->id = $id;
	}

	public function get_id() {
		return $this->id;
	}

	public function get_error() {
		return $this->error;
	}

	public function get_all() {
		$sql = "SELECT * FROM statistics ORDER BY name";

		$result = $this->db->query($sql);

		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}

		return $data;
	} // end get_all

	public function get_by_id($id) {
		$sql = "SELECT * FROM statistics WHERE id = $id";

		$result = $this->db->query($sql);

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}

		return false;
	} // end get_by_id

	public function delete_by_id($id) {
		$sql = "DELETE FROM statistics WHERE id=".id;

		return $this->db->query($sql);
	} // end delete_by_id

	public function get_where_name_like($name) {
		$sql = "SELECT id FROM statistics WHERE name LIKE '%".$name."%'";

		$result = $this->db->query($sql);

		while ($row = $result->fetch_object()) {
			$data[] = $row;
		}

		return $data;
	} // end get_where_name_like

	public function get_data($id = false) {
		$sql = "SELECT * FROM statistics";

		if ($id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($id);
		} else if ($this->id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($this->id);
		} else {
			$this->error = 'no id';

			return false;
		}

		$result = $this->db->query($sql);

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}
		$this->error = 'no data';

		return false;
	} // end get_data

	public function create($data) {
		if ($data) {
			$data['short_uri'] = $this->short_uri($data['short_uri'], $data['name']);

			$sql = "INSERT INTO statistics SET ";

			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);

				if ($this->theme_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;
	} // end create

	public function update($data) {
		if ($data) {
			$data['short_uri'] = $this->short_uri($data['short_uri'], $data['name'], $this->id);

			$sql = "UPDATE statistics SET ";

			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);

				if ($this->theme_fields[$var_name] == 'integer') {
					$sql .= $var_name.'='.$value.', ';
				} else {
					$sql .= $var_name.'="'.$value.'", ';
				}
			}

			$sql = substr($sql, 0, -2);
			$sql .= ' WHERE id='.$this->id;
			$this->base_object->logger->info("sql",$sql);
			$this->db->query($sql);
		}

		return;
	} // end update

	public function delete($id) {
		$sql = "DELETE FROM statistics WHERE id=".$id;

		return $this->db->query($sql);
	} // end delete

	public function data_filter($request) {
		$data = array();
		foreach ($this->theme_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;
	} // end data_filter



	public function save($data, $object_id = false) {
		if (isset($data['id'])) {
			$id = $data['id'];
			unset($data['id']);

			$sql = Database::update('statistics', $data, 'WHERE id='.$id);
		} else {
			$id = false;
			$sql = Database::insert('statistics', $data);
		}
		$return = $this->db->query($sql);

		if ($return) {
			if ($id) {
				return $id;
			} else {
				return $this->db->insert_id;
			}
		}

		return false;
	} // end save

}
