Location: PHPKode > projects > PHPEcho CMS > modules/admin/modules/voting.php
<?php
/*  +--------------------------------------------
*   |
*   |     Product:       PHPEchoCMS
*   |     Author:        Anas Husseini
*   |     License:       GPL
*   |     Last updated:  21.02.07
*   |     For version:   1.6  
*   |     Desc:          Module for working with Voting module
*   |        
*   +--------------------------------------------
*/

if( !ADMIN_LOAD )
{
    die( "Base not loaded!" );
}

$mlang = $core->getlang("admin_voting");

echo "<br><center><a href='index.php?module=admin&act=voting'>".$mlang['001']."</a> | <a href='index.php?module=admin&act=voting&op=add'>".$mlang['002']."</a></center><br>";

switch ( @$_GET['op'] )
{
    case "add":
        add($mlang);
        break;
    case "delete":
        del($mlang);
        break;
    case "edit";
	edit($mlang);
	break;
    default:
        nlist($mlang);
}

function add($lang)
{
    $mlang = $lang;

    if (@$_POST['finish']=='1')
    {
	// Final Step
	$nowdate = date("d-m-Y H:i");
	query ("insert into `".PREFIX."voting` values ('', '".addslashes($_POST['title'])."', '".$nowdate."')", 1);
	$parent_id = query ("select max(`id`) from `".PREFIX."voting`", 2);
	for ($i=1; $i<=$_POST['number']; $i++)
	{
		query ("insert into `".PREFIX."voting_elements` values ('', '".$parent_id."', '".addslashes($_POST['element'.$i])."', 0)", 1);
	}
	echo $mlang['003'];
    }
    elseif (@$_POST['title'] && $_POST['number'])
    { 
	// Second Step
	echo "<form action=index.php?module=admin&act=voting&op=add method='post'>";
	echo "<h3><u>".$mlang['004']."</u></h3>";
	for ($i=1; $i<=$_POST['number']; $i++)
	{
		echo $mlang['005'].$i.":&nbsp;<input type=text name='element".$i. "' size=50><br>";
	}
	echo "<input type=hidden name='finish' value='1'>";
	echo "<input type=hidden name=title value='".$_POST['title']."'><input type=hidden name=number value='".$_POST['number']."'>";
	echo "<input type=submit value='".$mlang['002']."'></form>";
    }
    else
    {    
	// First Step
	echo "<form action=index.php?module=admin&act=voting&op=add method=post>";
    	echo $mlang['006']."<input type=text name=title size='50'><br>";
    	echo $mlang['007']."<select name='number'><option>2</option><option>3</option><option>4</option><option>5</option>";
    	echo "<option>6</option><option>7</option><option>8</option><option>9</option><option>10</option></select><br>";
    	echo "<input type=submit value='".$mlang['008']."'>";
    	echo "</form>";
    }
}

function edit($lang)
{
    $mlang = $lang;
    if (@$_POST['title'] && $_POST['nb'] && $_POST['id'])
    {
	$q = "update `".PREFIX."voting` set `title`='".addslashes($_POST['title'])."' where `id`=".addslashes($_POST['id']);
	query ($q, 1);
	$nn = intval($_POST['nb']);
	for ($j=1; $j<=$nn; $j++)
	{
		$q = "update `".PREFIX."voting_elements` set `title`='".addslashes($_POST['title'.$j])."' where `id`=".addslashes($_POST['id'.$j]);
		query ($q, 1);
	}
	echo $mlang['009'];
    }
    if (@$_GET['id'])
    {
	echo "<form action='index.php?module=admin&act=voting&op=edit' method=post>";
	$voting = query ("select * from `".PREFIX."voting` where `id`=".addslashes($_GET['id']), 4);
    	echo $mlang['006']."<input type='text' name='title' size='50' value='".$voting['title']."'>";
	echo "<input type=hidden name=id value=".$voting['id']."><br>";
    	$elements = query ("select * from `".PREFIX."voting_elements` where `parent`=".addslashes($_GET['id']), 1);
	$i = 1;
	while ($element = mysql_fetch_array($elements))
	{
		echo $mlang['010'].$i.":&nbsp;<input type='text' name=title".$i." size='50' value='".$element['title']."'>";
		echo "<input type=hidden name=id".$i." value=".$element['id']."><br>";
		$i++;
	}
    	echo "<input type=hidden name='nb' value='".($i-1)."'>";
	echo "<input type=submit value='".$mlang['011']."'>";
    	echo "</form>";
    }
}

function del($lang)
{
    $mlang = $lang;

    $voting_exist = query ( "SELECT `id` FROM ".PREFIX."voting WHERE id = '".addslashes($_GET['id'])."'", 4);
    
    if ($voting_exist['id'])
    {
        query ( "DELETE FROM `".PREFIX."voting` WHERE id = '".$voting_exist['id']."'", 1);
	query ( "delete from `".PREFIX."voting_elements` where parent='".$voting_exist['id']."'", 1);
        echo $mlang['012'];
    }
}

function nlist($lang)
{
    $mlang = $lang;

    $list = query ("SELECT * FROM ".PREFIX."voting order by `id` desc", 1);
    
    echo "<table width='100%'>
    <tr>
        <td width='5%'>    
        <b>#</b>
        </td>
        <td width='65%'>
        <b>".$mlang['013']."</b>
        </td>
        <td width='15%'>
        <b>".$mlang['014']."</b>
        </td>
	<td width='15%'>
        <b>".$mlang['015']."</b>
        </td>
    </tr>
    ";
    
    while ( $voting = mysql_fetch_array ( $list ) )
    {
        echo "
        <tr>
            <td width='5%'>    
            ".$voting['id']."
            </td>
            <td width='65%'>
            ".$voting['title']."
            </td>
            <td width='15%'>
            <a href='index.php?module=admin&act=voting&op=delete&id=".$voting['id']."'>".$mlang['014']."</a>
            </td>
	    <td width='15%'>
            <a href='index.php?module=admin&act=voting&op=edit&id=".$voting['id']."'>".$mlang['015']."</a>
            </td>
        </tr>
        ";
    }
    
    echo "</table>";
}
?>
Return current item: PHPEcho CMS