<?php
class ControllerInformationInformation extends Controller {
	public function index() {
		$this->load->language('information/information');

		$this->load->model('catalog/information');

		$data['breadcrumbs'] = array();

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

        if (isset($this->request->get['information_id'])) {
            $information_id = (int)$this->request->get['information_id'];

            $information_info = $this->model_catalog_information->getInformation($information_id);

            if ($information_info) {

                if ($information_info['meta_title']) {
                    $this->document->setTitle($information_info['meta_title']);
                } else {
                    $this->document->setTitle($information_info['title']);
                }

                $this->document->setDescription($information_info['meta_description']);
                $this->document->setKeywords($information_info['meta_keyword']);

                $this->load->model('catalog/information');

                if ($information_info['parent_id'] != 0) {

                    $this->load->model('catalog/information');

                    $category_info = $this->model_catalog_information->getBreadcrumbs((int)$information_id);

                    if ($category_info) {
                        foreach($category_info as $category) {
                            $data['breadcrumbs'][] = array(
                                'text' => $category['title'],
                                'href' => $this->url->link('information/information', 'information_id=' . $category['path_id'])
                            );
                        }
                    }
                }

                $data['breadcrumbs'][] = array(
                    'text' => $information_info['title'],
                    'date' => $this->language->get('create_date'),
                    'seen' => $this->language->get('seen'),
                    'comments' => $this->language->get('comments'),
                    'href' => $this->url->link('information/information', 'information_id=' .  $information_id)
                );

                $pattern = false;

                if($information_info['pattern']) {
                    $pattern = substr($information_info['pattern'], 0, -4);
                }

                $data['heading_title'] = $information_info['title'];

                $data['button_continue'] = $this->language->get('button_continue');

                $data['text_write'] = $this->language->get('text_write');
                $data['review_guest'] = $this->language->get('review_guest');
                $data['text_login'] = $this->language->get('text_login');
                $data['entry_name'] = $this->language->get('entry_name');
                $data['entry_email'] = $this->language->get('entry_email');
                $data['entry_review'] = $this->language->get('entry_review');
                $data['text_note'] = $this->language->get('text_note');
                $data['entry_rating'] = $this->language->get('entry_rating');
                $data['entry_bad'] = $this->language->get('entry_bad');
                $data['entry_good'] = $this->language->get('entry_good');
                $data['captcha'] = $this->language->get('captcha');
                $data['date'] = $this->language->get('create_date');
                $data['seen'] = $this->language->get('seen');
                $data['comments'] = $this->language->get('comments');
                $data['form_lost_comment'] = $this->language->get('form_lost_comment');
                $data['form_comment'] = $this->language->get('form_comment');
                $data['form_name'] = $this->language->get('form_name');
                $data['form_email'] = $this->language->get('form_email');
                $data['form_enter_text'] = $this->language->get('form_enter_text');
                $data['send_form'] = $this->language->get('send_form');
                $data['news'] = $this->language->get('news');
                $data['other_news'] = $this->language->get('other_news');
                $data['text_details'] = $this->language->get('text_details');
                $data['text_previous_news'] = $this->language->get('text_previous_news');

                $data['information_id'] = $information_id;

                $data['informations'] = $this->model_catalog_information->getChildInformations($information_id);

                $this->load->model('tool/image');

                $data['description'] = html_entity_decode($information_info['description'], ENT_QUOTES, 'UTF-8');

                foreach($data['informations'] as $i => $information) {
                        $data['informations'][$i]['thumb'] = $this->model_tool_image->resize($information['image'], 250, 250);
                        $data['informations'][$i]['href'] = $this->url->link('information/information', 'information_id=' . $information['information_id']);
                        $data['informations'][$i]['prew'] = mb_substr(strip_tags(html_entity_decode($information['description'])), 0, 1000) . '...' ;
                        $data['informations'][$i]['short'] = $information['short_description'];
                        $data['informations'][$i]['title_help'] = $information['meta_description'];
                        $data['informations'][$i]['date_added'] = sprintf($this->language->get('text_published'), date('d.m.Y', strtotime($information['date_added'])));
                }

                $data['continue'] = $this->url->link('common/home');

                $data['column_left'] = $this->load->controller('common/column_left');
                $data['column_right'] = $this->load->controller('common/column_right');
                $data['content_top'] = $this->load->controller('common/content_top');
                $data['content_bottom'] = $this->load->controller('common/content_bottom');
                $data['footer'] = $this->load->controller('common/footer');
                $data['header'] = $this->load->controller('common/header');

                if($pattern && file_exists(DIR_TEMPLATE . 'brech' . '/template/information/patterns/'.$pattern.'.tpl')) {
                    $this->response->setOutput($this->load->view('information/patterns/'.$pattern, $data));
                } elseif (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/information.tpl')) {
                    $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/information/information.tpl', $data));
                } else {
                    $this->response->setOutput($this->load->view('/information/information.tpl', $data));
                }
            } else {
                $data['breadcrumbs'][] = array(
                    'text' => $this->language->get('text_error'),
                    'href' => $this->url->link('information/information', 'information_id=' . $information_id)
                );

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

                $data['heading_title'] = $this->language->get('text_error');

                $data['text_error'] = $this->language->get('text_error');

                $data['button_continue'] = $this->language->get('button_continue');

                $data['continue'] = $this->url->link('common/home');

                $this->response->addHeader($this->request->server['SERVER_PROTOCOL'] . ' 404 Not Found');

                $data['column_left'] = $this->load->controller('common/column_left');
                $data['column_right'] = $this->load->controller('common/column_right');
                $data['content_top'] = $this->load->controller('common/content_top');
                $data['content_bottom'] = $this->load->controller('common/content_bottom');
                $data['footer'] = $this->load->controller('common/footer');
                $data['header'] = $this->load->controller('common/header');

                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
                    $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/error/not_found.tpl', $data));
                } else {
                    $this->response->setOutput($this->load->view('default/template/error/not_found.tpl', $data));
                }
            }
        } else {
            $this->document->setTitle($this->config->get('config_meta_title'));
            $this->document->setDescription($this->config->get('config_meta_description'));
            $this->document->setKeywords($this->config->get('config_meta_keyword'));

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

            $data['heading_title'] = $this->language->get('text_error');

            $data['text_error'] = $this->language->get('text_error');

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_breadcrumbs'),
                'href' => $this->url->link('information/information')
            );

            $this->load->model('tool/image');

            $data['informations'] = $this->model_catalog_information->getInformations();

            foreach($data['informations'] as $i=>$information){
                    $data['informations'][$i]['thumb'] = $this->model_tool_image->resize($information['image'], 100, 100);
                    $data['informations'][$i]['href'] = $this->url->link('information/information', 'information_id=' . $information['information_id']);
                    $data['informations'][$i]['prew'] = mb_substr(strip_tags(html_entity_decode($information['description'])), 0, 100) . '...' ;
            }

            $data['column_left'] = $this->load->controller('common/column_left');
            $data['column_right'] = $this->load->controller('common/column_right');
            $data['content_top'] = $this->load->controller('common/content_top');
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
            $data['footer'] = $this->load->controller('common/footer');
            $data['header'] = $this->load->controller('common/header');

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/error/not_found.tpl')) {
                $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/information/error/not_found.tpl', $data));
            } else {
                $this->response->setOutput($this->load->view('/error/not_found.tpl', $data));
            }

        }
	}

	public function agree() {
		$this->load->model('catalog/information');

		if (isset($this->request->get['information_id'])) {
			$information_id = (int)$this->request->get['information_id'];
		} else {
			$information_id = 0;
		}

		$output = '';

		$information_info = $this->model_catalog_information->getInformation($information_id);

		if ($information_info) {
			$output .= html_entity_decode($information_info['description'], ENT_QUOTES, 'UTF-8') . "\n";
		}

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