From 272b46e8a9b46936a4ad8fb5a471e725511ac104 Mon Sep 17 00:00:00 2001 From: cuneytsenturk Date: Fri, 25 May 2018 16:57:58 +0300 Subject: [PATCH] Added Suggestion module on pages --- app/Http/ViewComposers/Header.php | 5 ++ app/Http/ViewComposers/Suggestions.php | 48 ++++++++++++++++++ app/Providers/ViewComposerServiceProvider.php | 5 ++ app/Traits/Modules.php | 49 +++++++++++++++++++ app/Traits/SiteApi.php | 3 +- app/Utilities/Updater.php | 1 + 6 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 app/Http/ViewComposers/Suggestions.php diff --git a/app/Http/ViewComposers/Header.php b/app/Http/ViewComposers/Header.php index 656aee05b..e71bf86eb 100644 --- a/app/Http/ViewComposers/Header.php +++ b/app/Http/ViewComposers/Header.php @@ -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, diff --git a/app/Http/ViewComposers/Suggestions.php b/app/Http/ViewComposers/Suggestions.php new file mode 100644 index 000000000..6060d25f0 --- /dev/null +++ b/app/Http/ViewComposers/Suggestions.php @@ -0,0 +1,48 @@ +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]); + } +} diff --git a/app/Providers/ViewComposerServiceProvider.php b/app/Providers/ViewComposerServiceProvider.php index 18609e7a7..b9088c412 100644 --- a/app/Providers/ViewComposerServiceProvider.php +++ b/app/Providers/ViewComposerServiceProvider.php @@ -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' diff --git a/app/Traits/Modules.php b/app/Traits/Modules.php index 446f9267d..fb6b4eca3 100644 --- a/app/Traits/Modules.php +++ b/app/Traits/Modules.php @@ -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/'; diff --git a/app/Traits/SiteApi.php b/app/Traits/SiteApi.php index 1f178edd4..3a07eb060 100644 --- a/app/Traits/SiteApi.php +++ b/app/Traits/SiteApi.php @@ -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; } -} \ No newline at end of file +} diff --git a/app/Utilities/Updater.php b/app/Utilities/Updater.php index e0f6c220f..5bbd7fd5d 100644 --- a/app/Utilities/Updater.php +++ b/app/Utilities/Updater.php @@ -21,6 +21,7 @@ class Updater Cache::forget('modules'); Cache::forget('updates'); Cache::forget('versions'); + Cache::forget('suggestions'); return true; }