we are more and more standalone !
[banana.git] / include / groups.inc.php
index d34c96f..5f762da 100644 (file)
 /** class for group lists
  */
 
+define ( 'BANANA_GROUP_ALL', 0 );
+define ( 'BANANA_GROUP_SUB', 1 );
+define ( 'BANANA_GROUP_NEW', 2 );
 class BananaGroups {
     /** group list */
-    var $overview;
+    var $overview = Array();
     /** last update */
     var $date;
 
+    var $type;
+
     /** constructor
-     * @param $_nntp RESOURCE handle to NNTP socket
      */
 
-    function BananaGroups(&$_nntp,$_type=0) {
-        global $profile;
-        $desc=$_nntp->xgtitle();
-        if ($_type==1) {
-            $list=$_nntp->newgroups($profile['lastnews']);
-        } else {
-            $list=$_nntp->liste();
-        }
-        if (!$list) {
-            $this->overview=array();
-            return false;
-        }
-        if (isset($desc)) {
-            foreach ($desc as $g=>$d) {
-                if ((($_type==0) and (in_array($g,$profile['subscribe']) or !count($profile['subscribe'])))
-                        or (($_type==1) and in_array($g,array_keys($list)))
-                        or ($_type==2)) {
-                    $this->overview[$g][0]=$d;
-                    $this->overview[$g][1]=$list[$g][0];
-                }
-            }
-            foreach (array_diff(array_keys($list),array_keys($desc)) as $g) {
-                if ((($_type==0) and (in_array($g,$profile['subscribe']) or !count($profile['subscribe'])))
-                        or (($_type==1) and in_array($g,array_keys($list)))
-                        or ($_type==2)) {
-                    $this->overview[$g][0]="-";
-                    $this->overview[$g][1]=$list[$g][0];
-                }
-            }
+    function BananaGroups($_type = BANANA_GROUP_SUB) {
+        global $banana;
+
+        $this->type = $_type;
+        $desc       = $banana->nntp->xgtitle();
+        
+        if ($_type == BANANA_GROUP_NEW) {
+            $list = $banana->nntp->newgroups($banana->profile['lastnews']);
         } else {
-            foreach ($list as $g=>$l) {
-                if ((($_type==0) and (!count($profile['subscribe']) and in_array($g,$profile['subscribe'])))
-                        or (($_type==1) and in_array($g,array_keys($list)))
-                        or ($_type==2)) {
-                    $this->overview[$g][0]="-";
-                    $this->overview[$g][1]=$l[0];
+            $list = $banana->nntp->liste();
+            if ($_type == BANANA_GROUP_SUB) {
+                $mylist = Array();
+                foreach ($banana->profile['subscribe'] as $g) {
+                    if (isset($list[$g])) {
+                        $mylist[$g] = $list[$g];
+                    }
                 }
+                $list = $mylist;
             }
         }
-        return true;
+
+        foreach ($list as $g=>$l) {
+            $this->overview[$g][0] = isset($desc[$g]) ? $desc[$g] : '-';
+            $this->overview[$g][1] = $l[0];
+        }
+        ksort($this->overview);
+
+        if (empty($this->overview) && $_type == BANANA_GROUP_SUB) {
+            $this = new BananaGroups(BANANA_GROUP_ALL);
+        }
     }
 
     /** updates overview 
-     * @param $_nntp RESOURCE handle to NNTP socket
      * @param date INTEGER date of last update
      */
-    function update(&$_nntp,$_date) {
-        $serverdate = $_nntp->date();
+    function update($_date) {
+        global $banana;
+        $serverdate = $banana->nntp->date();
         if (!$serverdate) $serverdate=time();
-        $newlist = $_nntp->newgroups($_date);
+        $newlist = $banana->nntp->newgroups($_date);
         if (!$newlist) return false;
         $this->date = $serverdate;
         foreach (array_keys($newlist) as $g) {
-            $groupstat = $_nntp->group($g);
-            $groupdesc = $_nntp->xgtitle($g);
+            $groupstat = $banana->nntp->group($g);
+            $groupdesc = $banana->nntp->xgtitle($g);
             $this->overview[$g][0]=($groupdesc?$groupdesc:"-");
             $this->overview[$g][1]=$groupstat[0];
         }
         return true;
     }
+
+    function to_html($show_form = false)
+    {
+        global $banana;
+        if (empty($this->overview)) {
+            return;
+        }
+
+        $html  = '<table class="bicol banana_group" cellspacing="0" cellpadding="2">'."\n";
+        $html .= '<tr><th>'._b_('Total').'</th><th>';
+        if ($show_form) {
+            $html .= _b_('Abo.').'</th><th>';
+        } elseif ($this->type == BANANA_GROUP_SUB) {
+            $html .= _b_('Nouveaux').'</th><th>';
+        }
+        $html .= _b_('Nom').'</th><th>'._b_('Description').'</th></tr>'."\n";
+
+        $b = true;
+        foreach ($this->overview as $g => $d) {
+            $b     = !$b;
+            $ginfo = $banana->nntp->group($g);
+            $new   = count($banana->nntp->newnews($banana->profile['lastnews'],$g));
+
+            $html .= '<tr class="'.($b ? 'pair' : 'impair').'">'."\n";
+            $html .= "<td class='all'>{$ginfo[0]}</td>";
+            if ($show_form) {
+                $html .= '<td class="new"><input type="checkbox" name="subscribe[]" value="'.$g.'"';
+                if (in_array($g, $banana->profile['subscribe'])) {
+                    $html .= ' checked="checked"';
+                }
+                $html .= ' /></td>';
+            } elseif ($this->type == BANANA_GROUP_SUB) {
+                $html .= '<td class="new">'.($new ? $new : '-').'</td>';
+            }
+            $html .= "<td class='grp'><a href='?group=$g'>$g</a></td><td class='dsc'>{$d[0]}</td></tr>";
+        }
+
+        $html .= '</table>';
+
+        if ($show_form) {
+            return '<form method="post" action="'.$_SERVER['PHP_SELF'].'"><div class="center"><input type="submit" /></div>'
+                .$html.'<div class="center"><input type="submit" /></div></form>';
+        }
+        
+        return $html;
+    }
 }
 
 ?>