Code

Updated feed list
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 30 Jun 2010 12:28:52 +0000 (12:28 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 30 Jun 2010 12:28:52 +0000 (12:28 +0000)
-Fixed sorting

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@18884 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/plugins/generic/dashBoard/dbInformation/class_dbInformation.inc
gosa-core/plugins/generic/dashBoard/dbInformation/class_rssReader.inc

index d9e9fc97815f58c73e8b174147a190f185d8136a..707447cbbb6e97d3a93940c3cb64b34aae21dbcd 100644 (file)
@@ -10,10 +10,10 @@ class dbInformation extends plugin
         $this->feedList = new sortableListing();
         $this->feedList->setDeleteable(false);
         $this->feedList->setEditable(false);
-#        $this->feedList->setColspecs(array('30px','120px','*','100px'));
-#        $this->feedList->setHeader(array('?',_("Name"),_("Descriptio"),_("Status")));
+        #$this->feedList->setColspecs(array('130px','*'));
+        #$this->feedList->setHeader(array(_("Date"),_("Description")));
         $this->feedList->setWidth("100%");
-#        $this->feedList->setDefaultSortColumn(1);
+        #$this->feedList->setDefaultSortColumn(0);
         $this->feedList->setHeight("200px");
         $this->feedList->setAcl("rwcdm");
     }
@@ -21,13 +21,16 @@ class dbInformation extends plugin
     function execute()
     {
         $smarty = get_smarty();
-        $feeds = rssReader::feadToArray('http://www.computerbild.de/rssfeed_2261.html?node=10');
-
+        $feeds = rssReader::feadToArray(array(
+                    'http://www.computerbild.de/rssfeed_2261.html?node=10',
+                    'http://www.charts-portal.de/games/Computerspiele.xml'));
+        $feeds = rssReader::sortFeedResultBy($feeds, 'timestamp');
         $data = $lData = array();
         foreach($feeds as $key => $feed){
             $data[$key] = $feed;
-            $lData[$key] = array('data'=> array(date('H:i:s', $feed['timestamp']),$feed['title']));
+            $lData[$key] = array('data'=> array($feed['title']));
         }
+
         $this->feedList->setListData($data, $lData);
         $this->feedList->update();
         $smarty->assign('feedList', $this->feedList->render());
index 11e8a7942667bd698220d6cf605e13eff40873c2..54d9cad99cd22ed39f9fc810c92002a34f3d7b32 100644 (file)
@@ -2,24 +2,74 @@
 
 class rssReader{
 
-    static function feadToArray($url)
-    {
+    public static $attributes = array( 
+            'title','link','description','language','copyright','skipHours','timestamp',
+            'managingEditor','webMaster','pubDate','lastBuildDate','category',
+            'generator','docs','cloud','ttl','image','rating','textInput','skipDays');
 
-        $doc = new DOMDocument();
-        $doc->load($url);
+    public static function feadToArray($urls)
+    {
         $entries = array();
-        foreach ($doc->getElementsByTagName('item') as $item) {
-            $entry = array ( 
-                    'title' => $item->getElementsByTagName('title')->item(0)->nodeValue,
-                    'desc' => $item->getElementsByTagName('description')->item(0)->nodeValue,
-                    'link' => $item->getElementsByTagName('link')->item(0)->nodeValue,
-                    'date' => $item->getElementsByTagName('pubDate')->item(0)->nodeValue,
-                    'timestamp' => strtotime($item->getElementsByTagName('pubDate')->item(0)->nodeValue)
-                    );
-            $entries[$entry['timestamp']."-".$entry['date']] = $entry;
+
+        // We support multiple urls at once. 
+        if(!is_array($urls)) $urls = array($urls);
+
+        foreach($urls as $url){ 
+            $doc = new DOMDocument();
+            $doc->load($url);
+            foreach ($doc->getElementsByTagName('item') as $item) {
+
+                // Collect data from feed
+                $entry = array();
+                $entry['url'] = $url;
+                foreach(self::$attributes as $attr){
+                    $entry[$attr] =NULL;
+                    $obj = $item->getElementsByTagName($attr);
+                    if(is_object($obj->item(0))){
+                        $entry[$attr] = $obj->item(0)->nodeValue;
+                    }
+                }   
+
+                // Fake timestamp in none is given.
+                if($entry['pubDate'] == NULL){
+                    $entry['timestamp'] = NULL;
+                }else{
+
+                    // Create an entry timestamp 
+                    $entry['timestamp'] = strtotime($entry['pubDate']);
+                }
+                $entries[] = $entry;
+            }
         }
         return($entries);
     }
+
+    public static function sortFeedResultBy($feedRes , $sortBy = 'timestamp')
+    {
+
+        // Do not try to sort for invalid attributes.
+        if(!in_array($sortBy, self::$attributes)){
+            trigger_error("Invalid sortby attribute '{$sortBy}'!");
+            return($feedRes);
+        }
+
+        // Prepare feeds to be sorted, put them in an array indexed by the 'sortBy' attribute.
+        $data = array();
+        foreach($feedRes as $feed){
+            $key = "{$feed[$sortBy]}";
+            $c = '1';
+            while(empty($key) || isset($data[$key])){
+                $key = "{$c}_{$feed[$sortBy]}";
+                $c++;
+            }
+            $data[$key] = $feed;
+        }
+
+        // Sort the natural way and return the results.
+        uksort($data, 'strnatcasecmp');
+        return($data);
+    }
+
 } 
 
 ?>