<?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 Rssfitjpopnupblog{
	var $dirname = 'popnupblog';
	var $modname;
	
	/*
    * private
	* Nothing here so far
	*/
	function Rssfitjpopnupblog(){
	}
	
	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'));
			$tarray=array();
			$Tarray=array();
			$farray=array();
			$Farray=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)=='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)=='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)) {
				$work_sql="";
				foreach($Tarray as $TopicId) {
					switch(strlen($TopicId)) {
						case 8:
							$startTime="'".substr($TopicId,0,4)."-".substr($TopicId,4,2)."-".substr($TopicId,6,2)." 00:00:00'";
							$endTime="'".substr($TopicId,0,4)."-".substr($TopicId,4,2)."-".substr($TopicId,6,2)." 23:59:59'";
							$work_sql .= "(l.blog_date>=".$startTime." AND l.blog_date<=".$endTime.") OR ";
						break;
						case 14 :
							$justTime="'".substr($TopicId,0,4)."-".substr($TopicId,4,2)."-".substr($TopicId,6,2)." ".substr($TopicId,8,2).":".substr($TopicId,10,2).":".substr($TopicId,12,2)."'";
							$work_sql .= "l.blog_date=".$justTime." OR ";
						break;
						default : break;
					}
				}
				if ($work_sql)
					$add_sql .= " AND (".substr($work_sql,0,strlen($work_sql)-4).")";
			}
			if (count($tarray)) {
				$work_sql="";
				foreach($tarray as $TopicId) {
					switch(strlen($TopicId)) {
						case 8:
							$startTime="'".substr($TopicId,0,4)."-".substr($TopicId,4,2)."-".substr($TopicId,6,2)." 00:00:00'";
							$endTime="'".substr($TopicId,0,4)."-".substr($TopicId,4,2)."-".substr($TopicId,6,2)." 23:59:59'";
							$work_sql .= "(l.blog_date>=".$startTime." AND l.blog_date<=".$endTime.") OR ";
						break;
						case 14 :
							$justTime="'".substr($TopicId,0,4)."-".substr($TopicId,4,2)."-".substr($TopicId,6,2)." ".substr($TopicId,8,2).":".substr($TopicId,10,2).":".substr($TopicId,12,2)."'";
							$work_sql .= "l.blog_date=".$justTime." OR ";
						break;
						default : break;
					}
				}
				if ($work_sql)
					$add_sql .= " AND NOT(".substr($work_sql,0,strlen($work_sql)-4).")";
			}
			if (count($Farray)) {
				$add_sql.=" AND (";
				foreach($Farray as $ForumId) {
					$add_sql .= "m.blogid=".$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 .= "m.blogid=".$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 .= "m.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 .= "m.cat_id=".$CatId." OR ";
				}
				$add_sql = substr($add_sql,0,strlen($add_sql)-4);
				$add_sql .= ")";
			}
		} else {
			$add_sql="";
		}

		//	The following example code grabs the latest entries from the module MyLinks
		$sql = "SELECT m.uid, m.cat_id, m.blogid, m.title as mtitle, l.title as ltitle, l.blog_date, l.post_text, c.cat_title FROM "
			.$xoopsDB->prefix("popnupblog_info")." m, ".$xoopsDB->prefix("popnupblog")." l, ".$xoopsDB->prefix("popnupblog_categories")
			." c  WHERE (m.blog_permission & 0x07)=0 and m.blogid=l.blogid and c.cat_id=m.cat_id".$add_sql." ORDER BY l.blog_date DESC";
		//echo $sql;

		$result = $xoopsDB->query($sql, $obj->getVar('rssfj_grab'), 0);
		while( $row = $xoopsDB->fetchArray($result) ){
		//	required items
			$link = XOOPS_URL.'/modules/'.$this->dirname.'/index.php?param='.$row['blogid']."-".preg_replace("(-| |:)","",$row['blog_date']);
												// The title of the item
			$desc=$this->modname." : ".$row['cat_title'].">>".$row['mtitle'].">>".$row['ltitle'];
			$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,
			$ret[$i]['timestamp'] = mktime(substr($row['blog_date'],11,2),substr($row['blog_date'],14,2),substr($row['blog_date'],17,2),substr($row['blog_date'],5,2),substr($row['blog_date'],8,2),substr($row['blog_date'],0,4));
												// must be in Unix time format
			$desc = $row['post_text'];			// The item synopsis, or 
												// description, whatever
			$ret[$i]['description'] = $myts->makeTareaData4Show($desc);
		//	optional items
			$ret[$i]['category'] = $this->modname;
			$ret[$i]['domain'] = XOOPS_URL.'/modules/'.$this->dirname.'/';
			
			$i++;
		}
		return $ret;
	}
}

?>