Skip to content

Commit 71e746d

Browse files
committed
Install devopensource_notification v0.1.0
1 parent feabc1e commit 71e746d

14 files changed

Lines changed: 493 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* @category Devopensource
4+
* @package Devopensource_Notification
5+
* @author Jose Ruzafa <jose.ruzafa@devopensource.com>
6+
* @version 0.1.0
7+
* @copyright Copyright (c) 2015 Devopensource
8+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
*/
10+
11+
class Devopensource_Notification_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template {
12+
13+
public function getMessage()
14+
{
15+
16+
$notification = Mage::getModel('devopennotify/notification')->getCollection()
17+
->addFieldToFilter('read', 0);
18+
19+
return $notification;
20+
}
21+
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* @category Devopensource
4+
* @package Devopensource_Notification
5+
* @author Jose Ruzafa <jose.ruzafa@devopensource.com>
6+
* @version 0.1.0
7+
* @copyright Copyright (c) 2015 Devopensource
8+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
*/
10+
11+
class Devopensource_Notification_Helper_Data extends Mage_Core_Helper_Abstract {
12+
13+
public function devopensourceModulesLoaded(){
14+
15+
$modules = array_keys((array)Mage::getConfig()->getNode('modules')->children());
16+
$devopensourceModules = array();
17+
18+
foreach ($modules as $_module) {
19+
if(strpos($_module, 'Devopensource_') !== false){
20+
21+
$devopensourceModules[] = array($_module, $this->getModuleVersion($_module));
22+
}
23+
}
24+
25+
return $devopensourceModules;
26+
}
27+
28+
public function getModuleVersion($_moduleName)
29+
{
30+
return (string) Mage::getConfig()->getNode('modules/'.$_moduleName.'/version');
31+
}
32+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* @category Devopensource
4+
* @package Devopensource_Notification
5+
* @author Jose Ruzafa <jose.ruzafa@devopensource.com>
6+
* @version 0.1.0
7+
* @copyright Copyright (c) 2015 Devopensource
8+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
*/
10+
11+
class Devopensource_Notification_Model_Feed extends Mage_Core_Model_Abstract {
12+
13+
protected $_frequency = 24;
14+
CONST DEVOPENSOURCE_URL_NOTIFICATIONS = 'http://modules.devopensource.com/checknotifications';
15+
16+
public function checkUpdate()
17+
{
18+
if (($this->getFrequency() + $this->getLastUpdate()) > time()) {
19+
return $this;
20+
}
21+
22+
$feedData = array();
23+
$feedDataDefault = array();
24+
$feedJson = $this->getFeedData();
25+
26+
if ( $feedJson ) {
27+
28+
foreach ($feedJson->items as $key => $item) {
29+
$feedData[] = array(
30+
'title' => (string)$item->title,
31+
'module' => (string)$item->module,
32+
'description' => (string)$item->description,
33+
'severity' => (int)$item->severity,
34+
'id_message' => (string)$item->id_message,
35+
'url' => (string)$item->url
36+
);
37+
38+
$feedDataDefault[] = array(
39+
'severity' => $feedData[$key]['severity'],
40+
'date_added' => now(),
41+
'title' => $feedData[$key]['title'],
42+
'description' => $feedData[$key]['description'],
43+
'url' => $feedData[$key]['url']
44+
);
45+
}
46+
47+
if ($feedData) {
48+
Mage::getModel('devopennotify/notification')->parse($feedData);
49+
Mage::getModel('adminnotification/inbox')->parse(array_reverse($feedDataDefault));
50+
}
51+
52+
}
53+
54+
$this->setLastUpdate();
55+
56+
return $this;
57+
}
58+
59+
public function getDate($rssDate)
60+
{
61+
return gmdate('Y-m-d H:i:s', strtotime($rssDate));
62+
}
63+
64+
public function getFrequency()
65+
{
66+
return $this->_frequency * 3600;
67+
}
68+
69+
public function getLastUpdate()
70+
{
71+
return Mage::app()->loadCache('devopennotify_notifications_lastcheck');
72+
}
73+
74+
public function setLastUpdate()
75+
{
76+
Mage::app()->saveCache(time(), 'devopennotify_notifications_lastcheck');
77+
return $this;
78+
}
79+
80+
public function getFeedData()
81+
{
82+
83+
$dataModules = Mage::helper('devopennotify')->devopensourceModulesLoaded();
84+
$jsonModules = json_encode($dataModules);
85+
86+
$curl = new Varien_Http_Adapter_Curl();
87+
88+
$curl->setConfig(array(
89+
'timeout' => 2
90+
));
91+
92+
$headers = array(
93+
"Content-Type: application/json",
94+
"Accept: application/json",
95+
"Content-Length: " .strlen($jsonModules)
96+
);
97+
98+
$curl->write(Zend_Http_Client::POST, self::DEVOPENSOURCE_URL_NOTIFICATIONS, null, $headers, $jsonModules);
99+
100+
$data = $curl->read();
101+
102+
if ($data === false) {
103+
return false;
104+
}
105+
$data = preg_split('/^\r?$/m', $data, 2);
106+
$data = trim($data[1]);
107+
$curl->close();
108+
109+
try {
110+
$json = json_decode($data);
111+
}
112+
catch (Exception $e) {
113+
return false;
114+
}
115+
116+
return $json;
117+
}
118+
119+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* @category Devopensource
4+
* @package Devopensource_Notification
5+
* @author Jose Ruzafa <jose.ruzafa@devopensource.com>
6+
* @version 0.1.0
7+
* @copyright Copyright (c) 2015 Devopensource
8+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
*/
10+
11+
class Devopensource_Notification_Model_Notification extends Mage_Core_Model_Abstract
12+
{
13+
14+
protected function _construct()
15+
{
16+
$this->_init('devopennotify/notification');
17+
}
18+
19+
public function parse(array $data)
20+
{
21+
foreach ($data as $_dat){
22+
23+
$notification = Mage::getModel('devopennotify/notification')->getCollection()
24+
->addFieldToFilter('id_message', $_dat['id_message'])
25+
->count();
26+
27+
if ($notification == 0) {
28+
29+
$notificationNew = Mage::getModel('devopennotify/notification');
30+
$notificationNew->title = $_dat['title'];
31+
$notificationNew->module = $_dat['module'];
32+
$notificationNew->description = $_dat['description'];
33+
$notificationNew->severity = $_dat['severity'];
34+
$notificationNew->id_message = $_dat['id_message'];
35+
$notificationNew->url = $_dat['url'];
36+
37+
$notificationNew->save();
38+
}
39+
}
40+
}
41+
42+
protected function _beforeSave()
43+
{
44+
parent::_beforeSave();
45+
46+
$this->_updateTimestamps();
47+
48+
return $this;
49+
}
50+
51+
protected function _updateTimestamps()
52+
{
53+
$timestamp = now();
54+
55+
/**
56+
* If we have a brand new object, set the created timestamp.
57+
*/
58+
if ($this->isObjectNew()) {
59+
$this->setCreatedAt($timestamp);
60+
}
61+
}
62+
63+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* @category Devopensource
4+
* @package Devopensource_Notification
5+
* @author Jose Ruzafa <jose.ruzafa@devopensource.com>
6+
* @version 0.1.0
7+
* @copyright Copyright (c) 2015 Devopensource
8+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
*/
10+
11+
class Devopensource_Notification_Model_Observer {
12+
13+
public function preDispatch(Varien_Event_Observer $observer)
14+
{
15+
if (Mage::getSingleton('admin/session')->isLoggedIn()) {
16+
$feedModel = Mage::getModel('devopennotify/feed');
17+
/* @var $feedModel Mage_AdminNotification_Model_Feed */
18+
19+
$feedModel->checkUpdate();
20+
}
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* @category Devopensource
4+
* @package Devopensource_Notification
5+
* @author Jose Ruzafa <jose.ruzafa@devopensource.com>
6+
* @version 0.1.0
7+
* @copyright Copyright (c) 2015 Devopensource
8+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
*/
10+
11+
class Devopensource_Notification_Model_Resource_Notification extends Mage_Core_Model_Resource_Db_Abstract
12+
{
13+
14+
protected function _construct()
15+
{
16+
$this->_init('devopennotify/notification', 'id');
17+
}
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* @category Devopensource
4+
* @package Devopensource_Notification
5+
* @author Jose Ruzafa <jose.ruzafa@devopensource.com>
6+
* @version 0.1.0
7+
* @copyright Copyright (c) 2015 Devopensource
8+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
*/
10+
11+
class Devopensource_Notification_Model_Resource_Notification_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
12+
{
13+
14+
protected function _construct()
15+
{
16+
$this->_init('devopennotify/notification');
17+
}
18+
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @category Devopensource
4+
* @package Devopensource_Notification
5+
* @author Jose Ruzafa <jose.ruzafa@devopensource.com>
6+
* @version 0.1.0
7+
* @copyright Copyright (c) 2015 Devopensource
8+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9+
*/
10+
11+
class Devopensource_Notification_Adminhtml_NotifyController extends Mage_Adminhtml_Controller_Action {
12+
13+
protected $_notification;
14+
15+
public function readAction(){
16+
17+
$id = $this->getRequest()->getParam('id');
18+
19+
$this->_readNotification($id);
20+
21+
//marcar como leido en el inbox de magento
22+
$this->_readNotificationInbox($id);
23+
24+
$this->_redirectReferer();
25+
}
26+
27+
protected function _readNotification ($idNotification){
28+
29+
$notification = Mage::getModel('devopennotify/notification')->load($idNotification);
30+
31+
// Guardamos la notificacion en variable para poder ser usada en la funcion _readNotificationInbox
32+
$this->_notification = $notification;
33+
34+
$notification->setRead(1);
35+
$notification->save();
36+
}
37+
38+
protected function _readNotificationInbox(){
39+
40+
$notification = Mage::getModel('adminnotification/inbox')->load($this->_notification->getTitle(), 'title');
41+
$notification->setIsRead(true);
42+
$notification->save();
43+
}
44+
}

0 commit comments

Comments
 (0)