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

	public function index() {
		$this->language->load('catalog/store_rating');

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

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

		$this->getList();
	}

	public function update() {
		$this->language->load('catalog/store_rating');

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

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

		if (isset($this->request->get['selected']) && isset($this->request->get['status'])) {
			$this->model_catalog_store_rating->updateStatusRating($this->request->get['selected'], $this->request->get['status']);

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

			$url = '';

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

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

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

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

		$this->getForm();
	}

	public function delete() {
		$this->language->load('catalog/store_rating');

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

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

		if (isset($this->request->get['selected']) && $this->validateDelete()) {
			
			$this->model_catalog_store_rating->deleteRating($this->request->get['selected']);

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

			$url = '';

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

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

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

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

		$this->getList();
	}


	public function StoreRatingHome() {

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

		$this->language->load('catalog/store_rating');

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

		$json = array();

		if(isset($this->request->get['action']) && ($this->request->get['action']=='delete') ){
			if (isset($this->request->get['selected'])) {
				$this->model_catalog_store_rating->deleteRating($this->request->get['selected']);
				$json['success_delete'] = $this->language->get('text_success_delete');
			}
		}

		if(isset($this->request->get['action']) && ($this->request->get['action']=='update') ){
			if (isset($this->request->get['selected']) && isset($this->request->get['status'])) {
				$type_status = $this->request->get['status'] ? Type_Status::Tp_Habilitado : Type_Status::Tp_Desabilitado;
				$this->model_catalog_store_rating->updateStatusNotificationRating($this->request->get['selected'], $type_status);
        $json['id'] = $this->request->get['selected'];
				$json['success_update'] = $this->language->get('text_success');
			}
		}


		$this->response->setOutput(json_encode($json));
	}


	protected function getList() {
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'r.date_added';
		}

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

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

		$url = '';

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

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

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

  		$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('heading_title'),
				'href'      => $this->url->link('catalog/store_rating', 'token=' . $this->session->data['token'] . $url, 'SSL'),
				'separator' => ' :: '
   		);

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

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

		$store_rating_total = $this->model_catalog_store_rating->getTotalRatings();

		$results = $this->model_catalog_store_rating->getRatings($data);

    foreach ($results as $result) {

			$action = array(
				'delete'      => $this->url->link('catalog/store_rating/delete', 'token=' . $this->session->data['token'].'&selected='.$result['store_rating_id'].$url, 'SSL'),
				'habilitar'   => $this->url->link('catalog/store_rating/update', 'token=' . $this->session->data['token'].'&selected='.$result['store_rating_id'].'&status='.Type_Status::Tp_Habilitado.$url, 'SSL'),
				'desabilitar' => $this->url->link('catalog/store_rating/update', 'token=' . $this->session->data['token'].'&selected='.$result['store_rating_id'].'&status='.Type_Status::Tp_Desabilitado.$url, 'SSL')
			);			

			$this->data['store_ratings'][] = array(
				'store_rating_id'  => $result['store_rating_id'],
				'author'     => $result['author'],
				'rating'     => $result['rating'],
				'text'       => $result['text'],
				'allow'      => $result['allow'],
				'status'     => Type_Status::getNickFromConstValue($result['status']),
				'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
				'action'     => $action
			);
		}

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');

 		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'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

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

		$this->data['sort_author'] = $this->url->link('catalog/store_rating', 'token=' . $this->session->data['token'] . '&sort=r.author' . $url, 'SSL');
		$this->data['sort_rating'] = $this->url->link('catalog/store_rating', 'token=' . $this->session->data['token'] . '&sort=r.rating' . $url, 'SSL');
		$this->data['sort_status'] = $this->url->link('catalog/store_rating', 'token=' . $this->session->data['token'] . '&sort=r.status' . $url, 'SSL');
		$this->data['sort_date_added'] = $this->url->link('catalog/store_rating', 'token=' . $this->session->data['token'] . '&sort=r.date_added' . $url, 'SSL');

		$url = '';

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

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

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

		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

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

		$this->response->setOutput($this->render('catalog/store_rating_list'));
	}

	protected function validateDelete() {
		if (!$this->user->hasPermission('modify', 'catalog/store_rating')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

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