<commit_before><?php

namespace WBB\BarBundle\Repository;

use WBB\CoreBundle\Repository\EntityRepository;
use WBB\BarBundle\Entity\Bar;

/**
 * BarRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class BarRepository extends EntityRepository
{
    const BAR_LOCATION_CITY = 1;
    const BAR_LOCATION_COUNTRY = 2;
    const BAR_LOCATION_WORLDWIDE = 3;


    public function findAllEnabled($city = null, $suburb = null)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias())
            ->where($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)));

        if($city){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.city', $city->getId()));
        }

        if($suburb){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.suburb', $suburb->getId()));
        }

        return $qb->getQuery()->getResult();
    }

    public function findBestBars($city = null)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias().", COUNT(tp) AS HIDDEN nbTips")
            ->leftjoin($this->getAlias().'.tips', 'tp')
//            ->where($qb->expr()->eq($this->getAlias().'.onTop', $qb->expr()->literal(true)))
            ->where($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
//            ->andWhere($qb->expr()->eq('tp.status', $qb->expr()->literal(1)))
            ->groupBy($this->getAlias())
            ->orderBy($this->getAlias().'.onTop', 'DESC')
            ->addOrderBy('nbTips', 'DESC')
            ->setMaxResults(6)
        ;

        if($city){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.city', $city->getId()));
        }

        //TODO : Add Favoris count

        return $qb->getQuery()->getResult();

    }

    public function findPopularBars($city = null)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias())
            ->where($qb->expr()->eq($this->getAlias().'.onTop', $qb->expr()->literal(true)))
            ->andWhere($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
            ->setMaxResults(5)
        ;

        if($city){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.city', $city->getId()));
        }

        //TODO : Add Favoris count

        return $qb->getQuery()->getResult();

    }

    public function findLatestBars($city = null, $limit = 5)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias())
            ->where($qb->expr()->eq($this->getAlias().'.onTop', $qb->expr()->literal(true)))
            ->andWhere($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
            ->orderBy($this->getAlias().'.createdAt', 'DESC')
            ->setMaxResults($limit)
        ;

        if($city){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.city', $city->getId()));
        }

        return $qb->getQuery()->getResult();
    }

    public function findYouMayAlsoLike($bar, $location, $exceptBars = null, $onTop = true, $tags = true, $limit = 4)
    {
        if($bar)
        {
            $ids = array($bar->getId());
            if($exceptBars != null)
            {
                foreach($exceptBars as $exBar)
                {
                    if($exBar and $exBar)
                        $ids[] = $exBar->getId();
                }
            }

            $qb = $this->createQuerybuilder($this->getAlias());

            $qb
                ->select($this->getAlias())
                ->innerjoin($this->getAlias().'.city', 'c')
                ->where($qb->expr()->notIn($this->getAlias().'.id',':exceptBars'))
                ->andWhere($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
                ->setParameter('exceptBars', $ids)
            ;

            if($onTop == true){
                $qb
                    ->andWhere($qb->expr()->eq($this->getAlias().'.onTop', $qb->expr()->literal(true)));
            }

            if($tags == true){
                $qb
                    ->innerjoin($this->getAlias().'.tags', 'bt')
                    ->innerjoin('bt.tag', 't')
                    ->andWhere($qb->expr()->in('t.id',':tags'))
                    ->setParameter('tags', $bar->getTagsIds());
            }

            if($location == BarRepository::BAR_LOCATION_CITY and !is_null($bar->getCity()))
            {
                $qb
                    ->andWhere($qb->expr()->eq($this->getAlias().'.city', ':city'))
                    ->setParameter('city', $bar->getCity());
            }

            if($location == BarRepository::BAR_LOCATION_COUNTRY and !is_null($bar->getCity()) and !is_null($bar->getCity()->getCountry()))
            {
                $qb
                    ->andWhere($qb->expr()->eq('c.country', ':country'))
                    ->setParameter('country', $bar->getCity()->getCountry());
            }

            $qb
                ->groupBy($this->getAlias())
                ->setMaxResults($limit);

            return $qb->getQuery()->getResult();
        }
        else{
            return null;
        }
    }

    public function findNearestBars($latitude = 0, $longitude = 0, $start = 0, $limit = 8)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias().",GEO(c.latitude = :latitude, c.longitude = :longitude) AS HIDDEN Distance")
            ->where($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
            ->setParameter('latitude', $latitude)
            ->setParameter('longitude', $longitude)
            ->orderBy('Distance', 'ASC')
            ->setFirstResult($start)
            ->setMaxResults($limit)
        ;

        return $qb->getQuery()->getResult();
    }
} <commit_msg>fixed issue in bar order<commit_after><?php

namespace WBB\BarBundle\Repository;

use WBB\CoreBundle\Repository\EntityRepository;
use WBB\BarBundle\Entity\Bar;

/**
 * BarRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class BarRepository extends EntityRepository
{
    const BAR_LOCATION_CITY = 1;
    const BAR_LOCATION_COUNTRY = 2;
    const BAR_LOCATION_WORLDWIDE = 3;


    public function findAllEnabled($city = null, $suburb = null)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias())
            ->where($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)));

        if($city){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.city', $city->getId()));
        }

        if($suburb){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.suburb', $suburb->getId()));
        }

        return $qb->getQuery()->getResult();
    }

    public function findBestBars($city = null)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias().", COUNT(tp) AS HIDDEN nbTips")
            ->leftjoin($this->getAlias().'.tips', 'tp', 'WITH', 'tp.status ='. $qb->expr()->literal(1))
//            ->where($qb->expr()->eq($this->getAlias().'.onTop', $qb->expr()->literal(true)))
            ->where($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
            ->groupBy($this->getAlias())
            ->orderBy($this->getAlias().'.onTop', 'DESC')
            ->addOrderBy('nbTips', 'DESC')
            ->setMaxResults(6)
        ;

        if($city){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.city', $city->getId()));
        }

        //TODO : Add Favoris count

        return $qb->getQuery()->getResult();

    }

    public function findPopularBars($city = null)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias())
            ->where($qb->expr()->eq($this->getAlias().'.onTop', $qb->expr()->literal(true)))
            ->andWhere($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
            ->setMaxResults(5)
        ;

        if($city){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.city', $city->getId()));
        }

        //TODO : Add Favoris count

        return $qb->getQuery()->getResult();

    }

    public function findLatestBars($city = null, $limit = 5)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias())
            ->where($qb->expr()->eq($this->getAlias().'.onTop', $qb->expr()->literal(true)))
            ->andWhere($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
            ->orderBy($this->getAlias().'.createdAt', 'DESC')
            ->setMaxResults($limit)
        ;

        if($city){
            $qb->andWhere($qb->expr()->eq($this->getAlias().'.city', $city->getId()));
        }

        return $qb->getQuery()->getResult();
    }

    public function findYouMayAlsoLike($bar, $location, $exceptBars = null, $onTop = true, $tags = true, $limit = 4)
    {
        if($bar)
        {
            $ids = array($bar->getId());
            if($exceptBars != null)
            {
                foreach($exceptBars as $exBar)
                {
                    if($exBar and $exBar)
                        $ids[] = $exBar->getId();
                }
            }

            $qb = $this->createQuerybuilder($this->getAlias());

            $qb
                ->select($this->getAlias())
                ->innerjoin($this->getAlias().'.city', 'c')
                ->where($qb->expr()->notIn($this->getAlias().'.id',':exceptBars'))
                ->andWhere($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
                ->setParameter('exceptBars', $ids)
            ;

            if($onTop == true){
                $qb
                    ->andWhere($qb->expr()->eq($this->getAlias().'.onTop', $qb->expr()->literal(true)));
            }

            if($tags == true){
                $qb
                    ->innerjoin($this->getAlias().'.tags', 'bt')
                    ->innerjoin('bt.tag', 't')
                    ->andWhere($qb->expr()->in('t.id',':tags'))
                    ->setParameter('tags', $bar->getTagsIds());
            }

            if($location == BarRepository::BAR_LOCATION_CITY and !is_null($bar->getCity()))
            {
                $qb
                    ->andWhere($qb->expr()->eq($this->getAlias().'.city', ':city'))
                    ->setParameter('city', $bar->getCity());
            }

            if($location == BarRepository::BAR_LOCATION_COUNTRY and !is_null($bar->getCity()) and !is_null($bar->getCity()->getCountry()))
            {
                $qb
                    ->andWhere($qb->expr()->eq('c.country', ':country'))
                    ->setParameter('country', $bar->getCity()->getCountry());
            }

            $qb
                ->groupBy($this->getAlias())
                ->setMaxResults($limit);

            return $qb->getQuery()->getResult();
        }
        else{
            return null;
        }
    }

    public function findNearestBars($latitude = 0, $longitude = 0, $start = 0, $limit = 8)
    {
        $qb = $this->createQuerybuilder($this->getAlias());

        $qb
            ->select($this->getAlias().",GEO(c.latitude = :latitude, c.longitude = :longitude) AS HIDDEN Distance")
            ->where($qb->expr()->eq($this->getAlias().'.status', $qb->expr()->literal(Bar::BAR_STATUS_ENABLED_VALUE)))
            ->setParameter('latitude', $latitude)
            ->setParameter('longitude', $longitude)
            ->orderBy('Distance', 'ASC')
            ->setFirstResult($start)
            ->setMaxResults($limit)
        ;

        return $qb->getQuery()->getResult();
    }
} 
{'fraction_non_alphanumeric': 0.2086885911490053, 'fraction_numerical': 0.00194884287454324, 'mean_word_length': 1.995864753101435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 358, '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': '666920', 'n_tokens_mistral': 3837, 'n_tokens_neox': 3702, 'n_words': 497}