<?php
//  ------------------------------------------------------------------------ //
//                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 //
//  ------------------------------------------------------------------------ //
//            This module; ThemeChanger Copyright (c) 2005 suin              //
//                          <http://www.suin.jp>                             //
//  ------------------------------------------------------------------------ //

include( '../../../mainfile.php' );
include_once( XOOPS_ROOT_PATH.'/include/cp_header.php' );
include_once( XOOPS_ROOT_PATH.'/class/xoopsmodule.php' );
include_once( XOOPS_ROOT_PATH.'/class/xoopsformloader.php' );

global $xoopsUser, $xoopsDB;

//サニタイザ呼び出し
$myts =& MyTextSanitizer::getInstance();
$db =& Database::getInstance();

//オペレーション初期化
$op = isset( $_REQUEST['op'] ) ? $_REQUEST['op'] : "default";

//ディレクトリ名定義
$mydirname = basename( dirname( dirname( __FILE__ ) ) ) ;
$myurl = XOOPS_URL . "/modules/" . $mydirname ;

//Gチケットシステム呼び出し
include_once( XOOPS_ROOT_PATH.'/modules/'.$mydirname.'/include/gtickets.php' );

switch($op){
case 'list':
	xoops_cp_header();
	
	include( XOOPS_ROOT_PATH.'/modules/'.$mydirname.'/admin/addform.php' );
	echo "\n".'<p>'._AM_HINTO.'</p>';
	echo "\n".'<table class="outer" cellSpacing="1" summary="theme and modules" style="width:100%"><tr>';
	echo "\n".'<th>'._AM_MODULES.'</th>';
	echo "\n".'<th>'._AM_THEMES.'</th>';
	echo "\n".'<th>'._AM_SITE_NAME.'</th>';
	echo "\n".'<th>'._AM_TITTLE.'</th>';
	echo "\n".'<th>'._AM_META_KEYWORDS.'</th>';
	echo "\n".'<th>'._AM_META_DESC.'</th>';
	echo "\n".'<th width="40">&nbsp;</th></tr>';
	
	//モジュールとのリンク
	$i = 0;
	$result = $db->query("SELECT c.lid, c.theme, c.title, c.subtitle, c.metakey, c.metadesc, m.name FROM ".$db->prefix("ctem_link")." c, ".$db->prefix("modules")." m WHERE c.lid=m.mid");
        while (list($lid, $theme, $title, $subtitle, $metakey, $metadesc, $name) = $db->fetchRow($result)) {
		$class = ( $i % 2 == 0 ) ? 'even' : 'odd' ; 
		echo "\n".'<tr class="'.$class.'">';
		echo "\n".'<td>'.$name.'</td>';
		echo "\n".'<td>'.$theme.'</td>';
		echo "\n".'<td>'.$title.'</td>';
		echo "\n".'<td>'.$subtitle.'</td>';
		echo "\n".'<td>'.$metakey.'</td>';
		echo "\n".'<td>'.$metadesc.'</td>';
		echo "\n".'<td>';
		echo "\n".'<a href="'.$myurl.'/admin/index.php?op=alt&amp;lid='.$lid.'">'._EDIT.'</a><br />';
		echo "\n".'<a href="'.$myurl.'/admin/index.php?op=del&amp;lid='.$lid.'">'._DELETE.'</a>';
		echo "\n".'</td>';
		echo "\n".'</tr>';
		$i++;
	}
	
	echo "\n".'<tr>';
	echo "\n".'<th>'._AM_PAGE.'</th>';
	echo "\n".'<th>'._AM_THEMES.'</th>';
	echo "\n".'<th>'._AM_SITE_NAME.'</th>';
	echo "\n".'<th>'._AM_TITTLE.'</th>';
	echo "\n".'<th>'._AM_META_KEYWORDS.'</th>';
	echo "\n".'<th>'._AM_META_DESC.'</th>';
	echo "\n".'<th width="40">&nbsp;</th></tr>';
	
	//ページとのリンク
	$i = 0;
	$result = $db->query("SELECT pagename, theme, title, subtitle, metakey, metadesc FROM ".$db->prefix("ctem_pagelink")." WHERE lid=1");
	while( list( $pagename, $theme, $title, $subtitle, $metakey, $metadesc ) = $db->fetchRow( $result ) ){
		$class = ( $i % 2 == 0 ) ? 'even' : 'odd' ; 
		echo "\n".'<tr class="'.$class.'">';
		echo "\n".'<td>'.$pagename.'</td>';
		echo "\n".'<td>'.$theme.'</td>';
		echo "\n".'<td>'.$title.'</td>';
		echo "\n".'<td>'.$subtitle.'</td>';
		echo "\n".'<td>'.$metakey.'</td>';
		echo "\n".'<td>'.$metadesc.'</td>';
		echo "\n".'<td>';
		echo "\n".'<a href="'.$myurl.'/admin/index.php?op=pagealt&amp;lid=1">'._EDIT.'</a>';
		echo "\n".'</td></tr>';
		$i++;
		
	}
	$result = $db->query("SELECT pagename, theme, title, subtitle, metakey, metadesc FROM ".$db->prefix("ctem_pagelink")." WHERE lid=0");
	list( $pagename, $theme, $title, $subtitle, $metakey, $metadesc ) = $db->fetchRow( $result );
	echo "\n".'<tr class="odd">';
	echo "\n".'<td>'.$pagename.'<br>('._AM_OTHERPAGES.')</td>';
	echo "\n".'<td>'.$theme.'</td>';
	echo "\n".'<td>'.$title.'</td>';
	echo "\n".'<td>'.$subtitle.'</td>';
	echo "\n".'<td>'.$metakey.'</td>';
	echo "\n".'<td>'.$metadesc.'</td>';
	echo "\n".'<td>';
	echo "\n".'<a href="'.$myurl.'/admin/index.php?op=pagealt&amp;lid=0">'._EDIT.'</a>';
	echo "\n".'</td></tr>';
	echo "\n".'</table>';
	echo "\n".'<div align="center" style="margin-top : 30px;">'._AM_CREDIT.'<br />'._AM_TRANSLATER.'</div>';
	xoops_cp_footer();
	break;
	
case 'add':
	
	if ( ! $xoopsGTicket->check() ) {
		redirect_header( $myurl.'/admin/index.php', 3, $xoopsGTicket->getErrors() );
		exit();
	}
	
	$lid      = isset( $_POST['lid'] )      ? intval( $_POST['lid'] ) : 0 ;
	$theme    = isset( $_POST['theme'] )    ? $myts->addSlashes( $_POST['theme'] ) : "" ;
	$title    = isset( $_POST['title'] )    ? $myts->addSlashes( $_POST['title'] ) : "" ;
	$subtitle = isset( $_POST['subtitle'] ) ? $myts->addSlashes( $_POST['subtitle'] ) : "" ;
	$metakey  = isset( $_POST['metakey'] )  ? $myts->addSlashes( $_POST['metakey'] ) : "" ;
	$metadesc = isset( $_POST['metadesc'] ) ? $myts->addSlashes( $_POST['metadesc'] ) : "" ;
	
	//エラー記憶初期化
	$stop = '';
	
	//リンクが既に存在するか
 	$sql = "SELECT COUNT(*) FROM ".$db->prefix('ctem_link')." WHERE lid='".$lid."'";
	list($count) = $db->fetchRow( $db->query($sql) );
	if($count > 0){
		$stop .= _AM_ALREADY_REGISTERED;
	}
	
	//モジュールが存在するか
 	$sql = "SELECT COUNT(*) FROM ".$db->prefix('modules')." WHERE mid='".$lid."'";
	list($count) = $db->fetchRow( $db->query($sql) );
	if($count == 0){
		$stop .= _AM_THIS_MODULE_NOTFOUND;
	}
	
	//テーマが存在するか
	if( !file_exists( XOOPS_ROOT_PATH."/themes/".$theme."/theme.html" ) ){
		$stop .= _AM_THIS_THEME_NOTFOUND;
	}
	
	if( empty( $stop ) ){
		//保存
		$sql  = "INSERT INTO ".$db->prefix("ctem_link")." (lid, theme, title, subtitle, metakey, metadesc) ";
		$sql .= "VALUES(".$lid.", '".$theme."', '".$title."', '".$subtitle."', '".$metakey."', '".$metadesc."')"; 
		$db->query($sql);
		
		//キャッシュクリア
		if( file_exists(XOOPS_CACHE_PATH."/theme_changer_routine.php") ){
			unlink(XOOPS_CACHE_PATH."/theme_changer_routine.php");
		}
		
		redirect_header( $myurl.'/admin/index.php?op=list', 2, _AM_REGSTERED);
		exit();
	}else{
		xoops_cp_header();
		echo $stop;
		include( XOOPS_ROOT_PATH.'/modules/'.$mydirname.'/admin/addform.php' );
		xoops_cp_footer();
		exit();
	}
	break;
	
case 'alt':
	$lid = isset( $_GET['lid'] ) ? intval( $_GET['lid'] ) : 0 ;
	
	if( $lid == 0 ){
		redirect_header( $myurl.'/admin/index.php?op=list', 5, _AM_THIS_MODULE_NOTFOUND);
		exit();
	}
	
	$result = $db->query("SELECT theme, title, subtitle, metakey, metadesc FROM ".$db->prefix("ctem_link")." WHERE lid=".$lid);
	list( $theme, $title, $subtitle, $metakey, $metadesc ) = $db->fetchRow( $result );
	
	if( empty( $theme ) ){
		redirect_header( $myurl.'/admin/index.php?op=list', 5, _AM_THIS_PAIR_NOTFOUND );
		exit();
	}
	
	xoops_cp_header();
	$form = new XoopsThemeForm(_AM_EDIT_PAIR_OF_THEME_MODULE, "form", "index.php");
	$module_radio = new XoopsFormSelect(_AM_MODULES,'lid', $lid);
		$module_handler =& xoops_gethandler('module');
		$criteria = new CriteriaCompo(new Criteria('hasmain', 1));
		$criteria->add(new Criteria('isactive', 1));
		$module_list =& $module_handler->getList($criteria);
		ksort($module_list);
	$module_radio->addOptionArray($module_list);
	$form->addElement($module_radio);
	$dirname = XOOPS_THEME_PATH.'/';
	$dirlist = array();
		if (is_dir($dirname) && $handle = opendir($dirname)) {
			while (false !== ($file = readdir($handle))) {
				if ( !preg_match("/^[\.]{1,2}$/",$file) ) {
					if (strtolower($file) != 'cvs' && is_dir($dirname.$file) && $file!='z_changeable_theme' ) {
						$dirlist[$file]=$file;
					}
				}
			}
			closedir($handle);
			asort($dirlist);
			reset($dirlist);
		}
	$theme_select = new XoopsFormSelect(_AM_THEMES,'theme' , $theme);
	$theme_select->addOptionArray($dirlist);
	$form->addElement($theme_select);
	$form->addElement(new XoopsFormText(_AM_SITE_NAME,'title', 50, 50, $title));
	$form->addElement(new XoopsFormText(_AM_TITTLE,'subtitle', 50, 50, $subtitle));
	$form->addElement(new XoopsFormTextArea(_AM_META_KEYWORDS,'metakey', $metakey));
	$form->addElement(new XoopsFormTextArea(_AM_META_DESC,'metadesc', $metadesc));
	$form->addElement(new XoopsFormHidden('op', 'updata'));
	$form->addElement(new XoopsFormButton('', 'submit', _SEND, 'submit'));
	//チケット発行
	$form->addElement($xoopsGTicket->getTicketXoopsForm( __LINE__ ));
	$form->display();
	xoops_cp_footer();
	
	break;
	
case 'pagealt':
	$lid = isset( $_GET['lid'] ) ? intval( $_GET['lid'] ) : 0 ;
	
	$result = $db->query("SELECT pagename, theme, title, subtitle, metakey, metadesc FROM ".$db->prefix("ctem_pagelink")." WHERE lid=".$lid);
	list( $pagename, $theme, $title, $subtitle, $metakey, $metadesc ) = $db->fetchRow( $result );
	
	if( empty( $theme ) ){
		redirect_header( $myurl.'/admin/index.php?op=list', 5, _AM_THIS_PAIR_NOTFOUND );
		exit();
	}
	
	xoops_cp_header();
	$form = new XoopsThemeForm(_AM_EDIT_PAIR_OF_THEME_PAGE, "form", "index.php");
	$form->addElement(new XoopsFormHidden('lid', $lid));
	$form->addElement(new XoopsFormLabel(_AM_PAGE, $pagename));
	$dirname = XOOPS_THEME_PATH.'/';
	$dirlist = array();
		if (is_dir($dirname) && $handle = opendir($dirname)) {
			while (false !== ($file = readdir($handle))) {
				if ( !preg_match("/^[\.]{1,2}$/",$file) ) {
					if (strtolower($file) != 'cvs' && is_dir($dirname.$file) && $file!='z_changeable_theme' ) {
						$dirlist[$file]=$file;
					}
				}
			}
			closedir($handle);
			asort($dirlist);
			reset($dirlist);
		}
	$theme_select = new XoopsFormSelect(_AM_THEMES,'theme' , $theme);
	$theme_select->addOptionArray($dirlist);
	$form->addElement($theme_select);
	$form->addElement(new XoopsFormText(_AM_SITE_NAME,'title', 50, 50, $title));
	$form->addElement(new XoopsFormText(_AM_TITTLE,'subtitle', 50, 50, $subtitle));
	$form->addElement(new XoopsFormTextArea(_AM_META_KEYWORDS,'metakey', $metakey));
	$form->addElement(new XoopsFormTextArea(_AM_META_DESC,'metadesc', $metadesc));
	$form->addElement(new XoopsFormHidden('op', 'pageupdata'));
	$form->addElement(new XoopsFormButton('', 'submit', _SEND, 'submit'));
	//チケット発行
	$form->addElement($xoopsGTicket->getTicketXoopsForm( __LINE__ ));
	$form->display();
	xoops_cp_footer();
	
	break;
	
case 'updata':
	
	if ( ! $xoopsGTicket->check() ) {
		redirect_header( $myurl.'/admin/index.php', 3, $xoopsGTicket->getErrors() );
		exit();
	}
	
	$lid      = isset( $_POST['lid'] )      ? intval( $_POST['lid'] ) : 0 ;
	$theme    = isset( $_POST['theme'] )    ? $myts->addSlashes( $_POST['theme'] ) : "" ;
	$title    = isset( $_POST['title'] )    ? $myts->addSlashes( $_POST['title'] ) : "" ;
	$subtitle = isset( $_POST['subtitle'] ) ? $myts->addSlashes( $_POST['subtitle'] ) : "" ;
	$metakey  = isset( $_POST['metakey'] )  ? $myts->addSlashes( $_POST['metakey'] ) : "" ;
	$metadesc = isset( $_POST['metadesc'] ) ? $myts->addSlashes( $_POST['metadesc'] ) : "" ;
	
	$stop = '';
 	$sql = "SELECT COUNT(*) FROM ".$db->prefix('ctem_link')." WHERE lid='".$lid."'";
	list($count) = $db->fetchRow( $db->query($sql) );
	
	if( $count == 0 ){
		$stop .= _AM_THIS_PAIR_NOTFOUND;
	}
	
	if( !file_exists( XOOPS_ROOT_PATH."/themes/".$theme."/theme.html" ) ){
		$stop .= _AM_THIS_THEME_NOTFOUND;
	}
	
	if( empty( $stop ) ){
		
		$sql = "UPDATE ".$db->prefix("ctem_link")." SET theme = '".$theme."', title = '".$title."', subtitle = '".$subtitle."', metakey = '".$metakey."', metadesc = '".$metadesc."' WHERE lid = ".$lid;
		$db->query($sql);
		//キャッシュクリア
		if( file_exists(XOOPS_CACHE_PATH."/theme_changer_routine.php") ){
			unlink(XOOPS_CACHE_PATH."/theme_changer_routine.php");
		}
		redirect_header( $myurl.'/admin/index.php?op=list', 2, _AM_RENEWED );
		exit();
	}else{
		xoops_cp_header();
		echo $stop;
		include( XOOPS_ROOT_PATH.'/modules/'.$mydirname.'/admin/addform.php' );
		xoops_cp_footer();
	}
	break;
	
case 'pageupdata':
	
	if ( ! $xoopsGTicket->check() ) {
		redirect_header( $myurl.'/admin/index.php', 3, $xoopsGTicket->getErrors() );
		exit();
	}
	
	$lid      = isset( $_POST['lid'] )      ? intval( $_POST['lid'] ) : 0 ;
	$theme    = isset( $_POST['theme'] )    ? $myts->addSlashes( $_POST['theme'] ) : "" ;
	$title    = isset( $_POST['title'] )    ? $myts->addSlashes( $_POST['title'] ) : "" ;
	$subtitle = isset( $_POST['subtitle'] ) ? $myts->addSlashes( $_POST['subtitle'] ) : "" ;
	$metakey  = isset( $_POST['metakey'] )  ? $myts->addSlashes( $_POST['metakey'] ) : "" ;
	$metadesc = isset( $_POST['metadesc'] ) ? $myts->addSlashes( $_POST['metadesc'] ) : "" ;
	
	$stop = '';
 	$sql = "SELECT COUNT(*) FROM ".$db->prefix('ctem_pagelink')." WHERE lid='".$lid."'";
	list($count) = $xoopsDB->fetchRow( $db->query($sql) );
	
	if( $count == 0 ){
		$stop .= _AM_THIS_PAIR_NOTFOUND;
	}
	
	if( !file_exists( XOOPS_ROOT_PATH."/themes/".$theme."/theme.html" ) ){
		$stop .= _AM_THIS_THEME_NOTFOUND;
	}

	if( empty( $stop ) ){
		$sql = "UPDATE ".$db->prefix("ctem_pagelink")." SET theme = '".$theme."', title = '".$title."', subtitle = '".$subtitle."', metakey = '".$metakey."', metadesc = '".$metadesc."' WHERE lid = ".$lid;
		$db->query($sql);
		//キャッシュクリア
		if( file_exists(XOOPS_CACHE_PATH."/theme_changer_routine.php") ){
			unlink(XOOPS_CACHE_PATH."/theme_changer_routine.php");
		}
		redirect_header( $myurl.'/admin/index.php?op=list', 2, _AM_RENEWED );
		exit();
	}else{
		xoops_cp_header();
		echo $stop;
		include( XOOPS_ROOT_PATH.'/modules/'.$mydirname.'/admin/addform.php' );
		xoops_cp_footer();
	}
	break;
case 'del':
	$lid = isset( $_GET['lid'] ) ? intval( $_GET['lid'] ) : 0 ;
	if( $lid == 0 ){
		redirect_header( $myurl.'/admin/index.php?op=list', 5, _AM_THIS_MODULE_NOTFOUND);
		exit();
	}
	xoops_cp_header();
	xoops_confirm(array('op' => 'delete', 'lid' => $lid ,'XOOPS_G_TICKET' => $xoopsGTicket->issue( __LINE__ ) ), 'index.php', _AM_REALLY_DEL);
	xoops_cp_footer();
	break;
	
case 'delete':
	
	if ( ! $xoopsGTicket->check() ) {
		redirect_header( $myurl.'/admin/index.php', 3, $xoopsGTicket->getErrors() );
		exit();
	}
	
	$lid = isset( $_POST['lid'] ) ? intval( $_POST['lid'] ) : 0 ;
	if($lid == 0){
		redirect_header($myurl.'/admin/index.php?op=list', 2, _AM_COULDNT_DEL);
		exit();
	}else{
		$sql = "DELETE FROM ".$db->prefix("ctem_link")." WHERE lid = ".$lid;
		$db->query($sql);
		//キャッシュクリア
		if( file_exists(XOOPS_CACHE_PATH."/theme_changer_routine.php") ){
			unlink(XOOPS_CACHE_PATH."/theme_changer_routine.php");
		}
		
		redirect_header($myurl.'/admin/index.php?op=list', 2, _AM_DELETED);
		exit();
	}
	break;
	
case 'default':
default:
	if ( file_exists(XOOPS_ROOT_PATH."/modules/".$mydirname."/language/".$xoopsConfig['language']."/modinfo.php") ) {
		include( XOOPS_ROOT_PATH."/modules/".$mydirname."/language/".$xoopsConfig['language']."/modinfo.php" );
	} else {
		include( XOOPS_ROOT_PATH."/modules/".$mydirname."/language/japanese/modinfo.php" );
	}
	include_once( XOOPS_ROOT_PATH.'/modules/'.$mydirname.'/admin/menu.php' );
	xoops_cp_header();
        echo "\n".'<table width="100%" border="0" cellspacing="1" class="outer">';
        echo "\n".'<tr><td class="odd">';
        echo "\n".'<a href="index.php"><h4>' ._MI_THEMEC_NAME. '</h4></a>';
        echo "\n".'<table border="0" cellpadding="4" cellspacing="1" width="100%">';
	while( list($k, $v) = each($adminmenu) )
	{
	        echo "\n".'<tr class="bg1" align="left">';
	        echo "\n".'<td><span class="fg2"><a href="'.XOOPS_URL.'/modules/'.$mydirname.'/'.$adminmenu[$k]['link']. '">' .$adminmenu[$k]['title']. '</a></span></td>';
	        echo "\n".'<td><span class="fg2">' .$adminmenu[$k]['desc']. '</span></td>';
	        echo "\n".'</tr>';
	}
//	echo "\n".'<tr class="bg1" align="left">';
//	echo "\n".'<td><span class="fg2"><a href="'.XOOPS_URL.'/modules/system/admin.php?fct=preferences&op=showmod&mod='.$module->getvar('mid').'">'._PREFERENCES.'</a></span></td>';
//	echo "\n".'<td><span class="fg2"></span></td>';
//	echo "\n".'</tr>';
        echo "\n".'</table>';
        echo "\n".'</td></tr>';
        echo "\n".'</table>';
	echo "\n".'<div align="center" style="margin-top : 30px;">'._AM_CREDIT.'<br />'._AM_TRANSLATER.'</div>';
	xoops_cp_footer();
	break;
}
?>