Merge pull request #356 from cuneytsenturk/master

Added Suggestion module on pages
This commit is contained in:
Cüneyt Şentürk 2018-05-25 17:10:54 +03:00 committed by GitHub
commit 806be29e0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 110 additions and 1 deletions

View File

@ -5,9 +5,12 @@ namespace App\Http\ViewComposers;
use Auth;
use App\Utilities\Updater;
use Illuminate\View\View;
use App\Traits\Modules;
class Header
{
use Modules;
/**
* Bind data to the view.
*
@ -57,6 +60,8 @@ class Header
$updates = count(Updater::all());
$this->loadSuggestions();
$view->with([
'user' => $user,
'notifications' => $notifications,

View 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]);
}
}

View File

@ -19,6 +19,11 @@ class ViewComposerServiceProvider extends ServiceProvider
'*', 'App\Http\ViewComposers\All'
);
// Suggestions
View::composer(
'*', 'App\Http\ViewComposers\Suggestions'
);
// Add company info to menu
View::composer(
['partials.admin.menu', 'partials.customer.menu'], 'App\Http\ViewComposers\Menu'

View File

@ -9,6 +9,8 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Module;
use ZipArchive;
use Cache;
use Date;
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())
{
$base = 'https://akaunting.com/api/';

View File

@ -18,6 +18,7 @@ trait SiteApi
'Authorization' => 'Bearer ' . setting('general.api_token'),
'Accept' => 'application/json',
'Referer' => env('APP_URL'),
'Akaunting' => version('short')
);
$data['http_errors'] = false;
@ -32,4 +33,4 @@ trait SiteApi
return $result;
}
}
}

View File

@ -21,6 +21,7 @@ class Updater
Cache::forget('modules');
Cache::forget('updates');
Cache::forget('versions');
Cache::forget('suggestions');
return true;
}