Skip to content

Commit

Permalink
Merge pull request Sylius#7785 from NeverResponse/hub
Browse files Browse the repository at this point in the history
[Admin] Sylius new version notification widget
  • Loading branch information
pjedrzejewski committed Mar 29, 2017
2 parents 2a84832 + af6ec66 commit a4551f2
Show file tree
Hide file tree
Showing 18 changed files with 492 additions and 2 deletions.
Expand Up @@ -12,7 +12,6 @@
namespace Sylius\Bundle\AdminBundle\Controller;

use Sylius\Component\Core\Customer\Statistics\CustomerStatisticsProviderInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
Expand Down
@@ -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());
}
}
@@ -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;
}
}
Expand Up @@ -26,8 +26,12 @@ final class SyliusAdminExtension extends Extension
*/
public function load(array $config, ContainerBuilder $container)
{
$config = $this->processConfiguration($this->getConfiguration([], $container), $config);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

$container->setParameter('sylius.admin.notification.enabled', $config['notifications']['enabled']);
$container->setParameter('sylius.admin.notification.frequency', $config['notifications']['frequency']);

$loader->load('services.xml');
}
}
Expand Up @@ -20,3 +20,8 @@ sylius_admin_ajax_render_province_form:
_controller: sylius.controller.province:choiceOrTextFieldFormAction
_sylius:
template: "@SyliusAdmin/Common/Form/_province.html.twig"

sylius_admin_ajax_get_version:
path: /get-version
defaults:
_controller: sylius.controller.admin.notification:getVersionAction
9 changes: 9 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Resources/config/services.xml
Expand Up @@ -24,5 +24,14 @@
<argument type="service" id="security.token_storage" />
<tag name="sylius.context.locale" priority="128" />
</service>

<service id="sylius.twig.extension.widget.admin_notification" class="Sylius\Bundle\AdminBundle\Twig\NotificationWidgetExtension">
<argument>%sylius.admin.notification.enabled%</argument>
<argument>%sylius.admin.notification.frequency%</argument>
<tag name="twig.extension" />
</service>

<service id="sylius.http_client" class="GuzzleHttp\Client" public="false" />
<service id="sylius.http_message_factory" class="Http\Message\MessageFactory\GuzzleMessageFactory" public="false" />
</services>
</container>
Expand Up @@ -12,6 +12,10 @@
-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="sylius.admin.notification.uri">http://hub.sylius.com/version</parameter>
</parameters>

<services>
<service id="sylius.controller.admin.dashboard" class="Sylius\Bundle\AdminBundle\Controller\DashboardController">
<argument type="service" id="sylius.dashboard.statistics_provider" />
Expand All @@ -33,5 +37,11 @@
<argument type='service' id="sylius.repository.customer" />
<argument type='service' id="templating" />
</service>

<service id="sylius.controller.admin.notification" class="Sylius\Bundle\AdminBundle\Controller\NotificationController">
<argument type="service" id="sylius.http_client" />
<argument type="service" id="sylius.http_message_factory" />
<argument type="string">%sylius.admin.notification.uri%</argument>
</service>
</services>
</container>
1 change: 1 addition & 0 deletions src/Sylius/Bundle/AdminBundle/Resources/private/js/app.js
Expand Up @@ -67,6 +67,7 @@
$('.ui.accordion').addAccordionErrors();
$('#sylius-product-taxonomy-tree').choiceTree('productTaxon', true, 1);

$(document).notification();
$(document).productSlugGenerator();
$(document).taxonSlugGenerator();
});
Expand Down
@@ -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);
@@ -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>
@@ -1,4 +1,4 @@
<div class="ui right floated simple dropdown item">
<div class="ui floated simple dropdown item">
{{ app.user.firstName|default(app.user.email) }}
<i class="dropdown icon"></i>
<div class="menu">
Expand Down
Expand Up @@ -18,8 +18,13 @@
{{ render(url('sylius_admin_partial_channel_index', {'template': '@SyliusAdmin/_channelLinks.html.twig'})) }}
{% include '@SyliusAdmin/_search.html.twig' %}

<div class="ui left floated dividing empty item"></div>

{{ sonata_block_render_event('sylius.admin.layout.topbar_middle') }}

<div class="ui right floated dividing empty item"></div>

{{ sylius_render_notifications_widget() }}
{% include '@SyliusAdmin/_security.html.twig' %}

{{ sonata_block_render_event('sylius.admin.layout.topbar_right') }}
Expand Down

0 comments on commit a4551f2

Please sign in to comment.