<?php

include_once './core/structure.class.php';
include_once './core/site_content.class.php';

class admin_site_designer{
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
	}
	
	function run() {
		$structure = new Structure($this->base_object);
		$siteContent = new SiteContent($this->base_object);
		
		if (isset($_GET['id'])) {
			$id = (int)$_GET['id'];
			if ($structure->is_set($id)){
				$config = $siteContent->get_by_structure_id($id);
				if($config == false) {
					$this->layout_object->assign('info_message', 'Diese Seite wurde noch nicht konfiguriert.');
					$this->layout_object->assign('textbox_num', '1');
					$this->layout_object->assign('textbox_activ', 0);
					$this->layout_object->assign('slider_num', '2');
					$this->layout_object->assign('slider_activ', 0);
					$this->layout_object->assign('article_list_num', '3');
					$this->layout_object->assign('article_list_activ', 0);
					$this->layout_object->assign('featured_num', '4');
					$this->layout_object->assign('featured_activ', 0);
					$this->layout_object->assign('manufacturers_num', '5');
					$this->layout_object->assign('manufacturers_activ', 0);
				} else {
					foreach($config as $obj) {
						if($obj->type == 'textbox') {
							$this->layout_object->assign('textbox_num', $obj->position);
							$this->layout_object->assign('textbox_activ', (int)$obj->activ);
							$this->layout_object->assign('textbox_content', $obj->content);
						} elseif($obj->type == 'slider') {
							$this->layout_object->assign('slider_num', $obj->position);
							$this->layout_object->assign('slider_activ', (int)$obj->activ);
							$this->layout_object->assign('slider_content', $obj->content);
						} elseif($obj->type == 'article_list') {
							$this->layout_object->assign('article_list_num', $obj->position);
							$this->layout_object->assign('article_list_activ', (int)$obj->activ);
							$this->layout_object->assign('article_list_content', $obj->content);
						} elseif($obj->type == 'featured') {
							$this->layout_object->assign('featured_num', $obj->position);
							$this->layout_object->assign('featured_activ', (int)$obj->activ);
							$this->layout_object->assign('featured_content', $obj->content);
						} elseif($obj->type == 'manufacturers') {
							$this->layout_object->assign('manufacturers_num', $obj->position);
							$this->layout_object->assign('manufacturers_activ', (int)$obj->activ);
						}
					}
				}
				
				$this->layout_object->assign('id', $id);
				$this->layout_object->assign('item_cats', $structure->get_all_item_categories());
				$this->layout_object->assign('site_info', true);
			} else {
				$this->layout_object->assign('error_message', 'Die von ihnen angegebenen Seiten ID ist im System nicht vorhanden.');
			}
			
			return $this->get_layout();
		} else {
			if (isset($_GET['mod']) && $_GET['mod'] == 'ajax') {
				if (isset($_GET['set']) && $_GET['set'] == 'widgets') {
					$id = $_POST['id'];
					
					$textbox = 'textbox';
					$textbox_activ = $_POST['content_box_textbox'] ? 1 : 0;
					$textbox_num = $_POST['textbox_num'];
					$textbox_content = $_POST['content_box_textbox_body'] ? $_POST['content_box_textbox_body'] : '';
					
					$slider = 'slider';
					$slider_activ = $_POST['content_box_slider'] ? 1 : 0;
					$slider_num =$_POST['slider_num'];
					$slider_content = $_POST['cat_id'] ? $_POST['cat_id'] : '';
										
					$article_list = 'article_list';
					$article_list_activ = $_POST['content_box_article_list'] ? 1 : 0;
					$article_list_num = $_POST['article_list_num'];
					$article_list_content = $_POST['article_list_style'] ? $_POST['article_list_style'] : '0';
					
					$featured = 'featured';
					$featured_activ = $_POST['content_box_featured'] ? 1 : 0;
					$featured_num = $_POST['featured_num'];
					$featured_content = $_POST['item_id'] ? $_POST['item_id'] : '0';
					
					$banner = 'banner';
					$banner_activ = $_POST['content_box_banner'] ? 1 : 0;
					$banner_num = $_POST['banner_num'];
					$banner_content = $_POST['banner_link'] ? $_POST['banner_link'] : '0';
					
					$manufacturers = 'manufacturers';
					$manufacturers_activ = $_POST['content_box_manufacturers'] ? 1 : 0;
					$manufacturers_num = $_POST['manufacturers_num'];
					
					if ($siteContent->get_by_structure_id($id) == false) {
						$siteContent->insert_by_structure_id($id, $textbox, $textbox_num, $textbox_activ, $textbox_content);
						$siteContent->insert_by_structure_id($id, $slider, $slider_num, $slider_activ, $slider_content);
						$siteContent->insert_by_structure_id($id, $article_list, $article_list_num, $article_list_activ, $article_list_content);
						$siteContent->insert_by_structure_id($id, $featured, $featured_num, $featured_activ, $featured_content);
						$siteContent->insert_by_structure_id($id, $banner, $banner_num, $banner_activ, $banner_content);
						$siteContent->insert_by_structure_id($id, $manufacturers, $manufacturers_num, $manufacturers_activ, '');
					} else {
						$siteContent->update_by_structure_id($id, $textbox, $textbox_num, $textbox_activ, $textbox_content);
						$siteContent->update_by_structure_id($id, $slider, $slider_num, $slider_activ, $slider_content);
						$siteContent->update_by_structure_id($id, $article_list, $article_list_num, $article_list_activ, $article_list_content);
						$siteContent->update_by_structure_id($id, $featured, $featured_num, $featured_activ, $featured_content);
						$siteContent->update_by_structure_id($id, $banner, $banner_num, $banner_activ, $banner_content);
						$siteContent->update_by_structure_id($id, $manufacturers, $manufacturers_num, $manufacturers_activ, '');
					}
				} else {
					echo 'FEHLER';
				}
			} else {
				$this->layout_object->assign('site_list', $structure->get_all_item_categories());
				
				return $this->get_layout();
			}
		}
	}
	
	function get_layout() {
		return $this->layout_object->fetch('admin_site_designer.tpl');
	}
}