<?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 RssfitjWordpress{
	var $dirname = 'wordpress';
	var $modname;
	
	/*
    * private
	* Nothing here so far
	*/
	function RssfitjWordpress(){
	}
	
	/*
    * private
	* 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
	* return module name 
	*/
	function getDirName(){
		return $this->dirname;
	}

	/*
    * public
	* Grab the entries of your module here
	*/
	function grabEntries(&$obj){
		global $xoopsDB,$module_handler;
		$myts =& MyTextSanitizer::getInstance();
		$ret = array();
		$i = 0;

		if ($obj->getVar('rssfj_param')) {
			$rcdId=explode(',',$obj->getVar('rssfj_param'));
			$Carray=array();
			$carray=array();
			$Tarray=array();
			$tarray=array();
			foreach ($rcdId as $eachId) {
				if (strpos($eachId,':')!==False) {
					$words=explode(':',$eachId);
					$word_cnt=0;
					foreach($words as $word) {
						if ($word_cnt > 0) {
							if (strpos($word,'C')!==False)
								$Carray[$words[0]][]=substr($word,1,strlen($word)-1);
							if (strpos($word,'c')!==False) {
								if (!isset($Carray[$words[0]]))
									$Carray[$words[0]]="";
								$carray[$words[0]][]=substr($word,1,strlen($word)-1);
							}
							if (strpos($word,'T')!==False) {
								if (!isset($Carray[$words[0]]))
									$Carray[$words[0]]="";
								$Tarray[$words[0]][]=substr($word,1,strlen($word)-1);
							}
							if (strpos($word,'N')!==False) {
								if (!isset($Carray[$words[0]]))
									$Carray[$words[0]]="";
								$tarray[$words[0]][]=substr($word,1,strlen($word)-1);
							}
							if (strpos($word,'t')!==False) {
								if (!isset($Carray[$words[0]]))
									$Carray[$words[0]]="";
								$tarray[$words[0]][]=substr($word,1,strlen($word)-1);
							}
						}
						$word_cnt++;
					}
				} else {
					if (!in_array($eachId,array_keys($Carray)))
						$Carray[$eachId]="";
				}
			}
			if (count($Carray)==0)
				$Carray['wordpress']="";
		} else {
			$Carray['wordpress']="";
		}

	//	The following example code grabs the latest entries from the module MyLinks
	foreach ($Carray as $key=>$val) {
		$add_sql="";
		if (is_array($Carray[$key])) {
			$add_sql.=" AND (";
			foreach($Carray[$key] as $eachId) {
				$add_sql .= "c.cat_ID=".$eachId." OR ";
			}
			$add_sql = substr($add_sql,0,strlen($add_sql)-4);
			$add_sql .= ")";
		}
		if (isset($carray[$key])) {
			$add_sql.=" AND NOT(";
			foreach($carray[$key] as $eachId) {
				$add_sql .= "c.cat_ID=".$eachId." OR ";
			}
			$add_sql = substr($add_sql,0,strlen($add_sql)-4);
			$add_sql .= ")";
		}
		if (isset($Tarray[$key])) {
			$add_sql.=" AND (";
			foreach($Tarray[$key] as $eachId) {
				$add_sql .= "p.ID=".$eachId." OR ";
			}
			$add_sql = substr($add_sql,0,strlen($add_sql)-4);
			$add_sql .= ")";
		}
		if (isset($tarray[$key])) {
			$add_sql.=" AND NOT(";
			foreach($tarray[$key] as $eachId) {
				$add_sql .= "p.ID=".$eachId." OR ";
			}
			$add_sql = substr($add_sql,0,strlen($add_sql)-4);
			$add_sql .= ")";
		}
		if ($key=='wordpress') {
			$sql = "SELECT p.ID, p.post_content, p.post_title, p.post_date, p.post_category, c.cat_name FROM ".$xoopsDB->prefix("wp_posts")." p, ".$xoopsDB->prefix("wp_categories")." c, ".$xoopsDB->prefix("wp_post2cat")." r WHERE p.ID=r.post_id AND r.category_id=c.cat_ID AND post_status='publish'".$add_sql." ORDER BY p.post_date DESC";
		} else {
			$sql = "SELECT p.ID, p.post_content, p.post_title, p.post_date, p.post_category, c.cat_name FROM ".$xoopsDB->prefix("wp".substr($key,-1,1)."_posts")." p, ".$xoopsDB->prefix("wp".substr($key,-1,1)."_categories")." c, ".$xoopsDB->prefix("wp".substr($key,-1,1)."_post2cat")." r WHERE p.ID=r.post_id AND r.category_id=c.cat_ID AND post_status='publish'".$add_sql." ORDER BY p.post_date DESC";
		}
		$result = $xoopsDB->query($sql, $obj->getVar('rssfj_grab'), 0);
		while( $row = $xoopsDB->fetchArray($result) ){
		//	required items
			$mod = $module_handler->getByDirname($key);
			if( !$mod || !$mod->getVar('isactive') ){
				return $ret;
			}
			$modname = $mod->getVar('name');

			$link = XOOPS_URL.'/modules/'.$key.'/index.php?p='.$row['ID'];
													// The title of the item
			$desc=$modname." : ".$row['cat_name'].">>".$row['post_title'];
			$ret[$i]['title'] = $myts->makeTareaData4Show($desc);
			$ret[$i]['link'] = $link;				// The URL of the item
			$ret[$i]['guid'] = $link;				// Unique identifier, usually url
													// Item modification date
			$date_sep=explode(' ',$row['post_date']);
			$date_ymd=explode('-',$date_sep[0]);
			$date_tms=explode(':',$date_sep[1]);
			$ret[$i]['timestamp'] = mktime($date_tms[0],$date_tms[1],$date_tms[2],$date_ymd[1],$date_ymd[2],$date_ymd[0]);
													// must be in Unix time format
			$desc = $row['post_content'];			// The item synopsis, or 
													// description, whatever
			$ret[$i]['description'] = $myts->makeTareaData4Show($desc);
		//	optional items
			$ret[$i]['category'] = $modname;
			$ret[$i]['domain'] = XOOPS_URL.'/modules/'.$key.'/';
			
			$i++;
		}
	}
		return $ret;
	}
}
?>