-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePresenterTrait.php
More file actions
147 lines (121 loc) · 4.79 KB
/
Copy pathBasePresenterTrait.php
File metadata and controls
147 lines (121 loc) · 4.79 KB
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
<?php
namespace ADT\FancyAdmin\UI\Presenters;
use ADT\FancyAdmin\DI\Injects\EntityManagerInject;
use ADT\FancyAdmin\DI\Injects\FancyAdminInject;
use ADT\FancyAdmin\DI\Injects\JsComponentsInject;
use ADT\FancyAdmin\DI\Injects\TranslatorInject;
use ADT\FancyAdmin\Model\Security\SecurityUser;
use Exception;
use Nette\Application\Attributes\Persistent;
use Nette\Utils\Json;
use stdClass;
trait BasePresenterTrait
{
use PresenterTrait;
use FancyAdminInject;
use EntityManagerInject;
use TranslatorInject;
use JsComponentsInject;
protected bool $primaryTemplate = false;
#[Persistent]
public string $backlink = '';
protected function beforeRender(): void
{
parent::beforeRender();
$this->getTemplate()->originalTemplate = __DIR__ . '/@layout.latte';
$this->getTemplate()->primaryTemplate = $this->primaryTemplate;
$this->getTemplate()->logoFileName = $this->_fancyAdmin->getLogoPublicPath();
$this->getTemplate()->faviconFileNamePng = $this->_fancyAdmin->getFaviconFileNamePng();
$this->getTemplate()->faviconFileNameSvg = $this->_fancyAdmin->getFaviconFileNameSvg();
$this->getTemplate()->icon = $this->_fancyAdmin->getLogoMenuPath();
$this->getTemplate()->loginPageLogoPath = $this->_fancyAdmin->getLoginPageLogoPath();
$this->getTemplate()->hmr = $this->_fancyAdmin->getHmr();
$this->getTemplate()->projectName = $this->_fancyAdmin->getProjectName();
$this->getTemplate()->colors = $this->_fancyAdmin->getColors();
$this->_jsComponents->setComponents($this->_fancyAdmin->getJsComponentsConfig());
$this->_jsComponents->setFirebaseLink('setFirebaseTokenLink', $this->getPresenter()->link('setFirebaseToken!', ['firebaseToken' => '__firebaseToken__']));
$this->_jsComponents->setFirebaseLink('removeFirebaseTokenLink', $this->getPresenter()->link('removeFirebaseToken!', ['firebaseToken' => '__firebaseToken__']));
$this->_jsComponents->setFirebaseLink('removeAllFirebaseTokensLink', $this->getPresenter()->link('removeAllFirebaseTokens!'));
$this->getTemplate()->jsComponentsConfig = $this->_jsComponents->generateConfig();
}
/************************
**** FLASH MESSAGES ****
***********************
* @param string $message
* @param string $type
* @return stdClass
* @throws Exception
*/
public function flashMessage($message, string $type = 'info'): stdClass
{
throw new Exception('Use one of flashMessageError / flashMessageWarning / flashMessageSuccess / flashMessageInfo method instead.');
}
public function flashMessageError(string $message, ?int $autoCloseDuration = null): stdClass
{
return $this->flashMessageCommon($message, 'danger', $autoCloseDuration);
}
public function flashMessageWarning(string $message, ?int $autoCloseDuration = null): stdClass
{
return $this->flashMessageCommon($message, 'warning', $autoCloseDuration);
}
public function flashMessageSuccess(string $message, ?int $autoCloseDuration = null): stdClass
{
return $this->flashMessageCommon($message, 'success', $autoCloseDuration);
}
public function flashMessageInfo(string $message, ?int $autoCloseDuration = null): stdClass
{
return $this->flashMessageCommon($message, 'info', $autoCloseDuration);
}
/** @internal */
private function flashMessageCommon(string $message, string $type, ?int $autoCloseDuration = null)
{
//$this->redrawControl('flashes');
$flash = parent::flashMessage($this->_translator->translate($message), $type);
$flash->closeDuration = $autoCloseDuration ?? BasePresenter::DEFAULT_AUTO_CLOSE_DURATION;
return $flash;
}
public function afterRender(): void
{
if (!$this->isControlInvalid()) {
$this->redrawControl('title');
$this->redrawControl('body');
}
}
/**
* Formats view template file names.
* @return array
*/
public function formatTemplateFiles(): array
{
$list = parent::formatTemplateFiles();
$list[] = __DIR__ . '/' . explode(':', $this->name)[1] . '/' . $this->view . '.latte';
return $list;
}
public function formatLayoutTemplateFiles(): array
{
$list = parent::formatLayoutTemplateFiles();
$list[] = __DIR__ . "/@layout.latte";
return $list;
}
public function handleSetFirebaseToken(string $firebaseToken): void
{
$this->getUser()->getIdentity()
->addFirebaseToken($firebaseToken);
$this->em->flush();
$this->flashMessageSuccess('fcadmin.firebase.notifications.flashes.success'); // TODO translate
}
public function handleRemoveFirebaseToken(string $firebaseToken): void
{
$this->getUser()->getIdentity()
->removeFirebaseToken($firebaseToken);
$this->em->flush();
$this->flashMessageSuccess('fcadmin.firebase.notifications.flashes.disabled'); // TODO translate
}
public function handleRemoveAllFirebaseTokens(): void
{
$this->getUser()->getIdentity()
->setFirebaseTokens([]);
$this->em->flush();
$this->flashMessageSuccess('fcadmin.firebase.notifications.flashes.disabled'); // TODO translate
}
}