notification files
This commit is contained in:
parent
c08f54fcd3
commit
2f6980b5d3
43
app/Http/ViewComposers/Notifications.php
Normal file
43
app/Http/ViewComposers/Notifications.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\ViewComposers;
|
||||||
|
|
||||||
|
use Route;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
use App\Traits\Modules as RemoteModules;
|
||||||
|
|
||||||
|
class Notifications
|
||||||
|
{
|
||||||
|
use RemoteModules;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind data to the view.
|
||||||
|
*
|
||||||
|
* @param View $view
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function compose(View $view)
|
||||||
|
{
|
||||||
|
// No need to add suggestions in console
|
||||||
|
if (app()->runningInConsole() || !env('APP_INSTALLED')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = Route::current()->uri();
|
||||||
|
|
||||||
|
if (empty($path)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$notifications = $this->getNotifications($path);
|
||||||
|
|
||||||
|
if (empty($notifications)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push to a stack
|
||||||
|
foreach ($notifications as $notification) {
|
||||||
|
$view->getFactory()->startPush('content_content_start', $notification->message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,11 @@ class ViewComposerServiceProvider extends ServiceProvider
|
|||||||
['partials.admin.content'], 'App\Http\ViewComposers\Suggestions'
|
['partials.admin.content'], 'App\Http\ViewComposers\Suggestions'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Notifications
|
||||||
|
View::composer(
|
||||||
|
['partials.admin.content'], 'App\Http\ViewComposers\Notifications'
|
||||||
|
);
|
||||||
|
|
||||||
// Add company info to menu
|
// Add company info to menu
|
||||||
View::composer(
|
View::composer(
|
||||||
['partials.admin.menu', 'partials.customer.menu'], 'App\Http\ViewComposers\Menu'
|
['partials.admin.menu', 'partials.customer.menu'], 'App\Http\ViewComposers\Menu'
|
||||||
|
@ -438,6 +438,42 @@ trait Modules
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function loadNotifications()
|
||||||
|
{
|
||||||
|
// Get data from cache
|
||||||
|
$data = Cache::get('notifications');
|
||||||
|
|
||||||
|
if (!empty($data)) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
$url = 'apps/notifications';
|
||||||
|
|
||||||
|
$response = $this->getRemote($url, 'GET', ['timeout' => 30, 'referer' => true]);
|
||||||
|
|
||||||
|
// Exception
|
||||||
|
if ($response instanceof RequestException) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bad response
|
||||||
|
if (!$response || ($response->getStatusCode() != 200)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$notifications = json_decode($response->getBody())->data;
|
||||||
|
|
||||||
|
foreach ($notifications as $notification) {
|
||||||
|
$data[$notification->path][] = $notification;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache::put('notifications', $data, Date::now()->addHour(6));
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
public function getSuggestions($path)
|
public function getSuggestions($path)
|
||||||
{
|
{
|
||||||
// Get data from cache
|
// Get data from cache
|
||||||
@ -454,6 +490,22 @@ trait Modules
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getNotifications($path)
|
||||||
|
{
|
||||||
|
// Get data from cache
|
||||||
|
$data = Cache::get('notifications');
|
||||||
|
|
||||||
|
if (empty($data)) {
|
||||||
|
$data = $this->loadNotifications();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($data) && array_key_exists($path, $data)) {
|
||||||
|
return $data[$path];
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
protected function getRemote($path, $method = 'GET', $data = array())
|
protected function getRemote($path, $method = 'GET', $data = array())
|
||||||
{
|
{
|
||||||
$base = 'https://akaunting.com/api/';
|
$base = 'https://akaunting.com/api/';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user