<?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                                                         ##
###############################################################################
include 'admin_header.php';
include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
define('_THIS_PAGE', RSSFITJ_URL.'admin/index.php');
$op = '';
if( isset($_POST['op']) ){
	$op = trim($_POST['op']);
}elseif( isset($_GET['op']) ){
	$op = trim($_GET['op']);
}

switch($op){
default:
	adminHtmlHeader();
break;

case 'intro':
	adminHtmlHeader();
	$intro = grabIntro();
	$myts =& MyTextSanitizer::getInstance();
	$title = $myts->makeTboxData4Edit($intro['title']);
	$content = $myts->makeTareaData4Edit($intro['content']);
	$text = new XoopsFormText(_AM_EDIT_INTRO_TITLE, 'title', 50, 255, $title);
	$text->setDescription(_AM_EDIT_INTRO_TITLE_DESC);
	$tarea = new XoopsFormDhtmlTextArea(_AM_EDIT_INTRO_TEXT, 'content', $content, 15, 60);
	$tarea->setDescription(_AM_EDIT_INTRO_TEXT_DESC);
	$button = new XoopsFormButton('', 'submit', _AM_SUBMIT, 'submit');
	$hidden = new XoopsFormHidden('op', 'saveintro');
	$form = new XoopsThemeForm(_AM_EDIT_INTRO, 'editintro', _THIS_PAGE);
	$form->addElement($text);
	$form->addElement($tarea);
	$form->addElement($button);
	$form->addElement($hidden);
	$form->display();
break;

case 'saveintro':
	$myts =& MyTextSanitizer::getInstance();
	$title = $myts->makeTboxData4Save($_POST['title']);
	$content = $myts->makeTareaData4Save($_POST['content']);
	$sql = 'UPDATE '.$xoopsDB->prefix('rssfitJ_misc')." SET misc_title = '".$title."', misc_content = '".$content."'  WHERE misc_category = 'intro'";
	if( !$result = $xoopsDB->query($sql) ){
		$err = $xoopsDB->error();
		adminHtmlHeader();
		echo $err.'<br />'.$sql;
	}else{
		redirect_header(_THIS_PAGE.'?op=intro', 0, _AM_DBUPDATED);
	}
break;

case 'plugins':
	$ret = '';
	adminHtmlHeader();
	// activated plugins
	$criteria = new Criteria('rssfj_activated', 1);
	$criteria->setSort('rssfj_order');
	if( $plugins =& $rssfj_mgr->getObjects($criteria) ){
		$ret .= "<table class='outer' width='100%'>\n"
			."<tr><th colspan='5'>"._AM_PLUGIN_ACTIVATED."</th></tr>\n"
			."<tr>\n<td class='head' align='center' width='33%'>"._AM_PLUGIN_FILENAME."</td>\n"
			."<td class='head' align='center'>"._AM_PLUGIN_MODNAME."</td>\n"
			."<td class='head' align='center'>"._AM_PLUGIN_SHOWXENTRIES."</td>\n"
			."<td class='head' align='center'>"._AM_PLUGIN_ORDER."</td>\n"
			."<td class='head' align='center' width='20%'>"._AM_ACTION."</td>\n"
			."</tr>\n";
		foreach( $plugins as $p ){
			if( $handler =& $rssfj_mgr->checkPlugin($p) ){
				$id = $p->getVar('rssfj_conf_id');
				$entries =& new XoopsFormText('', 'rssfj_grab['.$id.']', 3, 2, $p->getVar('rssfj_grab'));
				$order =& new XoopsFormText('', 'rssfj_order['.$id.']', 3, 2, $p->getVar('rssfj_order'));
				$action =& new XoopsFormSelect('', 'action['.$id.']', '');
				$action->addOption('', _SELECT);
				$action->addOption('d', _AM_PLUGIN_DEACTIVATE);
				$action->addOption('u', _AM_PLUGIN_UNINSTALL);
				$ret .= "<tr>\n"
					."<td class='odd' align='center'>"
						.$p->getVar('rssfj_filename')."</td>\n"
					."<td class='even' align='center'>"
						.$handler->modname."</td>\n"
					."<td class='odd' align='center'>"
						.$entries->render()."</td>\n"
					."<td class='odd' align='center'>"
						.$order->render()."</td>\n"
					."<td class='odd' align='center'>"
						.$action->render()."</td>\n"
					;
				$ret .= "</tr>\n";
			}else{
				$rssfj_mgr->forceDeactivate($p);
			}
		}
		$ret .= "</table>\n";
	}

	// inactive plugins
	if( $plugins =& $rssfj_mgr->getObjects(new Criteria('rssfj_activated', 0)) ){
		$ret .= "<br />\n<table class='outer' width='100%'>\n"
			."<tr><th colspan='3'>"._AM_PLUGIN_INACTIVE."</th></tr>\n"
			."<tr>\n<td class='head' align='center' width='33%'>"._AM_PLUGIN_FILENAME."</td>\n"
			."<td class='head' align='center'>"._AM_PLUGIN_MODNAME."</td>\n"
			."<td class='head' align='center' width='20%'>"._AM_ACTION."</td>\n"
			."</tr>\n";
		foreach( $plugins as $p ){
			$id = $p->getVar('rssfj_conf_id');
			$action =& new XoopsFormSelect('', 'action['.$id.']', '');
			$action->addOption('', _SELECT);
			$ret .= "<tr>\n"
				."<td class='odd' align='center'>"
					.$p->getVar('rssfj_filename')."</td>\n"
				."<td class='even' align='center'>";
			if( $handler =& $rssfj_mgr->checkPlugin($p) ){
				$ret .= $handler->modname;
				$action->addOption('a', _AM_PLUGIN_ACTIVATE);
			}else{
				if( count($p->getErrors()) > 0 ){
					$ret .= '<b>'._ERRORS."</b>\n";
					foreach( $p->getErrors() as $e ){
						$ret .= '<br />'.$e;
					}
				}else{
					$ret .= '<b>'._AM_PLUGIN_UNKNOWNERROR."</b>";
				}
			}
			$ret .= "</td>\n";
			$action->addOption('u', _AM_PLUGIN_UNINSTALL);
			$ret .= "<td class='odd' align='center'>"
				.$action->render()."</td>\n";
		}
		$ret .= "</table>\n";
	}

	// Non-installed plugins
	if( !$filelist =& $rssfj_mgr->getPluginFileList() ){
		$filelist = array();
	}
	$list =& XoopsLists::getFileListAsArray(RSSFITJ_ROOT_PATH.'plugins');
	$installable = array();
	foreach( $list as $f ){
		if( preg_match('/rssfitj\.+[a-zA-Z0-9_]+\.php/', $f) && !in_array($f, $filelist) ){
			$installable[] = $f;
		}
	}
	if( count($installable) > 0 ){
		$ret .= "<br />\n<table class='outer' width='100%'>\n"
			."<tr><th colspan='3'>"._AM_PLUGIN_NONINSTALLED."</th></tr>\n"
			."<tr>\n<td class='head' align='center' width='33%'>"._AM_PLUGIN_FILENAME."</td>\n"
			."<td class='head' align='center'>"._AM_PLUGIN_MODNAME."</td>\n"
			."<td class='head' align='center' width='20%'>"._AM_PLUGIN_INSTALL."</td>\n"
			."</tr>\n";
		foreach( $installable as $i ){
			$action =& new XoopsFormCheckbox('', 'install['.$i.']');
			$action->addOption('i', ' ');
			$ret .= "<tr>\n"
				."<td class='odd' align='center'>"
					.$i."</td>\n"
				."<td class='even' align='center'>";
			$p =& $rssfj_mgr->create();
			$p->setVar('rssfj_filename', $i);
			if( $handler =& $rssfj_mgr->checkPlugin($p) ){
				$ret .= $handler->modname;
			}else{
				if( count($p->getErrors()) > 0 ){
					$ret .= '<b>'._ERRORS."</b>\n";
					foreach( $p->getErrors() as $e ){
						$ret .= '<br />'.$e;
					}
				}else{
					$ret .= '<b>'._AM_PLUGIN_UNKNOWNERROR."</b>";
				}
				$action->setExtra('disabled="disabled"');
			}
			$ret .= "</td>\n";
			$ret .= "<td class='odd' align='center'>"
				.$action->render()."</td>\n";
		}
		$ret .= "</table>\n";
	}
	
	if( !empty($ret) ){
		$button = new XoopsFormButton('', 'submit', _AM_SUBMIT, 'submit');
		$hidden = new XoopsFormHidden('op', 'saveplugins');
		$ret = "<form action='"._THIS_PAGE."' method='post'>\n".$ret
				."<br /><table class='outer' width='100%'><tr><td class='foot' align='center'>\n"
				.$button->render()."\n".$hidden->render()."\n</td></tr></table></form>"
				;
		echo $ret;
	}
break;

case 'saveplugins':
	extract($_POST);
	$err = '';
	if (isset($action))
		$keys = array_keys($action);
	else
		$keys = array();
	foreach( $keys as $k ){
		$plugin =& $rssfj_mgr->get($k);
		if( isset($rssfj_grab[$k]) ){
			$plugin->setVar('rssfj_grab', $rssfj_grab[$k]);
		}
		if (isset($rssfj_order[$k]))
			$plugin->setVar('rssfj_order', $rssfj_order[$k]);
		switch($action[$k]){
		default:
			$result = $rssfj_mgr->insert($plugin);
		break;

		case 'u':	// uninstall
			$result = $rssfj_mgr->delete($plugin);
		break;
		
		case 'd':	// deactivate
			$plugin->setVar('rssfj_activated', 0);
			$result = $rssfj_mgr->insert($plugin);
		break;
		
		case 'a':	// activate
			$plugin->setVar('rssfj_activated', 1);
			$result = $rssfj_mgr->insert($plugin);
		break;
		}
		if( !$result ){
			$err .= $plugin->getHtmlErrors();
		}
	}
	if( !empty($install) ){
		$files = array_keys($install);
		foreach( $files as $f ){
			$p =& $rssfj_mgr->create();
			$p->setVar('rssfj_filename', $f);
			$p->setVar('rssfj_activated', 1);
			$p->setVar('rssfj_grab', $xoopsModuleConfig['plugin_entries']);
			if( !$result = $rssfj_mgr->insert($p) ){
				$err .= $p->getHtmlErrors();
			}
		}
	}
	if( !empty($err) ){
		adminHtmlHeader();
		echo $err;
	}else{
		redirect_header(_THIS_PAGE.'?op=plugins', 0, _AM_DBUPDATED);
	}
break;

case 'param' :
	$ret = '';
	adminHtmlHeader();
	// activated plugins
	$criteria = new Criteria('rssfj_activated', 1);
	$criteria->setSort('rssfj_order');
	if( $plugins =& $rssfj_mgr->getObjects($criteria) ){
		$ret .= "<table class='outer' width='100%'>\n"
			."<tr><th colspan='5'>"._AM_PLUGIN_ACTIVATED."</th></tr>\n"
			."<tr>\n<td class='head' align='center' width='33%'>"._AM_PLUGIN_FILENAME."</td>\n"
			."<td class='head' align='center'>"._AM_PLUGIN_MODNAME."</td>\n"
			."<td class='head' align='center'>"._AM_PLUGIN_PARAMETERS."</td>\n"
			."</tr>\n";
		foreach( $plugins as $p ){
			if( $handler =& $rssfj_mgr->checkPlugin($p) ){
				$id = $p->getVar('rssfj_conf_id');
				$entries =& new XoopsFormTextArea('', 'rssfj_param['.$id.']', $p->getVar('rssfj_param'), 5, 60);
				$ret .= "<tr>\n"
					."<td class='odd' align='center'>"
						.$p->getVar('rssfj_filename')."</td>\n"
					."<td class='even' align='center'>"
						.$handler->modname."</td>\n"
					."<td class='odd' align='center'>"
						.$entries->render()."</td>\n"
					;
				$ret .= "</tr>\n";
			}else{
				$rssfj_mgr->forceDeactivate($p);
			}
		}
		$ret .= "</table>\n";
	}
	if( !empty($ret) ){
		$button = new XoopsFormButton('', 'submit', _AM_SUBMIT, 'submit');
		$hidden = new XoopsFormHidden('op', 'saveparam');
		$ret = "<form action='"._THIS_PAGE."' method='post'>\n".$ret
				."<br /><table class='outer' width='100%'><tr><td class='foot' align='center'>\n"
				.$button->render()."\n".$hidden->render()."\n</td></tr></table></form>"
				;
		echo $ret;
	}
break;

case 'saveparam' :
	extract($_POST);
	$err = '';
	if( !empty($rssfj_param) ){
		$files = array_keys($rssfj_param);
		foreach( $files as $f ){
			$p =& $rssfj_mgr->get($f);
			$p->setVar('rssfj_param', $rssfj_param[$f]);
			if( !$result = $rssfj_mgr->insert($p) ){
				$err .= $p->getHtmlErrors();
			}
		}
	}
	if( !empty($err) ){
		adminHtmlHeader();
		echo $err;
	}else{
		redirect_header(_THIS_PAGE.'?op=param', 0, _AM_DBUPDATED);
	}
break;

}
include 'footer.php';
xoops_cp_footer();
?>