From b10557817eac967b6f8d9b6593baa5ba7c8ccd60 Mon Sep 17 00:00:00 2001 From: itskiprotich Date: Wed, 7 Aug 2024 09:40:43 +0300 Subject: [PATCH] UPD validation message --- src/Controller/UsersController.php | 19 ++++-- src/View/AppView.php | 8 ++- src/View/Helper/ValidationErrorsHelper.php | 79 ++++++++++++++++++++++ templates/element/flash/error.php | 47 ++----------- 4 files changed, 101 insertions(+), 52 deletions(-) create mode 100644 src/View/Helper/ValidationErrorsHelper.php diff --git a/src/Controller/UsersController.php b/src/Controller/UsersController.php index 57e85a4514..ccc8b40aa8 100755 --- a/src/Controller/UsersController.php +++ b/src/Controller/UsersController.php @@ -144,12 +144,21 @@ public function login() } // Attempt to identify the user $user = $this->Auth->identify(); - // dd($data); - // dd($user); + if ($user) { $this->Auth->setUser($user); + if ($this->Auth->User('is_active') == 0) { + $this->Flash->error('Your account is not activated! If you have just registered, please click the activation link + sent to your email. Remember to check you spam folder too!'); + $this->redirect($this->Auth->logout()); + } elseif ($this->Auth->User('deactivated') == 1) { + $this->Flash->error('Your account has been deactivated! Please contact PPB.'); + $this->redirect($this->Auth->logout()); + } + // User is authenticated, handle redirect based on user group + switch ($user['role_id']) { case '1': return $this->redirect(['controller' => 'Users', 'action' => 'dashboard', 'prefix' => 'Admin']); @@ -335,11 +344,7 @@ public function register() $this->QueuedJobs->createJob('GenericNotification', $datum); $this->QueuedJobs->createJob('GenericEmail', $datum); - } - - // $this->queuedJobs->createJob('GenericNotification', ['email' => $user->email]); - - + } return $this->redirect(['controller' => 'Pages', 'action' => 'home']); } else { $errorMessages = []; diff --git a/src/View/AppView.php b/src/View/AppView.php index 9c69a0285e..ab16263571 100755 --- a/src/View/AppView.php +++ b/src/View/AppView.php @@ -1,4 +1,5 @@ '{{label}}
{{input}}{{between}}{{after}}
' // ] // ]); - } + } // $this->loadHelper('Util'); - $this->loadHelper('Text'); + $this->loadHelper('Text'); $this->loadHelper('Paginator', ['templates' => 'paginator-templates']); + + $this->loadHelper('ValidationErrors'); } } diff --git a/src/View/Helper/ValidationErrorsHelper.php b/src/View/Helper/ValidationErrorsHelper.php new file mode 100644 index 0000000000..2d93adac07 --- /dev/null +++ b/src/View/Helper/ValidationErrorsHelper.php @@ -0,0 +1,79 @@ + $value) { + if (is_array($value)) { + $result = array_merge($result, $this->array_flatten($value)); + } else { + $result[$key] = $value; + } + } + return $result; + } + + public function flatten_array($arg) { + return is_array($arg) ? array_reduce($arg, function ($c, $a) { return array_merge($c, $this->flatten_array($a)); }, []) : [$arg]; + } + + public function flatten($array, $prefix = '') { + $result = []; + foreach ($array as $key => $value) { + if (is_array($value)) { + $result += $this->flatten($value, $prefix . $key . '.'); + } else { + $result[$prefix . $key] = $value; + } + } + return $result; + } + + public function displayErrors($entity) + { + if (empty($errors)) { + return ''; + } + + $myarr = $this->flatten($errors); + $output = ''; + + return $output; + } +} diff --git a/templates/element/flash/error.php b/templates/element/flash/error.php index 2c45633d57..879c7c5173 100755 --- a/templates/element/flash/error.php +++ b/templates/element/flash/error.php @@ -1,48 +1,9 @@
-
+
-
-

- $value) { - if (is_array($value)) { - $result = array_merge($result, array_flatten($value)); - } - else { - $result[$key] = $value; - } - } - return $result; - } - - // pr(array_flatten($this->validationErrors)); - $myarr = array_flatten($this->validationErrors); + - if(isset($myarr[0])) echo ' '.$myarr[0].'
'; - if(isset($myarr[1])) echo ' '.$myarr[1].'
'; - if(isset($myarr[2])) echo ' '.$myarr[2].'
'; - if(count($myarr) > 2) { - ?> - - more... - -

- '.$myarr[$i].'
'; } - } - ?> -
- -

-
+ \ No newline at end of file