<?php

class ControllerModuleFilter extends Controller
{

  public function getUrlFilters($removes = array())
  {

    $urlFilters = array_filter(array(
      'search' => isset($this->request->get['search']) ? $this->request->get['search'] : false,
      'tag' => isset($this->request->get['tag']) ? $this->request->get['tag'] : false,
      'description' => isset($this->request->get['description']) ? $this->request->get['description'] : false,
      'category_id' => isset($this->request->get['category_id']) ? $this->request->get['category_id'] : false,
      'sub_category' => isset($this->request->get['sub_category']) ? $this->request->get['sub_category'] : false,
      'sort' => isset($this->request->get['sort']) ? $this->request->get['sort'] : false,
      'order' => isset($this->request->get['order']) ? $this->request->get['order'] : false,
      'limit' => isset($this->request->get['limit']) ? $this->request->get['limit'] : false,
      'path' => isset($this->request->get['path']) ? $this->request->get['path'] : false,
      'manufacturer_id' => isset($this->request->get['manufacturer_id']) ? $this->request->get['manufacturer_id'] : false,
      'filbrand' => isset($this->request->get['filbrand']) ? $this->request->get['filbrand'] : false,
      'filcolor' => isset($this->request->get['filcolor']) ? $this->request->get['filcolor'] : false,
      'filsize' => isset($this->request->get['filsize']) ? $this->request->get['filsize'] : false,
      'filspecification' => isset($this->request->get['filspecification']) ? $this->request->get['filspecification'] : false,
      'price_initial' => isset($this->request->get['price_initial']) ? $this->request->get['price_initial'] : false,
      'price_laste' => isset($this->request->get['price_laste']) ? $this->request->get['price_laste'] : false,
      'name' => isset($this->request->get['name']) ? $this->request->get['name'] : false,
      'route2' => isset($this->request->get['route']) ? $this->request->get['route'] : false,
      'index' => isset($this->request->get['index']) ? $this->request->get['index'] : false
    ));

    if ($removes) {
      foreach ($removes as $remove) {
        unset($urlFilters[$remove]);
      }
    }

    $retorno = array();
    array_walk_recursive($urlFilters, function ($value, $key) use (&$retorno) {
      $retorno[] = "&" . $key . "=" . $value;
    });

    return implode($retorno);

  }

  public function getFilters()
  {

    $urlGet = $this->getUrlFilters();

    /* CÓDIGO P/ FICAR COMPATIVEL COM A URL AMIGAVEL */
    $this->data['linkclearall'] = html_entity_decode($this->url->link($this->request->get['route'], $urlGet));

    //variavel criada para alterar o link de apagar dos filtros
    $remove = '';
    if (isset($this->request->get['route'])) {
      if ($urlGet == '') {
        $remove = 'route2=' . $this->request->get['route'];
      } else {
        $remove = '&route2=' . $this->request->get['route'];
      }
    }

    //montando parametros inclusos da COR
    $this->data['getscor'] = $this->getUrlFilters(['filcolor']);
    $this->data['linkclearcor'] = $this->url->link($this->request->get['route'], str_ireplace($remove, '', $this->data['getscor']));

    //montando parametros inclusos do tamanho
    $this->data['getsize'] = $this->getUrlFilters(['filsize']);
    $this->data['linkclearsize'] = $this->url->link($this->request->get['route'], str_ireplace($remove, '', $this->data['getsize']));

    //montando parametros inclusos da especificação
    $this->data['getspecification'] = $this->getUrlFilters(['filspecification']);
    $this->data['linkclearspecifications'] = $this->url->link($this->request->get['route'], str_ireplace($remove, '', $this->data['getspecification']));

    $this->data['getsprice'] = $this->getUrlFilters(['price_initial', 'price_laste']);
    $this->data['linkclearprice'] = $this->url->link($this->request->get['route'], str_ireplace($remove, '', $this->data['getsprice']));

    $this->data['getsmarca'] = $this->getUrlFilters(['filbrand']);
    $this->data['linkclearmanufacturer'] = $this->url->link($this->request->get['route'], str_ireplace($remove, '', $this->data['getsmarca']));

    $this->data['getsrange'] = $this->getUrlFilters();

    /* FIM DO CÓDIGO P/ FICAR COMPATIVEL COM A URL AMIGAVEL*/

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

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

    if (isset($this->request->get['tag'])) {
      $tag = $this->request->get['tag'];
    } elseif (isset($this->request->get['search'])) {
      $tag = $this->request->get['search'];
    } else {
      $tag = '';
    }

    if (isset($this->request->get['sort'])) {
      $sort = $this->request->get['sort'];
    } else {
      $sort = 'p.sort_order';
    }

    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;
    }

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

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

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

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

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

      $parts = explode('_', (string)$this->request->get['path']);

      $category_id = (int)array_pop($parts);

      foreach ($parts as $path_id) {
        if (!$path) {
          $path = (int)$path_id;
        } else {
          $path .= '_' . (int)$path_id;
        }
      }
    } else {
      $category_id = 0;
    }

    if (isset($this->request->get['filcolor'])) {
      $filcolor = (string)$this->request->get['filcolor'];
    } else {
      $filcolor = "";
    }

    if (isset($this->request->get['filspecification'])) {
      $filspecification = (string)$this->request->get['filspecification'];
    } else {
      $filspecification = "";
    }

    if (isset($this->request->get['filsize'])) {
      $filsize = (string)$this->request->get['filsize'];
    } else {
      $filsize = "";
    }

    if (isset($this->request->get['filbrand'])) {
      $filbrand = (string)$this->request->get['filbrand'];
    } else {
      $filbrand = false;
    }

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

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


    return array(
      'filter_name' => $search,
      'filter_tag' => $tag,
      'filter_category_id' => $category_id,
      'filter_sub_category' => $sub_category,
      'filcolor' => $filcolor,
      'filsize' => $filsize,
      'filspecification' => $filspecification,
      'filter_manufacturer_id' => $manufacturer_id,
      'price_initial' => $price_initial,
      'price_laste' => $price_laste,
      'filbrand' => $filbrand,
      'filter_filter' => $filter,
      'sort' => $sort,
      'order' => $order
    );


  }

}
