<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v0.3 www.exoops.info 2006/03 
//0.2 from exoops-catala.org
include_once('./admin_header.php');
include_once(XOOPS_ROOT_PATH.'/class/xoopsformloader.php');
// Get the action to perform.
$act = isset($_POST['act']) ? $_POST['act'] : $_GET['act'];
if(!function_exists($act) || $act == '') $act = 'show_cats';
// Call the action function!
$act();
// Done!
CloseTable();
xoops_cp_footer();
////////////////////////////////////////////////////////////////////////////////
// Action Functions
////////////////////////////////////////////////////////////////////////////////
function show_cats() {
    global $db;
        // Navigation Links
        $arcade_home = "<a href='./index.php'><font size=+1>Arcade Admin</font></a>";
        echo $arcade_home.'&nbsp;<font size=+1> >> </font>&nbsp;<font size=+1>'._MI_ARCADE_ADMENU2.'</font><br><br>';
    echo "<br>";
    $cats = Category::getAllWhere(false, 'cat_name');
    echo '<table width="100%" cellpadding=0 cellspacing=0><tr><td class="bg2">';
    echo '<table width="100%" cellpadding=1 cellspacing=1>';
    echo "<tr class='bg4'><td><b>"._CATEGORY."</b></td><td><b>"._MDA_GAME_COUNT."</b></td><td><b>"._MDA_ACTIVE."</b></td><td><b>"._MDA_ACTIONS."</b></td></tr>";
    foreach ($cats as $cat)
    {
         echo '<tr class="bg3">';
                 // Cat Name
         echo '<td>';
         echo "<a href='index.php?act=show_cat&cat_id=".$cat->cat_id."'><b>".$cat->cat_name."&nbsp</b></a>";
         echo '</td>';
                 // Game Count
         $sql2 = "SELECT count(*) from ".$db->prefix('arcade_games')." WHERE cat_id=".$cat->cat_id;
         $res2 = $db->query($sql2);
         list($count) = $db->fetchArray($res2);
         echo "<td>$count</td>";
                 // Active
         $isactive = ($cat->is_active == 1)? 'checked' : '';
         echo '<td>';
         echo "<input type=checkbox class=check name=is_active $isactive disabled>";
         echo '</td>';
                 // Actions
         echo '<td>';
         echo "<input type=button class=button value='"._EDIT."' onclick=\"javascript:window.location='arcade.php?act=edit_cat&cat_id=".$cat->cat_id."'\">&nbsp;";
         echo "<input type=button class=button value='"._DELETE."' onclick=\"javascript:window.location='arcade.php?act=delete_cat&cat_id=".$cat->cat_id."'\">&nbsp;";
         echo '</td>';
         echo '</tr>';
     }
     echo '</table>';
     echo '</td></tr></table><br />';
     add_cat_form();
}
function add_game() {	
        // Navigation Links
        $arcade_home = "<a href='./index.php'><font size=+1>Arcade Admin</font></a>";
        echo $arcade_home.'&nbsp;<font size=+1> >> </font>&nbsp;<font size=+1>'._MI_ARCADE_ADMENU3.'</font><br><br>';
    echo "<br>";
         echo "<form name='add_game' method='post' action='arcade.php' enctype='multipart/form-data'>";
         echo "<input type='hidden' name='act' value='add_game_submit'>";
         echo '<table width="100%" cellpadding=0 cellspacing=0><tr><td class="bg2">';
         echo '<table width="100%" cellpadding=1 cellspacing=1>';
         // Title
         $gtitle = new XoopsFormText(_MDA_GAME_TITLE, 'gtitle', 40, 40);
         echo '<tr class="bg3">';
         echo '<td><b>'._MDA_GAME_TITLE.':</b></td>';
         echo '<td class="bg1">'.$gtitle->render().'</td>';
         echo '</tr>';
                // Category
         $cats = Category::getAllWhere(false);
         $cat_id = new XoopsFormSelect(_MDA_CATEGORY, 'cat_id');
         foreach ($cats as $cat)
         {
             $cat_id->addOption($cat->cat_id, $cat->cat_name);
         }
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_CATEGORY.':</b></td>';
         echo '<td class="bg1">'.$cat_id->render().'</td>';
         echo '</tr>';
			
                // BGCOLOR
         $bgcolor = new XoopsFormText(_MDA_BGCOLOR, 'bgcolor', 40, 10, '000');
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_BGCOLOR.':</b><br />'._MDA_BGCOLOR_INFO.'</td>';
         echo '<td class="bg1">'.$bgcolor->render().'</td>';
         echo '</tr>';
                // Width/Height
         $gwidth = new XoopsFormText(_MDA_GWIDTH, 'gwidth', 40, 10,'500');
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_GWIDTH.':</b><br />'._MDA_GSIZE_INFO.'</td>';
         echo '<td class="bg1">'.$gwidth->render().'</td>';
         echo '</tr>';
         $gheight = new XoopsFormText(_MDA_GHEIGHT, 'gheight', 40, 10,'500');
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_GHEIGHT.':</b><br />'._MDA_GSIZE_INFO.'</td>';
         echo '<td class="bg1">'.$gheight->render().'</td>';
         echo '</tr>';
	// Files to upload
        echo '<tr class="bg3" valign=top>';
        echo '<td><b>'._MDA_GAMEFILE.':</b><br />'._MDA_GAMEFILE_INFO.'</td>';
        echo '<td class="bg1">';
	$upload = new Upload();
	$upload->printFormField("gamefile");
         echo '</td></tr>';
         
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_IMGFILE1.':</b><br />'._MDA_IMGFILE1_INFO.'</td>';
         echo '<td class="bg1">';
	 $upload->printFormField("imgfile1");
         echo '</td></tr>';
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_IMGFILE2.':</b><br />'._MDA_IMGFILE2_INFO.'</td>';
         echo '<td class="bg1">';
	 $upload->printFormField("imgfile2");
         echo '</td></tr>';
         /* Optional stuff */
         // Description
         $gwords = new XoopsFormTextArea(_MDA_GWORDS, 'gwords');
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_GWORDS.':</b></td>';
         echo '<td class="bg1">'.$gwords->render().'</td>';
         echo '</tr>';
                // Objective
         $object = new XoopsFormTextArea(_MDA_OBJECT, 'object');
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_OBJECT.':</b></td>';
         echo '<td class="bg1">'.$object->render().'</td>';
         echo '</tr>';
                // Description
         $gkeys = new XoopsFormTextArea(_MDA_GKEYS, 'gkeys');
         echo '<tr class="bg3" valign=top>';
         echo '<td><b>'._MDA_GKEYS.':</b></td>';
         echo '<td class="bg1">'.$gkeys->render().'</td>';
         echo '</tr>';
         $submit = new XoopsFormButton('', 'submit', _SUBMIT, 'submit');
         echo '<tr class="bg3">';
         echo '<td></td>';
         echo '<td class="bg1">'.$submit->render().'</td>';
         echo '</tr>';
     echo '</table>';
     echo '</td></tr></table><br />';
}
function add_game_submit()
{
        global $_POST, $myts,$_FILES;
        $game = new Game();
        $game->gtitle = $myts->makeTboxData4Save($_POST['gtitle']);
        $game->bgcolor = $myts->makeTboxData4Save($_POST['bgcolor']);
        $game->gwords = $myts->makeTboxData4Save($_POST['gwords']);
        $game->object = $myts->makeTboxData4Save($_POST['object']);
        $game->gkeys = $myts->makeTboxData4Save($_POST['gkeys']);
        $game->gwidth = intval($_POST['gwidth']);
        $game->gheight = intval($_POST['gheight']);
        $game->cat_id = intval($_POST['cat_id']);
        $game->active=1;
        $game->date_added = time();
        // Upload the files
        $upload = new Upload();
	$upload->setUploadPath(XOOPS_ROOT_PATH.'/modules/arcade/cache');
	$upload->setAllowedMimeTypes('.swf');
	$upload->setDestinationFileName('gamefile');
	$result = $upload->doUpload();
	
	 if ($result == true) {
		$game->gname = $upload->FileName;
	} else {
		$errors = $upload->getUploadErrors();
		$errorstring = "";
		$errorstring .= "<strong>::Errors occured::</strong><br />\n";
		while(list($filename,$values) = each($errors)) {
			$errorstring .= "File: " . $filename . "<br />";
			$count = count($values);
			for($i=0; $i<$count; $i++) { $errorstring .= "==>" . $values[$i] . "<br />"; }
		}
		redirect_header("index.php", 3, $errorstring);
		exit();
	}
	
	 $upload = new Upload();
	$upload->setUploadPath(XOOPS_ROOT_PATH.'/modules/arcade/cache/images');
	$upload->setAllowedMimeTypes(".gif");
	$upload->setDestinationFileName('imgfile1');
	$result = $upload->doUpload();
	
	if ($result == false) {
		$errors = $upload->getUploadErrors();
		$errorstring = "";
		$errorstring .= "<strong>::Errors occured::</strong><br />\n";
		while(list($filename,$values) = each($errors)) {
			$errorstring .= "File: " . $filename . "<br />";
			$count = count($values);
			for($i=0; $i<$count; $i++) { $errorstring .= "==>" . $values[$i] . "<br />"; }
		}
		redirect_header("index.php", 3, $errorstring);
		exit();
	}
	
	 $upload = new Upload();
	$upload->setUploadPath(XOOPS_ROOT_PATH.'/modules/arcade/cache/images');
	$upload->setAllowedMimeTypes(".gif");
	$upload->setDestinationFileName('imgfile2');
	$result = $upload->doUpload();
	
	if ($result == false ) {
		$errors = $upload->getUploadErrors();
		$errorstring = "";
		$errorstring .= "<strong>::Errors occured::</strong><br />\n";
		while(list($filename,$values) = each($errors)) {
			$errorstring .= "File: " . $filename . "<br />";
			$count = count($values);
			for($i=0; $i<$count; $i++) { $errorstring .= "==>" . $values[$i] . "<br />"; }
		}
		redirect_header("index.php", 3, $errorstring);
		exit();
	}     
        // Store the info into the DB
        if($game->store())
        {
            redirect_header('arcade.php',2, _MDA_GAME_ADDED);
        }
        else
        {
            redirect_header('arcade.php',2, _MDA_GAME_ADD_FAILED);
        }
        exit();
}
function edit_cat()
{
    global $_GET;
        // Navigation Links
        $arcade_home = "<a href='./index.php'><font size=+1>Arcade Admin</font></a>";
        echo $arcade_home.'&nbsp;<font size=+1> >> </font>&nbsp;<font size=+1>'._MI_ARCADE_ADMENU2.'</font><br><br>';
    echo "<br>";
    $cat = Category::getById($_GET['cat_id']);
    $editform = new XoopsThemeForm(_MDA_EDIT_CAT, 'edit_cat', 'arcade.php');
    $act = new XoopsFormHidden('act', 'edit_cat_submit');
    $editform->addElement($act);
    $cat_id_el = new XoopsFormHidden('cat_id', $_GET['cat_id']);
    $editform->addElement($cat_id_el);
    // Title
    $cat_name = new XoopsFormText(_CATEGORY, 'cat_name', 40, 40, $cat->cat_name);
    $editform->setRequired('cat_name');
    $editform->addElement($cat_name);
    $is_active = new XoopsFormSelect(_MDA_ACTIVE, 'is_active', $cat->is_active);
    $is_active->addOption(1, _YES);
    $is_active->addOption(0, _NO);
    $editform->addElement($is_active);
    $submit = new XoopsFormButton('', 'submit', _SUBMIT, 'submit');
    $editform->addElement($submit);
    echo $editform->render();
}
function edit_cat_submit()
{
    global $_POST, $myts;
    $cat = Category::getById($_POST['cat_id']);
    $cat->cat_name = $myts->makeTboxData4Save($_POST['cat_name']);
    $cat->is_active = intval($_POST['is_active']);
    if($cat->store())
    {
        redirect_header('arcade.php',2, _MDA_CAT_UPDATED);
    }
    else
    {
        redirect_header('arcade.php',2, _MDA_CAT_UPDATE_FAILED);
    }
    exit();
}
function add_cat_form()
{
    $addform = new XoopsThemeForm(_MDA_ADD_CAT, 'add_cat', 'arcade.php');
    $act = new XoopsFormHidden('act', 'add_cat_submit');
    $addform->addElement($act);
    // Title
    $cat_name = new XoopsFormText(_CATEGORY, 'cat_name', 40, 40);
    $addform->setRequired('cat_name');
    $addform->addElement($cat_name);
    $is_active = new XoopsFormSelect(_MDA_ACTIVE, 'is_active', 1);
    $is_active->addOption(1, _YES);
    $is_active->addOption(0, _NO);
    $addform->addElement($is_active);
    $submit = new XoopsFormButton('', 'submit', _SUBMIT, 'submit');
    $addform->addElement($submit);
    echo $addform->render();
}
function add_cat_submit()
{
    global $_POST, $myts;
    $cat = new Category();
    $cat->cat_name = $myts->makeTboxData4Save($_POST['cat_name']);
    $cat->is_active = intval($_POST['is_active']);
    if($cat->store())
    {
        redirect_header('arcade.php',2, _MDA_CAT_ADDED);
    }
    else
    {
        redirect_header('arcade.php',2, _MDA_CAT_ADD_FAILED);
    }
    exit();
}
function delete_cat()
{
    global $_POST, $_GET, $myts;
        // Navigation Links
        $arcade_home = "<a href='./index.php'><font size=+1>Arcade Admin</font></a>";
        echo $arcade_home.'&nbsp;<font size=+1> >> </font>&nbsp;<font size=+1>'._MI_ARCADE_ADMENU2.'</font><br><br>';
    echo "<br>";
    $commit = isset($_POST['commit']) ? $_POST['commit'] : $_GET['commit'];
    $cat_id = isset($_POST['cat_id']) ? $_POST['cat_id'] : $_GET['cat_id'];
    if($commit == _YES)
    {
        $cat = Category::getById($cat_id);
        $games = Game::getAllWhere(array("cat_id=$cat_id"));
        foreach($games as $game)
        {
            @unlink(XOOPS_ROOT_PATH.'/modules/arcade/cache/'.$game->gname.'.swf');
            @unlink(XOOPS_ROOT_PATH.'/modules/arcade/cache/images/'.$game->gname.'1.gif');
            @unlink(XOOPS_ROOT_PATH.'/modules/arcade/cache/images/'.$game->gname.'2.gif');
            $game->delete();
        }
        $cat->delete();
        redirect_header('arcade.php',2, _MDA_CAT_DELETED);
    }
    else if($commit == _NO)
    {
        show_cats();
    }
    else
    {	$cat = Category::getById($cat_id);
	
        echo "
        <form method='post' action='arcade.php'>
        <input type='hidden' name='act' value='delete_cat'>
        <input type='hidden' name='cat_id' value='$cat_id'>
        <table>
        <tr>
            <td><font color=red><b>"._MDA_WARNING.":</b></font></td>
        </tr>
        <tr>
            <td><font color=red>"._MDA_DEL_CAT_WARNING." ".$cat->cat_name."</font></td>
        </tr>
        <tr>
            <td>
                 <input type='submit' class='button' name='commit' value='"._YES."'>&nbsp;
                 <input type='submit' class='button' name='commit' value='"._NO."'>
            </td>
        </tr>
        </table>
        </form>";
    }
}
function game_list()
{
        // Navigation Links
        $arcade_home = "<a href='./index.php'><font size=+1>Arcade Admin</font></a>";
        echo $arcade_home.'&nbsp;<font size=+1> >> </font>&nbsp;<font size=+1>'._MI_ARCADE_ADMENU4.'</font><br><br>';
    echo "<br>";
    $games = Game::getAllWhere(false, 'gtitle');
    echo '<table width="100%" cellpadding=0 cellspacing=0><tr><td class="bg2">';
    echo '<table width="100%" cellpadding=1 cellspacing=1>';
    echo "<tr class='bg4'><td><b>"._MDA_GAME_NAME."</b></td><td><b>"._MDA_GAME_PLAYCOUNT."</b></td><td><b>"._MDA_ACTIVE."</b></td><td><b>"._MDA_ACTIONS."</b></td></tr>";
    foreach ($games as $game)
    {
         echo '<tr class="bg3">';
         // Game Name
         echo '<td>';
         echo "<img src='".XOOPS_URL."/modules/arcade/cache/images/".$game->gname."2.gif'>&nbsp;";
         echo "<a href='".XOOPS_URL."/modules/arcade/index.php?act=play_game&gid=".$game->gid."'><b>".$game->gtitle."</b></a>";
         echo '</td>';
         // Play Count
         echo "<td>".$game->gcount."</td>";
         // Active
         $isactive = ($game->active == 1)? 'checked' : '';
         echo '<td>';
         echo "<input type=checkbox class=check name=is_active $isactive disabled>";
         echo '</td>';
         // Actions
         echo '<td>';
         echo "<input type=button class=button value='"._EDIT."' onclick=\"javascript:window.location='arcade.php?act=edit_game&gid=".$game->gid."'\">&nbsp;";
         echo "<input type=button class=button value='"._DELETE."' onclick=\"javascript:window.location='arcade.php?act=delete_game&gid=".$game->gid."'\">&nbsp;";
         echo '</td>';
         echo '</tr>';
     }
     echo '</table>';
     echo '</td></tr></table><br />';
}
function edit_game()
{
    global $_GET;
    $game = Game::getById($_GET['gid']);
    $editform = new XoopsThemeForm(_MDA_EDIT_GAME, 'edit_game', 'arcade.php');
    $act = new XoopsFormHidden('act', 'edit_game_submit');
    $editform->addElement($act);
    $game_id_el = new XoopsFormHidden('gid', $_GET['gid']);
    $editform->addElement($game_id_el);
    // Title
    $gtitle = new XoopsFormText(_MDA_GAME_TITLE, 'gtitle', 40, 40, $game->gtitle);
    $editform->setRequired('gtitle');
    $editform->addElement($gtitle);
    // Category
    $cats = Category::getAllWhere(false);
    $cat_id = new XoopsFormSelect(_MDA_CATEGORY, 'cat_id', $game->cat_id);
    foreach ($cats as $cat)
    {
        $cat_id->addOption($cat->cat_id, $cat->cat_name);
    }
    $editform->addElement($cat_id);
    $is_active = new XoopsFormSelect(_MDA_ACTIVE, 'active', $game->active);
    $is_active->addOption(1, _YES);
    $is_active->addOption(0, _NO);
    $editform->addElement($is_active);
    // BGCOLOR
    $bgcolor = new XoopsFormText(_MDA_BGCOLOR, 'bgcolor', 40, 10, $game->bgcolor);
    $editform->setRequired('bgcolor');
    $editform->addElement($bgcolor);
    // Width/Height
    $gwidth = new XoopsFormText(_MDA_GWIDTH, 'gwidth', 40, 10, $game->gwidth);
    $editform->setRequired('gwidth');
    $editform->addElement($gwidth);
    $gheight = new XoopsFormText(_MDA_GHEIGHT, 'gheight', 40, 10, $game->gheight);
    $editform->setRequired('gheight');
    $editform->addElement($gheight);
    // Description
    $gwords = new XoopsFormTextArea(_MDA_GWORDS, 'gwords', $game->gwords);
    $editform->addElement($gwords);
    // Objective
    $object = new XoopsFormTextArea(_MDA_OBJECT, 'object', $game->object);
    $editform->addElement($object);
    // Description
    $gkeys = new XoopsFormTextArea(_MDA_GKEYS, 'gkeys', $game->gkeys);
    $editform->addElement($gkeys);
    $submit = new XoopsFormButton('', 'submit', _SUBMIT, 'submit');
    $editform->addElement($submit);
    echo $editform->render();
}
function edit_game_submit()
{
    global $_POST, $myts;
        $game = Game::getById($_POST['gid']);
    $game->gtitle = $myts->makeTboxData4Save($_POST['gtitle']);
    $game->bgcolor = $myts->makeTboxData4Save($_POST['bgcolor']);
    $game->gwords = $myts->makeTboxData4Save($_POST['gwords']);
    $game->object = $myts->makeTboxData4Save($_POST['object']);
    $game->gkeys = $myts->makeTboxData4Save($_POST['gkeys']);
    $game->active = intval($_POST['active']);
    $game->gwidth = intval($_POST['gwidth']);
    $game->gheight = intval($_POST['gheight']);
    $game->cat_id = intval($_POST['cat_id']);
        // Store the info into the DB
        if($game->store())
        {
                redirect_header('arcade.php?act=game_list',2, _MDA_GAME_UPDATED);
        }
        else
        {
                redirect_header('arcade.php?act=game_list',2, _MDA_GAME_UPDATE_FAILED);
        }
        exit();
}
function delete_game()
{
    global $_POST, $_GET, $myts;
    $commit = isset($_POST['commit']) ? $_POST['commit'] : $_GET['commit'];
    $gid = isset($_POST['gid']) ? $_POST['gid'] : $_GET['gid'];
    if($commit == _YES)
    {
        $game = Game::getById($gid);
        @unlink(XOOPS_ROOT_PATH.'/modules/arcade/cache/'.$game->gname.'.swf');
        @unlink(XOOPS_ROOT_PATH.'/modules/arcade/cache/images/'.$game->gname.'1.gif');
        @unlink(XOOPS_ROOT_PATH.'/modules/arcade/cache/images/'.$game->gname.'2.gif');
        $game->delete();
        redirect_header('arcade.php?act=game_list',2, _MDA_GAME_DELETED);
    }
    else if($commit == _NO)
    {
        game_list();
    }
    else
    {	$game = Game::getById($gid);
        echo "
        <form method='post' action='arcade.php'>
        <input type='hidden' name='act' value='delete_game'>
        <input type='hidden' name='gid' value='$gid'>
        <table>
        <tr>
            <td><font color=red><b>"._MDA_WARNING.":</b></font></td>
        </tr>
        <tr>
            <td><font color=red>"._MDA_DEL_GAME_WARNING." ".$game->gtitle."</font></td>
        </tr>
        <tr>
            <td>
                 <input type='submit' class='button' name='commit' value='"._YES."'>&nbsp;
                 <input type='submit' class='button' name='commit' value='"._NO."'>
            </td>
        </tr>
        </table>
        </form>";
    }
}
?>