2022-06-01 10:15:55 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\View\Components;
|
|
|
|
|
|
|
|
use App\Abstracts\View\Component;
|
|
|
|
use App\Traits\Modules;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
|
|
|
|
class Tips extends Component
|
|
|
|
{
|
|
|
|
use Modules;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public $position;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public $path;
|
|
|
|
|
|
|
|
/** @var objcet */
|
|
|
|
public $tips;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new component instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
string $position = 'relative',
|
|
|
|
string $path = null,
|
|
|
|
$tips = []
|
|
|
|
) {
|
|
|
|
$this->position = $position;
|
|
|
|
$this->path = $path;
|
|
|
|
$this->tips = collect();
|
|
|
|
|
|
|
|
$this->setTips($tips);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the view / contents that represent the component.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\View|string
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
switch ($this->position) {
|
|
|
|
case 'fixed':
|
|
|
|
$view = 'components.tips.fixed';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$view = 'components.tips.relative';
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:01:32 +03:00
|
|
|
if ($this->tips->count() > 1) {
|
|
|
|
$view = 'components.tips.relative';
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return view($view);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setTips($tips)
|
|
|
|
{
|
2022-11-02 11:53:49 +03:00
|
|
|
if (! empty($tips)) {
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->tips = $tips;
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:53:49 +03:00
|
|
|
if (! $path = Route::current()->uri()) {
|
2022-06-01 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = Str::replace('{company_id}/', '', $path);
|
|
|
|
|
2022-11-02 11:53:49 +03:00
|
|
|
if (! $tips = $this->getTips($path)) {
|
2022-06-01 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:01:32 +03:00
|
|
|
$rows = collect();
|
|
|
|
|
|
|
|
shuffle($tips);
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
foreach ($tips as $tip) {
|
|
|
|
if ($tip->position != $this->position) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:53:49 +03:00
|
|
|
if (! empty($tip->alias) && $this->moduleIsEnabled($tip->alias)) {
|
2022-06-01 10:15:55 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:53:49 +03:00
|
|
|
if (Str::contains($tip->action, '{company_id}')) {
|
|
|
|
$tip->action = Str::replace('{company_id}', company_id(), $tip->action);
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:01:32 +03:00
|
|
|
$rows->push($tip);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($rows->count()) {
|
|
|
|
$row = $rows->shuffle()->first();
|
|
|
|
|
|
|
|
$this->tips->push($row);
|
2022-06-01 10:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|