<?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
 */

class Shop_opinion extends Main {

	protected $base_object;

	public $list_table_config = array (
		'title' => 'Kundenbewertungen',
		'db_table' => 'shop_opinion',
		'list_fields' => array(
			array(
				'db_field' => 'timestamp',
				'name' => 'Datum',
				'sortable' => 1
			),
			array(
				'db_field' => 'opinion_rang_1',
				'name' => 'Bewertung 1',
				'sortable' => 1
			),
			array(
				'db_field' => 'opinion_rang_2',
				'name' => 'Bewertung 2',
				'sortable' => 1
			),
			array(
				'db_field' => 'opinion_rang_3',
				'name' => 'Bewertung 3',
				'sortable' => 1
			),
			array(
				'db_field' => 'user_firstname',
				'name' => 'Vorame',
				'sortable' => 1
			),
			array(
				'db_field' => 'user_surename',
				'name' => 'Nachname',
				'sortable' => 1
			),
			array(
				'db_field' => 'user_email',
				'name' => 'E-Mail',
				'sortable' => 1
			),
			array(
				'db_field' => 'active',
				'name' => 'Veröffentlicht',
				'rewrite_function' => 'state_text',
				'sortable' => 1
			)
		),
		'search_fields' => array('opinion_text', 'user_email', 'user_firstname', 'user_surename'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=Shop_opinion&object_id=',
		'toolbar' => array(
			'delete' => 'index.php?admin_modul=admin_object_edit&object=Shop_opinion&object_id=',
			'new' => 'index.php?admin_modul=admin_object_edit&object=Shop_opinion',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Newsletter Abonnent',
		'edit_fields' => array (
			array(
				'name' => 'Stammdaten',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'user_salutation',
				'name' => 'Anrede',
				'values' => 'salutation_values',
				'type' => 'int'
			),
			array(
				'db_field' => 'user_firstname',
				'name' => 'Vorname',
				'type' => 'text'
			),
			array(
				'db_field' => 'user_surename',
				'name' => 'Nachname',
				'type' => 'text'
			),
			array(
				'db_field' => 'user_address',
				'name' => 'Ort',
				'type' => 'text'
			),
			array(
				'db_field' => 'user_phone',
				'name' => 'Telefon',
				'type' => 'text'
			),
			array(
				'db_field' => 'user_email',
				'name' => 'E-Mail',
				'type' => 'text'
			),
			array(
				'name' => 'Bewertung',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'active',
				'name' => 'Bewertung veröffentlichen',
				'values' => 'state_text',
				'type' => 'int'
			),
			array(
				'db_field' => 'opinion_text',
				'name' => 'Bewertungstext',
				'type' => 'multitext'
			),
		),
		'edit_mandatory_fields' => array('user_firstname', 'user_surename', 'user_email', 'opinion_text'),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_list&object=Newsletter_subscriber',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'delete' => 1
		)
	);

	private $config;
	private $shopConfiguration;
	protected $db;
	public $id = 0;


	public function __construct($base_object) {
		$this->config = $base_object->config;
		$this->db = $base_object->db;
		$this->base_object = $base_object;

		$this->id = $id;
		$this->shopConfiguration = $this->config->shopConfiguration;

	} // end __construct

	public function state_text() {
		return array (
			'1' => 'ja',
			'0' => 'nein'
		);
	}

	public function salutation_values() {
		return array (
			'0' => 'Herr',
			'1' => 'Frau'
		);
	}

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

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

	public function get_opinion_list() {
		$data = array();
		$sql = "SELECT * FROM shop_opinion WHERE active = 1 ORDER BY rand() DESC LIMIT 20";
		$result = $this->db->query($sql);
		while ($obj = $result->fetch_object()) {
			$obj->date = substr($obj->timestamp,8,2).'.'.substr($obj->timestamp,5,2).'.'.substr($obj->timestamp,0,4);
			$obj->avg = round((($obj->opinion_rang_1 + $obj->opinion_rang_2 + $obj->opinion_rang_3) / 3),1);
			$obj->graph_width = $obj->avg * 30;
			$data[] = $obj;
		}
		return $data;
	}

	public function create($data) {
		// save opinion in database
		$sql = "INSERT INTO
		shop_opinion (timestamp, opinion_rang_1, opinion_rang_2, opinion_rang_3, opinion_text, user_salutation, user_firstname, user_surename, user_email, user_phone, user_address, active)
		VALUES
		(current_timestamp, '".$this->db->real_escape_string($data['opinion_rang1'])."','".
		$this->db->real_escape_string($data['opinion_rang2'])."', '".
		$this->db->real_escape_string($data['opinion_rang3'])."', '".
		$this->db->real_escape_string($data['opinion_text'])."', '".
		$this->db->real_escape_string($data['user_salutation'])."', '".
		$this->db->real_escape_string($data['user_firstname'])."','".
		$this->db->real_escape_string($data['user_surename'])."', '".
		$this->db->real_escape_string($data['user_email'])."', '".
		$this->db->real_escape_string($data['user_phone'])."', '".
		$this->db->real_escape_string($data['user_address'])."',1)";
		echo $sql;
		$result = $this->db->query($sql);

		return $result;
	}

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

		$sql = "SELECT * FROM shop_opinion ORDER BY timestamp LIMIT $items OFFSET $page";

		$result = $this->db->query($sql);
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}

		return $data;
	} // end get_all

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

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

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

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

	public function set_active($id, $active) {
		$sql = "UPDATE shop_opinion
		SET active=".$this->db->real_escape_string($active).
		" WHERE id=".$this->db->real_escape_string($id);

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

		return $result;
	}

}

?>