Skip to content

Commit

Permalink
Added Suggestion module on pages
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed May 25, 2018
1 parent 2b97211 commit 272b46e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/Http/ViewComposers/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Auth;
use App\Utilities\Updater;
use Illuminate\View\View;
use App\Traits\Modules;

class Header
{
use Modules;

/**
* Bind data to the view.
*
Expand Down Expand Up @@ -57,6 +60,8 @@ public function compose(View $view)

$updates = count(Updater::all());

$this->loadSuggestions();

$view->with([
'user' => $user,
'notifications' => $notifications,
Expand Down
48 changes: 48 additions & 0 deletions app/Http/ViewComposers/Suggestions.php
Original file line number Diff line number Diff line change
@@ -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]);
}
}
5 changes: 5 additions & 0 deletions app/Providers/ViewComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function boot()
'*', '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'
Expand Down
49 changes: 49 additions & 0 deletions app/Traits/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use GuzzleHttp\Exception\RequestException;
use Module;
use ZipArchive;
use Cache;
use Date;

trait Modules
{
Expand Down Expand Up @@ -311,6 +313,53 @@ public function disableModule($alias)
];
}

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/';
Expand Down
3 changes: 2 additions & 1 deletion app/Traits/SiteApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected static function getRemote($url, $data = array())
'Authorization' => 'Bearer ' . setting('general.api_token'),
'Accept' => 'application/json',
'Referer' => env('APP_URL'),
'Akaunting' => version('short')
);

$data['http_errors'] = false;
Expand All @@ -32,4 +33,4 @@ protected static function getRemote($url, $data = array())

return $result;
}
}
}
1 change: 1 addition & 0 deletions app/Utilities/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static function clear()
Cache::forget('modules');
Cache::forget('updates');
Cache::forget('versions');
Cache::forget('suggestions');

return true;
}
Expand Down

0 comments on commit 272b46e

Please sign in to comment.