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

class Metatag {

	private $_db;
	private $layoutObject;
	private $baseObject;
	private $metaTags;
	private $metaVars = array(
		'Hersteller' => '',
		'VonHersteller' => '',
		'Shopname' => '',
		'Unterwarengruppen' => null,
		'Warengruppe' => null,
		'Bezeichnung' => null,
		'ArtikelAttribut6' => '',
		'Struktur1' => '',
		'Struktur2' => '',
		'Struktur3' => '',
		'Struktur4' => '',
		'Struktur5' => '',
		'Struktur6' => ''
	);

	private $collectedManufacturers;

	public function __construct($layoutObject = false, $baseObject = false) {
		$this->_db = Registry::get('base')->db;

		$this->layoutObject = $layoutObject;
		$this->baseObject = $baseObject;
	}

	public function get_all() {
		$sql = "SELECT * FROM metatags";

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

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				$return[] = $obj;
			}

			return $return;
		}

		return false;
	}

	public function get_by_group($groupName = 'DEFAULT') {
		$sql = "SELECT * FROM metatags WHERE groupName = '".$this->_db->real_escape_string($groupName)."'";

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

		if ($result->num_rows > 0) { 
			while ($obj = $result->fetch_object()) { 
				$this->layoutObject->allMetaTags[$obj->name] = $obj->content;
			}

			return true;
		}

		return false;
	}

	public function get_by_id($id) {
		$sql = "SELECT * FROM metatags WHERE id=$id AND active=1";

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

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

		return false;
	}

	public function save($data) {
		$data = Database::clean($data);

		if (isset($data['id'])) {
			$id = $data['id'];
			unset($data['id']);

			$sql = Database::update('metatags', $data, 'WHERE id='.$id);
		} else {
			$sql = Database::insert('metatags', $data);
		}

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

		if ($result) {
			return $this->_db->insert_id;
		}

		return false;
	}

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

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

		if ($result) {
			return true;
		}

		return false;
	}

	public function get_content_by_name($name) {
		$sql = "SELECT content FROM metatags WHERE name='$name'";

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

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

		return false;
	}

	public function assign() { 
		if (is_array($this->layoutObject->allMetaTags)) {

			if (!isset($this->metaVars['Hersteller']) || !$this->metaVars['Hersteller']) {
				$manufacturer = new Manufacturer($this->baseObject);
				$allManufacturer = $manufacturer->get_all_active();

				$allManus = array();
				foreach ($allManufacturer as $manuData) {
					$allManus[] = $manuData->name;
				}

				$this->metaVars['Hersteller'] = implode(', ', $allManus);
			}

			$all_metatags = array();
			$shopname = $this->baseObject->config->shopConfiguration['full_shop_name'];
			$domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
			$domain = str_replace('.com', '', $domain);
			$domain = str_replace('.de', '', $domain);
			if ($domain == 'mc-shower' ||
				$domain == 'mc-trolley' ||
				$domain == 'mc-heat' ||
				$domain == 'dusche-led' ||
				$domain == 'golf-trolley-caddy' ||
				$domain == 'led-handbrause' ||
				$domain == 'vgr-vertrieb'
			) {
				$shopname = $domain;
			}
			foreach ($this->layoutObject->allMetaTags as $metaTag => $content) { 
				$content = str_ireplace('[Shopname]', $shopname, $content);
				$content = str_ireplace('[Hersteller]', $this->metaVars['Hersteller'], $content);
				$content = str_ireplace('[VonHersteller]', $this->metaVars['VonHersteller'], $content);
				$content = str_ireplace('[ArtikelAttribut6]', $this->metaVars['ArtikelAttribut6'], $content);
				$content = str_ireplace('[Struktur1]', $this->metaVars['Struktur1'], $content);
				$content = str_ireplace('[Struktur2]', $this->metaVars['Struktur2'], $content);
				$content = str_ireplace('[Struktur3]', $this->metaVars['Struktur3'], $content);
				$content = str_ireplace('[Struktur4]', $this->metaVars['Struktur4'], $content);
				$content = str_ireplace('[Struktur5]', $this->metaVars['Struktur5'], $content);
				$content = str_ireplace('[Struktur6]', $this->metaVars['Struktur6'], $content);
				if (isset($this->metaVars['Warengruppe'])) {
					$content = str_ireplace('[Warengruppe]', $this->metaVars['Warengruppe'], $content);
				}
				if (isset($this->metaVars['Unterwarengruppen'])) {
					$content = str_ireplace('[Unterwarengruppen]', $this->metaVars['Unterwarengruppen'], $content);
				}
				if (isset($this->metaVars['Bezeichnung'])) {
					$content = str_ireplace('[Bezeichnung]', $this->metaVars['Bezeichnung'], $content);
				}
				$all_metatags[$metaTag] = $content;
			}
			$this->layoutObject->assign('metatags', $all_metatags);
		}
	}

	public function overwriteVar($var, $val) {
		$this->metaVars[$var] = $val;
	}

	public function overwriteMeta($metaTag, $val) {
		$this->layoutObject->allMetaTags[$metaTag] = $val;
	}

	public function overwriteMetas($metaTagsArray) {
		foreach ($metaTagsArray as $metaTag => $val) {
			if ($val) {
				$this->layoutObject->allMetaTags[$metaTag] = $val;
			}
		}
	}

	public function get_all_metatags_by_group($groupName = 'DEFAULT') {
		$sql = "SELECT * FROM metatags WHERE groupName = '".$this->_db->real_escape_string($groupName)."'";

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

	public function save_metatags($group, $data) {
		foreach ($data as $key => $value) {
			$sql = "UPDATE metatags 
					SET content='".$this->_db->real_escape_string($value)."'
					WHERE groupName = '".$this->_db->real_escape_string($group)."'
					AND name = '".$this->_db->real_escape_string($key)."'";
			$result = $this->_db->query($sql);
			if ($this->db->error) {
				$log_object = Logger::get_instance();
				$log_object->info('Error in /core/metatag.class.php->save_metatags()', $this->db->error.', sql:'.$sql);
			}
		}
		return;
	}

}

?>