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

	public function load(){
		$this->language = $this->load->language('setting/analyze');
    $this->data = array_merge($this->data, $this->language);
		$this->load->model('setting/analyze');
		$this->load->helper('ftr_geral');
	}

	public function index() {
		$this->load();
		$this->document->setTitle($this->language['heading_title']);
		$this->getList();
	}

	public function insert() {

 	  $this->load();

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {

			$analyze_id = $this->model_setting_analyze->addAnalyze($this->request->post);

			$url = '';

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

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

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

		$this->getForm();
	}

	public function update() {
	  $this->load();

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

  	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
				$this->model_setting_analyze->editAnalyze($this->request->get['analyze_id'], $this->request->post);

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

				$this->redirect($this->url->link('setting/analyze', 'token=' . $this->session->data['token'] . '&analyze_id=' . $this->request->get['analyze_id'], 'SSL'));
	  }

  	$this->getForm();
  }

  public function delete() {

	 $this->load();

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

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

 	 	if (isset($this->request->post['selected'])) {

	 		foreach ($this->request->post['selected'] as $analyze_id) {
	 			$this->model_setting_analyze->deleteAnalyze($analyze_id);
	 		}

			$url = '';

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

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

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

	   $this->getList();
	}

  public function deleteAjax() {
	  $this->load->model('setting/analyze');
 	 	if (isset($this->request->post['analyze_ids'])) {
      foreach ($this->request->post['analyze_ids'] as $analyze_id) {
        $this->model_setting_analyze->deleteAnalyze($analyze_id);
      }
    }
    return $this->response->setOutput(json_encode(1));
	}

	protected function getList() {

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

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

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

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

		$this->data['insert'] = $this->url->link('setting/analyze/insert', 'token=' . $this->session->data['token'].$url, 'SSL');
		$this->data['delete'] = $this->url->link('setting/analyze/delete', 'token=' . $this->session->data['token'].$url, 'SSL');

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

		$data = array(
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);

		$analyze_total = $this->model_setting_analyze->getTotalAnalyzes();

		$results = $this->model_setting_analyze->getAnalyzes($data);

		foreach ($results as $key => $result) {
					

			$this->data['analyzes'][] = array(
				'analyze_id' => $result['analyze_id'],
				'titulo'     => $result['titulo'],
				'codigo'     => $result['codigo'],
				'quantidade' => $result['quantidade'],
				'selected'   => isset($this->request->post['selected']) && in_array(0, $this->request->post['selected']),
				'action'     => $this->url->link('setting/analyze/update', 'token=' . $this->session->data['token'].'&analyze_id='.$result['analyze_id'].$url, 'SSL')
			);
		}


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

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$pagination = new Pagination();
		$pagination->total = $analyze_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language['text_pagination'];
		$pagination->url = $this->url->link('setting/analyze', 'token=' . $this->session->data['token']. '&page={page}', 'SSL');

		$this->data['pagination'] = $pagination->render();
		
		
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render('setting/analyze_list'));
	}

	protected function getForm() {

		$this->load();

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

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

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

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

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		if (!isset($this->request->get['analyze_id'])) {
			$this->data['action'] = $this->url->link('setting/analyze/insert', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$this->data['action'] = $this->url->link('setting/analyze/update', 'token=' . $this->session->data['token'] . '&analyze_id=' . $this->request->get['analyze_id'], 'SSL');
		}

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

		if (isset($this->request->get['analyze_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$analyze_info = $this->model_setting_analyze->getAnalyze($this->request->get['analyze_id']);
    }

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

		if (isset($this->request->post['analyze_title'])) {
			$this->data['analyze_title'] = $this->request->post['analyze_title'];
		} elseif (isset($analyze_info['titulo'])) {
			$this->data['analyze_title'] = $analyze_info['titulo'];
		} else {
			$this->data['analyze_title'] = '';
		}

		if (isset($this->request->post['analyze_code'])) {
			$this->data['analyze_code'] = $this->request->post['analyze_code'];
		} elseif (isset($analyze_info['codigo'])) {
			$this->data['analyze_code'] = $analyze_info['codigo'];
		} else {
			$this->data['analyze_code'] = ftr_geral::getUnicCodeAnalize();
		}


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

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

		
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render('setting/analyze_form'));

	}

	protected function validateForm() {

		$this->load();

		if (!$this->user->hasPermission('modify', 'setting/analyze')) {
			$this->error['warning'] = $this->language['error_permission'];
		}

		if((!$this->request->post['analyze_title']) || ((utf8_strlen($this->request->post['analyze_title']) < 1) || (utf8_strlen($this->request->post['analyze_title']) > 240)) ) {
			$this->error['analyze_title'] = $this->language['error_analyze_title'];
		}

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

	}

}
