<?php
require_once 'modules/admin/models/DashboardPlugin.php';
require_once 'modules/admin/models/Announcements.php';

class PluginRssreader extends DashboardPlugin
{
    var $hasTab = true;
    var $hasPanel = true;
    var $configFunction = "PopulateConfigWindow";
    var $name = "RSS Reader";
    var $description = "Mini RSS reader to list the news you care about in the dashboard.";
    var $default = true;

    var $jsLibs         = array('plugins/dashboard/rssreader/PluginRssreaderConfig.js');

    var $settings = null;

    function getTab()
    {
        return $this->getPanel();
    }

    function getPanel()
    {
        $this->tpl->define(array('rssfeed' => '../../plugins/dashboard/rssreader/PluginRssreader.tpl'));

        $this->_loadSettings();
        $this->tpl->setKey('latestLink', $this->settings['rssurl']);
        $this->tpl->setKey('rssitemcount',$this->settings['rssitemcount']);
        $this->tpl->setKey('rssfeedname',$this->settings['rssname']);

        $this->tpl->parse('rssfeed_out', 'rssfeed');
        return $this->tpl->fetch('rssfeed_out');
    }

    function doAction()
    {
        if (!isset($_GET['pluginAction'])) $_GET['pluginAction'] = 'listitems';

        switch($_GET['pluginAction']) {
            case 'listitems':
                //return $this->listitems($this->tasks);
                break;
            case 'feedproxy':

                include_once 'newedge/classes/NE_Cache.php';
                $cache = new NE_Cache("RSSFEED");
                $content = $cache->getContent();
                $lastupdated = $cache->GetLastUpdated();
                //Cache online users for 18000 seconds.
                if ($content != '' && $lastupdated != '' && $lastupdated >= time() - 18000) {
                    header('Content-Type: text/xml');
                    echo $content;
                    return;
                }else{
                    $feed = $_REQUEST['feed'];
                    if($feed != '' && strpos($feed, 'http') === 0){
                        header('Content-Type: text/xml');
                        $xml = @file_get_contents($feed);
                        $xml = str_replace('<content:encoded>', '<content>', $xml);
                        $xml = str_replace('</content:encoded>', '</content>', $xml);
                        $xml = str_replace('</dc:creator>', '</author>', $xml);
                        $xml = str_replace('<dc:creator', '<author', $xml);

                        $cache->SetContent($xml);
                        $cache->Update();
                        echo $xml;
                        return;
                    }
                }
                break;
            case 'editRssURL':
                if (!isset($_POST['url'])) break;
                //count,url,name
                $settings = array('rssurl'=>$_POST['url'],'rssitemcount'=>$_POST['count'], 'rssname'=>$_POST['name']);
                $this->storeData('settings', serialize($settings));
                include_once 'newedge/classes/NE_Cache.php';
                $cache = new NE_Cache("RSSFEED");
                $cache->deleteCache();
                break;
            case 'getFeeds':


                if (isset($_SERVER['SCRIPT_URI'])) {
                    $URI = $_SERVER['SCRIPT_URI'];
                } else {
                    $URI = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
                }
                $url = mb_substr($URI, 0, strpos($URI, "admin"));
                $companyURL = $url.'index.php?fuse=admin&view=RssAnnouncements';
                $rssArray[] = array("url"=>'http://www.clientexec.com/members/index.php?fuse=admin&view=RssAnnouncements',"text"=>'ClientExec Announcements');
                $rssArray[] = array("url"=>$companyURL,"text"=>'Your Company Announcements');
                $rssArray[] = array("url"=>'http://rss.news.yahoo.com/rss/mostviewed',"text"=>'Yahoo Most Viewed News');
                return json_encode(array("items"=>$rssArray));
                break;
        }
    }

    /*
    function _storeTasks()
    {
        $settings = array('rssurl'=>$this->rssurl,'rssitemcount'=>$this->rssitemcount, 'rssname'=>$this->rssname);
        $this->storeData('tasks', serialize($settings));
    }
    */

    function _loadSettings(){
        if ($this->storedDataExists('settings'))
        {
            $this->settings = unserialize($this->retrieveData('settings'));
        }else{
            //default settings
            $rssurl = "http://www.clientexec.com/members/index.php?fuse=admin&view=RssAnnouncements";
            $rssname = "ClientExec Announcements";
            $rssitemcount = 3;
            $this->settings = array('rssurl'=>$rssurl,'rssitemcount'=>$rssitemcount, 'rssname'=>$rssname);
        }
    }

}

?>
