Skip to content

Commit a5c299b

Browse files
committed
used a trait to avoid code duplication on frontend pages
1 parent 42c7cfe commit a5c299b

File tree

3 files changed

+10
-142
lines changed

3 files changed

+10
-142
lines changed

app/base/abstracts/FrontendPageWithObject.php

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,14 @@
1515
use \App\App;
1616
use \App\Site\Routing\RouteInfo;
1717
use \Exception;
18+
use \App\Base\Traits\FrontendTrait;
1819

1920
/**
2021
* Base for a page diplaying a model
2122
*/
2223
abstract class FrontendPageWithObject extends FrontendPage
2324
{
24-
/**
25-
* @var array template data
26-
*/
27-
protected $templateData = [];
28-
29-
/**
30-
* @var RouteInfo route info object
31-
*/
32-
protected $route_info = null;
33-
34-
/**
35-
* gets route group
36-
*
37-
* @return string
38-
*/
39-
public static function getRouteGroup()
40-
{
41-
return '';
42-
}
25+
use FrontendTrait;
4326

4427
/**
4528
* {@inheritdocs}
@@ -65,68 +48,6 @@ public function process(RouteInfo $route_info = null, $route_data = [])
6548
return $return;
6649
}
6750

68-
/**
69-
* {@inheritdocs}
70-
*
71-
* @return string
72-
*/
73-
public function getCurrentLocale()
74-
{
75-
if (isset($this->templateData['object']) && ($this->templateData['object'] instanceof Model) && $this->templateData['object']->isLoaded()) {
76-
if ($this->templateData['object']->getLocale()) {
77-
return $this->getApp()->setCurrentLocale($this->templateData['object']->getLocale())->getCurrentLocale();
78-
}
79-
}
80-
81-
return $this->getApp()->setCurrentLocale(parent::getCurrentLocale())->getCurrentLocale();
82-
}
83-
84-
/**
85-
* {@inheritdocs}
86-
*
87-
* @return array
88-
*/
89-
protected function getTemplateData()
90-
{
91-
return $this->templateData;
92-
}
93-
94-
/**
95-
* sets object to show
96-
*
97-
* @param Model $object
98-
*/
99-
protected function setObject(Model $object)
100-
{
101-
$this->templateData['object'] = $object;
102-
return $this;
103-
}
104-
105-
/**
106-
* gets object to show
107-
*
108-
* @return Model|null
109-
*/
110-
protected function getObject()
111-
{
112-
return $this->templateData['object'] ?? null;
113-
}
114-
115-
/**
116-
* {@inheritdocs}
117-
*
118-
* @return array
119-
*/
120-
public function getTranslations()
121-
{
122-
return array_map(
123-
function ($el) {
124-
return $this->getRouting()->getBaseUrl() . $el;
125-
},
126-
$this->getContainer()->call([$this->getObject(), 'getTranslations'])
127-
);
128-
}
129-
13051
/**
13152
* {@inheritdocs}
13253
*

app/base/traits/PageTrait.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ trait PageTrait
2525
*/
2626
protected $current_user = null;
2727

28+
/**
29+
* @var RouteInfo route info object
30+
*/
31+
protected $route_info = null;
32+
2833
/**
2934
* calculates JWT token id
3035
*
@@ -79,7 +84,7 @@ protected function getTokenData()
7984
$data = new \Lcobucci\JWT\ValidationData();
8085
$data->setIssuer($container->get('jwt_issuer'));
8186
$data->setAudience($container->get('jwt_audience'));
82-
87+
8388
$claimUID = (string) $token->getClaim('uid');
8489
$claimUserName = (string) $token->getClaim('username');
8590

app/site/controllers/Frontend/ContactForm.php

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,14 @@
2222
use \Symfony\Component\HttpFoundation\Response;
2323
use \DateTime;
2424
use \Exception;
25+
use \App\Base\Traits\FrontendTrait;
2526

2627
/**
2728
* Contact Form Page
2829
*/
2930
class ContactForm extends FormPage // and and is similar to FrontendPageWithObject
3031
{
31-
/**
32-
* @var array template data
33-
*/
34-
protected $templateData = [];
35-
36-
/**
37-
* @var RouteInfo route info object
38-
*/
39-
protected $route_info = null;
32+
use FrontendTrait;
4033

4134
/**
4235
* {@inheritdocs}
@@ -52,16 +45,6 @@ protected function getTemplateName()
5245
return 'contact_form';
5346
}
5447

55-
/**
56-
* gets route group
57-
*
58-
* @return string
59-
*/
60-
public static function getRouteGroup()
61-
{
62-
return '';
63-
}
64-
6548
/**
6649
* return route path
6750
*
@@ -91,22 +74,6 @@ public function process(RouteInfo $route_info = null, $route_data = [])
9174
return parent::process($route_info);
9275
}
9376

94-
/**
95-
* {@inheritdocs}
96-
*
97-
* @return string
98-
*/
99-
public function getCurrentLocale()
100-
{
101-
if ($this->templateData['object'] instanceof Model && $this->templateData['object']->isLoaded()) {
102-
if ($this->templateData['object']->getLocale()) {
103-
return $this->getApp()->setCurrentLocale($this->templateData['object']->getLocale())->getCurrentLocale();
104-
}
105-
}
106-
107-
return $this->getApp()->setCurrentLocale(parent::getCurrentLocale())->getCurrentLocale();
108-
}
109-
11077
/**
11178
* {@inheritdocs}
11279
*
@@ -119,16 +86,6 @@ protected function getBaseTemplateData()
11986
return $out;
12087
}
12188

122-
/**
123-
* {@inheritdocs}
124-
*
125-
* @return array
126-
*/
127-
protected function getTemplateData()
128-
{
129-
return $this->templateData;
130-
}
131-
13289
/**
13390
* {@inheritdocs}
13491
*
@@ -266,19 +223,4 @@ public static function getObjectClass()
266223
{
267224
return Contact::class;
268225
}
269-
270-
/**
271-
* {@inheritdocs}
272-
*
273-
* @return array
274-
*/
275-
public function getTranslations()
276-
{
277-
return array_map(
278-
function ($el) {
279-
return $this->getRouting()->getBaseUrl() . $el;
280-
},
281-
$this->getContainer()->call([$this->templateData['object'], 'getTranslations'])
282-
);
283-
}
284226
}

0 commit comments

Comments
 (0)