src/Repository/Ajustes/ConfigGlobalRepository.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Ajustes;
  3. use App\Entity\Ajustes\ConfigGlobal;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. /**
  7.  * @method ConfigGlobal|null find($id, $lockMode = null, $lockVersion = null)
  8.  * @method ConfigGlobal|null findOneBy(array $criteria, array $orderBy = null)
  9.  * @method ConfigGlobal[]    findAll()
  10.  * @method ConfigGlobal[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  11.  */
  12. class ConfigGlobalRepository extends ServiceEntityRepository
  13. {
  14.     public function __construct(ManagerRegistry $registry)
  15.     {
  16.         parent::__construct($registryConfigGlobal::class);
  17.     }
  18.     // /**
  19.     //  * @return ConfigGlobal[] Returns an array of ConfigGlobal objects
  20.     //  */
  21.     /*
  22.     public function findByExampleField($value)
  23.     {
  24.         return $this->createQueryBuilder('c')
  25.             ->andWhere('c.exampleField = :val')
  26.             ->setParameter('val', $value)
  27.             ->orderBy('c.id', 'ASC')
  28.             ->setMaxResults(10)
  29.             ->getQuery()
  30.             ->getResult()
  31.         ;
  32.     }
  33.     */
  34.     /*
  35.     public function findOneBySomeField($value): ?ConfigGlobal
  36.     {
  37.         return $this->createQueryBuilder('c')
  38.             ->andWhere('c.exampleField = :val')
  39.             ->setParameter('val', $value)
  40.             ->getQuery()
  41.             ->getOneOrNullResult()
  42.         ;
  43.     }
  44.     */
  45.     public function filter($data$partial ""$getQuery false){
  46.         $parameters = array();
  47.         $query      $this
  48.             ->createQueryBuilder('config')
  49.         ;
  50.         //dd($data);
  51.         if($partial != ""){
  52.             $query->select($partial);
  53.         }
  54.         if(($data['texto']['texto'] ?? $data['texto'] ?? '') != ''){
  55.             $query->andwhere('
  56.                 config.ConfigName like :where1 OR
  57.                 config.ConfigValue like :where1 OR
  58.                 config.ConfigDescription like :where1
  59.             ');
  60.             $parameters[':where1'] = '%'.( $data['texto']['texto'] ?? $data['texto'] ).'%';
  61.         }
  62.         
  63.         if(($data['enabled'] ?? '') != ''){
  64.             $query->andwhere('config.ConfigEnabled = :ena');
  65.             $parameters[':ena'] = $data['enabled'];
  66.         }
  67.         
  68.         if(!empty($parameters)){
  69.             $query->setParameters($parameters);
  70.         }
  71.         if($getQuery){
  72.             return $query->getQuery();
  73.         }
  74.         return $query->getQuery()->getResult();
  75.     }
  76.     public function getValueOrCreate(string $key$default null$desc null): ?string
  77.     {
  78.         $result $this->findOneBy(['ConfigName' => $key'ConfigEnabled' => true]);
  79.         if($result){
  80.             return $result->getConfigValue();
  81.         }
  82.         if($default === null){
  83.             return null;
  84.         }
  85.         $confg = (new ConfigGlobal)
  86.             ->setConfigName($key)
  87.             ->setConfigValue($default)
  88.             ->setConfigDescription($desc)
  89.         ;
  90.         try{
  91.             $this->_em->persist($confg);
  92.             $this->_em->flush();
  93.             return $default;
  94.         }catch(\Exception $ex){
  95.             $this->logger->error($ex);
  96.             return null;
  97.         }
  98.     }
  99.     public function getValueOrCreateEntity(string $key$default null$desc null)
  100.     {
  101.         $result $this->findOneBy(['ConfigName' => $key'ConfigEnabled' => true]);
  102.         if($result){
  103.             return $result;
  104.         }
  105.         if($default === null){
  106.             return null;
  107.         }
  108.         $confg = (new ConfigGlobal)
  109.             ->setConfigName($key)
  110.             ->setConfigValue($default)
  111.             ->setConfigDescription($desc)
  112.         ;
  113.         try{
  114.             $this->_em->persist($confg);
  115.             $this->_em->flush();
  116.             return $default;
  117.         }catch(\Exception $ex){
  118.             $this->logger->error($ex);
  119.             return null;
  120.         }
  121.     }
  122. }