<?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';
include_once './core/database.class.php';

class News extends Main {

	public $list_table_config = array (
		'title' => 'Aktuelles',
		'db_table' => 'news',
		'list_fields' => array(
			array(
				'db_field' => 'title',
				'name' => 'Title',
				'sortable' => 1
			),
			array(
				'db_field' => 'created',
				'name' => 'Erstellt am',
				'sortable' => 1
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'rewrite_function' => 'state_text',
				'sortable' => 1
			)
		),
		'default_sort_item' => 'id',
		'default_sort_direction' => 'down',
		'search_fields' => array('title', 'text'),
		'db_id_field' => 'id',
		'db_selectable_field' => 'email',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=News&object_id=',
		'toolbar' => array(
			'delete' => '1',
			'new' => 'index.php?admin_modul=admin_object_edit&object=News',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Aktuelles bearbeiten',
		'edit_fields' => array (
			array(
				'name' => 'Stammdaten',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'title',
				'name' => 'Titel',
				'multi_lang' => 1,
				'type' => 'text'
			),
			array(
				'db_field' => 'sub_title',
				'name' => 'Untertitel',
				'multi_lang' => 1,
				'type' => 'text'
			),
			array(
				'db_field' => 'text',
				'name' => 'Text',
				'multi_lang' => 1,
				'type' => 'formatedtext'
			),
			array(
				'db_field' => 'link',
				'name' => 'Link',
				'type' => 'text'
			),
			array(
				'db_field' => 'link_type',
				'name' => 'Link-Typ',
				'values' => 'link_type_values',
				'type' => 'int'
			),
			array(
				'db_field' => 'image',
				'name' => 'Bild',
				'type' => 'image',
				'file_directory' => '',
				'max_image_size' => array('height' => '70', 'width' => '70')
			),
			array(
				'db_field' => 'created',
				'name' => 'Erstellt am',
				'type' => 'create_time'
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'values' => 'state_text',
				'type' => 'int'
			)
		),
		'edit_mandatory_fields' => array('title', 'text'),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_list&object=News',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'language' => 1,
			'delete' => 1
		),
	);

	public function __construct($base_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config;
		$this->db = $base_object->db;
		$this->list_table_config['edit_fields'][6]['file_directory'] = './web/'.SHOP_SYSTEM.'/images/news/';
	}

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

	public function link_type_values() {
		return array (
			'0' => 'Im aktuellen Fenster öffnen',
			'1' => 'Neues Fenster öffnen'
		);
	}

	public function get_list() {
		$data = array();
		$sql = "SELECT *
		FROM news
		WHERE active = 1
		ORDER BY id DESC LIMIT 20";
		$result = $this->db->query($sql);
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}
		return $data;
	}

	public function set_logo($id, $name) {
		$sql = "UPDATE news SET image='$name' WHERE id=$id";
		return $this->db->query($sql);
	}

	public function delete_logo($id) {
		// get filename
		$sql = "SELECT image FROM news where id=$id";
		$result = $this->db->query($sql);
		if ($result->num_rows > 0) {
			$file_name = $result->fetch_object()->logo;
			unlink(ROOT_DIR.'web/'.SHOP_SYSTEM.'/images/news/'.$file_name);
			// update db
			$this->set_logo($id,'');
			return 1;
		}
		return 0;
	}

	public function save_logo($id) {
		$file_info = getimagesize($_FILES['Filedata']['tmp_name']);
		$image_type = array('', 'gif', 'jpg', 'png');
		$file_name = 'news_image_'.$id.'.'.$image_type[$file_info[2]];
		if (move_uploaded_file($_FILES['Filedata']['tmp_name'], ROOT_DIR.'web/'.SHOP_SYSTEM.'/images/news/'.$file_name)) {
			$imaginator = new phImaginator();
			$imaginator->add(ROOT_DIR.'web/'.SHOP_SYSTEM.'/images/news/'.$file_name);
			$imaginator->Resize_fitBoth($this->config->shopConfiguration['news_image_size'], $this->config->shopConfiguration['news_image_size']);
			$imaginator->save();

			$this->set_logo($id, $file_name);
			//return
			return './web/'.SHOP_SYSTEM.'/images/news/'.$file_name;
		} else {
			return false;
		}
	}

}

?>