Skip to content

Commit

Permalink
added automatic load notification from laravel session (errors,warnin…
Browse files Browse the repository at this point in the history
…gs...) + fixed flashed notifications
  • Loading branch information
leopado committed Apr 7, 2021
1 parent 123d0d2 commit 253687f
Showing 1 changed file with 100 additions and 8 deletions.
108 changes: 100 additions & 8 deletions src/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,85 @@ public function __construct(SessionManager $session)
} else {
$this->flashedNotifications = [];
}
$this->setNotifyFromSession();
}

/**
* Set all notification that are present in session
*
* @return void
*/
public function setNotifyFromSession(): void
{
$this->setNotifyErrorFromSession();
$this->setNotifyWarningFromSession();
$this->setNotifySuccessFromSession();
$this->setNotifyInfoFromSession();
}

/**
*/
protected function setNotifyErrorFromSession(): void
{
$this->setNotifyFromSessionByLabel('errors');
}

/**
*/
protected function setNotifyWarningFromSession(): void
{
$this->setNotifyFromSessionByLabel('warnings');
}

/**
*/
protected function setNotifyInfoFromSession(): void
{
$this->setNotifyFromSessionByLabel('info');
}

/**
*/
protected function setNotifySuccessFromSession(): void
{
$this->setNotifyFromSessionByLabel('success');
}
/**
* Set all notification of a label type that are present in session
*
* @param string $label
*
* @return void
*/
protected function setNotifyFromSessionByLabel(string $label): void
{

if (!$this->session->has($label)) {
return;
}

if ($label == 'errors') {
$errors = $this->session->get('errors')->all();
foreach ($errors as $error) {
$this->error($error,false);
}

return;
}


$messages = session()->get($label);
if ($label=='warnings'){
$label='warning';
}
if (!is_array($messages)) {
$this->$label($messages,false);
return;
}

foreach ($messages as $msg) {
$this->$label($msg,false);
}
}

/**
Expand Down Expand Up @@ -155,13 +234,16 @@ public function addForNextRequest($theme, $timeout, $type, $layout, $text, $soun
* @param string $title The notification's title
* @param array $options
*/
public function info($text, $onlyNextRequest = true, array $options = [])
public function info($text, $onlyNextRequest = false, array $options = [])
{
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : false;
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';

$this->add($theme, $timeout, 'info', $layout, $text, null, null, $onlyNextRequest);
if ($onlyNextRequest){
$this->addForNextRequest($theme, $timeout, 'info', $layout, $text, null, null);
return;
}
$this->add($theme, $timeout, 'info', $layout, $text, null, null);
}

/**
Expand All @@ -171,12 +253,15 @@ public function info($text, $onlyNextRequest = true, array $options = [])
* @param string $title The notification's title
* @param array $options
*/
public function error($text, $onlyNextRequest = true, array $options = [])
public function error($text, $onlyNextRequest = false, array $options = [])
{
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';

if ($onlyNextRequest){
$this->addForNextRequest($theme, $timeout, 'error', $layout, $text, null, null);
return;
}
$this->add($theme, $timeout, 'error', $layout, $text, null, null, $onlyNextRequest);
}

Expand All @@ -187,12 +272,15 @@ public function error($text, $onlyNextRequest = true, array $options = [])
* @param string $title The notification's title
* @param array $options
*/
public function warning($text, $onlyNextRequest = true, array $options = [])
public function warning($text, $onlyNextRequest = false, array $options = [])
{
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';

if ($onlyNextRequest){
$this->addForNextRequest($theme, $timeout, 'warning', $layout, $text, null, null);
return;
}
$this->add($theme, $timeout, 'warning', $layout, $text, null, null, $onlyNextRequest);
}

Expand All @@ -203,11 +291,15 @@ public function warning($text, $onlyNextRequest = true, array $options = [])
* @param string $title The notification's title
* @param array $options
*/
public function success($text, $onlyNextRequest = true, array $options = [])
public function success($text, $onlyNextRequest = false, array $options = [])
{
$theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
$timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
$layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
if ($onlyNextRequest){
$this->addForNextRequest($theme, $timeout, 'success', $layout, $text, null, null);
return;
}
$this->add($theme, $timeout, 'success', $layout, $text, null, null, $onlyNextRequest);
}

Expand Down

0 comments on commit 253687f

Please sign in to comment.