|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Block; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
|
17
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
18
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
|
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
22
|
|
|
use Twig\Environment; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @final since sonata-project/admin-bundle 3.52 |
|
26
|
|
|
* |
|
27
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class AdminStatsBlockService extends AbstractBlockService |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Pool |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $pool; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* NEXT_MAJOR: Remove `$templating` argument. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Environment|string $twigOrName |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($twigOrName, ?EngineInterface $templating, Pool $pool) |
|
42
|
|
|
{ |
|
43
|
|
|
// NEXT_MAJOR: Remove conditions, only pass the Environment |
|
44
|
|
|
if ($templating instanceof EngineInterface) { |
|
45
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
46
|
|
|
'Passing %s as argument 2 to %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x' |
|
47
|
|
|
. ' and will throw a \TypeError in version 4.0. You must pass null instead.', |
|
48
|
|
|
EngineInterface::class, |
|
49
|
|
|
__METHOD__ |
|
50
|
|
|
), E_USER_DEPRECATED); |
|
51
|
|
|
|
|
52
|
|
|
if (!\is_string($twigOrName)) { |
|
53
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
54
|
|
|
'Passing other than string as argument 1 to %s() is not allowed when %s is passed as argument 2.' |
|
55
|
|
|
. ' You must pass an string instead.', |
|
56
|
|
|
__METHOD__, |
|
57
|
|
|
EngineInterface::class |
|
58
|
|
|
)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
parent::__construct($twigOrName, $templating); |
|
|
|
|
|
|
62
|
|
|
} elseif ($twigOrName instanceof Environment) { |
|
63
|
|
|
if (!\is_null($templating)) { |
|
64
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
65
|
|
|
'Argument 2 passed to %s() must be null if you passing %s as first argument, %s given.', |
|
66
|
|
|
__METHOD__, |
|
67
|
|
|
Environment::class, |
|
68
|
|
|
EngineInterface::class |
|
69
|
|
|
)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
parent::__construct($twigOrName); |
|
73
|
|
|
} else { |
|
74
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
75
|
|
|
'Argument 1 passed to %s() should be an instance of %s, and you should leave argument 2 as null.', |
|
76
|
|
|
__METHOD__, |
|
77
|
|
|
Environment::class |
|
78
|
|
|
)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->pool = $pool; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response |
|
85
|
|
|
{ |
|
86
|
|
|
$admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('code')); |
|
87
|
|
|
|
|
88
|
|
|
$datagrid = $admin->getDatagrid(); |
|
89
|
|
|
|
|
90
|
|
|
$filters = $blockContext->getSetting('filters'); |
|
91
|
|
|
|
|
92
|
|
|
if (!isset($filters['_per_page'])) { |
|
93
|
|
|
$filters['_per_page'] = ['value' => $blockContext->getSetting('limit')]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
foreach ($filters as $name => $data) { |
|
97
|
|
|
$datagrid->setValue($name, $data['type'] ?? null, $data['value']); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$datagrid->buildPager(); |
|
101
|
|
|
|
|
102
|
|
|
return $this->renderPrivateResponse($blockContext->getTemplate(), [ |
|
103
|
|
|
'block' => $blockContext->getBlock(), |
|
104
|
|
|
'settings' => $blockContext->getSettings(), |
|
105
|
|
|
'admin_pool' => $this->pool, |
|
106
|
|
|
'admin' => $admin, |
|
107
|
|
|
'pager' => $datagrid->getPager(), |
|
108
|
|
|
'datagrid' => $datagrid, |
|
109
|
|
|
], $response); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getName() |
|
113
|
|
|
{ |
|
114
|
|
|
return 'Admin Stats'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
|
118
|
|
|
{ |
|
119
|
|
|
$resolver->setDefaults([ |
|
120
|
|
|
'icon' => 'fa-line-chart', |
|
121
|
|
|
'text' => 'Statistics', |
|
122
|
|
|
'translation_domain' => null, |
|
123
|
|
|
'color' => 'bg-aqua', |
|
124
|
|
|
'code' => false, |
|
125
|
|
|
'filters' => [], |
|
126
|
|
|
'limit' => 1000, |
|
127
|
|
|
'template' => '@SonataAdmin/Block/block_stats.html.twig', |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: