<?xml version="1.0" encoding="utf-8"?>
<modification>
  <name>Filter Just 0.2</name>
  <code>filter_just</code>
  <version>1</version>
  <author>John Doe</author>
  <link></link>
  <file path="catalog/controller/product/category.php">
  <operation>
      <search>
        <![CDATA[public function index() {]]>
      </search>
      <add position="after">
        <![CDATA[$this->document->addScript('catalog/view/javascript/bootstrap/js/bootstrap-slider.js');
    $this->document->addStyle('catalog/view/javascript/bootstrap/css/bootstrap-slider.css');]]>
      </add>
    </operation>
  </file>
  <file path="catalog/model/catalog/category.php">
  <operation>
      <search>
        <![CDATA[getCategoryFilters($category_id)]]>
      </search>
      <add position="replace">
        <![CDATA[getCategoryFiltersNotUse($category_id)]]>
      </add>
    </operation>
  <operation>
      <search>
        <![CDATA[public function getCategories]]>
      </search>
      <add position="before">
        <![CDATA[
      public function getCategoryFilters($category_id) {
              $query = $this->db->query("
              SELECT pa.attribute_id, pa.text, ades.name, 0 as manf_id, 2 as fororder FROM " . DB_PREFIX . "product_attribute pa 
                  INNER JOIN " . DB_PREFIX . "product_to_category ptc ON (ptc.product_id = pa.product_id and ptc.category_id = '" . (int)$category_id . "') 
                  LEFT JOIN " . DB_PREFIX . "attribute_description ades ON (pa.attribute_id = ades.attribute_id AND ades.language_id = '" . (int)$this->config->get('config_language_id') . "') 
                  INNER JOIN " . DB_PREFIX . "product p ON(pa.product_id = p.product_id)
                  WHERE p.status = 1 AND pa.language_id = '" . (int)$this->config->get('config_language_id') . "' 
              UNION SELECT 'manf', manf.name, 'Производитель', manf.manufacturer_id as manf_id, 1 FROM " . DB_PREFIX . "product_to_category ptc 
                  INNER JOIN " . DB_PREFIX . "product pr ON (pr.product_id = ptc.product_id AND ptc.category_id = '" . (int)$category_id . "') INNER JOIN " . DB_PREFIX . "manufacturer manf ON (pr.manufacturer_id = manf.manufacturer_id) 
              UNION SELECT 'price', min(pr.price), 'Цена', max(pr.price) as manf_id, 0 FROM " . DB_PREFIX . "product_to_category ptc 
                  INNER JOIN " . DB_PREFIX . "product pr ON (pr.product_id = ptc.product_id AND ptc.category_id = '" . (int)$category_id . "') ORDER BY fororder, attribute_id, text");
          $filter_group_data = array();
          foreach ($query->rows as $result) {
            $filter_group_data[$result['attribute_id']]['filter_group_id'] = $result['attribute_id'];
            $filter_group_data[$result['attribute_id']]['name'] = $result['name'];
            $filter_group_data[$result['attribute_id']]['filter'][] = array('filter_id' => $result['attribute_id'] == 'price' ? $result['manf_id'] : ($result['attribute_id'] . '[' . (($result['attribute_id'] == 'manf') ? $result['manf_id'] : $result['text']) . ']'), 'name' => $result['text']);
          }
          return $filter_group_data;
        }]]>
      </add>
    </operation>
  </file>
  <file path="catalog/model/catalog/product.php">
  <operation>
      <search>
        <![CDATA[$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id) LEFT JOIN " . DB_PREFIX . "product p ON (pf.product_id = p.product_id)";]]>
      </search>
      <add position="replace">
        <![CDATA[$filters = explode('],', $data['filter_filter']);
        $where_arr = array();
        foreach ($filters as $filter_id) {
            if(end($filters) != $filter_id){$filter_id  .= "]";}
          //$arr = preg_split('/[\[,\]]/',$filter_id);
          $arr = preg_split('/[\[\]]/',$filter_id);
          if (count($arr) > 1) {
            $where_arr[$arr[0]][] = $arr[1];
          }
        }
        $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
        foreach ($where_arr as $key => $value) {
          if ($key == 'manf') {
            

          } elseif ($key == 'minprice') {
            

          } elseif ($key == 'maxprice') {
            

          }
          else {
            $sql .= " LEFT JOIN " . DB_PREFIX . "product_attribute pa" . $key . " ON (p2c.product_id = pa" . $key . ".product_id AND pa" . $key . ".attribute_id = " . $key . ")";
          }
        }
        
        ]]>
      </add>
    </operation>
  <operation>
      <search>
        <![CDATA[$sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")";]]>
      </search>
      <add position="replace">
        <![CDATA[$where_arr = array();
        foreach ($filters as $filter_id) {            
        if(end($filters) != $filter_id){$filter_id  .= "]";}
          $arr = preg_split('/[\[\]]/',$filter_id);
          if (count($arr) > 1) {
            $where_arr[$arr[0]][] = $arr[1];
          }
        }
        foreach ($where_arr as $key => $value) {
          if ($key == 'manf') {
            $sql .= " AND (0=1";
            foreach ($value as $wvalue) {
              $sql .= " OR p.manufacturer_id = " . (int)$wvalue;
            }
            $sql .= ")";
          }
          elseif ($key == 'minprice') {
            $sql .= " AND p.price >= " . (float)$value[0];
          }
          elseif ($key == 'maxprice') {
            $sql .= " AND p.price <= " . (float)$value[0];
          }
          else {
            $sql .= " AND (0=1";
            foreach ($value as $wvalue) {
              $sql .= " OR pa" . $key . ".text = '" . addslashes($wvalue) . "'";
            }
            $sql .= ")";
          }
        }
    ]]>
      </add>
    </operation>
      <operation>
      <search>
        <![CDATA[$implode[] = (int)$filter_id;]]>
      </search>
      <add position="before">
        <![CDATA[if(end($filters) != $filter_id){$filter_id  .= "]";}]]>
      </add>
    </operation>  
      <operation>
      <search>
        <![CDATA[$filters = explode(',', $data['filter_filter']);]]>
      </search>
      <add position="replace">
        <![CDATA[$filters = explode('],', $data['filter_filter']);]]>
      </add>
    </operation>
    <operation>
      <search>
        <![CDATA[public function getTotalProducts($data = array()) {]]>
      </search>
      <add position="before">
        <![CDATA[   
        
    private function KeyIsInt($array){
        foreach ($array as $key => $item) {
            if(is_int($key)){
                return true;
            }else{
                return false;
            }
        }
    }
         public function getTotalProductsByFilter($data = array(), $filter_type = false)
    {
        $filters = explode('],', $data['filter_filter']);
        $where_arr = array();
        foreach ($filters as $filter_id) {
            if (end($filters) != $filter_id) {
                $filter_id .= "]";
            }
            //$arr = preg_split('/[\[,\]]/',$filter_id);
            $arr = preg_split('/[\[\]]/', $filter_id);
            if (count($arr) > 1) {
                $where_arr[$arr[0]][] = $arr[1];
            }
        }


        $sql = "SELECT count(*) AS total FROM `oc_product_to_category` ptc";

        $sql .= " LEFT JOIN `oc_product_attribute` pa ON(ptc.product_id = pa.product_id)";

        $sql .= " LEFT JOIN `oc_product` p ON(ptc.product_id = p.product_id) WHERE";

        if ($filter_type != 'manf' and isset($where_arr['manf']) and (count($where_arr['manf']) > 1)) {
            for ($c = 0; $c < count($where_arr['manf']); $c++){
                if($where_arr['manf'] > 0 and $c == 0){
                    $sql .= " (p.manufacturer_id = '" . intval($where_arr['manf'][$c]) ."'";
                }
                elseif(($c+1) == count($where_arr['manf'])){
                    $sql .= " OR p.manufacturer_id = '" . intval($where_arr['manf'][$c]) ."') AND";
                }
                else{
                    $sql .= " OR p.manufacturer_id = '" . intval($where_arr['manf'][$c]) ."'";
                }
            }
        }elseif(isset($where_arr['manf'])){
            $sql .= " p.manufacturer_id = '" . intval(end($where_arr['manf'])) ."' AND";
        }

        $i = 0;
        if ($this->KeyIsInt($where_arr)) {
            //перебор атрибутов
            foreach ($where_arr as $key => $value) {
                //атрибут ли
                if (is_int($key)) {
                    if ($i == 0) {
                        $sql .= " ((pa.attribute_id = '" . $key . "' AND pa.text = '" . addslashes($value[0]) . "')";
                    }else{
                        $sql .= " OR (pa.attribute_id = '" . $key . "' AND pa.text = '" . addslashes($value[0]) . "') ";
                    }
                    $i++;
                }
            }
            $sql .= ") AND";
        }
        $sql .= " ptc.category_id = '" . $data['filter_category_id'] . "' AND pa.language_id = '". (int)$this->config->get('config_language_id')  ."' AND p.status = 1";
        if($i > 1){
            $sql .= " GROUP BY p.product_id HAVING count(*) = '" . (int)$i . "'";
            $sql = "SELECT count(*) as total FROM (" . $sql . ") total";
        }elseif(isset($where_arr['manf'])){
            $sql .= " GROUP BY p.product_id";
            $sql = "SELECT count(*) as total FROM (" . $sql . ") total";
        }

        $query = $this->db->query($sql);
        //print_r($sql . PHP_EOL);die();
        //var_dump($query);die();
        return $query->row['total'];

    }]]>
      </add>
    </operation>
  </file>
</modification>