| 1: | <?php |
| 2: | namespace TIC\ListBundle\Repository; |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | use TIC\DormBundle\Base\TICRepository as BaseRepository; |
| 9: | use Doctrine\ORM\QueryBuilder; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | class ListItemRepository extends BaseRepository |
| 15: | { |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | public $sortBy = array('ranking' => 'ASC', 'label' => 'ASC'); |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | public function qbFilters(array $filters = array(), $qb = null): QueryBuilder |
| 30: | { |
| 31: | $alias = is_string($qb) ? $qb : (isset($this->alias) ? $this->alias : 'a'); |
| 32: | $qb = parent::qbFilters($filters, $qb); |
| 33: | |
| 34: | foreach ($filters as $type => $value) switch ($type) { |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | case 'allorval': |
| 40: | if ($value === true) break; |
| 41: | if ($value === null || $value === false) { $qb->andWhere($alias.'.enabled = true'); break; } |
| 42: | $qb->andWhere('('.$alias.'.enabled = true OR '.$alias.'.value = :curval)')->setParameter('curval', is_object($value)?$value->getValue():$value); |
| 43: | break; |
| 44: | |
| 45: | case 'list' : |
| 46: | case 'info' : if ($value) $qb->andWhere($alias.'.info = :info')->setParameter('info', $value); break; |
| 47: | case 'ref' : |
| 48: | case 'value' : if ($value) $qb->andWhere($alias.'.'.$type.' = :'.$type)->setParameter($type, $value); break; |
| 49: | } |
| 50: | return $qb; |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function qbList($list, $all_or_current = false, $locale = true) { |
| 58: | $filters = array( |
| 59: | 'list' => $list, |
| 60: | 'allorcur' => $all_or_current, |
| 61: | |
| 62: | 'locale' => $locale, |
| 63: | ); |
| 64: | return $this->qbFilters($filters)->orderBy('a.ranking', 'ASC')->addOrderBy('a.label', 'ASC'); |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | public function listChoices($list, $locale = 'fr') { |
| 72: | $filters = array( |
| 73: | 'list' => $list, |
| 74: | 'locale' => $locale, |
| 75: | 'enabled' => true, |
| 76: | ); |
| 77: | return $this->qbFilters($filters)->select('a.value, COALESCE(atrans.label, a.label)') |
| 78: | ->orderBy('a.ranking', 'ASC')->addOrderBy('atrans.label', 'ASC')->addOrderBy('a.label', 'ASC') |
| 79: | ->getQuery()->getResult('keyval') |
| 80: | ; |
| 81: | } |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | public function listJoinAll($filters = array()) { |
| 90: | if (! isset($filters['locale'])) $filters['locales'] = true; |
| 91: | return $this->qbFilters($filters)->orderBy('a.ranking', 'ASC')->addOrderBy('a.label', 'ASC')->getQuery()->getResult(); |
| 92: | } |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function getOne($filters = array()) { |
| 98: | return $this->qbFilters($filters)->getQuery()->getOneOrNullResult(); |
| 99: | } |
| 100: | |
| 101: | } |
| 102: | |