<?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_theme extends Main {

	protected $base_object;

	public $list_table_config = array (
		'title' => 'Webfrontend Themes',
		'db_table' => 'themes',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Name',
				'sortable' => 1
			),
			array(
				'db_field' => 'description',
				'name' => 'Beschreibung',
				'sortable' => 1
			),
			array(
				'db_field' => 'parent_theme',
				'name' => 'Theme',
				'sortable' => 1
			)
		),
		'search_fields' => array('name', 'description'),
		'db_id_field' => 'id',
		'db_selectable_field' => 'name',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=Shop_theme&object_id=',
		'toolbar' => array(
			'delete' => 1,
			'new' => 'index.php?admin_modul=admin_object_edit&object=Shop_theme',
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Shop Theme',
		'edit_fields' => array (
			array(
				'name' => 'Stammdaten',
				'type' => 'form_title'
			),
			array(
				'db_field' => 'name',
				'name' => 'Name',
				'type' => 'text'
			),
			array(
				'db_field' => 'description',
				'name' => 'Beschreibung',
				'type' => 'multitext'
			),
			array(
				'db_field' => 'parent_theme',
				'name' => 'Themevorlage',
				'values' => 'parent_theme_values',
				'type' => 'text'
			),
			array(
				'db_field' => 'parent_theme_version',
				'name' => 'Theme',
				'values' => 'theme_values',
				'type' => 'text'
			),
			array(
				'db_field' => 'css',
				'name' => 'CSS',
				'type' => 'multitext'
			)
		),
		'edit_mandatory_fields' => array('name'),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_list&object=Shop_theme',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'delete' => 1
		),
	);

	private $theme_fields = array(
		'name' => 'text',
		'description' => 'text',
		'parent_theme' => 'text',
		'parent_theme_version' => 'text',
		'directory' => 'text',
		'css' => 'text',
		'settings' => 'text'
	);

	public $id;

	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 parent_theme_values() {
		$data = array();
		$handle=opendir ("./themes");
		while ($datei = readdir ($handle)) {
			if ($datei != '.' && $datei != '..' && $datei != 'admin') {
				$data[$datei] = $datei;
			}
		}
		closedir($handle);
		return $data;
	}

	public function theme_values() {
		$data = array();
		$handle=opendir ("./web/".$this->base_object->config->shopConfiguration['SHOP_SYSTEM']."/themes/");
		while ($datei = readdir ($handle)) {
			if ($datei != '.' && $datei != '..' && $datei != 'admin') {
				$data[$datei] = $datei;
			}
		}
		closedir($handle);
		return $data;
	}

	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 themes 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 themes 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 get_where_name_like($name) {
		$sql = "SELECT id FROM themes WHERE name LIKE '%".$name."%'";

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

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

		return $data;
	} // end get_where_name_like

}

?>