akaunting/app/Http/ViewComposers/Suggestions.php

58 lines
1.4 KiB
PHP
Raw Normal View History

2018-05-25 16:57:58 +03:00
<?php
namespace App\Http\ViewComposers;
use Illuminate\View\View;
use App\Traits\Modules as RemoteModules;
2018-05-25 16:57:58 +03:00
use Route;
2018-05-25 17:45:41 +03:00
use App\Models\Module\Module;
2018-05-25 16:57:58 +03:00
class Suggestions
{
use RemoteModules;
2018-05-25 16:57:58 +03:00
/**
* 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() || !config('app.installed')) {
return;
}
$modules = false;
2018-05-25 16:57:58 +03:00
2019-12-26 10:53:50 +03:00
if (user()) {
$path = Route::current()->uri();
if ($path) {
$suggestions = $this->getSuggestions($path);
2019-12-26 10:53:50 +03:00
if ($suggestions) {
$suggestion_modules = $suggestions->modules;
2019-12-26 10:53:50 +03:00
foreach ($suggestion_modules as $key => $module) {
$installed = Module::where('company_id', session('company_id'))->where('alias', $module->alias)->first();
if ($installed) {
unset($suggestion_modules[$key]);
}
2018-05-25 16:57:58 +03:00
}
2019-12-26 10:53:50 +03:00
if ($suggestion_modules) {
shuffle($suggestion_modules);
2018-05-25 16:57:58 +03:00
2019-12-26 10:53:50 +03:00
$modules[] = $suggestion_modules[0];
}
2018-05-25 16:57:58 +03:00
}
}
}
$view->with(['suggestion_modules' => $modules]);
2018-05-25 16:57:58 +03:00
}
}