<?php
// $Id: index.php,v 1.1 2004/07/11 10:28:06 Farsus Exp $
// -------------------------------------------------------------------------//
//			                 RUNCMS                                         //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Half-Dead
// Author Website : http://www.e-xoops.com
// License Type   : Proprietary: See /manual/LICENSES/E-Xoops.txt
// ------------------------------------------------------------------------- //

include_once("./admin_header.php");

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function admin_headlines() {
global $db, $myts;

xoops_cp_header();
OpenTable();

?>
<form name="order" action="./index.php" method="post">
<table border="0" cellpadding="0" cellspacing="0" valign="top" width="100%"><tr>

<td class="bg2">
<table width="100%" border="0" cellpadding="4" cellspacing="1"><tr class="bg3" align="center">
<td><b><?php echo _AM_SITENAME;?></b></td>
<td><b><?php echo _TYPE;?></b></td>
<td><b><?php echo _AM_ITEMS;?></b></td>
<td><b><?php echo _AM_CACHE;?></b></td>
<td><b><?php echo _AM_TEMPLATE;?></b></td>
<td><b><?php echo _STATUS;?></b></td>
<td><b><?php echo _AM_WEIGHT;?></b></td>
<td><b><?php echo _AM_FUNCTION;?></b></td>
<?php

$result = $db->query("SELECT id, name, url, status, template, cache, items, type, weight FROM ".$db->prefix("headlines")." ORDER BY status DESC, weight ASC, name ASC");

while ( list($id, $name, $url, $status, $template, $cache, $items, $type, $weight) = $db->fetch_row($result) ) {
	$name     = $myts->makeTboxData4Show($name);
	$url      = $myts->makeTboxData4Show($url);
	$template = str_replace('.txt', '', $template);
	echo "
	<tr class='bg1'>
	<td><a href='$url' target='new'>$name</a></td>
	<td>$type</td>
	<td>$items</td>
	<td>$cache</td>
	<td>$template</td>";
	if ($status > 0) {
		$stat = "<b><u><i>"._ACTIVE."<i></u></b>";
		} else {
			$stat = _INACTIVE;
		}
	echo "<td>$stat</td><td>";
	if ($status == 1) {
		echo "<input type='text' class='text' name='weight[$id]' value='$weight' size='3' />";
		} else {
			echo "&nbsp;";
		}
	echo "</td><td><a href='./index.php?op=edit_headlines&amp;id=$id'>"._EDIT."</a> | <a href='./index.php?op=delete_headlines&amp;id=$id&amp;ok=0'>"._DELETE."</a></td></tr>";
}

?>
<tr>
<td colspan="8" align="center" class="bg3">
<input type="hidden" name="op" value="reorder_headlines" />
<input type="submit" class="button" value="<?php echo _AM_REORDER;?>" />
</td>
</tr></table></td>
</tr></table>
</form>

<br /><br />
<h4 style="text-align:left;"><?php echo _AM_ADDHEADL;?></h4>
<form name="new" action="./index.php" method="post">
<table border="0" cellpadding="0" cellspacing="0" valign="top" width="100%"><tr>

<td class="bg2">
<table width="100%" border="0" cellpadding="4" cellspacing="1"><tr>

<td class="bg3"><b><?php echo _AM_SITENAMET;?></b></td>
<td class="bg1"><input type="text" class="text" name="name" size="31" maxlength="30" /></td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_URLT;?></b></td>
<td class="bg1"><input type="text" class="text" name="url" size="50" maxlength="100" /></td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_URLEDFXMLT;?></b></td>
<td class="bg1"><input type="text" class="text" name="headlineurl" size="50" maxlength="200" /></td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_TEMPLATE;?></b></td>
<td class="bg1"><select class="select" name="template">
<?php

$path = "../templates/";
if ( $handle = opendir($path) ) {
	while (false !== ($file = readdir($handle))) {
		if ( @!is_dir($path.$file) ) {
			$text = str_replace('.txt', '', $file);
			if ($file == 'default.txt') { $sel = "selected";
				} else {
					$sel = '';
				}
				echo "<option value='$file'$sel>$text</option>";
		}
	}
	closedir($handle);
}

?>
</select>
</td>

</tr><tr>

<td class="bg3"><b><?php echo _TYPE;?></b></td>
<td class="bg1">
<select class="select" name="type">
<option value="block">block</option>
<option value="main">main</option>
<option value="both">both</option>
</select>
</td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_CACHE;?></b></td>
<td class="bg1">
<select class="select" name="cache">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="60">60</option>
<option value="90">90</option>
<option value="120">120</option>
<option value="180">180</option>
<option value="240">240</option>
<option value="520">520</option>
</select>
</td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_ITEMS;?></b></td>
<td class="bg1">
<select class="select" name="items">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
</select>
</td>

</tr><tr>

<td class="bg3"><b><?php echo _STATUS;?>:</b></td>
<td class="bg1">
<select class="select" name="status">
<option value="1"><?php echo _ACTIVE;?></option>
<option value="0" selected="selected"><?php echo _INACTIVE;?></option>
</select></td>

</tr><tr>

<td class="bg3">&nbsp;</td>
<td class="bg1">
<input type="hidden" name="op" value="add_headlines" />
<input type="submit" class="button" value="<?php echo _ADD;?>" />
</td>

</tr></table>
</td>
</tr></table>
</form>
<?php

CloseTable();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function edit_headlines($id) {
global $db, $myts;

xoops_cp_header();

$result = $db->query("SELECT name, url, headlineurl, template, cache, items, type, status FROM ".$db->prefix("headlines")." WHERE id=$id");
list($name, $url, $headlineurl, $template, $cache, $items, $type, $status) = $db->fetch_row($result);
$name        = $myts->makeTboxData4Edit($name);
$url         = $myts->makeTboxData4Edit($url);
$template    = $myts->makeTboxData4Edit($template);
$headlineurl = $myts->makeTboxData4Edit($headlineurl);

OpenTable();

?>
<h4 style="text-align:left;"><?php echo _AM_EDITHEADL;?></h4>
<form action="./index.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>" />
<table border="0" cellpadding="0" cellspacing="0" valign="top" width="100%"><tr>

<td class="bg2">
<table width="100%" border="0" cellpadding="4" cellspacing="1"><tr>
<td class="bg3"><b><?php echo _AM_SITENAMET;?></b></td>
<td class="bg1"><input type="text" class="text" name="name" size="31" maxlength="30" value="<?php echo $name;?>" /></td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_URLT;?></b></td>
<td class="bg1"><input type="text" class="text" name="url" size="50" maxlength="100" value="<?php echo $url;?>" /></td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_URLEDFXMLT;?></b></td>
<td class="bg1"><input type="text" class="text" name="headlineurl" size="50" maxlength="200" value="<?php echo $headlineurl;?>" /></td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_TEMPLATE;?></b></td>
<td class="bg1">
<select class="select" name="template">
<?php

$path = "../templates/";
if ( $handle = opendir($path) ) {
	while (false !== ($file = readdir($handle))) {
		if ( @!is_dir($path.$file) ) {
			$text = str_replace('.txt', '', $file);
			if ($file == $template) { $sel = "selected";
				} else {
					$sel = '';
				}
				echo "<option value='$file'$sel>$text</option>";
		}
	}
	closedir($handle);
}

?>
</select>
</td>

</tr><tr>

<td class="bg3"><b><?php echo _TYPE;?></b></td>
<td class="bg1">
<select class="select" name="type">
<option value="<?php echo $type;?>" selected><?php echo $type;?></option>
<option value="block">block</option>
<option value="main">main</option>
<option value="both">both</option>
</select>
</td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_CACHE;?></b></td>
<td class="bg1">
<select class="select" name="cache">
<option value="<?php echo $cache;?>" selected><?php echo $cache;?></option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="60">60</option>
<option value="90">90</option>
<option value="120">120</option>
<option value="180">180</option>
<option value="240">240</option>
<option value="520">520</option>
</select>
</td>

</tr><tr>

<td class="bg3"><b><?php echo _AM_ITEMS;?></b></td>
<td class="bg1">
<select class="select" name="items">
<option value="<?php echo $items;?>" selected><?php echo $items;?></option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
</select>
</td>

</tr><tr>

<td class="bg3"><b><?php echo _STATUS;?>:</b></td>
<td class="bg1">
<select class="select" name="status">
<?php

if ($status > 0) {
	$sel_a = "selected='selected'";
	} else {
		$sel_i = "selected='selected'";
	}

?>
<option value="1" <?php echo $sel_a;?>><?php echo _ACTIVE;?></option>
<option value="0" <?php echo $sel_i;?>><?php echo _INACTIVE;?></option>
</select></td>

</tr><tr>

<td class="bg3">&nbsp;</td>
<td class="bg1">
<input type="hidden" name="op" value="save_headlines" />
<input type="submit" class="button" value="<?php echo _SAVE;?>" />
</td>

</tr></table>
</td></tr></table>
</form>
<?php

CloseTable();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function save_headlines($id) {
global $db, $myts, $HTTP_POST_VARS;

$name        = $myts->makeTboxData4Save(trim($HTTP_POST_VARS['name']));
$headlineurl = $myts->makeTboxData4Save(trim($HTTP_POST_VARS['headlineurl']));
$url         = $myts->makeTboxData4Save(trim($HTTP_POST_VARS['url']));
$template    = $HTTP_POST_VARS['template'];
$status      = $HTTP_POST_VARS['status'];
$cache       = $HTTP_POST_VARS['cache'];
$items       = $HTTP_POST_VARS['items'];
$type        = $HTTP_POST_VARS['type'];

$db->query("UPDATE ".$db->prefix("headlines")." SET name='$name', url='$url', headlineurl='$headlineurl', status=$status, template='$template', cache='$cache', items='$items', type='$type' WHERE id=$id");
redirect_header("./index.php", 1, _UPDATED);
exit();
}


//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function add_headlines() {
global $db, $myts, $HTTP_POST_VARS;

$name        = $myts->makeTboxData4Save(trim($HTTP_POST_VARS['name']));
$headlineurl = $myts->makeTboxData4Save(trim($HTTP_POST_VARS['headlineurl']));
$url         = $myts->makeTboxData4Save(trim($HTTP_POST_VARS['url']));
$template    = $HTTP_POST_VARS['template'];
$status      = $HTTP_POST_VARS['status'];
$cache       = $HTTP_POST_VARS['cache'];
$items       = $HTTP_POST_VARS['items'];
$type        = $HTTP_POST_VARS['type'];

$newid = $db->genId($db->prefix("headlines")."_id_seq");
$db->query("INSERT INTO ".$db->prefix("headlines")." SET id=$newid, name='$name', url='$url', headlineurl='$headlineurl', status=$status, template='$template', cache='$cache', items='$items', type='$type'");
redirect_header("./index.php", 1, _UPDATED);
exit();
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function delete_headlines($id, $ok=0) {
global $db;

if ( $ok == 1 ) {
	$db->query("DELETE FROM ".$db->prefix("headlines")." WHERE id=$id");
	redirect_header("./index.php", 1, _UPDATED);
	exit();
	} else {
		xoops_cp_header();
		OpenTable();
		echo "<b>"._AM_WANTDEL."</b><br /><br /><table><tr><td>";
		echo myTextForm("./index.php?op=delete_headlines&id=$id&ok=1", _YES);
		echo "</td><td>";
		echo myTextForm("./index.php", _NO);
		echo "</td></tr></table>";
		CloseTable();
	}
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function reorder_headlines($weight) {
global $db;

foreach ($weight as $id=>$order) {
	if ( isset($id) && is_numeric($id) && isset($order) ) {
		if ( !is_numeric($order) ) {
			$order = 0;
			}
		$db->query("UPDATE ".$db->prefix("headlines")." SET weight='$order' WHERE id=$id");
	}
}
redirect_header("./index.php", 1, _UPDATED);
}

//---------------------------------------------------------------------------------------//
$op = !empty($HTTP_POST_VARS['op']) ? $HTTP_POST_VARS['op'] : $HTTP_GET_VARS['op'];

switch($op) {

	case "delete_headlines":
		delete_headlines($HTTP_GET_VARS['id'], $HTTP_GET_VARS['ok']);
		break;

	case "add_headlines":
		add_headlines();
		break;

	case "save_headlines":
		save_headlines($HTTP_POST_VARS['id']);
		break;

	case "edit_headlines":
		edit_headlines($HTTP_GET_VARS['id']);
		break;

	case "reorder_headlines":
		reorder_headlines($HTTP_POST_VARS['weight']);
		break;

	default:
		admin_headlines();
		break;
}
xoops_cp_footer();
?>
