1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obblm\Core\Twig; |
4
|
|
|
|
5
|
|
|
use Obblm\Core\Event\TwigCollectorEvent; |
6
|
|
|
use Obblm\Core\Helper\CoreTranslation; |
7
|
|
|
use Obblm\Core\Helper\LocaleHelper; |
8
|
|
|
use Obblm\Core\Helper\RuleHelper; |
9
|
|
|
use Obblm\Core\Twig\Parts\NavigationCollection; |
10
|
|
|
use Obblm\Core\Twig\Parts\NavigationElementInterface; |
11
|
|
|
use Obblm\Core\Twig\Parts\NavigationLink; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
use Symfony\Component\Routing\RouterInterface; |
14
|
|
|
use Twig\Extension\AbstractExtension; |
15
|
|
|
use Twig\TwigFilter; |
16
|
|
|
use Twig\TwigFunction; |
17
|
|
|
|
18
|
|
|
class CoreExtension extends AbstractExtension |
19
|
|
|
{ |
20
|
|
|
private $ruleHelper; |
21
|
|
|
private $dispatcher; |
22
|
|
|
private $localeHelper; |
23
|
|
|
private $router; |
24
|
|
|
|
25
|
|
|
public function __construct(RuleHelper $ruleHelper, EventDispatcherInterface $dispatcher, LocaleHelper $localeHelper, RouterInterface $router) |
26
|
|
|
{ |
27
|
|
|
$this->ruleHelper = $ruleHelper; |
28
|
|
|
$this->dispatcher = $dispatcher; |
29
|
|
|
$this->localeHelper = $localeHelper; |
30
|
|
|
$this->router = $router; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getFilters() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
new TwigFilter('is_collection', [$this, 'isCollection']), |
37
|
|
|
new TwigFilter('is_link', [$this, 'isLink']), |
38
|
|
|
new TwigFilter('price', [$this, 'formatPrice']), |
39
|
|
|
new TwigFilter('yesno', [$this, 'formatBooleanToString']), |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getFunctions() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
new TwigFunction('get_navigation_links', [$this, 'getNavigationLinks']), |
47
|
|
|
new TwigFunction('get_admin_links', [$this, 'getAdminLinks']), |
48
|
|
|
new TwigFunction('rules_navigation', [$this, 'getRulesNavigation']), |
49
|
|
|
new TwigFunction('available_rules', [$this, 'getAvailableRules']), |
50
|
|
|
new TwigFunction('get_available_locales', [$this, 'getAvailableLocales']), |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getAvailableLocales($current = null) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return $this->localeHelper->getLocalizedRoutes(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getRulesNavigation() |
60
|
|
|
{ |
61
|
|
|
$rulesCollection = new NavigationCollection('obblm.forms.team.create.select.rules', 'list'); |
62
|
|
|
foreach ($this->getAvailableRules() as $rule) { |
63
|
|
|
$ruleName = CoreTranslation::getRuleTitle($rule->getRuleKey()); |
64
|
|
|
$rulesCollection->addToCollection( |
65
|
|
|
(new NavigationLink('obblm_team_create_rule', $ruleName, ['rule' => $rule->getId()])) |
66
|
|
|
->setTranslationDomain($rule->getRuleKey()) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$rulesNavigation = (new NavigationCollection()) |
71
|
|
|
->addToCollection($rulesCollection); |
72
|
|
|
$collector = new TwigCollectorEvent($rulesNavigation); |
73
|
|
|
$this->dispatcher->dispatch($collector, TwigCollectorEvent::COLLECT_TEAM_CREATION_BAR); |
74
|
|
|
return $rulesNavigation->getCollection(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getAvailableRules() |
78
|
|
|
{ |
79
|
|
|
return $this->ruleHelper->getRulesAvailableForTeamCreation(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getNavigationLinks():array |
83
|
|
|
{ |
84
|
|
|
$collection = new NavigationCollection(); |
85
|
|
|
$this->dispatcher->dispatch(new TwigCollectorEvent($collection), TwigCollectorEvent::COLLECT_NAV_BAR); |
86
|
|
|
return $collection->getCollection(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function isCollection(NavigationElementInterface $item) |
90
|
|
|
{ |
91
|
|
|
return $item instanceof NavigationCollection; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function isLink(NavigationElementInterface $item) |
95
|
|
|
{ |
96
|
|
|
return $item instanceof NavigationLink; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getAdminLinks():array |
100
|
|
|
{ |
101
|
|
|
$collection = new NavigationCollection(); |
102
|
|
|
$this->dispatcher->dispatch(new TwigCollectorEvent($collection), TwigCollectorEvent::COLLECT_ADMIN_BAR); |
103
|
|
|
return $collection->getCollection(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function formatPrice($number, $decimals = 0, $decPoint = '.', $thousandsSep = ','):string |
107
|
|
|
{ |
108
|
|
|
if ($number === '') { |
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
return number_format($number, $decimals, $decPoint, $thousandsSep); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function formatBooleanToString(bool $var):string |
115
|
|
|
{ |
116
|
|
|
return ($var) ? 'yes' : 'no'; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.