<?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 './libs/phImaginator/phImaginator.class.php';

class SiteContent {

	// hack as associative array since there are no real lists
	private $content_types = array(
		'textbox' => 0,
		'slider' => 0,
		'article_list' => 0, // should be items or better, items should be products
		'popular' => 0,
		'substructure' => 0,
		'banner' => 0,
		'manufacturers' => 0
	);

	private $db;
	private $base_object;
	private $error;

	public $id; // this is a hack, make it private and check the entire shop

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

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

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

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

	public function get_by_structure_id($id) {
		$sql = "SELECT * FROM site_content
				WHERE structure_id=".$this->db->real_escape_string($id)." ORDER BY position";

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

		if ($result->num_rows > 0) {
			$types = $this->content_types;

			$i = 1;
			while ($obj = $result->fetch_object()) {
				unset($types[$obj->type]);
				$return_data[] = $obj;
				$i++;
			}

			foreach ($types as $type => $trash) {
				$obj = new stdClass();

				$obj->structure_id = $id;
				$obj->type = $type;
				$obj->position = $i++;
				$obj->activ = 0;

				$return_data[] = $obj;
			}

			return $return_data;
		}
		$this->error = 'no result';

		return false;
	}

	// TODO: get rid of this again?
	public function get_item_category_name_by_id($id) {
		$sql = "SELECT name FROM structure WHERE id=$id";

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

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

		return false;
	}

	public function is_set($id, $type) {
		$sql = "SELECT * FROM site_content 
				WHERE structure_id=".$this->db->real_escape_string($id)." 
				AND type='".$this->db->real_escape_string($type)."'";

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

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

		return false;
	}

	public function save_by_structure_id($id, $type, $position, $activ, $content, $title = '', $textbox_image = false, $content_en = '', $title_en = '', $title_style = '') {
		if ($this->is_set($id, $type)) {
			return $this->update_by_structure_id($id, $type, $position, $activ, $content, $title, $textbox_image, $content_en, $title_en, $title_style);
		} else {
			return $this->insert_by_structure_id($id, $type, $position, $activ, $content, $title, $textbox_image, $content_en, $title_en, $title_style);
		}
	}

	public function insert_by_structure_id($id, $type, $position, $activ, $content, $title, $textbox_image, $content_en, $title_en, $title_style) {
		if ($type == 'banner') {
			$images = array();

			foreach ($content as $key => $value) {
				$images[] = array('image_name' => $key, 'image_link_target' => 0, 'image_link' => $value);
			}

			$config = array(
				'images' => $images,
				'type' => 'standard'
			);

			$configuration = '$config = '.$this->php_2_string($config).';';

			$sql = "INSERT into site_content (structure_id, type, position, activ, content, configuration, title)
					VALUES ('".$this->db->real_escape_string($id)."', '".
						$this->db->real_escape_string($type)."', '".
						$this->db->real_escape_string($position)."', '".
						$this->db->real_escape_string($activ)."', '".
						$this->db->real_escape_string($content)."', '".
						$this->db->real_escape_string($configuration)."', '".
						$this->db->real_escape_string($title)."')" ;
		}
		else if ($type == 'textbox' || $type == 'slider' || $type == 'substructure') {
			//??$configuration = '$config = '.$this->php_2_string($textbox_image).';';

			if ($type == 'textbox') {
				$config = array(
					'images' => $content,
					'type' => 'standard'
				);
				$configuration = '$config = '.$this->php_2_string($config).';';
			}

			if ($type == 'substructure') {
				$config = array(
					'cat_id' => $content['cat_id'],
					'ebenen' => $content['ebenen']
				);
				$configuration = '$config = '.$this->php_2_string($config).';';
			}


			if ($content_en || $title_en) {
				$sql = "INSERT into site_content (structure_id, type, position, activ, content, configuration, title, style, content_en, title_en)
						VALUES ('".
							$this->db->real_escape_string($id)."', '".
							$this->db->real_escape_string($type)."', '".
							$this->db->real_escape_string($position)."', '".
							$this->db->real_escape_string($activ)."', '".
							$this->db->real_escape_string($content)."', '".
							$this->db->real_escape_string($configuration)."', '".
							$this->db->real_escape_string($title)."', '".
							$this->db->real_escape_string($title_style)."', '".
							$this->db->real_escape_string($content_en)."', '".
							$this->db->real_escape_string($title_en)."')" ;
			} else {
				$sql = "INSERT into site_content (structure_id, type, position, activ, content, configuration, title, style)
					VALUES ('".
					$this->db->real_escape_string($id)."', '".
					$this->db->real_escape_string($type)."', '".
					$this->db->real_escape_string($position)."', '".
					$this->db->real_escape_string($activ)."', '".
					$this->db->real_escape_string($content)."', '".
					$this->db->real_escape_string($configuration)."', '".
					$this->db->real_escape_string($title)."', '".
					$this->db->real_escape_string($title_style)."')" ;

			}
		} else {
			$sql = "INSERT into site_content (structure_id, type, position, activ, content, style, title)
					VALUES ('".
						$this->db->real_escape_string($id)."', '".
						$this->db->real_escape_string($type)."', '".
						$this->db->real_escape_string($position)."', '".
						$this->db->real_escape_string($activ)."', '".
						$this->db->real_escape_string($content)."', '".
						$this->db->real_escape_string($title_style)."', '".
						$this->db->real_escape_string($title)."')" ;
		}

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

		if ($result) {
			return true;
		}
		$this->error = "can't insert data";
		$this->base_object->logger->info('speichern', $sql);

		return false;
	}

	public function update_by_structure_id($id, $type, $position, $activ, $content, $title, $textbox_image, $content_en, $title_en, $title_style) {
		if ($type == 'banner') {

			$config = array(
				'images' => $content,
				'type' => 'standard'
			);

			$configuration = '$config = '.$this->php_2_string($config).';';

			$sql = "UPDATE site_content
					SET position='".
						$this->db->real_escape_string($position)."', activ='".
						$this->db->real_escape_string($activ)."', content='".
						$this->db->real_escape_string($content)."', title='".
						$this->db->real_escape_string($title)."', configuration='".
						$this->db->real_escape_string($configuration)."'
					WHERE structure_id='".$this->db->real_escape_string($id)."'
					AND type='".$type."'";

		} else if ($type == 'textbox' || $type == 'slider' || $type == 'substructure') {

			if ($type == 'textbox') {
				$configuration = '$config = '.$this->php_2_string($textbox_image).';';
			}

			if ($type == 'substructure') {
				$config = array(
					'cat_id' => $content['cat_id'],
					'ebenen' => $content['ebenen']
				);
				$configuration = '$config = '.$this->php_2_string($config).';';
			}

			if ($content_en || $title_en) {
				$sql = "UPDATE site_content
						SET position='".
							$this->db->real_escape_string($position)."', activ='".
							$this->db->real_escape_string($activ)."', content='".
							$this->db->real_escape_string($content)."', title='".
							$this->db->real_escape_string($title)."', style='".
							$this->db->real_escape_string($title_style)."', configuration='".
							$this->db->real_escape_string($configuration)."', title_en='".
							$this->db->real_escape_string($title_en)."', content_en='".
							$this->db->real_escape_string($content_en)."'
						WHERE structure_id='".$this->db->real_escape_string($id)."'
						AND type='".$type."'";
			} else {
				$sql = "UPDATE site_content SET position='".
						$this->db->real_escape_string($position)."', activ='".
						$this->db->real_escape_string($activ)."', content='".
						$this->db->real_escape_string($content)."', title='".
						$this->db->real_escape_string($title)."', style='".
						$this->db->real_escape_string($title_style)."', configuration='".
						$this->db->real_escape_string($configuration)."'
						WHERE structure_id='".$this->db->real_escape_string($id)."'
						AND type='".$type."'";
			}
		} else {
			$sql = "UPDATE site_content
					SET position='".$this->db->real_escape_string($position).
					"', activ='".$this->db->real_escape_string($activ).
					"', title='".$this->db->real_escape_string($title).
					"', style='".$this->db->real_escape_string($title_style).
					"', content='".$this->db->real_escape_string($content)."'
					WHERE structure_id='".$this->db->real_escape_string($id)."'
					AND type='".$type."'";
		}

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

		if ($result) {
			return true;
		}
		$this->error = "can't insert data";
		$this->base_object->logger->info('speichern', $sql);

		return false;
	}

	public function get_textbox_data_by_id($id) {
		$sql = "SELECT * FROM site_content WHERE type='textbox' AND structure_id = ".$id;
		$result = $this->db->query($sql);
		if ($obj = $result->fetch_object()) {
			return $obj->content;
		} else {
			return '';
		}
	} // end get_data

	public function add_banner($file_data, $id) {
		$tempFile = $file_data['Filedata']['tmp_name'];
		// is file a image
		$file_info = getimagesize ( $tempFile );
		if (isset($file_info[2]) && $file_info[2] > 0 && $file_info[2] < 4) {
			$targetPath = $_SERVER['DOCUMENT_ROOT'].'/web/'.SHOP_SYSTEM.'/images/site/';
			$image_type = array('', 'gif', 'jpg', 'png');
			$file_id=md5(uniqid(rand(),TRUE));
			$file_name = $file_id.'.'.$image_type[$file_info[2]];

			// save article picture
			$targetFile =  str_replace('//','/',$targetPath).$file_name;
			move_uploaded_file($tempFile,$targetFile);
			$imaginator = new phImaginator();
			$imaginator->add($targetFile);
			if (isset($this->base_object->config->shopConfiguration['site_content_banner_width']) && $this->base_object->config->shopConfiguration['site_content_banner_width']) {
				$imaginator->Resize_fitWidth($this->base_object->config->shopConfiguration['site_content_banner_width']);
			}

			$imaginator->save();

			$sql = "SELECT configuration FROM site_content WHERE id=".$id;
			$result = $this->db->query($sql);
			$obj = $result->fetch_object();
			$config = array();
			eval($obj->configuration);
			if (!isset($config['images'])) {
				$config = array(
					'images' => array(),
					'type' => 'standard'
				);
			};
			// save configuration
			$new_banner = array(
				'image_name' =>$file_name,
				'image_link_target' => 0,
				'image_link' => ''
			);
			$config['images'][] = $new_banner;
			$config_string = '$config = '.$this->php_2_string($config).';';

			// save picture in database
			$sql = "UPDATE site_content SET configuration = '".$this->db->real_escape_string($config_string)."'
					WHERE id=".$id;

			if ($this->db->query($sql)) {
				return $file_name;
			}
		}

		return false;
	}

	public function add_textbox_image($file_data, $id) {
		$tempFile = $file_data['Filedata']['tmp_name'];
		// is file a image
		$file_info = getimagesize ( $tempFile );
		if (isset($file_info[2]) && $file_info[2] > 0 && $file_info[2] < 4) {
			$targetPath = $_SERVER['DOCUMENT_ROOT'].'/web/'.SHOP_SYSTEM.'/images/site/';
			$image_type = array('', 'gif', 'jpg', 'png');
			$file_id=md5(uniqid(rand(),TRUE));
			$file_name = $file_id.'.'.$image_type[$file_info[2]];

			// save article picture
			$targetFile =  str_replace('//','/',$targetPath).$file_name;
			move_uploaded_file($tempFile,$targetFile);
			$imaginator = new phImaginator();
			$imaginator->add($targetFile);
			/*if (isset($this->base_object->config->shopConfiguration['site_content_banner_width']) && $this->base_object->config->shopConfiguration['site_content_banner_width']) {
				$imaginator->Resize_fitWidth($this->base_object->config->shopConfiguration['site_content_banner_width']);
			}*/

			$imaginator->save();

			// save configuration
			$new_image = array(
				'image_name' =>$file_name,
				'image_position' => 'right',
				'link' => ''
			);
			$config_string = '$config = '.$this->php_2_string($new_image).';';

			// save picture in database
			$sql = "UPDATE site_content SET configuration = '".$this->db->real_escape_string($config_string)."'
					WHERE id=".$id;

			if ($this->db->query($sql)) {
				return $file_name;
			}
		}

		return false;
	}

		private function php_2_string($data) {
			$string = '';
			if (is_array($data)) {
				$string .= 'array(';
				foreach ($data as $key => $value) {
					if (is_array($value)) {
						$string .= "'$key' => ".$this->php_2_string($value).", ";
					} else {
						$string .= "'$key' => '$value', ";
					}
				}
				$string .= ")";
			}
			else {
				$string .= "'".$data."'";
			};
			return $string;
		}



	public function delete_banner($id, $image_name) {
		// get pic infos
		$sql = "SELECT * FROM site_content WHERE id=".$id;
		$result = $this->db->query($sql);
		$obj = $result->fetch_object();
		$config = array();
		eval($obj->configuration);
		$new_banners = array();
		foreach ($config['images'] as $banner) {
			if ($banner['image_name'] != $image_name) {
				$new_banners[] = $banner;
			} else {
				unlink($_SERVER['DOCUMENT_ROOT'].'/web/'.SHOP_SYSTEM.'/images/site/'.$image_name);
			}
		}
		$config['images'] = $new_banners;
		$config_string = '$config = '.$this->php_2_string($config).';';

		// save picture in database
		$sql = "UPDATE site_content SET configuration = '".$this->db->real_escape_string($config_string)."' WHERE id=".$id;
		return $this->db->query($sql);
	}

	public function delete_textbox_image($id) {
		// get pic infos
		$sql = "SELECT * FROM site_content WHERE id=".$id;
		$result = $this->db->query($sql);
		$obj = $result->fetch_object();
		$config = array();
		eval($obj->configuration);
		$new_banners = array();
		unlink($_SERVER['DOCUMENT_ROOT'].'/web/'.SHOP_SYSTEM.'/images/site/'.$config['image_name']);

		// delete picture from database
		$sql = "
		UPDATE site_content
		SET configuration = ''
		WHERE id=".$id;
		return $this->db->query($sql);
	}

	public function get_banner_image($id) {
		// get pic infos
		$sql = "SELECT * FROM site_content WHERE id=".$id;
		$result = $this->db->query($sql);
		$obj = $result->fetch_object();

		$config = array();
		eval($obj->configuration);

		return $config;
	}
}