<?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                                                         ##
###############################################################################
require_once XOOPS_ROOT_PATH.'/kernel/object.php';
define('RSSFITJ_PLUGINS_TABLE', $GLOBALS['xoopsDB']->prefix("rssfitJ_plugins"));

class RssjRssfitj extends XoopsObject{
	function RssjRssfitj(){
		$this->XoopsObject();
	//	key, data_type, value, req, max, opt
		$this->initVar("rssfj_conf_id", XOBJ_DTYPE_INT, NULL, false);
		$this->initVar("rssfj_filename", XOBJ_DTYPE_TXTBOX, NULL, false);
		$this->initVar("rssfj_activated", XOBJ_DTYPE_INT);
		$this->initVar("rssfj_grab", XOBJ_DTYPE_INT, NULL, true);
		$this->initVar("rssfj_order", XOBJ_DTYPE_INT, NULL);
		$this->initVar("rssfj_param", XOBJ_DTYPE_TXTAREA, NULL, false);
	}
}

class RssjRssfitjHandler{
	var $db;
	function RssjRssfitjHandler(&$db){
		$this->db =& $db;
	}
	function &getInstance(&$db){
		static $instance;
		if( !isset($instance) ){
			$instance = new RssjRssfitjHandler($db);
		}
		return $instance;
	}
	function &create(){
		return new RssjRssfitj();
	}

	function &get($id){
		$id = intval($id);
		if( $id > 0 ){
			$sql = "SELECT * FROM ".RSSFITJ_PLUGINS_TABLE." WHERE rssfj_conf_id=".$id;
			if( !$result = $this->db->query($sql) ){
				return false;
			}
			$numrows = $this->db->getRowsNum($result);
			if( $numrows == 1 ){
				$obj = new RssjRssfitj();
				$obj->assignVars($this->db->fetchArray($result));
				return $obj;
			}
		}
		return false;
	}

	function insert(&$obj, $force=false){
        if( strtolower(get_class($obj)) != 'rssjrssfitj'){
            return false;
        }
        if( !$obj->isDirty() ){
            return true;
        }
        if( !$obj->cleanVars() ){
            return false;
        }
		foreach( $obj->cleanVars as $k=>$v ){
			${$k} = $v;
		}
		
		if( $obj->isNew() || empty($rssfj_conf_id) ){
			$rssfj_conf_id = $this->db->genId(RSSFITJ_PLUGINS_TABLE."_rssfj_conf_id_seq");
			$sql = sprintf(
					"INSERT INTO %s (
					rssfj_conf_id,
					rssfj_filename,
					rssfj_activated,
					rssfj_grab,
					rssfj_order,
					rssfj_param
					) VALUES (
					%u, %s, %u, %u, %u, %s
					)",
					RSSFITJ_PLUGINS_TABLE,
					$rssfj_conf_id,
					$this->db->quoteString($rssfj_filename),
					$rssfj_activated,
					$rssfj_grab,
					$rssfj_order,
					$this->db->quoteString($rssfj_param)
					);
		}else{
			$sql = sprintf(
					"UPDATE %s SET
					rssfj_filename = %s,
					rssfj_activated = %u,
					rssfj_grab = %u,
					rssfj_order = %u,
					rssfj_param = %s
					WHERE rssfj_conf_id = %u",
					RSSFITJ_PLUGINS_TABLE,
					$this->db->quoteString($rssfj_filename),
					$rssfj_activated,
					$rssfj_grab,
					$rssfj_order,
					$this->db->quoteString($rssfj_param),
					$rssfj_conf_id
					);
		}
        if( false != $force ){
            $result = $this->db->queryF($sql);
        }else{
            $result = $this->db->query($sql);
        }
		if( !$result ){
			$obj->setErrors("Could not store data in the database.<br />".$this->db->error().' ('.$this->db->errno().')<br />'.$sql);
			return false;
		}
		if( empty($rssfj_conf_id) ){
			return $this->db->getInsertId();
		}
        $obj->assignVar('rssfj_conf_id', $rssfj_conf_id);
		return $rssfj_conf_id;
	}
	
	function delete(&$obj, $force = false){
		if( strtolower(get_class($obj)) != 'rssjrssfitj' ){
			return false;
		}
		$sql = "DELETE FROM ".RSSFITJ_PLUGINS_TABLE." WHERE rssfj_conf_id=".$obj->getVar("rssfj_conf_id")."";
        if( false != $force ){
            $result = $this->db->queryF($sql);
        }else{
            $result = $this->db->query($sql);
        }
		return true;
	}

	function &getObjects($criteria = null, $id_as_key = false){
		$ret = array();
		$limit = $start = 0;
		$sql = 'SELECT * FROM '.RSSFITJ_PLUGINS_TABLE;
		if( isset($criteria) && is_subclass_of($criteria, 'criteriaelement') ){
			$sql .= ' '.$criteria->renderWhere();
			if( $criteria->getSort() != '' ){
				$sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
			}
			$limit = $criteria->getLimit();
			$start = $criteria->getStart();
		}
		$result = $this->db->query($sql, $limit, $start);
		if( !$result ){
			return false;
		}
		while( $myrow = $this->db->fetchArray($result) ){
			$obj = new RssjRssfitj();
			$obj->assignVars($myrow);
			if( !$id_as_key ){
				$ret[] =& $obj;
			}else{
				$ret[$myrow['rssfj_conf_id']] =& $obj;
			}
			unset($obj);
		}
		return $ret;
	}
	
    function getCount($criteria = null){
		$sql = 'SELECT COUNT(*) FROM '.RSSFITJ_PLUGINS_TABLE;
		if( isset($criteria) && is_subclass_of($criteria, 'criteriaelement') ){
			$sql .= ' '.$criteria->renderWhere();
		}
		$result = $this->db->query($sql);
		if( !$result ){
			return false;
		}
		list($count) = $this->db->fetchRow($result);
		return $count;
	}
    
    function deleteAll($criteria = null){
		$sql = 'DELETE FROM '.RSSFITJ_PLUGINS_TABLE;
		if( isset($criteria) && is_subclass_of($criteria, 'criteriaelement') ){
			$sql .= ' '.$criteria->renderWhere();
		}
		if( !$result = $this->db->query($sql) ){
			return false;
		}
		return true;
	}
	
	function forceDeactivate(&$obj){
		$sql = 'UPDATE '.RSSFITJ_PLUGINS_TABLE.' SET rssfj_activated = 0 WHERE rssfj_conf_id = '.$obj->getVar('rssfj_conf_id');
		$this->db->queryF($sql);
		return true;
	}
	
	function getPluginFileList(){
		$sql = 'SELECT rssfj_filename FROM '.RSSFITJ_PLUGINS_TABLE;
		if( !$result = $this->db->query($sql) ){
			return false;
		}
		$ret="";
		while( list($filename) = $this->db->fetchRow($result) ){
			$ret[] =& $filename;
			unset($filename);
		}
		return $ret;
	}
	
	function checkPlugin(&$obj){
		global $module_handler;
		$file = RSSFITJ_ROOT_PATH.'plugins/'.$obj->getVar('rssfj_filename');
		if( file_exists($file) ){
			$require = require_once $file;
			$name = explode('.', $obj->getVar('rssfj_filename'));
			$class = 'Rssfitj'.ucfirst($name[1]);
			if( class_exists($class) ){
				$handler = new $class;
				if( !method_exists($handler, 'loadmodule') || !method_exists($handler, 'grabentries') ){
					$obj->setErrors(_AM_PLUGIN_FUNCNOTFOUND);
				}else{
					$dirname = $handler->dirname;
					if( !empty($dirname) && is_dir(XOOPS_ROOT_PATH.'/modules/'.$dirname) ){
						if( !$handler->loadModule() ){
							$obj->setErrors(_AM_PLUGIN_MODNOTFOUND);
						}else{
							return $handler;
						}
					}else{
						$obj->setErrors(_AM_PLUGIN_MODNOTFOUND);
					}
				}
			}else{
				$obj->setErrors(_AM_PLUGIN_CLASSNOTFOUND);
			}
		}else{
			$obj->setErrors(_AM_PLUGIN_FILENOTFOUND);
		}
		return false;
	}

}

?>