|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\BlogBundle\Filter; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
6
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
7
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
8
|
|
|
use Victoire\Bundle\FilterBundle\Filter\BaseFilter; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* DateFilter form type. |
|
12
|
|
|
*/ |
|
13
|
|
|
class DateFilter extends BaseFilter |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Build the query. |
|
17
|
|
|
* |
|
18
|
|
|
* @param QueryBuilder &$qb |
|
19
|
|
|
* @param array $parameters |
|
20
|
|
|
* |
|
21
|
|
|
* @return QueryBuilder |
|
22
|
|
|
*/ |
|
23
|
|
|
public function buildQuery(QueryBuilder $qb, array $parameters) |
|
24
|
|
|
{ |
|
25
|
|
|
$emConfig = $this->getEntityManager()->getConfiguration(); |
|
26
|
|
|
$emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year'); |
|
27
|
|
|
$emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month'); |
|
28
|
|
|
$emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day'); |
|
29
|
|
|
|
|
30
|
|
|
if (isset($parameters['year'])) { |
|
31
|
|
|
$qb->andWhere('YEAR(main_item.publishedAt) = :year') |
|
32
|
|
|
->setParameter('year', $parameters['year']); |
|
33
|
|
|
} |
|
34
|
|
|
if (isset($parameters['month'])) { |
|
35
|
|
|
$qb->andWhere('MONTH(main_item.publishedAt) = :month') |
|
36
|
|
|
->setParameter('month', $parameters['month']); |
|
37
|
|
|
} |
|
38
|
|
|
if (isset($parameters['day'])) { |
|
39
|
|
|
$qb->andWhere('DAY(main_item.publishedAt) = :day') |
|
40
|
|
|
->setParameter('day', $parameters['day']); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $qb; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* define form fields. |
|
48
|
|
|
* |
|
49
|
|
|
* @param FormBuilderInterface $builder |
|
50
|
|
|
* @param array $options |
|
51
|
|
|
* |
|
52
|
|
|
* @SuppressWarnings checkUnusedFunctionParameters |
|
53
|
|
|
*/ |
|
54
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
55
|
|
|
{ |
|
56
|
|
|
$articles = $this->getEntityManager()->getRepository('VictoireBlogBundle:Article')->getAll(true)->run(); |
|
57
|
|
|
$years = $months = $days = []; |
|
58
|
|
|
foreach ($articles as $key => $_article) { |
|
59
|
|
|
$years[$_article->getPublishedAt()->format('Y')] = $_article->getPublishedAt()->format('Y'); |
|
60
|
|
|
|
|
61
|
|
|
if ($options['widget']->getFormat() != 'year') { |
|
62
|
|
|
//init $months array |
|
63
|
|
|
if (!isset($months[$_article->getPublishedAt()->format('Y')])) { |
|
64
|
|
|
$months[$_article->getPublishedAt()->format('Y')] = []; |
|
65
|
|
|
} |
|
66
|
|
|
$months[$_article->getPublishedAt()->format('Y')][] = $_article->getPublishedAt()->format('M'); |
|
67
|
|
|
if ($options['widget']->getFormat() != 'month') { |
|
68
|
|
|
//init $days array |
|
69
|
|
|
if (!isset($days[$_article->getPublishedAt()->format('M')])) { |
|
70
|
|
|
$days[$_article->getPublishedAt()->format('M')] = []; |
|
71
|
|
|
} |
|
72
|
|
|
//assign values |
|
73
|
|
|
$days[$_article->getPublishedAt()->format('M')][] = $_article->getPublishedAt()->format('M'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$data = ['year' => null, 'month' => null, 'day' => null]; |
|
79
|
|
|
if ($this->getRequest()->query->has('filter') && array_key_exists('date_filter', $this->getRequest()->query->get('filter'))) { |
|
80
|
|
|
$_request = $this->getRequest()->query->get('filter')['date_filter']; |
|
81
|
|
|
$data = $_request; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (in_array($options['widget']->getFormat(), ['year', 'month', 'day'])) { |
|
85
|
|
|
if (!$data['year']) { |
|
86
|
|
|
// set default value to date filter and set listing to request while not better way |
|
87
|
|
|
$data['year'] = $options['widget']->getDefaultValue(); |
|
88
|
|
|
$this->getRequest()->query->replace( |
|
89
|
|
|
[ |
|
90
|
|
|
'filter' => [ |
|
91
|
|
|
self::class => [ |
|
92
|
|
|
'year' => $options['widget']->getDefaultValue(), |
|
93
|
|
|
], |
|
94
|
|
|
'listing' => $options['widget']->getListing()->getId(), |
|
95
|
|
|
], |
|
96
|
|
|
] |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
$builder |
|
100
|
|
|
->add( |
|
101
|
|
|
'year', ChoiceType::class, [ |
|
102
|
|
|
'label' => false, |
|
103
|
|
|
'choices' => $years, |
|
104
|
|
|
'required' => false, |
|
105
|
|
|
'expanded' => true, |
|
106
|
|
|
'multiple' => false, |
|
107
|
|
|
'empty_value' => false, |
|
108
|
|
|
'data' => $data['year'], |
|
109
|
|
|
] |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* get name. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string name |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getName() |
|
120
|
|
|
{ |
|
121
|
|
|
return 'date_filter'; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|