Skip to content

Commit 2b716b3

Browse files
committed
reviewed controller action flow
1 parent 923cb31 commit 2b716b3

File tree

14 files changed

+82
-45
lines changed

14 files changed

+82
-45
lines changed

app/base/abstracts/AdminFormPage.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,22 @@ private function processFormSubmit()
6666
}
6767
}
6868

69+
/**
70+
* check if form is submitted
71+
*/
72+
protected function isSubmitted()
73+
{
74+
return ($this->templateData['form'] && $this->templateData['form']->isSubmitted());
75+
}
76+
6977
/**
7078
* {@inheritdocs}
7179
*
7280
* @return Response|self
7381
*/
7482
protected function beforeRender()
7583
{
76-
if ($this->templateData['form']->isSubmitted()) {
84+
if ($this->isSubmitted()) {
7785
$this->getApp()->event('form_submitted', ['form' => $this->templateData['form']]);
7886
return $this->templateData['form']->getSubmitResults(get_class($this).'::formSubmitted');
7987
}

app/base/abstracts/BaseHtmlPage.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ protected function prepareTemplate()
5555
*/
5656
public function process(RouteInfo $route_info = null, $route_data = [])
5757
{
58-
$result = parent::process($route_info);
59-
if ($result instanceof Response) {
60-
return $result;
61-
}
6258
$this->template = $this->prepareTemplate();
6359
$this->getApp()->setCurrentLocale($this->getCurrentLocale());
6460
if ($this->getEnv('DEBUG')) {
@@ -164,7 +160,7 @@ public function addFlashMessage($type, $message)
164160
$flash_messages[$type][] = $message;
165161

166162
$this->getResponse()->headers->setCookie(new Cookie('flash_messages', json_encode($flash_messages), time()+3600, "/"));
167-
163+
168164
return $this;
169165
}
170166

app/base/abstracts/BaseJsonPage.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ public function __construct(ContainerInterface $container)
4343
*/
4444
public function process(RouteInfo $route_info = null, $route_data = [])
4545
{
46-
$result = parent::process($route_info);
47-
if ($result instanceof Response) {
48-
return $result;
49-
}
5046
try {
5147
return $this
5248
->getResponse()

app/base/abstracts/BasePage.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function hasLoggedUser()
9191
* @param array $route_data
9292
* @return Response|self
9393
*/
94-
public function process(RouteInfo $route_info = null, $route_data = [])
94+
public function renderPage(RouteInfo $route_info = null, $route_data = [])
9595
{
9696
$this->route_info = $route_info;
9797

@@ -100,9 +100,10 @@ public function process(RouteInfo $route_info = null, $route_data = [])
100100
return $before_result;
101101
}
102102

103-
return $this;
103+
return $this->process($route_info, $route_data);
104104
}
105105

106+
106107
/**
107108
* checks if current user has specified permission
108109
*
@@ -259,4 +260,13 @@ protected function doRedirect($url)
259260
]
260261
);
261262
}
263+
264+
/**
265+
* controller action
266+
*
267+
* @param RouteInfo|null $route_info
268+
* @param array $route_data
269+
* @return Response|self
270+
*/
271+
abstract public function process(RouteInfo $route_info = null, $route_data = []);
262272
}

app/base/abstracts/BaseXMLPage.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ public function __construct(ContainerInterface $container)
4343
*/
4444
public function process(RouteInfo $route_info = null, $route_data = [])
4545
{
46-
$result = parent::process($route_info);
47-
if ($result instanceof Response) {
48-
return $result;
49-
}
5046
try {
5147
return $this
5248
->getResponse()

app/base/abstracts/FormPage.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,30 @@ abstract class FormPage extends FrontendPage
3131
/**
3232
* {@inheritdocs}
3333
*
34-
* @param RouteInfo|null $route_info
35-
* @param array $route_data
36-
* @return Response
34+
* @param ContainerInterface $container
3735
*/
38-
public function process(RouteInfo $route_info = null, $route_data = [])
36+
public function __construct(ContainerInterface $container)
3937
{
40-
$this->route_info = $route_info;
38+
parent::__construct($container);
4139

4240
$this->templateData += [
4341
'form' => FAPI\FormBuilder::getForm([$this, 'getFormDefinition'])
4442
->setValidate([ [$this, 'formValidate'] ])
4543
->setSubmit([ [$this, 'formSubmitted'] ]),
4644
];
45+
4746
$this->processFormSubmit();
47+
}
4848

49+
/**
50+
* {@inheritdocs}
51+
*
52+
* @param RouteInfo|null $route_info
53+
* @param array $route_data
54+
* @return Response
55+
*/
56+
public function process(RouteInfo $route_info = null, $route_data = [])
57+
{
4958
return parent::process($route_info, $route_data);
5059
}
5160

@@ -54,7 +63,7 @@ public function process(RouteInfo $route_info = null, $route_data = [])
5463
*
5564
* @return void
5665
*/
57-
private function processFormSubmit()
66+
protected function processFormSubmit()
5867
{
5968
$this->getApp()->event('before_form_process', ['form' => $this->templateData['form']]);
6069
$this->templateData['form']->process();
@@ -67,13 +76,21 @@ private function processFormSubmit()
6776
*/
6877
protected function beforeRender()
6978
{
70-
if ($this->templateData['form']->isSubmitted()) {
79+
if ($this->templateData['form'] && $this->templateData['form']->isSubmitted()) {
7180
$this->getApp()->event('form_submitted', ['form' => $this->templateData['form']]);
7281
return $this->templateData['form']->getSubmitResults(get_class($this).'::formSubmitted');
7382
}
7483
return parent::beforeRender();
7584
}
7685

86+
/**
87+
* check if form is submitted
88+
*/
89+
protected function isSubmitted()
90+
{
91+
return ($this->templateData['form'] && $this->templateData['form']->isSubmitted());
92+
}
93+
7794
/**
7895
* gets form definition object
7996
*

app/site/blocks/ChangeLanguage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ function ($el, $key) use ($config) {
5050
$text = '';
5151
$language = $this->getContainer()->call([Language::class, 'loadBy'], ['field' => 'locale', 'value' => $key]);
5252

53-
if ($config['show-language'] == 'code') {
53+
if (isset($config['show-language']) && $config['show-language'] == 'code') {
5454
$text = $key;
5555
}
56-
if ($config['show-language'] == 'full') {
56+
if (isset($config['show-language']) && $config['show-language'] == 'full') {
5757
$text = $language->native;
5858
}
59-
if (boolval($config['show-flags'])) {
59+
if (isset($config['show-flags']) && boolval($config['show-flags'])) {
6060
$text .= ' '.$this->getHtmlRenderer()->renderFlag($key);
6161
}
6262

app/site/controllers/Admin/Json/ContactCallback.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ public function emptyForm(FAPI\Form $form, &$form_state)
6464
*/
6565
public function process(RouteInfo $route_info = null, $route_data = [])
6666
{
67-
$result = parent::process($route_info);
68-
if ($result instanceof Response) {
69-
return $result;
70-
}
7167
try {
7268
$contact_form_controller = $this->getContainer()->make(ContactFormsController::class);
7369
$this->form = $contact_form_controller->getForm();

app/site/controllers/Admin/Json/SitemapCallback.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ public function emptyForm(FAPI\Form $form, &$form_state)
6464
*/
6565
public function process(RouteInfo $route_info = null, $route_data = [])
6666
{
67-
$result = parent::process($route_info);
68-
if ($result instanceof Response) {
69-
return $result;
70-
}
7167
try {
7268
$sitemap_controller = $this->getContainer()->make(SitemapsController::class);
7369
$this->form = $sitemap_controller->getForm();

app/site/controllers/Frontend/ContactForm.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use \App\Base\Abstracts\FormPage;
1717
use \App\App;
1818
use \App\Base\Abstracts\Model;
19+
use \App\Base\Abstracts\FrontendPage;
1920
use \App\Site\Models\Contact;
2021
use \App\Site\Models\ContactSubmission;
2122
use \App\Site\Routing\RouteInfo;
@@ -31,6 +32,20 @@ class ContactForm extends FormPage // and and is similar to FrontendPageWithObje
3132
{
3233
use FrontendTrait;
3334

35+
/**
36+
* {@inheritdocs}
37+
*
38+
* @param ContainerInterface $container
39+
*/
40+
public function __construct(ContainerInterface $container)
41+
{
42+
// construct must be override in order to skip
43+
// form construction
44+
45+
FrontendPage::__construct($container);
46+
}
47+
48+
3449
/**
3550
* {@inheritdocs}
3651
*
@@ -71,6 +86,14 @@ public function process(RouteInfo $route_info = null, $route_data = [])
7186
return $this->getUtils()->errorPage(404);
7287
}
7388

89+
$this->templateData += [
90+
'form' => FAPI\FormBuilder::getForm([$this, 'getFormDefinition'])
91+
->setValidate([ [$this, 'formValidate'] ])
92+
->setSubmit([ [$this, 'formSubmitted'] ]),
93+
];
94+
95+
$this->processFormSubmit();
96+
7497
return parent::process($route_info);
7598
}
7699

@@ -95,7 +118,7 @@ protected function getBaseTemplateData()
95118
*/
96119
public function getFormDefinition(FAPI\Form $form, &$form_state)
97120
{
98-
$contact = $this->templateData['object'];
121+
$contact = $this->templateData['object'] ?? null;
99122
if ($contact instanceof Contact && $contact->isLoaded()) {
100123
$form->setId($contact->getName());
101124
$form->addField(

0 commit comments

Comments
 (0)