Skip to content

Commit 0c63fb8

Browse files
committed
added user logged in area + code review / fixes
1 parent c44af1f commit 0c63fb8

File tree

33 files changed

+1321
-232
lines changed

33 files changed

+1321
-232
lines changed

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ DISABLE_CACHE=0
2121
ENABLE_FPC=0
2222

2323
#-- Other Info
24+
LOGGEDPAGES_GROUP=users
2425
DEBUG=1
2526
GTMID=GTM-XXXX
2627

app/App.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use \App\Site\Routing\RouteInfo;
2424
use \App\Base\Exceptions\OfflineException;
2525
use \App\Base\Exceptions\BlockedIpException;
26+
use \App\Base\Exceptions\NotFoundException;
27+
use \App\Base\Exceptions\NotAllowedException;
2628
use \Exception;
2729

2830
/**
@@ -184,12 +186,11 @@ public function bootstrap()
184186
switch ($routeInfo->getStatus()) {
185187
case Dispatcher::NOT_FOUND:
186188
// ... 404 Not Found
187-
$this->getUtils()->errorPage(404, $request)->send();
189+
throw new NotFoundException();
188190
break;
189191
case Dispatcher::METHOD_NOT_ALLOWED:
190-
$allowedMethods = $this->getRouteInfo()->getAllowedMethods();
191192
// ... 405 Method Not Allowed
192-
$this->getUtils()->errorPage(405, $request, ['allowedMethods' => $allowedMethods])->send();
193+
throw new NotAllowedException();
193194
break;
194195
case Dispatcher::FOUND:
195196
$handler = $this->getRouteInfo()->getHandler();
@@ -225,6 +226,11 @@ public function bootstrap()
225226
$response = $this->getUtils()->offlinePage($request);
226227
} catch (BlockedIpException $e) {
227228
$response = $this->getUtils()->blockedIpPage($request);
229+
} catch (NotFoundException $e) {
230+
$response = $this->getUtils()->errorPage(404, $request);
231+
} catch (NotAllowedException $e) {
232+
$allowedMethods = $this->getRouteInfo()->getAllowedMethods();
233+
$this->getUtils()->errorPage(405, $request, ['allowedMethods' => $allowedMethods])->send();
228234
} catch (Exception $e) {
229235
$response = $this->getUtils()->exceptionPage($e, $request);
230236
}

app/base/abstracts/Controllers/AdminFormPage.php

Lines changed: 3 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
use \Symfony\Component\HttpFoundation\Response;
1717
use \Degami\PHPFormsApi as FAPI;
1818
use \App\App;
19+
use \App\Base\Traits\FormPageTrait;
1920

2021
/**
2122
* Base for admin form page
2223
*/
2324
abstract class AdminFormPage extends AdminPage
2425
{
26+
use FormPageTrait;
27+
2528
/**
2629
* {@inheritdocs}
2730
*
@@ -40,17 +43,6 @@ public function __construct(ContainerInterface $container, Request $request = nu
4043
$this->processFormSubmit();
4144
}
4245

43-
/**
44-
* gets form id
45-
*
46-
* @return string
47-
*/
48-
protected function getFormId()
49-
{
50-
$arr = explode("\\", strtolower(get_class($this)));
51-
return array_pop($arr);
52-
}
53-
5446
/**
5547
* process form submission
5648
*
@@ -65,122 +57,4 @@ private function processFormSubmit()
6557
$this->templateData['form']->process();
6658
}
6759
}
68-
69-
/**
70-
* check if form is submitted
71-
*/
72-
protected function isSubmitted()
73-
{
74-
return ($this->templateData['form'] && $this->templateData['form']->isSubmitted());
75-
}
76-
77-
/**
78-
* {@inheritdocs}
79-
*
80-
* @return Response|self
81-
*/
82-
protected function beforeRender()
83-
{
84-
if ($this->isSubmitted()) {
85-
$this->getApp()->event('form_submitted', ['form' => $this->templateData['form']]);
86-
return $this->templateData['form']->getSubmitResults(get_class($this).'::formSubmitted');
87-
}
88-
return parent::beforeRender();
89-
}
90-
91-
/**
92-
* get form object
93-
*
94-
* @return FAPI\Form
95-
*/
96-
public function getForm()
97-
{
98-
return $this->getTemplateData()['form'];
99-
}
100-
101-
/**
102-
* gets a form for confirmation
103-
*
104-
* @param string $confirm_message
105-
* @param FAPI\Form $form
106-
* @return FAPI\Form
107-
*/
108-
protected function fillConfirmationForm($confirm_message, $form)
109-
{
110-
$form->addField(
111-
'confirm',
112-
[
113-
'type' => 'markup',
114-
'value' => $this->getUtils()->translate($confirm_message, $this->getCurrentLocale()),
115-
'suffix' => '<br /><br />',
116-
]
117-
)
118-
->addMarkup('<a class="btn btn-danger btn-sm" href="'.$this->getControllerUrl().'">'.$this->getUtils()->translate('Cancel', $this->getCurrentLocale()).'</a>');
119-
$this->addSubmitButton($form, true);
120-
return $form;
121-
}
122-
123-
/**
124-
* adds submit button to form
125-
*
126-
* @param FAPI\Form $form
127-
* @param boolean $inline_button
128-
* @return FAPI\Form
129-
*/
130-
protected function addSubmitButton(FAPI\Form $form, $inline_button = false)
131-
{
132-
if ($inline_button) {
133-
$form
134-
->addField(
135-
'button',
136-
[
137-
'type' => 'submit',
138-
'container_tag' => null,
139-
'prefix' => '&nbsp;',
140-
'value' => 'Ok',
141-
'attributes' => ['class' => 'btn btn-primary btn-sm'],
142-
]
143-
);
144-
} else {
145-
$form
146-
->addField(
147-
'button',
148-
[
149-
'type' => 'submit',
150-
'value' => 'Ok',
151-
'container_class' => 'form-item mt-3',
152-
'attributes' => ['class' => 'btn btn-primary btn-lg btn-block'],
153-
]
154-
);
155-
}
156-
157-
return $form;
158-
}
159-
160-
/**
161-
* gets form definition object
162-
*
163-
* @param FAPI\Form $form
164-
* @param array &$form_state
165-
* @return FAPI\Form
166-
*/
167-
abstract public function getFormDefinition(FAPI\Form $form, &$form_state);
168-
169-
/**
170-
* validates form submission
171-
*
172-
* @param FAPI\Form $form
173-
* @param array &$form_state
174-
* @return boolean|string
175-
*/
176-
abstract public function formValidate(FAPI\Form $form, &$form_state);
177-
178-
/**
179-
* handles form submission
180-
*
181-
* @param FAPI\Form $form
182-
* @param array &$form_state
183-
* @return mixed|Response
184-
*/
185-
abstract public function formSubmitted(FAPI\Form $form, &$form_state);
18660
}

app/base/abstracts/Controllers/BasePage.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function renderPage(RouteInfo $route_info = null, $route_data = [])
118118
protected function checkPermission($permission_name)
119119
{
120120
try {
121-
return in_array($permission_name, $this->getCurrentUser()->permissions);
121+
return $this->getCurrentUser() && $this->getCurrentUser()->checkPermission($permission_name);
122122
} catch (\Exception $e) {
123123
$this->getUtils()->logException($e);
124124
}
@@ -132,13 +132,18 @@ protected function checkPermission($permission_name)
132132
*
133133
* @return array|null
134134
*/
135-
protected function getRouteData()
135+
protected function getRouteData($varname = null)
136136
{
137137
if (is_null($this->route_info)) {
138138
return null;
139139
}
140140

141-
return $this->getRouteInfo()->getVars();
141+
if ($varname == null) {
142+
return $this->getRouteInfo()->getVars();
143+
}
144+
145+
$vars = $this->getRouteInfo()->getVars();
146+
return is_array($vars) && isset($vars[$varname]) ? $vars[$varname] : null;
142147
}
143148

144149
/**
@@ -239,15 +244,20 @@ public function getControllerUrl()
239244
$path = str_replace("app/site/controllers/", "", str_replace("\\", "/", strtolower(get_class($this))));
240245
if (method_exists(static::class, 'getRoutePath')) {
241246
$path = call_user_func([static::class, 'getRoutePath']);
242-
}
247+
if (method_exists(static::class, 'getRouteGroup')) {
248+
$path = call_user_func([static::class, 'getRouteGroup']).'/'.$path;
249+
}
243250

244-
$route_vars = [];
245-
if ($this->getRouteInfo() instanceof RouteInfo) {
246-
$route_vars = $this->getRouteInfo()->getVars();
247-
}
251+
$route_vars = [];
252+
if ($this->getRouteInfo() instanceof RouteInfo) {
253+
$route_vars = $this->getRouteInfo()->getVars();
254+
}
255+
256+
foreach ($route_vars as $varname => $value) {
257+
$path = preg_replace("/\{".$varname."(:.*?)?\}/", $value, $path);
258+
}
248259

249-
foreach ($route_vars as $varname => $value) {
250-
$path = preg_replace("/\{".$varname."(:.*?)?\}/", $value, $path);
260+
return $this->getRouting()->getBaseUrl().$path;
251261
}
252262

253263
$routename = str_replace("/", ".", trim($path, "/"));

app/base/abstracts/Controllers/FormPage.php

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
use \App\Site\Routing\RouteInfo;
1818
use \Degami\PHPFormsApi as FAPI;
1919
use \App\App;
20+
use \App\Base\Traits\FormPageTrait;
2021

2122
/**
2223
* Base frontend page for displaying a form
2324
*/
2425
abstract class FormPage extends FrontendPage
2526
{
26-
/**
27-
* @var array template data
28-
*/
29-
protected $templateData = [];
27+
use FormPageTrait;
3028

3129
/**
3230
* {@inheritdocs}
@@ -56,63 +54,4 @@ protected function processFormSubmit()
5654
$this->getApp()->event('before_form_process', ['form' => $this->getForm()]);
5755
$this->getForm()->process();
5856
}
59-
60-
/**
61-
* gets form object
62-
*
63-
* @return FAPI\Form|null
64-
*/
65-
protected function getForm()
66-
{
67-
return $this->templateData['form'] ?? null;
68-
}
69-
70-
/**
71-
* {@intheritdocs}
72-
*
73-
* @return Response|self
74-
*/
75-
protected function beforeRender()
76-
{
77-
if ($this->getForm() && $this->getForm()->isSubmitted()) {
78-
$this->getApp()->event('form_submitted', ['form' => $this->getForm()]);
79-
return $this->getForm()->getSubmitResults(get_class($this).'::formSubmitted');
80-
}
81-
return parent::beforeRender();
82-
}
83-
84-
/**
85-
* check if form is submitted
86-
*/
87-
protected function isSubmitted()
88-
{
89-
return ($this->getForm() && $this->getForm()->isSubmitted());
90-
}
91-
92-
/**
93-
* gets form definition object
94-
*
95-
* @param FAPI\Form $form
96-
* @param array &$form_state
97-
* @return FAPI\Form
98-
*/
99-
abstract public function getFormDefinition(FAPI\Form $form, &$form_state);
100-
101-
/**
102-
* validates form submission
103-
*
104-
* @param FAPI\Form $form
105-
* @param array &$form_state
106-
* @return boolean|string
107-
*/
108-
abstract public function formValidate(FAPI\Form $form, &$form_state);
109-
110-
/**
111-
* handles form submission
112-
*
113-
* @param FAPI\Form $form
114-
* @param array &$form_state
115-
* @return mixed|Response
116-
*/
117-
abstract public function formSubmitted(FAPI\Form $form, &$form_state);
11857
}

app/base/abstracts/Controllers/FrontendPage.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ public function getCurrentLocale()
244244
$rewrite = $this->getContainer()->make(\App\Site\Models\Rewrite::class, ['dbrow' => $rewrite]);
245245
$this->locale = $rewrite->locale;
246246
}
247+
248+
if ($this->locale == null && $this->getRouteData('locale') != null) {
249+
$this->locale = $this->getCurrentUser()->getLocale();
250+
}
251+
252+
if ($this->locale == null && $this->getCurrentUser()) {
253+
$this->locale = $this->getCurrentUser()->getLocale();
254+
}
255+
256+
if ($this->locale == null) {
257+
$this->locale = $this->getSiteData()->getDefaultLocale();
258+
}
247259
}
248260
$this->getApp()->setCurrentLocale($this->locale);
249261
return $this->locale;

0 commit comments

Comments
 (0)