Skip to content

Commit

Permalink
Issue #2 - Add badge notification config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Jul 13, 2020
1 parent 0f4270c commit cb8ce32
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 9 deletions.
41 changes: 39 additions & 2 deletions Block/Adminhtml/Badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,60 @@

use Magento\Backend\Block\Template;
use MagePal\Core\Controller\Adminhtml\Version\Index;
use MagePal\Core\Helper\Data;

class Badge extends Template
{
const SEARCH_URL = 'https://www.magepal.com/catalogsearch/result/?utm_source=search&utm_medium=admin&utm_campaign=core';
const SEARCH_URL = 'https://www.magepal.com/catalogsearch/result/';
/**
* @var Data
*/
private $dataHelper;

/**
* Badge constructor.
* @param Template\Context $context
* @param Data $dataHelper
* @param array $data
*/
public function __construct(
Template\Context $context,
Data $dataHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->dataHelper = $dataHelper;
}

/**
* @return bool
*/
public function getNotificationOption()
{
return $this->dataHelper->getBadgeNotificationValue();
}

/**
* @return string
*/
public function getNotificationUrl()
{
return $this->getUrl('magepal/version/index');
}

/**
* @return bool
*/
public function isAuthorized()
{
return $this->_authorization->isAllowed(Index::ADMIN_RESOURCE);
}

/**
* @return string
*/
public function getSearchUrl()
{
return self::SEARCH_URL;
return self::SEARCH_URL . '?utm_source=search&utm_medium=admin&utm_campaign=core';
}
}
34 changes: 34 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* https://www.magepal.com | [email protected]
*/

namespace MagePal\Core\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
const XML_PATH_ACTIVE = 'magepal_core/general/badge_notification';
const NOTIFICATION_DISABLED = 0;
const NOTIFICATION_ENABLED = 1;
const NOTIFICATION_WITHIN_TAB = 2;

/**
* If enabled
*
* @param null $scopeCode
* @return bool
*/
public function getBadgeNotificationValue($scopeCode = null)
{
return (int) $this->scopeConfig->getValue(
self::XML_PATH_ACTIVE,
ScopeInterface::SCOPE_STORE,
$scopeCode
);
}
}
26 changes: 26 additions & 0 deletions Model/Config/Source/BadgeNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/

namespace MagePal\Core\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;
use MagePal\Core\Helper\Data;

class BadgeNotification implements ArrayInterface
{
/**
* @return array
*/
public function toOptionArray()
{
return [
['value' => Data::NOTIFICATION_ENABLED, 'label' => 'Yes'],
['value' => Data::NOTIFICATION_WITHIN_TAB, 'label' => 'When Tab Open'],
['value' => Data::NOTIFICATION_DISABLED, 'label' => __('No')]
];
}
}
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
],
"require": {
"magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*"
},
"suggest": {

},
"type": "magento2-module",
"version": "1.1.4",
"version": "1.1.5",
"autoload": {
"files": [
"registration.php"
Expand Down
8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
<attribute type="expanded">1</attribute>
<frontend_model>MagePal\Core\Block\Adminhtml\System\Config\Field\Extensions</frontend_model>
</group>
<group id="general" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="20" translate="label">
<label>Setting</label>
<field id="badge_notification" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Show Badge Notification</label>
<source_model>MagePal\Core\Model\Config\Source\BadgeNotification</source_model>
<comment><![CDATA[When Tab Open - Badge update count only shows when MagePal tab is expanded, in the "Notifications & Updates" section.]]></comment>
</field>
</group>
</section>
</system>
</config>
17 changes: 17 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* https://www.magepal.com | [email protected]
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<magepal_core>
<general>
<badge_notification>1</badge_notification>
</general>
</magepal_core>
</default>
</config>
3 changes: 2 additions & 1 deletion view/adminhtml/templates/badge.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"*": {
"magePalCoreNotificationIcon": {
"url":"<?= $block->escapeUrl($block->getNotificationUrl()) ?>",
"searchUrl":"<?= $block->escapeUrl($block->getSearchUrl()) ?>"
"searchUrl":"<?= $block->escapeUrl($block->getSearchUrl()) ?>",
"notificationOption": "<?= $block->getNotificationOption() ?>"
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions view/adminhtml/web/js/notification-icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ define([
var html = '<span class="mp-logo"></span>';
$element.before(html);

return function (config) {
var showBadge = function (config) {
$.ajax({
url: config.url,
type: 'get',
Expand All @@ -25,10 +25,19 @@ define([
}).done(function (response) {
if (typeof response === 'object' && response.hasOwnProperty('count') && response.count > 0) {
var html = '<span class="notifications-counter">' + response.count + '</span>';
$element.append(html);
if (config.notificationOption !== 1) {
$element.append(html);
}

$element.parent().parent().find('ul.items li.item:first').append(html)
}
});
};

return function (config) {
if (config.notificationOption !== 0) {
showBadge(config);
}

var openWindow = function ($element) {
if ($($element).val().length > 2) {
Expand Down

0 comments on commit cb8ce32

Please sign in to comment.