<?php
###############################################################################
##                RSSFit - Extendable XML news feed generator                ##
##                   Copyright (c) 2004 NS Tai (aka tuff)                    ##
##                       <http://www.brandycoke.com/>                        ##
##                      Modified By 2005 CACHE RSSfitJ                       ##
##                       <http://gyakubiki.kir.jp/>                          ##
###############################################################################
##                    XOOPS - PHP Content Management System                  ##
##                       Copyright (c) 2000 XOOPS.org                        ##
##                          <http://www.xoops.org/>                          ##
###############################################################################
##  This program is free software; you can redistribute it and/or modify     ##
##  it under the terms of the GNU General Public License as published by     ##
##  the Free Software Foundation; either version 2 of the License, or        ##
##  (at your option) any later version.                                      ##
##                                                                           ##
##  You may not change or alter any portion of this comment or credits       ##
##  of supporting developers from this source code or any supporting         ##
##  source code which is considered copyrighted (c) material of the          ##
##  original comment or credit authors.                                      ##
##                                                                           ##
##  This program is distributed in the hope that it will be useful,          ##
##  but WITHOUT ANY WARRANTY; without even the implied warranty of           ##
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            ##
##  GNU General Public License for more details.                             ##
##                                                                           ##
##  You should have received a copy of the GNU General Public License        ##
##  along with this program; if not, write to the Free Software              ##
##  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA ##
###############################################################################
##  Author of this file: CACHE                                               ##
##  URL: http://gyakubiki.kir.jp/                                            ##
##  Project: RSSFitJ                                                         ##
###############################################################################
/*
* Step 0: Stop here if you are not sure what you are doing, it's no fun at all
* Step 1: Clone this file and rename as rssfitj.[mod_dir].php
* Step 2: Replace the text "RssfitjSample" with "Rssfitj[mod_dir]" at line 47 and line 55, i.e. "RssfitjNews" for the module "News"
* Step 3: Modify the word in line 48 from 'sample' to [mod_dir]
* Step 4: Modify the function "grabEntries" to satisfy your needs
* Step 5: Move the new plug-in file to the plugins folder, i.e.  your-xoops-root/modules/rss/plugins
* Step 6: Install your plug-in by pointing your browser to your-xoops-url/modules/rss/admin/index.php
* [mod_dir]: Name of the driectory of your module, i.e. 'news'
*/

class RssfitjBluesbb{
	var $dirname = 'bluesbb';
	var $modname;
	
	/*
    * private
	* Nothing here so far
	*/
	function RssfitjBluesbb(){
	}
	
	function loadModule(){
		global $module_handler;
		$mod = $module_handler->getByDirname($this->dirname);
		if( !$mod || !$mod->getVar('isactive') ){
			return false;
		}
		$this->modname = $mod->getVar('name');
		return $mod;
	}

	/*
    * public
	* return module name 
	*/
	function getDirName(){
		return $this->dirname;
	}

	function grabEntries(&$obj){
		global $xoopsDB;

		$myts =& MyTextSanitizer::getInstance();
		$ret = array();
		$i = 0;

		if ($obj->getVar('rssfj_param')) {
			$rcdId=explode(',',$obj->getVar('rssfj_param'));
			$Farray=array();
			$farray=array();
			$Tarray=array();
			$tarray=array();
			$Carray=array();
			$carray=array();
			$Garray=array();
			$garray=array();
			foreach ($rcdId as $eachId) {
				if (substr($eachId,0,1)=='F')
					array_push($Farray,substr($eachId,1,strlen($eachId)-1));
				if (substr($eachId,0,1)=='f')
					array_push($farray,substr($eachId,1,strlen($eachId)-1));
				if (substr($eachId,0,1)=='T')
					array_push($Tarray,substr($eachId,1,strlen($eachId)-1));
				if (substr($eachId,0,1)=='N' || substr($eachId,0,1)=='t')
					array_push($tarray,substr($eachId,1,strlen($eachId)-1));
				if (substr($eachId,0,1)=='C')
					array_push($Carray,substr($eachId,1,strlen($eachId)-1));
				if (substr($eachId,0,1)=='c')
					array_push($carray,substr($eachId,1,strlen($eachId)-1));
				if (substr($eachId,0,1)=='G')
					array_push($Garray,substr($eachId,1,strlen($eachId)-1));
				if (substr($eachId,0,1)=='g')
					array_push($garray,substr($eachId,1,strlen($eachId)-1));
			}
			$add_sql="";
			if (count($Tarray)) {
				$add_sql.=" AND (";
				foreach($Tarray as $TopicId) {
					$add_sql .= "t.sread_id=".$TopicId." OR ";
				}
				$add_sql = substr($add_sql,0,strlen($add_sql)-4);
				$add_sql .= ")";
			}
			if (count($tarray)) {
				$add_sql.=" AND NOT(";
				foreach($tarray as $TopicId) {
					$add_sql .= "t.sread_id=".$TopicId." OR ";
				}
				$add_sql = substr($add_sql,0,strlen($add_sql)-4);
				$add_sql .= ")";
			}
			if (count($Farray)) {
				$add_sql.=" AND (";
				foreach($Farray as $forumId) {
					$add_sql .= "t.topic_id=".$forumId." OR ";
				}
				$add_sql = substr($add_sql,0,strlen($add_sql)-4);
				$add_sql .= ")";
			}
			if (count($farray)) {
				$add_sql.=" AND NOT(";
				foreach($farray as $forumId) {
					$add_sql .= "t.topic_id=".$forumId." OR ";
				}
				$add_sql = substr($add_sql,0,strlen($add_sql)-4);
				$add_sql .= ")";
			}
			if (count($Carray)) {
				$add_sql.=" AND (";
				foreach($Carray as $CatId) {
					$add_sql .= "f.cat_id=".$CatId." OR ";
				}
				$add_sql = substr($add_sql,0,strlen($add_sql)-4);
				$add_sql .= ")";
			}
			if (count($carray)) {
				$add_sql.=" AND NOT(";
				foreach($carray as $CatId) {
					$add_sql .= "f.cat_id=".$CatId." OR ";
				}
				$add_sql = substr($add_sql,0,strlen($add_sql)-4);
				$add_sql .= ")";
			}
			$add_sql2="";
			if (count($Garray)) {
				foreach($Garray as $GroupId) {
					$add_sql2 .= " OR (f.topic_access=6 AND f.topic_group=".$GroupId.")";
				}
			}
			if (count($garray)) {
				foreach($garray as $GroupId) {
					$add_sql2 .= " OR (f.topic_access=6 AND f.topic_group<>".$GroupId.")";
				}
			}
		} else {
			$add_sql="";
			$add_sql2="";
		}

		$sql = 'SELECT t.post_id, t.topic_id, t.sread_id, t.res_id, t.title, t.message, t.post_time, f.topic_name, c.cat_title FROM '.$xoopsDB->prefix('bluesbb').' t, '.$xoopsDB->prefix('bluesbb_topic').' f, '.$xoopsDB->prefix('bluesbb_categories').' c WHERE f.topic_id = t.topic_id AND f.cat_id = c.cat_id'.$add_sql.' AND (f.topic_access <= 2 OR f.topic_access=5'.$add_sql2.') ORDER BY t.post_time DESC';
		if( !$result = $xoopsDB->query($sql, $obj->getVar('rssfj_grab'), 0) ){
			return false;
		}

		while( $row = $xoopsDB->fetchArray($result) ){
			$desc = $myts->makeTareaData4Show($row['message']);
			$link = XOOPS_URL.'/modules/'.$this->dirname.'/viewsread.php?topic='.$row['topic_id'].'&amp;sread_id='.$row['sread_id'].'&amp;number=l50';
		//	required
			$desc2=$this->modname." : ".$row['cat_title']." : ".$row['topic_name']." : ".$row['title'];
			$ret[$i]['title'] = $myts->makeTareaData4Show($desc2);
			$ret[$i]['link'] = $ret[$i]['guid'] = $link;
			$ret[$i]['timestamp'] = $row['post_time'];
			$ret[$i]['description'] = $desc;
		//	optional
			$ret[$i]['category'] = $this->modname;
			$ret[$i]['domain'] = XOOPS_URL.'/modules/'.$this->dirname.'/';			$i++;
		}
		return $ret;
	}
}

?>