<commit_before><?php

namespace WebPlatform\AseagleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;



class MainController extends Controller
{
    public function indexAction(Request $request)
    {
        //$cat_list = $this->getDoctrine()->getRepository('AseagleBundle:Category')->findBy(array(), array('lft' => 'ASC'));
        //$parents = $this->getDoctrine()->getRepository('AseagleBundle:Category')->findBy(array('parentId' => null), array('id' => 'ASC'));
        //self::get_cat_tree($parents, $cat_tree);
        return $this->render('AseagleBundle:Main:index.html.twig', array('cat_id' => $request->get('category_id')));
    }

    public function left_side_barAction(Request $request)
    {
        $category_id = $request->get('category_id');
        $cat_list = self::get_cat_lr_tree($category_id);
        $country_list = self::get_country($category_id);
        $filter_list =self::get_filter($category_id);
        $left_side_bar = array('current_cat_id' => $category_id, 'country' => $country_list, 'cat' => $cat_list, 'filter' => $filter_list);
        return new Response(json_encode($left_side_bar),200,array('Content-Type'=>'application/json'));
    }

    private function get_filter( $category_id ){
        $filter_list = $this->getDoctrine()->getRepository('AseagleBundle:ProductDetailNameMapping')->createQueryBuilder('p')
            ->select('p.colId')
            ->leftJoin('AseagleBundle:ProductDetailDefaultValue','pv',"WITH","pv.productDetailId=p.id")
            ->where('p.catId = :category_id')
            ->groupBy('p.colId')
            ->having("sum(pv.count) > 0")
            ->setParameter('category_id', $category_id)
            ->getQuery()
            ->getResult();
        $result = array();
        foreach ($filter_list as $f){
            array_push($result,$f['colId']);
        }
        return $result;
    }

    private function get_country( $category_id ){
        $country_list = $this->getDoctrine()->getRepository('AseagleBundle:Country')->createQueryBuilder('r')
            ->select('r.id','r.count')
            ->getQuery()
            ->getResult();
        return $country_list;
    }

    private function get_cat_lr_tree( $category_id ){
        $cat_list = $this->getDoctrine()->getRepository('AseagleBundle:Category')->createQueryBuilder('c')
            ->select('c.id','c.parent_id','c.count')
            ->from('AseagleBundle:Category', 'p')
            ->where('p.id = :category_id')
            ->andWhere('c.lft BETWEEN p.lft AND p.rgt')
            ->setParameter('category_id', $category_id)
            ->getQuery()
            ->getResult();
        return $cat_list;
    }

    private function get_cat_tree( $parent_list, &$result ){
        foreach($parent_list as $parent){
            array_push($result,array('cat' => $parent, 'par' => $parent->getParent()));
            $children = $this->getDoctrine()->getRepository('AseagleBundle:Category')->findBy(array('parentId' => $parent->getId()), array('id' => 'ASC'));
            if (sizeof($children)>0){
                self::get_cat_tree($children, $result);
            }
        }
    }

    public function filterAction(Request $request)
    {
        $params = $request->query->all();
        $filter_string = '';
        foreach($params as $key => $value){
			$key = trim($key, "_");
            if(is_numeric($key) && $value != ''){
                $pieces = explode(",", $value);
                if(count($pieces) > 0){
                    $filter_string .= ' (';
                    foreach($pieces as $pi){
                        if(is_numeric($pi)){
                            $filter_string .= " p.product_detail_".$key."=".$pi. " or";
                        }
                    }
                    $filter_string = substr($filter_string, 0, -2);
                    $filter_string .= ') ';
                }
                $filter_string .= ' and';
            }
        }
        if(!empty($filter_string)){
            $filter_string = substr($filter_string, 0, -3);
        }

        //$mapping_helper = $this->get('mapping_helper');
        $category_id = $request->get('category_id');
        $category = $this->getDoctrine()->getRepository('AseagleBundle:Category')->find($category_id);
        $last_index = $request->query->get('last_id');
        $search_string = $request->query->get('search_string');
		$from_id = $request->query->get('from_id');
		$from_id_string = '';
		if(isset($from_id)) {
			$from_id_string.= ' and p.id < '.$from_id;
		} 
		
        $country_id = $request->query->get('country');
		$pieces = explode(",", $country_id);
		$country_string ='';
		if(count($pieces) > 0){
			$country_string .= ' (';
			foreach($pieces as $pi){
				if(is_numeric($pi)){
					$country_string .= " p.place_of_origin=".$pi. " or";
				}
			}
			$country_string = substr($country_string, 0, -2);
			$country_string .= ') ';
		}				
				
        $products = $this->getDoctrine()->getRepository('AseagleBundle:Product')->createQueryBuilder('p')
            ->where('p.category_id >= :lft and p.category_id <= :rgt '.($country_id != "0" && $country_id != "" ? " and ".$country_string : "").($search_string != "" ? " and p.title LIKE '%".$search_string."%'" : "").($filter_string != "" ? " and ".$filter_string : "" ).$from_id_string)
            ->setParameter('lft', $category->getLft())
            ->setParameter('rgt', $category->getRgt())
			->setMaxResults(3)
			->addOrderBy('p.id', 'DESC')
            ->getQuery()
            ->getResult();
<commit_msg>fixed load products with lft rgt in main<commit_after><?php

namespace WebPlatform\AseagleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;



class MainController extends Controller
{
    public function indexAction(Request $request)
    {
        //$cat_list = $this->getDoctrine()->getRepository('AseagleBundle:Category')->findBy(array(), array('lft' => 'ASC'));
        //$parents = $this->getDoctrine()->getRepository('AseagleBundle:Category')->findBy(array('parentId' => null), array('id' => 'ASC'));
        //self::get_cat_tree($parents, $cat_tree);
        return $this->render('AseagleBundle:Main:index.html.twig', array('cat_id' => $request->get('category_id')));
    }

    public function left_side_barAction(Request $request)
    {
        $category_id = $request->get('category_id');
        $cat_list = self::get_cat_lr_tree($category_id);
        $country_list = self::get_country($category_id);
        $filter_list =self::get_filter($category_id);
        $left_side_bar = array('current_cat_id' => $category_id, 'country' => $country_list, 'cat' => $cat_list, 'filter' => $filter_list);
        return new Response(json_encode($left_side_bar),200,array('Content-Type'=>'application/json'));
    }

    private function get_filter( $category_id ){
        $filter_list = $this->getDoctrine()->getRepository('AseagleBundle:ProductDetailNameMapping')->createQueryBuilder('p')
            ->select('p.colId')
            ->leftJoin('AseagleBundle:ProductDetailDefaultValue','pv',"WITH","pv.productDetailId=p.id")
            ->where('p.catId = :category_id')
            ->groupBy('p.colId')
            ->having("sum(pv.count) > 0")
            ->setParameter('category_id', $category_id)
            ->getQuery()
            ->getResult();
        $result = array();
        foreach ($filter_list as $f){
            array_push($result,$f['colId']);
        }
        return $result;
    }

    private function get_country( $category_id ){
        $country_list = $this->getDoctrine()->getRepository('AseagleBundle:Country')->createQueryBuilder('r')
            ->select('r.id','r.count')
            ->getQuery()
            ->getResult();
        return $country_list;
    }

    private function get_cat_lr_tree( $category_id ){
        $cat_list = $this->getDoctrine()->getRepository('AseagleBundle:Category')->createQueryBuilder('c')
            ->select('c.id','c.parent_id','c.count')
            ->from('AseagleBundle:Category', 'p')
            ->where('p.id = :category_id')
            ->andWhere('c.lft BETWEEN p.lft AND p.rgt')
            ->setParameter('category_id', $category_id)
            ->getQuery()
            ->getResult();
        return $cat_list;
    }

    private function get_cat_tree( $parent_list, &$result ){
        foreach($parent_list as $parent){
            array_push($result,array('cat' => $parent, 'par' => $parent->getParent()));
            $children = $this->getDoctrine()->getRepository('AseagleBundle:Category')->findBy(array('parentId' => $parent->getId()), array('id' => 'ASC'));
            if (sizeof($children)>0){
                self::get_cat_tree($children, $result);
            }
        }
    }

    public function filterAction(Request $request)
    {
        $params = $request->query->all();
        $filter_string = '';
        foreach($params as $key => $value){
			$key = trim($key, "_");
            if(is_numeric($key) && $value != ''){
                $pieces = explode(",", $value);
                if(count($pieces) > 0){
                    $filter_string .= ' (';
                    foreach($pieces as $pi){
                        if(is_numeric($pi)){
                            $filter_string .= " p.product_detail_".$key."=".$pi. " or";
                        }
                    }
                    $filter_string = substr($filter_string, 0, -2);
                    $filter_string .= ') ';
                }
                $filter_string .= ' and';
            }
        }
        if(!empty($filter_string)){
            $filter_string = substr($filter_string, 0, -3);
        }

        //$mapping_helper = $this->get('mapping_helper');
        $category_id = $request->get('category_id');
        $category = $this->getDoctrine()->getRepository('AseagleBundle:Category')->find($category_id);
        $last_index = $request->query->get('last_id');
        $search_string = $request->query->get('search_string');
		$from_id = $request->query->get('from_id');
		$from_id_string = '';
		if(isset($from_id)) {
			$from_id_string.= ' and p.id < '.$from_id;
		} 
		
        $country_id = $request->query->get('country');
		$pieces = explode(",", $country_id);
		$country_string ='';
		if(count($pieces) > 0){
			$country_string .= ' (';
			foreach($pieces as $pi){
				if(is_numeric($pi)){
					$country_string .= " p.place_of_origin=".$pi. " or";
				}
			}
			$country_string = substr($country_string, 0, -2);
			$country_string .= ') ';
		}

        $cats = $this->getDoctrine()->getManager()->createQuery('SELECT c.id FROM AseagleBundle:Category c WHERE c.lft >= :lft and c.rgt <= :rgt ')
            ->setParameter('lft', $category->getLft())
            ->setParameter('rgt', $category->getRgt())
            ->getResult();

        $products = $this->getDoctrine()->getRepository('AseagleBundle:Product')->createQueryBuilder('p')
            ->where('p.category_id IN(:cats) '.($country_id != "0" && $country_id != "" ? " and ".$country_string : "").($search_string != "" ? " and p.title LIKE '%".$search_string."%'" : "").($filter_string != "" ? " and ".$filter_string : "" ).$from_id_string)
            ->setParameter('cats', array_values($cats))
			->setMaxResults(3)
			->addOrderBy('p.id', 'DESC')
            ->getQuery()
            ->getResult();

{'fraction_non_alphanumeric': 0.18254312212880297, 'fraction_numerical': 0.0026003293750541737, 'mean_word_length': 2.65126582278481, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 182, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3869914', 'n_tokens_mistral': 3618, 'n_tokens_neox': 3458, 'n_words': 509}