/
FishbowlAdmin.php
163 lines (148 loc) · 5.71 KB
/
FishbowlAdmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/*
* This file is part of the Stooa codebase.
*
* (c) 2020 - present Runroom SL
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Admin;
use App\Entity\Fishbowl;
use App\Service\FishbowlService;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
use Sonata\DoctrineORMAdminBundle\Filter\ModelAutocompleteFilter;
use Sonata\Form\Type\BooleanType;
use Sonata\Form\Type\DateTimePickerType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
/** @extends AbstractAdmin<Fishbowl> */
class FishbowlAdmin extends AbstractAdmin
{
protected ?FishbowlService $fishbowlService;
public function setFishbowlService(FishbowlService $fishbowlService): void
{
$this->fishbowlService = $fishbowlService;
}
public function prePersist($object): void
{
if (null !== $this->fishbowlService) {
$object->setSlug($this->fishbowlService->generateRandomSlug($object));
$this->fishbowlService->generateDefaultTitle($object);
}
}
public static function hostCallbackFunction(): callable
{
return static function (AdminInterface $admin, string $property, string $value): void {
$datagrid = $admin->getDatagrid();
$valueParts = explode(':', $value);
if (2 === \count($valueParts) && \in_array($valueParts[0], ['name', 'surnames'], true)) {
[$property, $value] = $valueParts;
}
$datagrid->setValue($datagrid->getFilter($property)->getFormName(), null, $value);
};
}
/** @param mixed[] $sortValues */
protected function configureDefaultSortValues(array &$sortValues): void
{
$sortValues['_sort_by'] = 'createdAt';
$sortValues['_sort_order'] = 'DESC';
}
protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('name')
->add('host', ModelAutocompleteFilter::class, [], null, [
'property' => 'email',
'callback' => self::hostCallbackFunction(),
])
->add('currentStatus', null, [], ChoiceType::class, [
'choices' => Fishbowl::$statusChoices,
])
->add('hasIntroduction');
}
protected function configureListFields(ListMapper $list): void
{
$list
->add('createdAt')
->addIdentifier('name')
->add('description')
->add('host')
->add('currentStatus', null, [
'template' => 'sonata/fishbowl_status.html.twig',
])
->add('hasIntroduction')
->add('startDateTimeTz', FieldDescriptionInterface::TYPE_DATETIME)
->add('endDateTimeTz', FieldDescriptionInterface::TYPE_DATETIME)
->add('duration', FieldDescriptionInterface::TYPE_TIME)
->add('timezone')
->add('locale')
->add('slug');
}
protected function configureFormFields(FormMapper $form): void
{
$form
->with('Fishbowl')
->add('name')
->add('description')
->add('startDateTime', DateTimePickerType::class, [
'dp_use_seconds' => false,
'dp_minute_stepping' => 15,
'format' => 'dd/MM/yyyy HH:mm',
])
->add('duration', DateTimePickerType::class, [
'dp_pick_date' => false,
'dp_use_seconds' => false,
'dp_minute_stepping' => 15,
'format' => 'HH:mm',
])
->add('timezone', TimezoneType::class, [
'intl' => true,
])
->add('locale', LocaleType::class)
->add('host', ModelAutocompleteType::class, [
'property' => 'email',
'callback' => self::hostCallbackFunction(),
])
->add('hasIntroduction', BooleanType::class, [
'transform' => true,
])
->end()
->with('Disabled')
->add('currentStatus', ChoiceType::class, [
'choices' => Fishbowl::$statusChoices,
'disabled' => true,
'required' => true,
])
->add('introducedAt', DateTimePickerType::class, [
'disabled' => true,
'dp_use_seconds' => false,
'dp_minute_stepping' => 15,
'format' => 'dd/MM/yyyy HH:mm',
])
->add('runnedAt', DateTimePickerType::class, [
'disabled' => true,
'dp_use_seconds' => false,
'dp_minute_stepping' => 15,
'format' => 'dd/MM/yyyy HH:mm',
])
->add('finishedAt', DateTimePickerType::class, [
'disabled' => true,
'dp_use_seconds' => false,
'dp_minute_stepping' => 15,
'format' => 'dd/MM/yyyy HH:mm',
])
->add('slug', null, [
'disabled' => true,
])
->end();
}
}