<?php
/**
* @version      $Id: admin_seo.php 
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       Richard Kammermayer <rk@ta-edv.de>
* Easyshop is a web shop system
*/

include_once './core/metatag.class.php';

class admin_seo {
	private $base_object;
	private $layout_object;
	private $manufacturer_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
	} // end __construct

	function run() {
		if (isset($_POST['action']) && $_POST['action'] == 'save_analytics_code') {
			$this->save_analytics_code();
		}
		else if (isset($_GET['action']) && $_GET['action'] == 'save_metatags') {
			$this->save_metatags();
		}
		else {
			return $this->default_action();
		}
	}
	
	private function default_action() {
		// get default metatags
		$metatag_object = new Metatag(false, $this->base_object);
		
		$metatags = $metatag_object->get_all_metatags_by_group('DEFAULT');
		$this->layout_object->assign('metatags', $metatags);
		
		$item_structure_metatags = $metatag_object->get_all_metatags_by_group('MENU');
		$this->layout_object->assign('item_structure_metatags', $item_structure_metatags);
                
                $item_structure_metatags = $metatag_object->get_all_metatags_by_group('MENU1');
		$this->layout_object->assign('item_structure_metatags1', $item_structure_metatags);

                $item_structure_metatags = $metatag_object->get_all_metatags_by_group('MENU2');
		$this->layout_object->assign('item_structure_metatags2', $item_structure_metatags);

                $item_structure_metatags = $metatag_object->get_all_metatags_by_group('MENU3');
		$this->layout_object->assign('item_structure_metatags3', $item_structure_metatags);

                $item_structure_metatags = $metatag_object->get_all_metatags_by_group('MENU4');
		$this->layout_object->assign('item_structure_metatags4', $item_structure_metatags);


		$item_details_metatags = $metatag_object->get_all_metatags_by_group('ARTIKEL');
		$this->layout_object->assign('item_details_metatags', $item_details_metatags);

		return $this->layout_object->fetch('admin_seo.tpl');
	}
	
	private function save_analytics_code() {
		if (isset($_POST['google_analytics_code'])) {
			$this->base_object->config->update_config_element('google_analytics_code', $_POST['google_analytics_code']);
			
			$return['status'] = 'success';
			$return['message'] = 'Google Analytics Code gespeichert!';
				
		} else {
			$return['status'] = 'error';
			$return['message'] = 'Kein Argument übergeben.';
		}
		
		echo json_encode($return);
	}

	private function save_metatags() {
		if (isset($_POST['metatag_field'])) {
			$metatag_object = new Metatag(false, $this->base_object);
			
			$default_data = array();
			$item_details_data = array();
			$structure_data = array();
                        $structure_data1 = array();
                        $structure_data2 = array();
                        $structure_data3 = array();
                        $structure_data4 = array();
			foreach($_POST['metatag_field'] as $key => $value) {
				if (substr($key, 0, 10) == 'structure_') {
					$structure_data[substr($key,10)] = $value;
				}
                                elseif (substr($key, 0, 11) == 'structure1_') {
					$structure_data1[substr($key,11)] = $value;
				}
                                elseif (substr($key, 0, 11) == 'structure2_') {
					$structure_data2[substr($key,11)] = $value;
				}
                                elseif (substr($key, 0, 11) == 'structure3_') {
					$structure_data3[substr($key,11)] = $value;
				}
                                elseif (substr($key, 0, 11) == 'structure4_') {
					$structure_data4[substr($key,11)] = $value;
				}
				elseif(substr($key, 0, 13) == 'item__detail_') {
					$item_details_data[substr($key,13)] = $value;
				}
				else {
					$default_data[$key] = $value;
				}
			}
			$metatags = $metatag_object->save_metatags('DEFAULT', $default_data);
			$metatags = $metatag_object->save_metatags('ARTIKEL', $item_details_data);
			$metatags = $metatag_object->save_metatags('MENU', $structure_data);
                        $metatags = $metatag_object->save_metatags('MENU1', $structure_data1);
                        $metatags = $metatag_object->save_metatags('MENU2', $structure_data2);
                        $metatags = $metatag_object->save_metatags('MENU3', $structure_data3);
                        $metatags = $metatag_object->save_metatags('MENU4', $structure_data4);
			
			$return['status'] = 'success';
			$return['message'] = 'Metatags gespeichert!!';
				
		} else {
			$return['status'] = 'error';
			$return['message'] = 'Kein Argument übergeben.';
		}

		echo json_encode($return);
	}
	
}

