Skip to content

Commit

Permalink
Create alerts and make a alert for missing storage symlink show on da…
Browse files Browse the repository at this point in the history
…shboard.
  • Loading branch information
marktopper committed Jan 7, 2017
1 parent 39f4371 commit 8d50666
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 0 deletions.
13 changes: 13 additions & 0 deletions publishable/assets/css/voyager.css
Original file line number Diff line number Diff line change
Expand Up @@ -1414,4 +1414,17 @@ ul.checkbox label {
.show-res{
margin-left: 5px;
margin-top: 15px;
}

.alerts {
padding: 10px 10px 0px 10px;
}

.alerts .alert {
margin-right: 15px;
}

.alerts .alert>p,
.alerts .alert>ul {
margin-top: 0;
}
9 changes: 9 additions & 0 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

@section('content')
<div class="page-content">
<div class="alerts">
@foreach ($alerts as $alert)
<div class="alert alert-{{ $alert->type }} alert-name-{{ $alert->name }}">
@foreach($alert->components as $component)
<?php echo $component->render(); ?>
@endforeach
</div>
@endforeach
</div>
<div class="widgets">
<?php if (Illuminate\Support\Facades\Schema::hasTable(with(new TCG\Voyager\Models\User())->getTable())) { ?>
<div class="panel widget center bgimage" style="background-image:url({{ config('voyager.assets_path') }}/images/widget-backgrounds/02.png);">
Expand Down
41 changes: 41 additions & 0 deletions src/Alert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace TCG\Voyager;

use TCG\Voyager\Alert\Components\ComponentInterface;

class Alert
{
protected $components;

protected $name;
protected $type;

public function __construct($name, $type = 'default')
{
$this->name = $name;
$this->type = $type;
}

public function addComponent(ComponentInterface $component)
{
$this->components[] = $component;

return $this;
}

public function __get($name)
{
return $this->$name;
}

public function __call($name, $arguments)
{
$component = app('voyager.alert.components.'.$name, ['alert' => $this])
->setAlert($this);

call_user_func_array([$component, 'create'], $arguments);

return $this->addComponent($component);
}
}
22 changes: 22 additions & 0 deletions src/Alert/Components/AbstractComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace TCG\Voyager\Alert\Components;

use TCG\Voyager\Alert;

abstract class AbstractComponent implements ComponentInterface
{
protected $alert;

public function setAlert(Alert $alert)
{
$this->alert = $alert;

return $this;
}

public function __call($name, $arguments)
{
return call_user_func_array([$this->alert, $name], $arguments);
}
}
22 changes: 22 additions & 0 deletions src/Alert/Components/ButtonComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace TCG\Voyager\Alert\Components;

class ButtonComponent extends AbstractComponent
{
protected $text;
protected $link;
protected $style;

public function create($text, $link = '#', $style = 'default')
{
$this->text = $text;
$this->link = $link;
$this->style = $style;
}

public function render()
{
return "<a href='{$this->link}' class='btn btn-{$this->style}'>{$this->text}</a>";
}
}
8 changes: 8 additions & 0 deletions src/Alert/Components/ComponentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TCG\Voyager\Alert\Components;

interface ComponentInterface
{
public function render();
}
18 changes: 18 additions & 0 deletions src/Alert/Components/TextComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace TCG\Voyager\Alert\Components;

class TextComponent extends AbstractComponent
{
protected $text;

public function create($text)
{
$this->text = $text;
}

public function render()
{
return "<p>{$this->text}</p>";
}
}
18 changes: 18 additions & 0 deletions src/Alert/Components/TitleComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace TCG\Voyager\Alert\Components;

class TitleComponent extends AbstractComponent
{
protected $title;

public function create($title)
{
$this->title = $title;
}

public function render()
{
return "<strong>{$this->title}</strong>";
}
}
12 changes: 12 additions & 0 deletions src/Voyager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Voyager
protected $version;
protected $filesystem;

protected $alerts = [];

public function __construct()
{
$this->filesystem = app(Filesystem::class);
Expand Down Expand Up @@ -78,6 +80,16 @@ public function getVersion()
return $this->version;
}

public function addAlert(Alert $alert)
{
$this->alerts[] = $alert;
}

public function alerts()
{
return $this->alerts;
}

protected function findVersion()
{
if (!is_null($this->version)) {
Expand Down
46 changes: 46 additions & 0 deletions src/VoyagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\AliasLoader;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Intervention\Image\ImageServiceProvider;
use TCG\Voyager\Facades\Voyager as VoyagerFacade;
use TCG\Voyager\Http\Middleware\VoyagerAdminMiddleware;
Expand All @@ -28,6 +29,9 @@ public function register()
return new Voyager();
});

$this->registerViewComposers();
$this->registerAlertComponents();

if ($this->app->runningInConsole()) {
$this->registerPublishableResources();
$this->registerConsoleCommands();
Expand Down Expand Up @@ -57,6 +61,48 @@ public function boot(Router $router)
$this->loadViewsFrom(__DIR__.'/../resources/views', 'voyager');

$router->middleware('admin.user', VoyagerAdminMiddleware::class);

$this->addStorageSymlinkAlert();
}

/**
* Register view composers.
*/
protected function registerViewComposers()
{
// Register alerts
View::composer('voyager::index', function ($view) {
$view->with('alerts', VoyagerFacade::alerts());
});
}

/**
* Add storage symlink alert.
*/
protected function addStorageSymlinkAlert()
{
if (!is_link(public_path('storage-off'))) {
$alert = (new Alert('missing-storage-symlink', 'warning'))
->title('Missing storage symlink')
->text('We could not find a storage symlink. This could cause problems with loading media files from the browser.')
->button('Fix it', '?fix-missing-storage-symlink');

VoyagerFacade::addAlert($alert);
}
}

/**
* Register alert components.
*/
protected function registerAlertComponents()
{
$components = ['title', 'text', 'button'];

foreach ($components as $component) {
$class = 'TCG\\Voyager\\Alert\\Components\\'.ucfirst(camel_case($component)).'Component';

$this->app->bind("voyager.alert.components.{$component}", $class);
}
}

/**
Expand Down

0 comments on commit 8d50666

Please sign in to comment.