Added Suggestion module on pages
This commit is contained in:
parent
2b9721195d
commit
272b46e8a9
@ -5,9 +5,12 @@ namespace App\Http\ViewComposers;
|
|||||||
use Auth;
|
use Auth;
|
||||||
use App\Utilities\Updater;
|
use App\Utilities\Updater;
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
|
use App\Traits\Modules;
|
||||||
|
|
||||||
class Header
|
class Header
|
||||||
{
|
{
|
||||||
|
use Modules;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bind data to the view.
|
* Bind data to the view.
|
||||||
*
|
*
|
||||||
@ -57,6 +60,8 @@ class Header
|
|||||||
|
|
||||||
$updates = count(Updater::all());
|
$updates = count(Updater::all());
|
||||||
|
|
||||||
|
$this->loadSuggestions();
|
||||||
|
|
||||||
$view->with([
|
$view->with([
|
||||||
'user' => $user,
|
'user' => $user,
|
||||||
'notifications' => $notifications,
|
'notifications' => $notifications,
|
||||||
|
48
app/Http/ViewComposers/Suggestions.php
Normal file
48
app/Http/ViewComposers/Suggestions.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\ViewComposers;
|
||||||
|
|
||||||
|
use Illuminate\View\View;
|
||||||
|
use App\Traits\Modules;
|
||||||
|
use Route;
|
||||||
|
use Module;
|
||||||
|
|
||||||
|
class Suggestions
|
||||||
|
{
|
||||||
|
use Modules;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind data to the view.
|
||||||
|
*
|
||||||
|
* @param View $view
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function compose(View $view)
|
||||||
|
{
|
||||||
|
$suggestion_module = false;
|
||||||
|
|
||||||
|
$path = Route::current()->uri();
|
||||||
|
|
||||||
|
if ($path) {
|
||||||
|
$suggestions = $this->getSuggestions($path);
|
||||||
|
|
||||||
|
if ($suggestions) {
|
||||||
|
$suggestion_modules = $suggestions->modules;
|
||||||
|
|
||||||
|
foreach ($suggestion_modules as $key => $module) {
|
||||||
|
if (Module::findByAlias($module->alias)) {
|
||||||
|
unset($suggestion_modules[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($suggestion_modules) {
|
||||||
|
shuffle($suggestion_modules);
|
||||||
|
|
||||||
|
$suggestion_module[] = $suggestion_modules[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$view->with(['suggestion_modules' => $suggestion_module]);
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,11 @@ class ViewComposerServiceProvider extends ServiceProvider
|
|||||||
'*', 'App\Http\ViewComposers\All'
|
'*', 'App\Http\ViewComposers\All'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Suggestions
|
||||||
|
View::composer(
|
||||||
|
'*', 'App\Http\ViewComposers\Suggestions'
|
||||||
|
);
|
||||||
|
|
||||||
// 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'
|
||||||
|
@ -9,6 +9,8 @@ use GuzzleHttp\Client;
|
|||||||
use GuzzleHttp\Exception\RequestException;
|
use GuzzleHttp\Exception\RequestException;
|
||||||
use Module;
|
use Module;
|
||||||
use ZipArchive;
|
use ZipArchive;
|
||||||
|
use Cache;
|
||||||
|
use Date;
|
||||||
|
|
||||||
trait Modules
|
trait Modules
|
||||||
{
|
{
|
||||||
@ -311,6 +313,53 @@ trait Modules
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function loadSuggestions()
|
||||||
|
{
|
||||||
|
// Get data from cache
|
||||||
|
$data = Cache::get('suggestions');
|
||||||
|
|
||||||
|
if (!empty($data)) {
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
$url = 'apps/suggestions';
|
||||||
|
|
||||||
|
$response = $this->getRemote($url, 'GET', ['timeout' => 30, 'referer' => true]);
|
||||||
|
|
||||||
|
// Bad response
|
||||||
|
if ($response->getStatusCode() != 200) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$suggestions = json_decode($response->getBody())->data;
|
||||||
|
|
||||||
|
foreach ($suggestions as $suggestion) {
|
||||||
|
$data[$suggestion->path] = $suggestion;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache::put('suggestions', $data, Date::now()->addHour(6));
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSuggestions($path)
|
||||||
|
{
|
||||||
|
// Get data from cache
|
||||||
|
$data = Cache::get('suggestions');
|
||||||
|
|
||||||
|
if (empty($data)) {
|
||||||
|
$data = $this->loadSuggestions();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (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/';
|
||||||
|
@ -18,6 +18,7 @@ trait SiteApi
|
|||||||
'Authorization' => 'Bearer ' . setting('general.api_token'),
|
'Authorization' => 'Bearer ' . setting('general.api_token'),
|
||||||
'Accept' => 'application/json',
|
'Accept' => 'application/json',
|
||||||
'Referer' => env('APP_URL'),
|
'Referer' => env('APP_URL'),
|
||||||
|
'Akaunting' => version('short')
|
||||||
);
|
);
|
||||||
|
|
||||||
$data['http_errors'] = false;
|
$data['http_errors'] = false;
|
||||||
|
@ -21,6 +21,7 @@ class Updater
|
|||||||
Cache::forget('modules');
|
Cache::forget('modules');
|
||||||
Cache::forget('updates');
|
Cache::forget('updates');
|
||||||
Cache::forget('versions');
|
Cache::forget('versions');
|
||||||
|
Cache::forget('suggestions');
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user