<?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 48 and line 56, i.e. "RssfitjNews" for the module "News"
* Step 3: Modify the word in line 49 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'
*/
if (file_exists(XOOPS_ROOT_PATH.'/modules/xfsection/include/groupaccess.php'))
	include_once XOOPS_ROOT_PATH.'/modules/xfsection/include/groupaccess.php';
class RssfitjXfsection{
	var $dirname = 'xfsection';
	var $modname;
	
	/*
    * private
	* Nothing here so far
	*/
	function RssfitjXfsection(){
	}

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

	/*
    * private, do not modify
	* Load the module
	*/
	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
	* Grab the entries of your module here
	*/
	function grabEntries(&$obj){
		global $xoopsDB;
		$myts =& MyTextSanitizer::getInstance();
		$ret = array();
		$i = 0;

		if ($obj->getVar('rssfj_param')) {
			$rcdId=explode(',',$obj->getVar('rssfj_param'));
			$tarray=array();
			$Tarray=array();
			$Carray=array();
			$carray=array();
			foreach ($rcdId as $eachId) {
				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));
			}
			$add_sql="";
			if (count($Tarray)) {
				$add_sql.=" AND (";
				foreach($Tarray as $TopicId) {
					$add_sql .= "a.articleid=".$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 .= "a.articleid=".$TopicId." 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 .= "a.categoryid=".$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 .= "a.categoryid=".$CatId." OR ";
				}
				$add_sql = substr($add_sql,0,strlen($add_sql)-4);
				$add_sql .= ")";
			}
		} else {
			$add_sql="";
		}

		$sql = "SELECT a.articleid, a.categoryid, a.title as atitle, a.published, a.expired, a.counter, a.groupid, a.maintext, a.summary, b.title as btitle FROM ".$xoopsDB->prefix("xfs_article")." a, ".$xoopsDB->prefix("xfs_category")." b WHERE a.published < ".time()." AND a.published > 0 AND (a.expired = 0 OR a.expired > ".time().") AND a.noshowart = 0 AND a.offline = 0 AND a.categoryid = b.id".$add_sql." ORDER BY published DESC";

		$result = $xoopsDB->query($sql, $obj->getVar('rssfj_grab'), 0);
		while( $row = $xoopsDB->fetchArray($result) ){
			if(checkAccess($row["groupid"])){
				$link = XOOPS_URL.'/modules/'.$this->dirname.'/article.php?articleid='.$row['articleid'];
			//	required items
				$desc=$this->modname." : ".$row['btitle'].">>".$row['atitle'];
				$ret[$i]['title'] = $myts->makeTareaData4Show($desc);
				$ret[$i]['link'] = $link;
				$ret[$i]['guid'] = $link;
				$ret[$i]['timestamp'] = $row['published'];
//				$desc = !empty($row['summary']) ? $row['summary'] : $row['maintext'];
				$desc = $row['summary']."::". $row['maintext'];
				$ret[$i]['description'] = $myts->makeTareaData4Show($desc);
			//	optional items
				$ret[$i]['category'] = $this->modname;
				$ret[$i]['domain'] = XOOPS_URL.'/modules/'.$this->dirname.'/';
				$i++;
			}
		}
		return $ret;
	}
}
?>