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

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

    $this->data['languageForJS'] = array(
      'module/starter_kit' => $language
    );

		$this->load->type('tipocomum');

    $this->data['ftr_geral'] = new ftr_geral();
    $this->data['Type_Status'] = $this->load->typeForTwig('Type_Status');

    $this->document->addScript('admin/public/js/module/starter-kit.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('starter_kit', $post);

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

			$this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
		}

    $this->data['error'] = $this->error;

    $this->data['error_warning'] = isset($this->error['warning']) ? $this->error['warning'] : '';

    $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->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'),
      'separator' => ' :: '
    );

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

    $this->data['action'] = $this->url->link('specific/' . PREFIX_NAME . '/module/starter_kit', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');

		$this->data['cancel'] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');

    $this->data['token'] = $this->session->data['token'];

    $this->data['starter_kit_product'] = isset($post['starter_kit_product']) ? $post['starter_kit_product'] : $this->config->get('starter_kit_product');
    $this->data['starter_kit_status'] = isset($post['starter_kit_status']) ? $post['starter_kit_status'] : $this->config->get('starter_kit_status');
	$this->data['starter_kit_title'] = isset($post['starter_kit_title']) ? $post['starter_kit_title'] : $this->config->get('starter_kit_title');

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

		$products = isset($post['starter_kit_product']) ? explode(',', $post['starter_kit_product']) : explode(',', $this->config->get('starter_kit_product'));

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

		foreach ($products as $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);

			if ($product_info) {
				$this->data['products'][] = array(
					'product_id' => $product_info['product_id'],
					'name'       => $product_info['name']
				);
			}
		}

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

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

		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render('module/starter_kit'));
	}

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

    if (isset($this->request->post['starter_kit_title'])) {
      if (trim($this->request->post['starter_kit_title']) === '') {
        $this->error['starter_kit_title'] = $this->language->get('error_empty_title');
      } else if (strlen(trim($this->request->post['starter_kit_title'])) < 3 || strlen(trim($this->request->post['starter_kit_title'])) > 64) {
        $this->error['starter_kit_title'] = $this->language->get('error_title');
      }
    }

    if ($this->error && !isset($this->error['warning'])) {
      $this->error['warning'] = $this->language->get('error_warning');
    }

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