<?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 './modules/list_and_edit.class.php';
include_once './core/structure.class.php';
include_once './core/site_content.class.php';
include_once './core/manufacturer.class.php';
include_once './core/customer_group.class.php';
include_once './core/shop_theme.class.php';

class admin_structure {

	private $base_object;
	private $layout_object;
	private $config;
	private $structure_object;
	private $site_content_object;

	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->config = $base_object->config_object;
		$this->structure_object = new Structure($base_object);
		$this->site_content_object = new SiteContent($base_object);
		$this->customer_group_object = new Customer_group($base_object);
	}

	function run() {
		if (isset($_GET['id'])) {
			$id = $_GET['id'];
		} else if (isset($_POST['id'])) {
			$id = $_POST['id'];
		} else {
			$id = false;
		}

		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} else if (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}

		if ($action == 'delete') {
			$this->del($id);
		} else if ($action == 'save_site') {
			$this->save_site($id);
		} else if ($action == 'save_discount') {
			$this->save_discount($id);
		} else if ($action == 'get_discount') {
			$this->get_discount($id, $_POST['customerGroup']);
		} else if ($action == 'save_layout') {
			$this->save_layout($id);
		} else if ($action == 'get_sub_structure') {
			$this->get_sub_structure($id);
		} else if ($action == 'upload_banner') {
			$this->upload_banner($id);
		} else if ($action == 'upload_textbox_image') {
			$this->upload_textbox_image($id);
		} else if ($action == 'get_banner') {
			$this->get_banner($id);
		} else if ($action == 'delete_banner') {
			$this->delete_banner($id, $_GET['banner_image']);
		} else if ($action == 'delete_textbox_image') {
			$this->delete_textbox_image($id);
		} else if ($action == 'set_configuration_type') {
			$this->set_configuration_type($id);
		} else if ($action == 'copy') {
			return $this->edit($id, true);
		} else if ($action == 'upload') {
			$this->upload($id);
		} else if ($action == 'delete_image') {
			$this->delete_image($id);
		} else if ($action == 'upload_header_image') {
			$this->upload_header_image($id);
		} else if ($action == 'delete_header_image') {
			$this->delete_header_image($id);
		} else {
			return $this->edit($id);
		}
	}

	private function delete_image($id) {
		if ($id) {
			if ($this->structure_object->set_image($id, '')) {
				$return = array('status' => 'success');
			} else {
				$return = array('status' => 'error');
			}

		} else {
			$return = array('status' => 'error');
		}

		echo json_encode($return);

		exit();
	}

	private function delete_header_image($id) {
		if ($id) {
			if ($this->structure_object->set_header_image($id, '')) {
				$return = array('status' => 'success');
			} else {
				$return = array('status' => 'error');
			}

		} else {
			$return = array('status' => 'error');
		}

		echo json_encode($return);

		exit();
	}

	private function upload_header_image($id) {
		if ($id) {
			$temp_file = $_FILES['Filedata']['tmp_name'];
			if ($temp_file != '') {
				$result = $this->structure_object->save_header_image($id, $temp_file);

				if ($result) {
					$return = array('status' => 'success');
				} else {
					$return = array('status' => 'error', 'message' => "ERROR: can't save", 'data', $result);
				}
			} else {
				$return = array('status' => 'error', 'message' => 'ERROR: no file');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'ERROR: no id');
		}

		echo json_encode($return);

		exit();
	} // end upload

	private function upload($id) {
		if ($id) {
			$temp_file = $_FILES['Filedata']['tmp_name'];
			if ($temp_file != '') {
				$result = $this->structure_object->save_image($id, $temp_file);

				if ($result) {
					$return = array('status' => 'success');
				} else {
					$return = array('status' => 'error', 'message' => "ERROR: can't save", 'data', $result);
				}
			} else {
				$return = array('status' => 'error', 'message' => 'ERROR: no file');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'ERROR: no id');
		}

		echo json_encode($return);

		exit();
	} // end upload


	private function save_discount($id) {
		if (isset($_POST)) {
			$form_data = $_POST['form_field'];
		} else {
			$form_data = false;
		}

		if ($form_data) {
			if ($id) {

				// todo save discounts
				$form_data['structure_id'] = $id;
				$manufacturer_object = new Manufacturer($this->base_object);
				$manufacturer_list = $manufacturer_object->get_all_active();
				$save_request = $this->structure_object->save_discounts($form_data, $manufacturer_list);

				if (!isset($save_request['error'])) {
					$return_data = array('status' => 'success', 'data' => true);
				} else {
					$message_text = $save_request['error'];

					$return_data = array('status' => 'error', 'message' => $message_text, 'save_errors' => $save_errors);
				}
			} else {
				$return_data = array('status' => 'error', 'message' => 'no id');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'no data');
		}

		echo json_encode($return_data);
		exit();
	}

	private function get_discount($structure_id, $customer_group_id) {
		$discounts = $this->structure_object->get_discounts($structure_id, $customer_group_id);
		// get manufacturer
		$manufacturer_object = new Manufacturer($this->base_object);
		$manufacturer_list = $manufacturer_object->get_all_active();
		$data = array();
		foreach ($manufacturer_list as $obj) {
			$data[$obj->id] = (isset($discounts[$obj->id]))? $discounts[$obj->id] : 0;
		}

		$return_data = array('status' => 'success', 'message' => '', 'discounts' => $data);


		echo json_encode($return_data);
		exit();
	}

	// needs some more work
	private function upload_banner($id) {
		$result = $this->site_content_object->add_banner($_FILES,  $id);

		if ($result) {
			$return_array = array('status' => 'success', 'path' => '/web/'.SHOP_SYSTEM.'/images/site/', 'image' => $result);
		} else {
			$return_array = array('status' => 'error', 'message' => 'no result');
		}

		echo json_encode($return_array);
		exit();
	}

	// needs some more work
	private function upload_textbox_image($id) {
		$result = $this->site_content_object->add_textbox_image($_FILES,  $id);

		if ($result) {
			$return_array = array('status' => 'success', 'path' => '/web/'.SHOP_SYSTEM.'/images/site/', 'image' => $result);
		} else {
			$return_array = array('status' => 'error', 'message' => 'no result');
		}

		echo json_encode($return_array);
		exit();
	}

	// needs some more work
	private function get_banner($id) {
		echo "TODO";
		exit();
	}

	private function delete_banner($id, $image_name) {
		if ($id) {
			$result = $this->site_content_object->delete_banner($id, $image_name);

			if ($result) {
				$return_data = array('status' => 'success', 'data' => $result);
			} else {
				$return_data = array('status' => 'error', 'message' => 'no result');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'no id');
		}

		echo json_encode($return_data);
		exit();
	}

	private function delete_textbox_image($id) {
		if ($id) {
			$result = $this->site_content_object->delete_textbox_image($id);

			if ($result) {
				$return_data = array('status' => 'success', 'data' => $result);
			} else {
				$return_data = array('status' => 'error', 'message' => 'no result');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'no id');
		}

		echo json_encode($return_data);
		exit();
	}

	private function set_configuration_type($id) {
		if (isset($_GET['checked'])) {
			$is_checked = $_GET['checked'];
		} else {
			$is_checked = false;
		}

		if ($id) {
			if ($is_checked !== false) {
				$this->structure_object->structure_id = $id;
				$result = $this->structure_object->set_site_configuration_type($is_checked);

				if ($result) {
					$return_data = array('status' => 'success', 'data' => $result);
				} else {
					$return_data = array('status' => 'error', 'message' => 'no result with '.$is_checked.' this argument');
				}
			} else {
				$return_data = array('status' => 'error', 'message' => 'no data');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'no id');
		}

		echo json_encode($return_data);
		exit();
	}

	private function del($id) {
		if ($id) {
			$result = $this->structure_object->delete($id);

			if ($result) {
				$return_data = array('status' => 'success', 'data' => $result);
			} else {
				$return_data = array('status' => 'error', 'message' => 'not implemented');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'no id');
		}

		echo json_encode($return_data);
		exit();
	}

	private function save_site($id) {
		if ($id) {
			// save logo
			$temp_file = $_FILES['logo_image']['tmp_name'];
			if ($temp_file != '') {
				$result = $this->structure_object->save_image($id, $temp_file);
			}

			// save header
			$temp_file = $_FILES['header_image']['tmp_name'];
			if ($temp_file != '') {
				$result = $this->structure_object->save_header_image($id, $temp_file);
			}

			$form_data = $this->structure_object->data_filter($_POST['form_field']);
			$this->structure_object->structure_id = $id;
			$return = $this->structure_object->update($form_data);

			$sort_item_ids = $_POST['sort_item_id'];
			$sort_item_pos = $_POST['sort_item_pos'];
			$this->structure_object->update_sort($sort_item_ids, $sort_item_pos);

			if ($return) {
				header('location: http://'.$_SERVER['SERVER_NAME'].'/index.php?admin_modul=admin_structure&id='.$id);
				exit();
			}
		} else {
			$form_data = $this->structure_object->data_filter($_POST['form_field']);

			$return_id = $this->structure_object->create($form_data);

			$sort_item_ids = $_POST['sort_item_id'];
			$sort_item_pos = $_POST['sort_item_pos'];
			$this->structure_object->update_sort($sort_item_ids, $sort_item_pos);

			if ($return_id) {
				header('location: http://'.$_SERVER['SERVER_NAME'].'/index.php?admin_modul=admin_structure&id='.$return_id);
				exit();
			}
		}

		header('location: '.$_SERVER["HTTP_REFERER"]);
		exit();
	}

	private function save_layout($id) {
		if (isset($_POST)) {
			$form_data = $_POST;
		} else {
			$form_data = false;
		}

		if ($form_data) {
			if ($id) {
				$textbox = 'textbox';
				$textbox_activ = $form_data['content_box_textbox'] ? 1 : 0;
				$textbox_num = $form_data['textbox_num'];
				$textbox_title = $form_data['content_box_textbox_title'];
				$textbox_title_style = $form_data['content_box_textbox_title_style'];
				$textbox_title_en = $form_data['content_box_textbox_title_en'];
				$textbox_content = $form_data['content_box_textbox_body'] ? $form_data['content_box_textbox_body'] : '';
				$textbox_content_en = $form_data['content_box_textbox_body_en'] ? $form_data['content_box_textbox_body_en'] : '';
				$textbox_image = array('image_name' => $form_data['textbox_image_name'],'image_position' => $form_data['textbox_image_position'], 'image_link' => $form_data['textbox_image_link']);

				$slider = 'slider';
				$slider_activ = $form_data['content_box_slider'] ? 1 : 0;
				$slider_num = $form_data['slider_num'];
				$slider_title = $form_data['content_box_slider_title'];
				$slider_title_style = $form_data['content_box_slider_title_style'];
				$slider_title_en = $form_data['content_box_slider_title_en'];
				$slider_content = $form_data['cat_id'] ? $form_data['cat_id'] : '';

				$article_list = 'article_list';
				$article_list_activ = $form_data['content_box_article_list'] ? 1 : 0;
				$article_list_num = $form_data['article_list_num'];
				$article_list_title_style = $form_data['content_box_article_list_title_style'];
				$article_list_content = $form_data['article_list_style'] ? $form_data['article_list_style'] : '0';

				$banner = 'banner';
				$banner_activ = $form_data['content_box_banner'] ? 1 : 0;
				$banner_num = $form_data['banner_num'];
				// formated banner configuration
				$banner_content = array();
				foreach ($form_data['banner_link'] as $key => $value) {
					$banner_content[] = array(
						'image_name' => $key,
						'image_link' => $value,
						'image_link_target' => $form_data['image_link_target'][$key]
					);
				}

				$manufacturers = 'manufacturers';
				$manufacturers_activ = $form_data['content_box_manufacturers'] ? 1 : 0;
				$manufacturers_num = $form_data['manufacturers_num'];

				$popular = 'popular';
				$popular_num = $form_data['popular_num'];
				$popular_active = $form_data['content_box_popular'] ? 1 : 0;
				$popular_content = $form_data['popular_cat_id'] ? $form_data['popular_cat_id'] : '';

				$substructure = 'substructure';
				$substructure_num = $form_data['substructure_num'];
				$substructure_title = $form_data['content_box_substructure_title'];
				$substructure_title_style = $form_data['content_box_substructure_title_style'];
				$substructure_title_en = $form_data['content_box_substructure_title_en'];
				$substructure_active = $form_data['content_box_substructure'] ? 1 : 0;
				$substructure_cat_id = $form_data['substructure_cat_id'] ? $form_data['substructure_cat_id'] : '';
				$substructure_ebenen = $form_data['substructure_ebenen'] ? $form_data['substructure_ebenen'] : '';

				$save_errors = false;
				$textbox_result = $this->site_content_object->save_by_structure_id($id, $textbox, $textbox_num, $textbox_activ, $textbox_content, $textbox_title, $textbox_image, $textbox_content_en, $textbox_title_en, $textbox_title_style);
				if (!$textbox_result) $save_errors = true;

				$slider_result = $this->site_content_object->save_by_structure_id($id, $slider, $slider_num, $slider_activ, $slider_content, $slider_title, '', '', $slider_title_en, $slider_title_style);
				if (!$slider_result) $save_errors = true;

				$article_result = $this->site_content_object->save_by_structure_id($id, $article_list, $article_list_num, $article_list_activ, $article_list_content, '', '', '', '',$article_list_title_style);
				if (!$article_result) $save_errors = true;

				$banner_result = $this->site_content_object->save_by_structure_id($id, $banner, $banner_num, $banner_activ, $banner_content);
				if (!$banner_result) $save_errors = true;

				$manufacturer_result = $this->site_content_object->save_by_structure_id($id, $manufacturers, $manufacturers_num, $manufacturers_activ, '');
				if (!$manufacturer_result) $save_errors = true;

				$popular_result = $this->site_content_object->save_by_structure_id($id, $popular, $popular_num, $popular_active, $popular_content);
				if (!$popular_result) $save_errors = true;

				$substructure_result = $this->site_content_object->save_by_structure_id($id, $substructure, $substructure_num, $substructure_active, array('cat_id' => $substructure_cat_id,'ebenen' => $substructure_ebenen), $substructure_title, '', '', $substructure_title_en, $substructure_title_style);
				if (!$substructure_result) $save_errors = true;

				if (!$save_errors) {
					$return_data = array('status' => 'success', 'data' => true);
				} else {
					$message_text = '';

					if (!$textbox_result) $message_text .= " can't save textbox ";
					if (!$slider_result) $message_text .= " can't save slider ";
					if (!$article_result) $message_text .= " can't save article ";
					if (!$banner_result) $message_text .= " can't save banner ";
					if (!$manufacturer_result) $message_text .= " can't save manufacturer ";
					if (!$popular_result) $message_text .= " can't save popular ";
					if (!$substructure_result) $message_text .= " can't save substructure ";

					$return_data = array('status' => 'error', 'message' => $message_text, 'save_errors' => $save_errors);
				}
			} else {
				$return_data = array('status' => 'error', 'message' => 'no id');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'no data');
		}

		echo json_encode($return_data);
		exit();
	}

	private function get_sub_structure() {
		if (isset($_GET['parent_id'])) {
			$parent_id = $_GET['parent_id'];
		} else {
			$parent_id = false;
		}

		if ($parent_id) {
			$structure_tree = $this->structure_object->get_all_tree_full_path();
			echo "Structure Tree 2";
			var_dump($structure_tree);

			if ($id) {
				$structure_data = $this->structure_object->get_data($id);
				$this->layout_object->assign('structure_data', $structure_data);
			}
			$this->layout_object->assign('structure_tree', $structure_tree);
			$this->layout_object->assign('parent_id', $parent_id);

			echo $this->layout_object->fetch('admin_sub_structure.tpl');
		}

		exit();
	}

	private function edit($id, $copy = false) {
		if ($id) {
			$is_set = $this->structure_object->is_set($id);
			if ($is_set) {
				$structure_data = $this->structure_object->get_data($id);

				if ($structure_data->image != '') {
					$image = $this->structure_object->get_image_url().$structure_data->image;
					$this->layout_object->assign('image', $image);
				}

				if ($structure_data->header_image != '') {
					$image = $this->structure_object->get_header_image_url().$structure_data->header_image;
					$this->layout_object->assign('header_image', $image);
				}

				// get themes
				$shop_theme_object = new Shop_theme($this->base_object);
				$this->layout_object->assign('shop_themes', $shop_theme_object->get_all());

				// get item types
				include_once './core/itemtype.class.php';
				$item_type_oject = new ItemType($this->base_object);
				$this->layout_object->assign('item_types',$item_type_oject->get_all_objects());

				// get customer groups
				include_once './core/customer_group.class.php';
				$customer_group_oject = new Customer_group($this->base_object);
				$this->layout_object->assign('customer_groups',$customer_group_oject->get_all_objects());

				if ($copy) {
					unset($structure_data->id);
				}

				$this->layout_object->assign('form_data', $structure_data);
				$config = $this->site_content_object->get_by_structure_id($id);
				if ($config == false) {
					$i = 1;
					$this->layout_object->assign('textbox_num', $i++);
					$this->layout_object->assign('textbox_activ', 0);
					$this->layout_object->assign('slider_num', $i++);
					$this->layout_object->assign('slider_activ', 0);
					$this->layout_object->assign('article_list_num', $i++);
					$this->layout_object->assign('article_list_activ', 0);
					$this->layout_object->assign('banner_num', $i++);
					$this->layout_object->assign('banner_activ', 0);
					$this->layout_object->assign('manufacturers_num', $i++);
					$this->layout_object->assign('manufacturers_activ', 0);
					$this->layout_object->assign('popular_num', $i++);
					$this->layout_object->assign('popular_active', 0);
					$this->layout_object->assign('substructure_num', $i++);
					$this->layout_object->assign('substructure_active', 0);
				}
				else {
					$show_item_discount = 0;
					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_title', $obj->title);
							$this->layout_object->assign('textbox_title_style', $obj->style);
							$this->layout_object->assign('textbox_title_en', $obj->title_en);
							$this->layout_object->assign('textbox_content', $obj->content);
							$this->layout_object->assign('textbox_content_en', $obj->content_en);
							$config = array();
							eval($obj->configuration);
							$this->layout_object->assign('textbox_configuration', $config);
							$this->layout_object->assign('textbox_id', $obj->id);
						} else if ($obj->type == 'slider') {
							$this->layout_object->assign('slider_num', $obj->position);
							$this->layout_object->assign('slider_title', $obj->title);
							$this->layout_object->assign('slider_title_style', $obj->style);
							$this->layout_object->assign('slider_title_en', $obj->title_en);
							$this->layout_object->assign('slider_activ', (int)$obj->activ);
							$this->layout_object->assign('slider_content', $obj->content);
						} else if ($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_title_style', $obj->style);
							$this->layout_object->assign('article_list_content', $obj->content);
							if ($obj->activ == 1) {
								$show_item_discount = 1;
							}
						} else if ($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);
						} else if ($obj->type == 'banner') {
							$this->layout_object->assign('banner_num', $obj->position);
							$this->layout_object->assign('banner_activ', (int)$obj->activ);
							$this->layout_object->assign('banner_content', $obj->content);
							$config = array();
							eval($obj->configuration);
							$this->layout_object->assign('banner_configuration', $config);
							$this->layout_object->assign('banner_id', $obj->id);
						} else if ($obj->type == 'manufacturers') {
							$this->layout_object->assign('manufacturers_num', $obj->position);
							$this->layout_object->assign('manufacturers_activ', (int)$obj->activ);
						} else if ($obj->type == 'popular') {
							$this->layout_object->assign('popular_num', $obj->position);
							$this->layout_object->assign('popular_activ', (int)$obj->activ);
							$this->layout_object->assign('popular_content', $obj->content);
						} else if ($obj->type == 'substructure') {
							$config = array();
							eval($obj->configuration);
							$this->layout_object->assign('substructure_configuration', $config);
							$this->layout_object->assign('substructure_num', $obj->position);
							$this->layout_object->assign('substructure_title', $obj->title);
							$this->layout_object->assign('substructure_title_style', $obj->style);
							$this->layout_object->assign('substructure_title_en', $obj->title_en);
							$this->layout_object->assign('substructure_activ', (int)$obj->activ);
							$config = array();
							eval($obj->configuration);
							$this->layout_object->assign('substructure_configuration', $config);
							$this->layout_object->assign('substructure_ebenen', $config);
						}
					}
					if ($show_item_discount) {
						// get customer groups
						$customer_groups = $this->customer_group_object->get_all_groups();
						$this->layout_object->assign('customer_groups', $customer_groups);

						// get group discounts for first customergroup
						$first_customer_group = $customer_groups[100];

						$discounts = $this->structure_object->get_discounts($id, $first_customer_group->id);

						// get manufacturer
						$manufacturer_object = new Manufacturer($this->base_object);
						$manufacturer_list = $manufacturer_object->get_all_active();
						$manufacturers = array();
						foreach ($manufacturer_list as $obj) {
							$obj->discount = (isset($discounts[$obj->id]))? $discounts[$obj->id] : 0;
							$manufacturers[] = $obj;
						}

						$this->layout_object->assign('manufacturer_list', $manufacturers);
					}
				}

				$this->layout_object->assign('item_cats', $this->structure_object->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.');
			}
		}

		// get structure tree
		$structure_tree = $this->structure_object->get_all_tree_full_path();
		/*echo "Structure Tree 1:<br>";
		var_dump($structure_tree);*/
		$this->layout_object->assign('structure_tree', $structure_tree);

		// get structure tree html
		$structure_tree_html = $this->structure_object->get_tree_html('./index.php?admin_modul=admin_structure&id=');
		$this->layout_object->assign('structure_tree_html', $structure_tree_html);

		return $this->layout_object->fetch('admin_structure.tpl');
	}
}

?>