src/Repository/PropertyRepository.php line 115

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Property;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Symfony\Component\HttpFoundation\Request;
  7. /**
  8.  * @extends ServiceEntityRepository<Property>
  9.  *
  10.  * @method Property|null find($id, $lockMode = null, $lockVersion = null)
  11.  * @method Property|null findOneBy(array $criteria, array $orderBy = null)
  12.  * @method Property[]    findAll()
  13.  * @method Property[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14.  */
  15. class PropertyRepository extends ServiceEntityRepository
  16. {
  17.     public function __construct(ManagerRegistry $registry)
  18.     {
  19.         parent::__construct($registryProperty::class);
  20.     }
  21.     public function findById(int $id): ?Property
  22.     {
  23.         $property $this->find($id);
  24.         if (!$property) {
  25.             return null;
  26.         }
  27.         return $property;
  28.     }
  29.     public function create(): Property
  30.     {
  31.         return new Property();
  32.     }
  33.     public function add(Property $entitybool $flush false): void
  34.     {
  35.         $this->getEntityManager()->persist($entity);
  36.         if ($flush) {
  37.             $this->getEntityManager()->flush();
  38.         }
  39.     }
  40.     public function remove(int $id): void
  41.     {
  42.         /** @var object $bien */
  43.         $bien $this->getEntityManager()->getReference(
  44.             $this->getClassName(),
  45.             $id
  46.         );
  47.         $this->getEntityManager()->remove($bien);
  48.         $this->getEntityManager()->flush();
  49.     }
  50.     public function findOneByApiId($value): ?Property
  51.     {
  52.         return $this->createQueryBuilder('p')
  53.             ->andWhere('p.api_id = :val')
  54.             ->setParameter('val'$value)
  55.             ->getQuery()
  56.             ->getOneOrNullResult()
  57.         ;
  58.     }
  59.     public function findByLocation($value,$id): array
  60.     {
  61.         return $this->createQueryBuilder('p')
  62.             ->andWhere('p.adresse = :val')
  63.             ->andWhere('p.id != :val1')
  64.             ->setParameter('val'$value)
  65.             ->setParameter('val1'$id)
  66.             ->orderBy('p.id''ASC')
  67.             ->setMaxResults(2)
  68.             ->getQuery()
  69.             ->getResult()
  70.         ;
  71.     }
  72.     public function filterByStatus($transactionType)
  73.     {
  74.         return $this->createQueryBuilder('p')
  75.             ->andWhere('p.transactionType = :val')
  76.             ->setParameter('val'$transactionType)
  77.             ->orderBy('p.id''ASC')
  78.             ->getQuery()
  79.             ->getResult()
  80.         ;
  81.     }
  82.     public function findFeatured(int $nbrint $type_transction 1)
  83.     {
  84.         $builder $this->createQueryBuilder('p')
  85.             ->andWhere('p.featured = 1')
  86.             ->andWhere('p.transactionType = :transction')
  87.             ->setParameter('transction'$type_transction)
  88.         ;
  89.         if($nbr>0){
  90.             $builder->setMaxResults($nbr);
  91.         }
  92.         $builder->orderBy('p.lastupdate''DESC');
  93.         $query $builder->getQuery();
  94.         return $query->getResult();
  95.     }
  96.     public function search(Request $requestint $isRent):array
  97.     {
  98.         $typeArray $request->query->get('type');
  99.         /**@var array */
  100.         $location $request->query->get('location');
  101.         $surfaceMin $request->query->get('surfaceMin');
  102.         $surfaceMax $request->query->get('surfaceMax');
  103.         $priceMin $request->query->get('priceMin');
  104.         $priceMax $request->query->get('priceMax');
  105.         $boundsJson $request->query->get('bounds');
  106.         $isNewProject $request->query->get('isNewProject');
  107.         $bounds $boundsJson json_decode($boundsJson) : null;
  108.         $radius $request->query->get('radius');
  109.         $qb $this->createQueryBuilder('p')
  110.             ->join('p.type''t')
  111.             // ->join('p.canton', 'c')
  112.             // ->join('p.district', 'd')
  113.             // ->join('p.city', 'city')
  114.             // ->join('p.zone', 'z')
  115.             ->andWhere('p.transactionType = :isrent')
  116.             ->setParameter('isrent'$isRent)
  117.         ;
  118.         if ($typeArray && !(!empty($typeArray) && empty($typeArray[0]))) {
  119.             $qb->andWhere('t.title IN (:titles)')
  120.                     ->setParameter('titles'$typeArray)
  121.             ;
  122.         }
  123.         if ($radius) {
  124.             foreach ($location as $value) {
  125.                 $arr explode(','$value);
  126.                 
  127.                 if (count($arr) >= 4) {
  128.                     $lat $arr[2];
  129.                     $lng $arr[3];
  130.     
  131.                     $latPerDg 111;
  132.                     $lngPerDg 77;
  133.                 
  134.                     $qb ->andWhere('p.latitude BETWEEN :south AND :north')
  135.                         ->andWhere('p.longitude BETWEEN :west AND :east')
  136.                         ->setParameter('north'$lat $radius $latPerDg)
  137.                         ->setParameter('south'$lat $radius $latPerDg)
  138.                         ->setParameter('east',  $lng $radius $lngPerDg)
  139.                         ->setParameter('west',  $lng $radius $lngPerDg);
  140.                 }
  141.             }
  142.         } else if ($location && !(!empty($location) && empty($location[0]))) {
  143.             $or "";
  144.             $sql "";
  145.             foreach ($location as $value) {
  146.                 $arr explode(','$value);
  147.                 $value $arr[0];
  148.                 $code = isset($arr[1]) ? $arr[1] : '';
  149.                 if (strpos($value'-') && is_numeric(substr($value04))) {
  150.                     $value substr($value0strpos($value'-') - 1);
  151.                 }
  152.                 switch ($code) {
  153.                     case 'canton':
  154.                         $sql .= $or."p.cantonFr = '".$value."'";
  155.                         break;
  156.                     case 'district':
  157.                         $sql .= $or."p.districtFr = '".$value."'";
  158.                         break;
  159.                     case 'zone':
  160.                         $sql .= $or."p.zoneFr = '".$value."'";
  161.                         break;
  162.                     case 'zip':
  163.                         $sql .= $or."p.zip = '".$value."'";
  164.                         break;
  165.                     default:
  166.                         $sql .= $or."p.cityFr = '".$value."'";
  167.                         break;
  168.                 }
  169.                 $or " OR ";
  170.             }
  171.             $qb->andWhere($sql);
  172.         }
  173.         if ($surfaceMin) {
  174.             $qb->andWhere('CAST(p.surface AS DECIMAL) >= :surfaceMin')
  175.                 ->setParameter('surfaceMin', (float)$surfaceMin)
  176.             ;
  177.         }
  178.         if ($surfaceMax) {
  179.             $qb->andWhere('CAST(p.surface AS DECIMAL) <= :surfaceMax')
  180.                 ->setParameter('surfaceMax', (float)$surfaceMax)
  181.             ;
  182.         }
  183.         if ($priceMin) {
  184.             $qb->andWhere('p.price >= :priceMin')
  185.                 ->setParameter('priceMin'$priceMin)
  186.             ;
  187.         }
  188.         if ($priceMax) {
  189.             $qb->andWhere('p.price <= :priceMax')
  190.                 ->setParameter('priceMax'$priceMax)
  191.             ;
  192.         }
  193.         if ($isNewProject) {
  194.             $qb->andWhere('p.status = :isNewProject')
  195.                 ->setParameter('isNewProject''NEW')
  196.             ;
  197.         }
  198.         if ($bounds) {
  199.             $qb ->andWhere('p.latitude BETWEEN :south AND :north')
  200.                 ->andWhere('p.longitude BETWEEN :west AND :east')
  201.                 ->setParameter('north'$bounds->north)
  202.                 ->setParameter('south'$bounds->south)
  203.                 ->setParameter('east'$bounds->east)
  204.                 ->setParameter('west'$bounds->west)
  205.             ;
  206.         }
  207.         return $qb
  208.             ->orderBy('p.id''DESC')
  209.             ->getQuery()
  210.             ->getResult()
  211.         ;
  212.     }
  213.     public function searchSimilaire($idint $isRent):array
  214.     {
  215.         $qb $this->_em->createQueryBuilder()
  216.             ->select('p, SQRT((p2.latitude - p.latitude)*(p2.latitude - p.latitude) + ( p2.longitude - p.longitude)*( p2.longitude - p.longitude)) as dist')
  217.             ->from("App:Property""p")
  218.             ->andWhere('p.transactionType = :isrent')
  219.             ->setParameter('isrent'$isRent)
  220.             ->distinct('p.reference')
  221.         ;
  222.         $qb->join("App:Property"'p2')
  223.             ->andWhere('p2.id = ' $id)//p2 pour bien encours
  224.             ->andWhere('p.id <> ' $id)
  225.             ->andWhere('p2.type = p.type')
  226.         ;
  227.         $qb->andHaving("dist <= 35000")
  228.             ->orderBy('dist''ASC');
  229.         $qb->andWhere('p.surface <= p2.surface + (p2.surface * 0.5)')
  230.             ->andWhere('p.surface >= p2.surface - (p2.surface * 0.5)')
  231.         ;
  232.         $qb->andWhere('p.price <= p2.price + (p2.price * 0.5)')
  233.             ->andWhere('p.price >= p2.price - (p2.price * 0.5)')
  234.         ;
  235.         $qb->orderBy('p.id''DESC');
  236.         $query $qb->getQuery();
  237.         return $query->getResult();
  238.     }
  239. }