<?php
class ControllerExtensionModuleBlogLatest extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('extension/module/blog_latest');
		
		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('extension/module');
		$this->load->model('localisation/language');
		$data['languages'] = $this->model_localisation_language->getLanguages();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			if (!isset($this->request->get['module_id'])) {
				$this->model_extension_module->addModule('blog_latest', $this->request->post);
			} else {
				$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
			}

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

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

		$data['heading_title'] = $this->language->get('heading_title');
		
		$data['text_edit'] = $this->language->get('text_edit');
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');
		$data['text_qq_open_signup'] = $this->language->get('text_qq_open_signup');
		$data['text_yes'] = $this->language->get('text_yes');
		$data['text_no'] = $this->language->get('text_no');
		$data['text_mainc'] = $this->language->get('text_mainc');
		$data['text_mainfw'] = $this->language->get('text_mainfw');
		$data['text_cwkj_wxts'] = $this->language->get('text_cwkj_wxts');
		
		$data['entry_name'] = $this->language->get('entry_name');
		$data['entry_title'] = $this->language->get('entry_title');
		$data['entry_titlename'] = $this->language->get('entry_titlename');
		$data['entry_thumb_size'] = $this->language->get('entry_thumb_size');
		$data['entry_limit'] = $this->language->get('entry_limit');
		$data['entry_category'] = $this->language->get('entry_category');
		$data['text_none'] = $this->language->get('text_none');
		$data['entry_characters'] = $this->language->get('entry_characters');
		$data['entry_characters_h'] = $this->language->get('entry_characters_h');
		$data['entry_columns'] = $this->language->get('entry_columns');
		$data['entry_carousel'] = $this->language->get('entry_carousel');
		$data['entry_status'] = $this->language->get('entry_status');
		$data['entry_thumb'] = $this->language->get('entry_thumb');
		$data['text_1'] = $this->language->get('text_1');
		$data['text_2'] = $this->language->get('text_2');
		$data['text_3'] = $this->language->get('text_3');
		$data['text_4'] = $this->language->get('text_4');
		$data['entry_segmentingline'] = $this->language->get('entry_segmentingline');
		$data['entry_linew'] = $this->language->get('entry_linew');
		$data['entry_lineh'] = $this->language->get('entry_lineh');
        $data['entry_linec'] = $this->language->get('entry_linec');	
        $data['entry_describe'] = $this->language->get('entry_describe');		
		$data['entry_button1'] = $this->language->get('entry_button1');
		$data['entry_button2'] = $this->language->get('entry_button2');
		$data['entry_buttonname1'] = $this->language->get('entry_buttonname1');
		$data['entry_buttonname2'] = $this->language->get('entry_buttonname2');
		$data['entry_buttoncolor1'] = $this->language->get('entry_buttoncolor1');
		$data['entry_buttoncolor2'] = $this->language->get('entry_buttoncolor2');
		$data['entry_main'] = $this->language->get('entry_main');
		$data['entry_autoplay'] = $this->language->get('entry_autoplay');
		$data['entry_fade'] = $this->language->get('entry_fade');
		$data['entry_speed'] = $this->language->get('entry_speed');
        $data['entry_columns'] = $this->language->get('entry_columns');
        $data['entry_column'] = $this->language->get('entry_column');
		$data['help_column'] = $this->language->get('help_column');		
		$data['entry_rows'] = $this->language->get('entry_rows');
		$data['entry_scroll'] = $this->language->get('entry_scroll');	
		$data['entry_section'] = $this->language->get('entry_section');
		$data['entry_sectionbj'] = $this->language->get('entry_sectionbj');
		$data['entry_sectionjbbj'] = $this->language->get('entry_sectionjbbj');
		$data['entry_sectiontitle'] = $this->language->get('entry_sectiontitle');
		$data['entry_sectiondescribe'] = $this->language->get('entry_sectiondescribe');
		$data['entry_sectionptop'] = $this->language->get('entry_sectionptop');
        $data['entry_sectionpbuttom'] = $this->language->get('entry_sectionpbuttom');
		$data['entry_box'] = $this->language->get('entry_box');
		$data['entry_boxbg'] = $this->language->get('entry_boxbg');
		$data['entry_boxjbbj'] = $this->language->get('entry_boxjbbj');
		$data['entry_boxtitle'] = $this->language->get('entry_boxtitle');
		$data['entry_boxdescribe'] = $this->language->get('entry_boxdescribe');
		$data['entry_boxxinxi'] = $this->language->get('entry_boxxinxi');
        $data['entry_boxmagin'] = $this->language->get('entry_boxmagin');
		$data['entry_boxpadding'] = $this->language->get('entry_boxpadding');
		$data['entry_boxborder'] = $this->language->get('entry_boxborder');
		$data['entry_boxcolor'] = $this->language->get('entry_boxcolor');
		$data['entry_boxround'] = $this->language->get('entry_boxround');
		$data['entry_boxposition'] = $this->language->get('entry_boxposition');
		$data['entry_boxshadow'] = $this->language->get('entry_boxshadow');
		$data['entry_boxshadowhover'] = $this->language->get('entry_boxshadowhover');
		$data['entry_buttonstyle'] = $this->language->get('entry_buttonstyle');
		$data['entry_buttonsize'] = $this->language->get('entry_buttonsize');
		$data['entry_buttoninset'] = $this->language->get('entry_buttoninset');
		$data['entry_buttonborder'] = $this->language->get('entry_buttonborder');
		$data['entry_buttonround'] = $this->language->get('entry_buttonround');
		$data['entry_buttonhover'] = $this->language->get('entry_buttonhover');
        $data['entry_buttond'] = $this->language->get('entry_buttond');		
		
		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');
		
		$this->load->model('blog/blog_category');
		$data['blog_categories'] = array();
		$results = $this->model_blog_blog_category->getBlogCategories(0);
		foreach ($results as $result) {
			$data['blog_categories'][] = array(
				'blog_category_id' => $result['blog_category_id'],
				'name'        => $result['name']
			);
		}
		
		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
		
		if (isset($this->error['name'])) {
			$data['error_name'] = $this->error['name'];
		} else {
			$data['error_name'] = '';
		}
		
		
		$data['breadcrumbs'] = array();

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

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

		if (!isset($this->request->get['module_id'])) {
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('heading_title'),
				'href' => $this->url->link('extension/module/blog_latest', 'token=' . $this->session->data['token'], true)
			);
		} else {
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('heading_title'),
				'href' => $this->url->link('extension/module/blog_latest', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true)
			);			
		}

		if (!isset($this->request->get['module_id'])) {
			$data['action'] = $this->url->link('extension/module/blog_latest', 'token=' . $this->session->data['token'], true);
		} else {
			$data['action'] = $this->url->link('extension/module/blog_latest', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true);
		}
		
		$data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true);
		
		if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
		}
		
		if (isset($this->request->post['name'])) {
			$data['name'] = $this->request->post['name'];
		} elseif (!empty($module_info)) {
			$data['name'] = $module_info['name'];
		} else {
			$data['name'] = '';
		}
		
		if (isset($this->request->post['title'])) {
			$data['title'] = $this->request->post['title'];
		} elseif (!empty($module_info)) {
			$data['title'] = $module_info['title'];
		} else {
			$data['title'] = array();
		}
		if (isset($this->request->post['titlename'])) {
			$data['titlename'] = $this->request->post['titlename'];
		} elseif (!empty($module_info)) {
			$data['titlename'] = $module_info['titlename'];
		} else {
			$data['titlename'] = '';
		}
		if (isset($this->request->post['linew'])) {
			$data['linew'] = $this->request->post['linew'];
		} elseif (!empty($module_info)) {
			$data['linew'] = $module_info['linew'];
		} else {
			$data['linew'] = '30';
		}	
		if (isset($this->request->post['lineh'])) {
			$data['lineh'] = $this->request->post['lineh'];
		} elseif (!empty($module_info)) {
			$data['lineh'] = $module_info['lineh'];
		} else {
			$data['lineh'] = '2';
		}
		if (isset($this->request->post['linec'])) {
			$data['linec'] = $this->request->post['linec'];
		} elseif (!empty($module_info)) {
			$data['linec'] = $module_info['linec'];
		} else {
			$data['linec'] = '#000000';
		}
		if (isset($this->request->post['describe'])) {
			$data['describe'] = $this->request->post['name'];
		} elseif (!empty($module_info)) {
			$data['describe'] = $module_info['describe'];
		} else {
			$data['describe'] = '';
		}
		if (isset($this->request->post['buttonname1'])) {
			$data['buttonname1'] = $this->request->post['buttonname1'];
		} elseif (!empty($module_info)) {
			$data['buttonname1'] = $module_info['buttonname1'];
		} else {
			$data['buttonname1'] = '';
		}
		if (isset($this->request->post['buttonname2'])) {
			$data['buttonname2'] = $this->request->post['buttonname2'];
		} elseif (!empty($module_info)) {
			$data['buttonname2'] = $module_info['buttonname2'];
		} else {
			$data['buttonname2'] = '';
		}
		if (isset($this->request->post['buttoncolor1'])) {
			$data['buttoncolor1'] = $this->request->post['buttoncolor1'];
		} elseif (!empty($module_info)) {
			$data['buttoncolor1'] = $module_info['buttoncolor1'];
		} else {
			$data['buttoncolor1'] = 'red';
		}
		if (isset($this->request->post['buttoncolor2'])) {
			$data['buttoncolor2'] = $this->request->post['buttoncolor2'];
		} elseif (!empty($module_info)) {
			$data['buttoncolor2'] = $module_info['buttoncolor2'];
		} else {
			$data['buttoncolor2'] = 'black';
		}
		if (isset($this->request->post['main'])) {
			$data['main'] = $this->request->post['main'];
		} elseif (!empty($module_info)) {
			$data['main'] = $module_info['main'];
		} else {
			$data['main'] = 0;
		}
		if (isset($this->request->post['autoplay'])) {
			$data['autoplay'] = $this->request->post['autoplay'];
		} elseif (!empty($module_info)) {
			$data['autoplay'] = $module_info['autoplay'];
		} else {
			$data['autoplay'] = '';
		}
		if (isset($this->request->post['fade'])) {
			$data['fade'] = $this->request->post['fade'];
		} elseif (!empty($module_info)) {
			$data['fade'] = $module_info['fade'];
		} else {
			$data['fade'] = '';
		}
		if (isset($this->request->post['speed'])) {
			$data['speed'] = $this->request->post['speed'];
		} elseif (!empty($module_info)) {
			$data['speed'] = $module_info['speed'];
		} else {
			$data['speed'] = '3000';
		}
		if (isset($this->request->post['column'])) {
			$data['column'] = $this->request->post['column'];
		} elseif (!empty($module_info)) {
			$data['column'] = $module_info['column'];
		} else {
			$data['column'] = '3';
		}
		if (isset($this->request->post['rows'])) {
			$data['rows'] = $this->request->post['rows'];
		} elseif (!empty($module_info)) {
			$data['rows'] = $module_info['rows'];
		} else {
			$data['rows'] = '2';
		}
		if (isset($this->request->post['scroll'])) {
			$data['scroll'] = $this->request->post['scroll'];
		} elseif (!empty($module_info)) {
			$data['scroll'] = $module_info['scroll'];
		} else {
			$data['scroll'] = '1';
		}				
		if (isset($this->request->post['sectionbj'])) {
			$data['sectionbj'] = $this->request->post['sectionbj'];
		} elseif (!empty($module_info)) {
			$data['sectionbj'] = $module_info['sectionbj'];
		} else {
			$data['sectionbj'] = '';
		}
		if (isset($this->request->post['sectionjbbj'])) {
			$data['sectionjbbj'] = $this->request->post['sectionjbbj'];
		} elseif (!empty($module_info)) {
			$data['sectionjbbj'] = $module_info['sectionjbbj'];
		} else {
			$data['sectionjbbj'] = '';
		}
		if (isset($this->request->post['sectiontitle'])) {
			$data['sectiontitle'] = $this->request->post['sectiontitle'];
		} elseif (!empty($module_info)) {
			$data['sectiontitle'] = $module_info['sectiontitle'];
		} else {
			$data['sectiontitle'] = '';
		}		
		if (isset($this->request->post['sectiondescribe'])) {
			$data['sectiondescribe'] = $this->request->post['sectiondescribe'];
		} elseif (!empty($module_info)) {
			$data['sectiondescribe'] = $module_info['sectiondescribe'];
		} else {
			$data['sectiondescribe'] = '';
		}
		if (isset($this->request->post['sectionptop'])) {
			$data['sectionptop'] = $this->request->post['sectionptop'];
		} elseif (!empty($module_info)) {
			$data['sectionptop'] = $module_info['sectionptop'];
		} else {
			$data['sectionptop'] = '70';
		}
		if (isset($this->request->post['sectionpbuttom'])) {
			$data['sectionpbuttom'] = $this->request->post['sectionpbuttom'];
		} elseif (!empty($module_info)) {
			$data['sectionpbuttom'] = $module_info['sectionpbuttom'];
		} else {
			$data['sectionpbuttom'] = '70';
		}
		if (isset($this->request->post['boxbg'])) {
			$data['boxbg'] = $this->request->post['boxbg'];
		} elseif (!empty($module_info)) {
			$data['boxbg'] = $module_info['boxbg'];
		} else {
			$data['boxbg'] = '';
		}
		if (isset($this->request->post['boxjbbj'])) {
			$data['boxjbbj'] = $this->request->post['boxjbbj'];
		} elseif (!empty($module_info)) {
			$data['boxjbbj'] = $module_info['boxjbbj'];
		} else {
			$data['boxjbbj'] = '';
		}
		if (isset($this->request->post['boxtitle'])) {
			$data['boxtitle'] = $this->request->post['boxtitle'];
		} elseif (!empty($module_info)) {
			$data['boxtitle'] = $module_info['boxtitle'];
		} else {
			$data['boxtitle'] = '';
		}
		if (isset($this->request->post['boxdescribe'])) {
			$data['boxdescribe'] = $this->request->post['boxdescribe'];
		} elseif (!empty($module_info)) {
			$data['boxdescribe'] = $module_info['boxdescribe'];
		} else {
			$data['boxdescribe'] = '';
		}
		if (isset($this->request->post['boxxinxi'])) {
			$data['boxxinxi'] = $this->request->post['boxxinxi'];
		} elseif (!empty($module_info)) {
			$data['boxxinxi'] = $module_info['boxxinxi'];
		} else {
			$data['boxxinxi'] = '';
		}			
		if (isset($this->request->post['boxmagin'])) {
			$data['boxmagin'] = $this->request->post['boxmagin'];
		} elseif (!empty($module_info)) {
			$data['boxmagin'] = $module_info['boxmagin'];
		} else {
			$data['boxmagin'] = '10';
		}
		if (isset($this->request->post['boxpadding'])) {
			$data['boxpadding'] = $this->request->post['boxpadding'];
		} elseif (!empty($module_info)) {
			$data['boxpadding'] = $module_info['boxpadding'];
		} else {
			$data['boxpadding'] = '10';
		}		
		if (isset($this->request->post['boxborder'])) {
			$data['boxborder'] = $this->request->post['boxborder'];
		} elseif (!empty($module_info)) {
			$data['boxborder'] = $module_info['boxborder'];
		} else {
			$data['boxborder'] = '0';
		}	
		if (isset($this->request->post['boxcolor'])) {
			$data['boxcolor'] = $this->request->post['boxcolor'];
		} elseif (!empty($module_info)) {
			$data['boxcolor'] = $module_info['boxcolor'];
		} else {
			$data['boxcolor'] = '';
		}		
		if (isset($this->request->post['boxround'])) {
			$data['boxround'] = $this->request->post['boxround'];
		} elseif (!empty($module_info)) {
			$data['boxround'] = $module_info['boxround'];
		} else {
			$data['boxround'] = '5';
		}
		if (isset($this->request->post['boxposition'])) {
			$data['boxposition'] = $this->request->post['boxposition'];
		} elseif (!empty($module_info)) {
			$data['boxposition'] = $module_info['boxposition'];
		} else {
			$data['boxposition'] = 'center';
		}
		if (isset($this->request->post['boxshadow'])) {
			$data['boxshadow'] = $this->request->post['boxshadow'];
		} elseif (!empty($module_info)) {
			$data['boxshadow'] = $module_info['boxshadow'];
		} else {
			$data['boxshadow'] = '11';
		}
		if (isset($this->request->post['boxshadowhover'])) {
			$data['boxshadowhover'] = $this->request->post['boxshadowhover'];
		} elseif (!empty($module_info)) {
			$data['boxshadowhover'] = $module_info['boxshadowhover'];
		} else {
			$data['boxshadowhover'] = '21';
		}
		if (isset($this->request->post['buttonsize'])) {
			$data['buttonsize'] = $this->request->post['buttonsize'];
		} elseif (!empty($module_info)) {
			$data['buttonsize'] = $module_info['buttonsize'];
		} else {
			$data['buttonsize'] = 'md';
		}		
		if (isset($this->request->post['buttoninset'])) {
			$data['buttoninset'] = $this->request->post['buttoninset'];
		} elseif (!empty($module_info)) {
			$data['buttoninset'] = $module_info['buttoninset'];
		} else {
			$data['buttoninset'] = 'inset';
		}	
		if (isset($this->request->post['buttonborder'])) {
			$data['buttonborder'] = $this->request->post['buttonborder'];
		} elseif (!empty($module_info)) {
			$data['buttonborder'] = $module_info['buttonborder'];
		} else {
			$data['buttonborder'] = '1';
		}		
		if (isset($this->request->post['buttonround'])) {
			$data['buttonround'] = $this->request->post['buttonround'];
		} elseif (!empty($module_info)) {
			$data['buttonround'] = $module_info['buttonround'];
		} else {
			$data['buttonround'] = '50';
		}
		if (isset($this->request->post['buttonhover'])) {
			$data['buttonhover'] = $this->request->post['buttonhover'];
		} elseif (!empty($module_info)) {
			$data['buttonhover'] = $module_info['buttonhover'];
		} else {
			$data['buttonhover'] = '1';
		}
		if (isset($this->request->post['buttond'])) {
			$data['buttond'] = $this->request->post['buttond'];
		} elseif (!empty($module_info)) {
			$data['buttond'] = $module_info['buttond'];
		} else {
			$data['buttond'] = '3';
		}			
		if (isset($this->request->post['limit'])) {
			$data['limit'] = $this->request->post['limit'];
		} elseif (!empty($module_info)) {
			$data['limit'] = $module_info['limit'];
		} else {
			$data['limit'] = 5;
		}
		
		if (isset($this->request->post['category'])) {
			$data['category'] = $this->request->post['category'];
		} elseif (!empty($module_info)) {
			$data['category'] = $module_info['category'];
		} else {
			$data['category'] = '';
		}
		
		if (isset($this->request->post['characters'])) {
			$data['characters'] = $this->request->post['characters'];
		} elseif (!empty($module_info)) {
			$data['characters'] = $module_info['characters'];
		} else {
			$data['characters'] = 200;
		}
		
		if (isset($this->request->post['carousel'])) {
			$data['carousel'] = $this->request->post['carousel'];
		} elseif (!empty($module_info)) {
			$data['carousel'] = $module_info['carousel'];
		} else {
			$data['carousel'] = 0;
		}
		
		if (isset($this->request->post['columns'])) {
			$data['columns'] = $this->request->post['columns'];
		} elseif (!empty($module_info)) {
			$data['columns'] = $module_info['columns'];
		} else {
			$data['columns'] = 0;
		}
		
		if (isset($this->request->post['thumb'])) {
			$data['thumb'] = $this->request->post['thumb'];
		} elseif (!empty($module_info)) {
			$data['thumb'] = $module_info['thumb'];
		} else {
			$data['thumb'] = 1;
		}
								
		if (isset($this->request->post['width'])) {
			$data['width'] = $this->request->post['width'];
		} elseif (!empty($module_info)) {
			$data['width'] = $module_info['width'];
		} else {
			$data['width'] = 150;
		}	
			
		if (isset($this->request->post['height'])) {
			$data['height'] = $this->request->post['height'];
		} elseif (!empty($module_info)) {
			$data['height'] = $module_info['height'];
		} else {
			$data['height'] = 150;
		}	
				
		if (isset($this->request->post['status'])) {
			$data['status'] = $this->request->post['status'];
		} elseif (!empty($module_info)) {
			$data['status'] = $module_info['status'];
		} else {
			$data['status'] = '';
		}
		

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

		$this->response->setOutput($this->load->view('extension/module/blog_latest', $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'extension/module/blog_latest')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
			$this->error['name'] = $this->language->get('error_name');
		}
		
		return !$this->error;
	}
}