Skip to content

Commit

Permalink
UPD validation message
Browse files Browse the repository at this point in the history
  • Loading branch information
Itskiprotich committed Aug 7, 2024
1 parent 1fa6028 commit b105578
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 52 deletions.
19 changes: 12 additions & 7 deletions src/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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 = [];
Expand Down
8 changes: 6 additions & 2 deletions src/View/AppView.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

/**
Expand All @@ -13,6 +14,7 @@
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace App\View;

use Cake\View\View;
Expand Down Expand Up @@ -47,10 +49,12 @@ public function initialize(): void
// 'checkboxContainer' => '{{label}}<div class="checkbox">{{input}}{{between}}{{after}}</div>'
// ]
// ]);
}
}
// $this->loadHelper('Util');

$this->loadHelper('Text');
$this->loadHelper('Text');
$this->loadHelper('Paginator', ['templates' => 'paginator-templates']);

$this->loadHelper('ValidationErrors');
}
}
79 changes: 79 additions & 0 deletions src/View/Helper/ValidationErrorsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\View\Helper;

use Cake\View\Helper;

class ValidationErrorsHelper extends Helper
{

public function array_flatten($array) {
if (!is_array($array)) {
return false;
}
$result = [];
foreach ($array as $key => $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 = '<div class="alert alert-danger alert-dismissible fade in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span></button>';

$i = 0;
foreach ($myarr as $value) {
$i++;
if ($i <= 3) {
$output .= '<i class="fa fa-exclamation-triangle"></i> ' . h($value) . '<br/>';
}
}

if (count($myarr) > 3) {
$output .= '<small class="accordion-toggle" data-toggle="collapse" data-target="#demo">
more...
</small>
<div id="demo" class="collapse">';
$i = 0;
foreach ($myarr as $value) {
$i++;
if ($i > 3) {
$output .= '<i class="fa fa-exclamation-triangle"></i> ' . h($value) . '<br/>';
}
}
$output .= '</div>';
}

$output .= '</div>';

return $output;
}
}
47 changes: 4 additions & 43 deletions templates/element/flash/error.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,9 @@
<div class="alert alert-block alert-error fade in">
<button data-dismiss="alert" class="close" type="button">&times;</button>
<h5 class="alert-heading">
<h5 class="alert-heading">
<?php
echo $message;
echo $message;
?>
</h5>
<p>
<?php
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $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);
</h5>

if(isset($myarr[0])) echo '<i class="icon-warning-sign"></i> '.$myarr[0].'<br/>';
if(isset($myarr[1])) echo '<i class="icon-warning-sign"></i> '.$myarr[1].'<br/>';
if(isset($myarr[2])) echo '<i class="icon-warning-sign"></i> '.$myarr[2].'<br/>';
if(count($myarr) > 2) {
?>
<small class="accordion-toggle" data-toggle="collapse" data-target="#demo">
more...
</small>
<div id="demo" class="collapse">
<?php
for ($i = 3; $i < count($myarr); $i++) {
if($i <= 12) { echo '<i class="icon-warning-sign"></i> '.$myarr[$i].'<br/>'; }
}
?>
</div>
<?php
}
?>
</p>
</div>
</div>

0 comments on commit b105578

Please sign in to comment.