<?php
namespace App\Repository\Ajustes;
use App\Entity\Ajustes\ConfigGlobal;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method ConfigGlobal|null find($id, $lockMode = null, $lockVersion = null)
* @method ConfigGlobal|null findOneBy(array $criteria, array $orderBy = null)
* @method ConfigGlobal[] findAll()
* @method ConfigGlobal[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ConfigGlobalRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ConfigGlobal::class);
}
// /**
// * @return ConfigGlobal[] Returns an array of ConfigGlobal objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?ConfigGlobal
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
public function filter($data, $partial = "", $getQuery = false){
$parameters = array();
$query = $this
->createQueryBuilder('config')
;
//dd($data);
if($partial != ""){
$query->select($partial);
}
if(($data['texto']['texto'] ?? $data['texto'] ?? '') != ''){
$query->andwhere('
config.ConfigName like :where1 OR
config.ConfigValue like :where1 OR
config.ConfigDescription like :where1
');
$parameters[':where1'] = '%'.( $data['texto']['texto'] ?? $data['texto'] ).'%';
}
if(($data['enabled'] ?? '') != ''){
$query->andwhere('config.ConfigEnabled = :ena');
$parameters[':ena'] = $data['enabled'];
}
if(!empty($parameters)){
$query->setParameters($parameters);
}
if($getQuery){
return $query->getQuery();
}
return $query->getQuery()->getResult();
}
public function getValueOrCreate(string $key, $default = null, $desc = null): ?string
{
$result = $this->findOneBy(['ConfigName' => $key, 'ConfigEnabled' => true]);
if($result){
return $result->getConfigValue();
}
if($default === null){
return null;
}
$confg = (new ConfigGlobal)
->setConfigName($key)
->setConfigValue($default)
->setConfigDescription($desc)
;
try{
$this->_em->persist($confg);
$this->_em->flush();
return $default;
}catch(\Exception $ex){
$this->logger->error($ex);
return null;
}
}
public function getValueOrCreateEntity(string $key, $default = null, $desc = null)
{
$result = $this->findOneBy(['ConfigName' => $key, 'ConfigEnabled' => true]);
if($result){
return $result;
}
if($default === null){
return null;
}
$confg = (new ConfigGlobal)
->setConfigName($key)
->setConfigValue($default)
->setConfigDescription($desc)
;
try{
$this->_em->persist($confg);
$this->_em->flush();
return $default;
}catch(\Exception $ex){
$this->logger->error($ex);
return null;
}
}
}