forked from Sylius/Sylius
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Sylius#7785 from NeverResponse/hub
[Admin] Sylius new version notification widget
- Loading branch information
Showing
18 changed files
with
492 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Sylius/Bundle/AdminBundle/Controller/NotificationController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\AdminBundle\Controller; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\Psr7\Uri; | ||
use Http\Message\MessageFactory; | ||
use Psr\Http\Message\UriInterface; | ||
use Sylius\Bundle\CoreBundle\Application\Kernel; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @author Jan Góralski <jan.goralski@lakion.com> | ||
*/ | ||
class NotificationController | ||
{ | ||
/** | ||
* @var ClientInterface | ||
*/ | ||
protected $client; | ||
|
||
/** | ||
* @var MessageFactory | ||
*/ | ||
protected $messageFactory; | ||
|
||
/** | ||
* @var UriInterface | ||
*/ | ||
protected $hubUri; | ||
|
||
/** | ||
* @param ClientInterface $client | ||
* @param MessageFactory $messageFactory | ||
* @param string $hubUri | ||
*/ | ||
public function __construct(ClientInterface $client, MessageFactory $messageFactory, $hubUri) | ||
{ | ||
$this->client = $client; | ||
$this->messageFactory = $messageFactory; | ||
$this->hubUri = new Uri($hubUri); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function getVersionAction(Request $request) | ||
{ | ||
$content = [ | ||
'version' => Kernel::VERSION, | ||
'hostname' => $request->getHost(), | ||
'locale' => $request->getLocale(), | ||
'user_agent' => $request->headers->get('User-Agent'), | ||
]; | ||
|
||
$headers = ['Content-Type' => 'application/json']; | ||
|
||
$hubRequest = $this->messageFactory->createRequest( | ||
Request::METHOD_GET, | ||
$this->hubUri, | ||
$headers, | ||
json_encode($content) | ||
); | ||
|
||
try { | ||
$hubResponse = $this->client->send($hubRequest, ['verify' => false]); | ||
} catch (GuzzleException $exception) { | ||
return JsonResponse::create('', JsonResponse::HTTP_NO_CONTENT); | ||
} | ||
|
||
return JsonResponse::fromJsonString($hubResponse->getBody()->getContents()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Sylius/Bundle/AdminBundle/DependencyInjection/Configuration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\AdminBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
/** | ||
* @author Jan Góralski <jan.goralski@lakion.com> | ||
*/ | ||
final class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('sylius_admin'); | ||
|
||
$rootNode | ||
->children() | ||
->arrayNode('notifications') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
->booleanNode('enabled') | ||
->defaultTrue() | ||
->end() | ||
->integerNode('frequency') | ||
->defaultValue(60) | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/Sylius/Bundle/AdminBundle/Resources/private/js/sylius-notification.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
(function ($) { | ||
'use strict'; | ||
|
||
$.fn.extend({ | ||
notification: function () { | ||
var HUB_REQUEST_TIME = 'hub_request_time'; | ||
var LAST_HUB_SYLIUS_VERSION = 'last_sylius_version'; | ||
var SYLIUS_VERSION_DISMISSED = 'sylius_version_dismissed'; | ||
var MILISECONDS_MULTIPLIER = 1000; | ||
|
||
var notificationMenu = $('#sylius-version-notification'); | ||
var askFrequency = notificationMenu.attr('data-frequency') * MILISECONDS_MULTIPLIER; | ||
|
||
initializeWidget(); | ||
|
||
if (retrieve(HUB_REQUEST_TIME) == undefined || milisecondsSinceLastRequest() > askFrequency) { | ||
askVersion(); | ||
} | ||
|
||
$(notificationMenu).find('i[data-dismiss]').on('click', function () { | ||
store(SYLIUS_VERSION_DISMISSED, retrieve(LAST_HUB_SYLIUS_VERSION)); | ||
|
||
notify(false); | ||
}); | ||
|
||
function askVersion() { | ||
$.ajax({ | ||
type: "GET", | ||
url: notificationMenu.attr('data-url'), | ||
accept: "application/json", | ||
success: function (data) { | ||
if (undefined != data && data.version != retrieve(LAST_HUB_SYLIUS_VERSION)) { | ||
store(LAST_HUB_SYLIUS_VERSION, data.version.toString()); | ||
|
||
notify(true); | ||
} | ||
}, | ||
complete: function () { | ||
store(HUB_REQUEST_TIME, new Date().getTime().toString()); | ||
} | ||
}); | ||
} | ||
|
||
function initializeWidget() { | ||
if (undefined == retrieve(LAST_HUB_SYLIUS_VERSION)) { | ||
store(LAST_HUB_SYLIUS_VERSION, '0'); | ||
} | ||
if (undefined == retrieve(SYLIUS_VERSION_DISMISSED)) { | ||
store(SYLIUS_VERSION_DISMISSED, '0'); | ||
} | ||
|
||
if (retrieve(LAST_HUB_SYLIUS_VERSION) == retrieve(SYLIUS_VERSION_DISMISSED)) { | ||
notify(false); | ||
} else { | ||
notify(true); | ||
} | ||
} | ||
|
||
function notify(bool) { | ||
var notificationMenu = $('#sylius-version-notification'); | ||
|
||
if (true === bool) { | ||
$('#notifications').css('display', 'block'); | ||
$('#no-notifications').css('display', 'none'); | ||
notificationMenu.find('.bell.icon').removeClass('outline').addClass('yellow'); | ||
} else { | ||
$('#notifications').css('display', 'none'); | ||
$('#no-notifications').css('display', 'block'); | ||
notificationMenu.find('.bell.icon').removeClass('yellow').addClass('outline'); | ||
} | ||
} | ||
|
||
function milisecondsSinceLastRequest() { | ||
return new Date().getTime() - parseInt(retrieve(HUB_REQUEST_TIME)); | ||
} | ||
|
||
function store(key, value) { | ||
localStorage.setItem(key, value); | ||
} | ||
function retrieve(key) { | ||
return localStorage.getItem(key); | ||
} | ||
} | ||
}); | ||
})(jQuery); |
16 changes: 16 additions & 0 deletions
16
src/Sylius/Bundle/AdminBundle/Resources/views/_notification.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<div class="ui floated simple dropdown icon item" id="sylius-version-notification" data-frequency="{{ frequency }}" data-url="{{ path('sylius_admin_ajax_get_version') }}"> | ||
<i class="outline bell icon"></i> | ||
<div class="menu"> | ||
<div class="ui message" id="no-notifications"> | ||
<span>{{ 'sylius.ui.no_new_notifications'|trans }}</span> | ||
</div> | ||
<div class="ui message" id="notifications"> | ||
<span> | ||
{{ 'sylius.ui.there_is_a_new_version_of_sylius_available'|trans }} | ||
<i class="remove link icon" data-dismiss style="margin-left: 1em; margin-right: -0.5em;"></i> | ||
</span> | ||
<br/> | ||
<span>{{ 'sylius.ui.contact_your_technician_to_upgrade'|trans }}</span> | ||
</div> | ||
</div> | ||
</div> |
2 changes: 1 addition & 1 deletion
2
src/Sylius/Bundle/AdminBundle/Resources/views/_security.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.