Skip to content

Commit 963ae16

Browse files
committed
Initial commit
1 parent 7c9fedf commit 963ae16

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/**
3+
* Copyright © 2017 Magefan (support@magefan.com). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Notifications\Observer\Controller;
10+
11+
class ActionPredispatch implements \Magento\Framework\Event\ObserverInterface
12+
{
13+
/**
14+
* Check every 10 min
15+
*/
16+
const TIMEOUT = 600;
17+
18+
/**
19+
* @var \Magento\Framework\App\Cache\TypeListInterface
20+
*/
21+
private $cacheTypeList;
22+
23+
/**
24+
* @var \Magento\Framework\Message\ManagerInterface
25+
*/
26+
private $messageManager;
27+
28+
/**
29+
* @var \Magento\Backend\Model\Auth\Session
30+
*/
31+
private $backendSession;
32+
33+
/**
34+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
35+
*/
36+
private $date;
37+
38+
/**
39+
* @var \Magento\Framework\UrlInterface
40+
*/
41+
private $url;
42+
43+
/**
44+
* @var \Magento\Review\Model\ResourceModel\Review\CollectionFactory
45+
*/
46+
protected $reviewCollectionFactory;
47+
48+
/**
49+
* Initialization
50+
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
51+
* @param \Magento\Framework\Message\ManagerInterface $messageManager
52+
* @param \Magento\Backend\Model\Auth\Session $backendSession
53+
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date,
54+
* @param \Magento\Framework\UrlInterface $url
55+
* @param \Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollectionFactory
56+
*/
57+
public function __construct(
58+
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
59+
\Magento\Framework\Message\ManagerInterface $messageManager,
60+
\Magento\Backend\Model\Auth\Session $backendSession,
61+
\Magento\Framework\Stdlib\DateTime\DateTime $date,
62+
\Magento\Framework\UrlInterface $url,
63+
\Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollectionFactory
64+
) {
65+
$this->cacheTypeList = $cacheTypeList;
66+
$this->messageManager = $messageManager;
67+
$this->backendSession = $backendSession;
68+
$this->date = $date;
69+
$this->url = $url;
70+
$this->reviewCollectionFactory = $reviewCollectionFactory;
71+
}
72+
73+
/**
74+
* Execute observer
75+
*
76+
* @param \Magento\Framework\Event\Observer $observer
77+
* @return void
78+
*/
79+
public function execute(
80+
\Magento\Framework\Event\Observer $observer
81+
) {
82+
if (!$this->backendSession->isLoggedIn()) {
83+
return; // Isn't logged in
84+
}
85+
86+
if ($observer->getRequest()->isXmlHttpRequest()) {
87+
return; // It's ajax request
88+
}
89+
90+
$time = $this->date->gmtTimestamp();
91+
if ($this->backendSession->getLastMfNtfCheck() > $time - self::TIMEOUT) {
92+
return; // It's not time
93+
}
94+
95+
$this->checkCacheTypes();
96+
$this->checkReviews();
97+
98+
$this->backendSession->setLastMfNtfCheck($time);
99+
}
100+
101+
/**
102+
* Check if cache types are enabled
103+
* @return void
104+
*/
105+
protected function checkCacheTypes()
106+
{
107+
$disabled = [];
108+
foreach ($this->cacheTypeList->getTypes() as $cacheType) {
109+
if (!$cacheType->getStatus()) {
110+
$disabled[] = $cacheType->getCacheType();
111+
}
112+
}
113+
114+
if (count($disabled)) {
115+
$this->messageManager->addNotice(
116+
__('The following Cache Type(s) are disabled: %1. <a href="%2">Manage Cache</a>.',
117+
implode(', ', $disabled),
118+
$this->url->getUrl('adminhtml/cache')
119+
)
120+
);
121+
}
122+
}
123+
124+
/**
125+
* Check if any pending review exists
126+
* @return void
127+
*/
128+
protected function checkReviews()
129+
{
130+
$pendignReview = $this->reviewCollectionFactory->create()
131+
->addFieldToFilter('status_id', \Magento\Review\Model\Review::STATUS_PENDING)
132+
->setPageSize(1)
133+
->getFirstItem();
134+
135+
if ($pendignReview->getId()) {
136+
$this->messageManager->addNotice(
137+
__('Some customer reviews are pending for approval. <a href="%1">Manage Reviews</a>.',
138+
$this->url->getUrl('review/product/index')
139+
)
140+
);
141+
}
142+
}
143+
}

composer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "magefan/module-notifications",
3+
"description": "Admin notifications about diferent events",
4+
"type": "magento2-module",
5+
"version": "2.0.0",
6+
"license": [
7+
"OSL-3.0",
8+
"AFL-3.0"
9+
],
10+
"autoload": {
11+
"files": [ "registration.php" ],
12+
"psr-4": {
13+
"Magefan\\Notifications\\": ""
14+
}
15+
}
16+
}

etc/adminhtml/events.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2017 Magefan (support@magefan.com). All rights reserved.
5+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
6+
*
7+
* Glory to Ukraine! Glory to the heroes!
8+
*/
9+
-->
10+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
11+
<event name="controller_action_predispatch">
12+
<observer instance="Magefan\Notifications\Observer\Controller\ActionPredispatch" name="Magefan_Notifications::controller_action_predispatch"/>
13+
</event>
14+
</config>

etc/module.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2017 Magefan (support@magefan.com). All rights reserved.
5+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
6+
*
7+
* Glory to Ukraine! Glory to the heroes!
8+
*/
9+
-->
10+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
11+
<module name="Magefan_Notifications" setup_version="2.0.0">
12+
<sequence>
13+
<module name="Magento_Backend"/>
14+
</sequence>
15+
</module>
16+
</config>

i18n/en_US.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"The following Cache Type(s) are disabled: %1. <a href=""%2"">Manage Cache</a>.","The following Cache Type(s) are disabled: %1. <a href=""%2"">Manage Cache</a>."
2+
"Some customer reviews are pending for approval. <a href=""%1"">Manage Reviews</a>.","Some customer reviews are pending for approval. <a href=""%1"">Manage Reviews</a>."

registration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © 2017 Magefan (support@magefan.com). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
\Magento\Framework\Component\ComponentRegistrar::register(
10+
\Magento\Framework\Component\ComponentRegistrar::MODULE,
11+
'Magefan_Notifications',
12+
__DIR__
13+
);

0 commit comments

Comments
 (0)