<?php
/* Droconut SLIDERS v1.3; Droconut - Dropping Coconut KG
http://www.droconut.com 
Support: office@droconut.com

Copyright 2012, Attila Santo (Dropping Coconut KG) */

class ControllerModuleSliders extends Controller {
	private $error = array();

	public function index() {
		$this->data = array_merge($this->data, $this->load->language('module/sliders'));

		$this->document->addScript('view/javascript/jquery/jquery.form.js');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('sliders', $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['dimension'])) {
			$this->data['error_dimension'] = $this->error['dimension'];
		} else {
			$this->data['error_dimension'] = array();
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'	  => $this->language->get('text_home'),
			'href'	  => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'	  => $this->language->get('text_module'),
			'href'	  => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		$this->data['breadcrumbs'][] = array(
			'text'	  => $this->language->get('heading_title'),
			'href'	  => $this->url->link('module/sliders', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		$this->data['action'] = $this->url->link('module/sliders', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['modules'] = array();

		if (isset($this->request->post['sliders_module'])) {
			$this->data['modules'] = $this->request->post['sliders_module'];
		} elseif ($this->config->get('sliders_module')) {
			$this->data['modules'] = $this->config->get('sliders_module');
		}

		$this->load->model('design/layout');

		$this->data['layouts'] = $this->model_design_layout->getLayouts();

		$this->load->model('design/banner');

		$banners = $this->model_design_banner->getBanners();

		foreach ($banners as $banner) {

			$banner_images = array();
			$banner_images = $this->model_design_banner->getBannerImages($banner['banner_id']);

			foreach ($banner_images as $banner_image) {

				list($width, $height) = getimagesize(HTTP_CATALOG . 'image/' . $banner_image['image']);

				$image_data = array();
				$image_data[] = array(
					'banner_image_description'	=> $banner_image['banner_image_description'],
					'link'						=> $banner_image['link'],
					'image'						=> (HTTP_CATALOG . 'image/' . $banner_image['image']),
					'width'						=> $width,
					'height'					=> $height
				);
			}

			$banner_extended[] = array(
				'banner_id'	 => $banner['banner_id'],
				'name'		 => $banner['name'],
				'image'		 => $image_data
			);
		}

		$this->data['banners'] = $banner_extended;

		$this->template = 'module/sliders.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}


	public function install() {
		/*if ($this->validate()) {*/
			$column = $this->db->query("DESCRIBE " . DB_PREFIX . "banner_image");
				if(!$this->recursive_array_search("collocation",$column->rows)) {
					$this->db->query("ALTER TABLE " .DB_PREFIX. "banner_image ADD COLUMN collocation INT(4) NULL");
				};
			$column = $this->db->query("DESCRIBE " . DB_PREFIX . "banner_image_description");
				if(!$this->recursive_array_search("description",$column->rows)) {
					$this->db->query("ALTER TABLE " .DB_PREFIX. "banner_image_description ADD COLUMN description TEXT NULL");
				};
		/*}*/
	}


	public function uninstall() {
		/*if ($this->validate()) {*/
			$column = $this->db->query("DESCRIBE " . DB_PREFIX . "banner_image");
				if($this->recursive_array_search("collocation",$column->rows)) {
					$this->db->query("ALTER TABLE " .DB_PREFIX. "banner_image DROP COLUMN collocation");
				};
			$column = $this->db->query("DESCRIBE " . DB_PREFIX . "banner_image_description");
				if($this->recursive_array_search("description",$column->rows)) {
					$this->db->query("ALTER TABLE " .DB_PREFIX. "banner_image_description DROP COLUMN description");
				};
		/*}*/
	}


	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/sliders')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (isset($this->request->post['sliders_module'])) {
			foreach ($this->request->post['sliders_module'] as $key => $value) {
				if (!$value['width'] || !$value['height']) {
					$this->error['dimension'][$key] = $this->language->get('error_dimension');
				}
			}
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function recursive_array_search($needle,$haystack) {
		foreach($haystack as $key=>$value) {
			$current_key=$key;
			if($needle===$value OR (is_array($value) && $this->recursive_array_search($needle,$value) !== false)) {
				return $current_key;
			}
		}
		return false;
	}
}
?>