Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load feeds on dashboard with AJAX instead of on page render #13507

Merged
merged 3 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/lexicon/en/default.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
$_lang['error'] = 'Error';
$_lang['error_sending_email'] = 'Error sending email';
$_lang['error_sending_email_to'] = 'Error while sending mail to ';
$_lang['error_loading_feed'] = 'An error occurred loading the feed.';
$_lang['event_id'] = 'Event Id';
$_lang['existing_category'] = 'Existing Category';
$_lang['expand_all'] = 'Expand All';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* Class modDashboardWidgetFeedProcessor
*
* Used to load the news and security feeds on the dashboard over AJAX. The processed feed content
* (i.e. HTML) is returned in object->html.
*/
class modDashboardWidgetFeedProcessor extends modProcessor
{
/**
* @var modRSSParser
*/
protected $rss;

public function process()
{
$feed = $this->getProperty('feed', 'news');
if (!in_array($feed, array('news', 'security'), true)) {
return $this->failure('Invalid feed type');
}

$enabled = $this->modx->getOption('feed_modx_' . $feed . '_enabled', null, true);
if (!$enabled) {
return $this->failure();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really a failure ? Seems perfectly legit to have those disabled.

Or maybe is that because the request should have never occurred ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... i should have reviewed the whole diff first!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this shouldn't actually trigger. Was tempted to take it out, and happy to do so if you think that's better ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, i'm fine with it (we can never assume only core components would use it).
I was just concerned about the "failure" semantic...

}

$url = $this->modx->getOption('feed_modx_' . $feed);
// Check if the domain is valid before attempting to load it
$feedHost = parse_url($url, PHP_URL_HOST);
if ($feedHost && function_exists('checkdnsrr') && !checkdnsrr($feedHost, 'A')) {
return $this->failure();
}

return $this->loadFeed($url);
}

public function loadFeed($url)
{
$this->modx->loadClass('xmlrss.modRSSParser','',false,true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not getService ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason. Mostly just copied the old code from the widgets into the processor, and that used it like this... getService is probably cleaner though.

$this->rss = new modRSSParser($this->modx);

$o = array();
$rss = $this->rss->parse($url);
if (is_object($rss)) {
foreach (array_keys($rss->items) as $key) {
$item= &$rss->items[$key];
$item['pubdate'] = strftime('%c',$item['date_timestamp']);
$o[] = $this->getFileChunk('dashboard/rssitem.tpl',$item);
}
}
return $this->success('', array('html' => implode("\n",$o)));
}

/**
* @param string $tpl
* @param array $placeholders
* @return string
*/
public function getFileChunk($tpl,array $placeholders = array()) {
$output = '';
$file = $tpl;
if (!file_exists($file)) {
$file = $this->modx->getOption('manager_path').'templates/'.$this->modx->getOption('manager_theme',null,'default').'/'.$tpl;
}
if (!file_exists($file)) {
$file = $this->modx->getOption('manager_path').'templates/default/'.$tpl;
}
if (file_exists($file)) {
/** @var modChunk $chunk */
$chunk = $this->modx->newObject('modChunk');
$chunk->setCacheable(false);
$tplContent = file_get_contents($file);
$chunk->setContent($tplContent);
$output = $chunk->process($placeholders);
}
return $output;
}
}

return 'modDashboardWidgetFeedProcessor';
33 changes: 33 additions & 0 deletions manager/assets/modext/widgets/modx.panel.welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,40 @@ Ext.extend(MODx.panel.Welcome, MODx.Panel,{
if (this.config.dashboard && this.config.dashboard.hide_trees) {
Ext.getCmp('modx-layout').hideLeftbar(false);
}
var newsContainer = Ext.get('modx-news-feed-container');
if (newsContainer) {
this.loadFeed(newsContainer, 'news');
}
var securityContainer = Ext.get('modx-security-feed-container');
if (securityContainer) {
this.loadFeed(securityContainer, 'security');
}
MODx.fireEvent('ready');
},

loadFeed: function(container, feed) {
MODx.Ajax.request({
url: MODx.config.connector_url + '?action=system/dashboard/widget/feed&feed=' + feed
,listeners: {
success: {
fn: function(response) {
if (response.success) {
container.update(response.object.html);
}
else if (response.message.length > 0) {
container.update('<p class="error">' + response.message + '</p>');
}
}, scope: this
}
,failure: {
fn: function(response) {
var message = response.message.length > 0 ? response.message : _('error_loading_feed');
container.update('<p class="error">' + message + '</p>');
}, scope: this
}
}
}
);
}
});
Ext.reg('modx-panel-welcome', MODx.panel.Welcome);
26 changes: 3 additions & 23 deletions manager/controllers/default/dashboard/widget.modx-news.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,16 @@
* @subpackage dashboard
*/
class modDashboardWidgetNewsFeed extends modDashboardWidgetInterface {
/**
* @var modRSSParser $rss
*/
public $rss;

/**
* @return string
*/
public function render() {
$url = $this->modx->getOption('feed_modx_news');
$feedHost = parse_url($url, PHP_URL_HOST);
if ($feedHost && function_exists('checkdnsrr') && !checkdnsrr($feedHost, 'A')) {
$enabled = $this->modx->getOption('feed_modx_news_enabled',null,true);
if (!$enabled) {
return '';
}
$this->modx->loadClass('xmlrss.modRSSParser','',false,true);
$this->rss = new modRSSParser($this->modx);

$o = array();
$newsEnabled = $this->modx->getOption('feed_modx_news_enabled',null,true);
if (!empty($url) && !empty($newsEnabled)) {
$rss = $this->rss->parse($url);
if (is_object($rss)) {
foreach (array_keys($rss->items) as $key) {
$item= &$rss->items[$key];
$item['pubdate'] = strftime('%c',$item['date_timestamp']);
$o[] = $this->getFileChunk('dashboard/rssitem.tpl',$item);
}
}
}
return implode("\n",$o);
return '<div id="modx-news-feed-container" class="feed-loading" data-feed="news"><i class="icon icon-refresh icon-spin" aria-hidden="true"></i> ' . $this->modx->lexicon('loading') . '</div>';
}
}
return 'modDashboardWidgetNewsFeed';
26 changes: 3 additions & 23 deletions manager/controllers/default/dashboard/widget.modx-security.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,13 @@
* @subpackage dashboard
*/
class modDashboardWidgetSecurityFeed extends modDashboardWidgetInterface {
/**
* @var modRSSParser $rss
*/
public $rss;

public function render() {
$url = $this->modx->getOption('feed_modx_security');
$feedHost = parse_url($url, PHP_URL_HOST);
if ($feedHost && function_exists('checkdnsrr') && !checkdnsrr($feedHost, 'A')) {
$enabled = $this->modx->getOption('feed_modx_security_enabled',null,true);
if (!$enabled) {
return '';
}
$this->modx->loadClass('xmlrss.modRSSParser','',false,true);
$this->rss = new modRSSParser($this->modx);

$o = array();
$newsEnabled = $this->modx->getOption('feed_modx_security_enabled',null,true);
if (!empty($url) && !empty($newsEnabled)) {
$rss = $this->rss->parse($url);
if (is_object($rss)) {
foreach (array_keys($rss->items) as $key) {
$item= &$rss->items[$key];
$item['pubdate'] = strftime('%c',$item['date_timestamp']);
$o[] = $this->getFileChunk('dashboard/rssitem.tpl',$item);
}
}
}
return implode("\n",$o);
return '<div id="modx-security-feed-container" class="feed-loading" data-feed="news"><i class="icon icon-refresh icon-spin" aria-hidden="true"></i> ' . $this->modx->lexicon('loading') . '</div>';
}
}
return 'modDashboardWidgetSecurityFeed';