Skip to content

Commit 9204ab7

Browse files
authored
Merge pull request #47 from ihorvansach/17-notification-banner
17 notification banner
2 parents 00cfc97 + a37d222 commit 9204ab7

File tree

11 files changed

+323
-0
lines changed

11 files changed

+323
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomer\CustomerData;
9+
10+
use Magento\Customer\CustomerData\SectionSourceInterface;
11+
use Magento\Customer\Model\Session;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Customer data for the logged_as_customer section
16+
*/
17+
class LoginAsCustomer implements SectionSourceInterface
18+
{
19+
/**
20+
* @var Session
21+
*/
22+
private $customerSession;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* LoginAsCustomer constructor.
31+
* @param Session $customerSession
32+
* @param StoreManagerInterface $storeManager
33+
*/
34+
public function __construct(
35+
Session $customerSession,
36+
StoreManagerInterface $storeManager
37+
) {
38+
$this->customerSession = $customerSession;
39+
$this->storeManager = $storeManager;
40+
}
41+
42+
/**
43+
* Retrieve private customer data for the logged_as_customer section
44+
* @return array
45+
*/
46+
public function getSectionData():array
47+
{
48+
if (!$this->customerSession->getCustomerId()) {
49+
return [];
50+
}
51+
52+
return [
53+
'admin_user_id' => $this->customerSession->getLoggedAsCustomerAdmindId(),
54+
'website_name' => $this->storeManager->getWebsite()->getName()
55+
];
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomer\ViewModel;
9+
10+
use Magento\Customer\Model\Context;
11+
12+
/**
13+
* View model to get extension configuration in the template
14+
*/
15+
class Configuration implements \Magento\Framework\View\Element\Block\ArgumentInterface
16+
{
17+
18+
/**
19+
* @var \Magento\LoginAsCustomer\Model\Config
20+
*/
21+
private $config;
22+
23+
/**
24+
* Customer session
25+
*
26+
* @var \Magento\Framework\App\Http\Context
27+
*/
28+
private $httpContext;
29+
30+
/**
31+
* Configuration constructor.
32+
* @param \Magento\LoginAsCustomer\Model\Config $config
33+
* @param \Magento\Framework\App\Http\Context $httpContext
34+
*/
35+
public function __construct(
36+
\Magento\LoginAsCustomer\Model\Config $config,
37+
\Magento\Framework\App\Http\Context $httpContext
38+
) {
39+
$this->config = $config;
40+
$this->httpContext = $httpContext;
41+
}
42+
43+
/**
44+
* Retrieve true if login as a customer is enabled
45+
* @return bool
46+
*/
47+
public function isEnabled():bool
48+
{
49+
return $this->config->isEnabled() && $this->isLoggedIn();
50+
}
51+
52+
/**
53+
* Is logged in
54+
*
55+
* @return bool
56+
*/
57+
private function isLoggedIn():bool
58+
{
59+
return (bool)$this->httpContext->getValue(Context::CONTEXT_AUTH);
60+
}
61+
}

app/code/Magento/LoginAsCustomer/etc/frontend/di.xml

+8
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@
99
<type name="Magento\PageCache\Model\Config">
1010
<plugin name="las-cache-is-enabled" type="Magento\LoginAsCustomer\Model\PageCache\ConfigPlugin"/>
1111
</type>
12+
13+
<type name="Magento\Customer\CustomerData\SectionPoolInterface">
14+
<arguments>
15+
<argument name="sectionSourceMap" xsi:type="array">
16+
<item name="logged_as_customer" xsi:type="string">Magento\LoginAsCustomer\CustomerData\LoginAsCustomer</item>
17+
</argument>
18+
</arguments>
19+
</type>
1220
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"Close Session","Close Session"
2+
"You are connected as <strong>%1</strong> on %2","You are connected as <strong>%1</strong> on %2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<body>
10+
<referenceContainer name="after.body.start">
11+
<block name="login-as-customer-notice" template="Magento_LoginAsCustomer::html/notices.phtml">
12+
<arguments>
13+
<argument name="config" xsi:type="object">Magento\LoginAsCustomer\ViewModel\Configuration</argument>
14+
</arguments>
15+
16+
<container name="login-as-customer-notice-links">
17+
<block class="Magento\Customer\Block\Account\AuthorizationLink" name="login-as-customer-logout-link"
18+
template="Magento_LoginAsCustomer::html/notices/logout-link.phtml" />
19+
</container>
20+
</block>
21+
</referenceContainer>
22+
</body>
23+
</page>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* @var \Magento\Framework\View\Element\Template $block
9+
* @var \Magento\Framework\Escaper $escaper
10+
*/
11+
?>
12+
<?php if ($block->getConfig()->isEnabled()) : ?>
13+
<div data-bind="scope: 'loginAsCustomer'" >
14+
<div class="lac-notification clearfix" data-bind="visible: isVisible" style="display: none">
15+
<div class="top-container">
16+
<div class="lac-notification-icon wrapper">
17+
<img class="logo-img" src="<?= $escaper->escapeUrl($block->getViewFileUrl('Magento_LoginAsCustomer::images/magento-icon.svg')) ?>" alt="Magento" />
18+
</div>
19+
<div class="lac-notification-text wrapper">
20+
<span data-bind="html: notificationText"></span>
21+
</div>
22+
<div class="lac-notification-links wrapper">
23+
<?= $block->getChildHtml('login-as-customer-notice-links') ?>
24+
</div>
25+
</div>
26+
</div>
27+
</div>
28+
<script type="text/x-magento-init">
29+
{
30+
"*": {
31+
"Magento_Ui/js/core/app": {
32+
"components": {
33+
"loginAsCustomer": {
34+
"component": "Magento_LoginAsCustomer/js/view/loginAsCustomer"
35+
}
36+
}
37+
}
38+
}
39+
}
40+
</script>
41+
<?php endif; ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* @var \Magento\Customer\Block\Account\AuthorizationLink $block
9+
* @var \Magento\Framework\Escaper $escaper
10+
*/
11+
$dataPostParam = '';
12+
if ($block->isLoggedIn()) {
13+
$dataPostParam = sprintf(" data-post='%s'", $block->getPostParams());
14+
}
15+
?>
16+
17+
<a class="lac-notification-close-link" <?= /* @noEscape */ $block->getLinkAttributes() ?><?= /* @noEscape */ $dataPostParam ?>>
18+
<?= $escaper->escapeHtml(__('Close Session')) ?>
19+
</a>
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
& when (@media-common = true) {
7+
8+
.lac-notification {
9+
background-color: #373330;
10+
color: #fff;
11+
font-size: 16px;
12+
13+
.lac-notification-icon {
14+
float:left;
15+
margin: 10px 25px 10px 10px;
16+
17+
.logo-img {
18+
display: block
19+
}
20+
}
21+
22+
.lac-notification-text {
23+
float:left;
24+
padding: 15px 0;
25+
}
26+
27+
.lac-notification-links {
28+
float: right;
29+
padding: 15px 0;
30+
a {
31+
color:#fff;
32+
font-size: 14px;
33+
}
34+
35+
.lac-notification-close-link {
36+
&:after {
37+
content: ' ';
38+
vertical-align: middle;
39+
display: inline-block;
40+
background: url('../Magento_LoginAsCustomer/images/close.svg');
41+
height: 12px;
42+
width: 12px;
43+
margin-left: 5px;
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
51+
.media-width(@extremum, @break) when (@extremum = 'max') and (@break = @screen__m) {
52+
.lac-notification {
53+
padding: 5px 0;
54+
55+
.lac-notification-icon {
56+
display: none;
57+
}
58+
59+
.lac-notification-text,
60+
.lac-notification-links {
61+
float: none;
62+
text-align: center;
63+
padding: 5px 0;
64+
}
65+
66+
}
67+
}
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'uiComponent',
9+
'Magento_Customer/js/customer-data',
10+
'mage/translate'
11+
], function ($, Component, customerData) {
12+
'use strict';
13+
14+
return Component.extend({
15+
16+
defaults: {
17+
isVisible: false
18+
},
19+
20+
/** @inheritdoc */
21+
initialize: function () {
22+
this._super();
23+
24+
this.customer = customerData.get('customer');
25+
this.loginAsCustomer = customerData.get('logged_as_customer');
26+
this.isVisible(this.loginAsCustomer().admin_user_id);
27+
28+
this.notificationText = $.mage.__('You are connected as <strong>%1</strong> on %2')
29+
.replace('%1', this.customer().fullname)
30+
.replace('%2', this.loginAsCustomer().website_name);
31+
},
32+
33+
/** @inheritdoc */
34+
initObservable: function () {
35+
this._super()
36+
.observe('isVisible');
37+
38+
return this;
39+
}
40+
});
41+
});

0 commit comments

Comments
 (0)