Location: PHPKode > projects > Tinbox > accountadmin/accounts.php
<?php
//Administration of account details


//handle submitted data
switch($submit)
        {
        case "Update":
        echo "<h1>Result - Update Account</h1>";

        if (isset($accountadminidx) && isset($accountidx) && $accountname!="" && isset($accounttheme) && $accountmailfrom!="" && $accountmailsender!="" && $accountcontact!="" && $accountemail!="")
                {
                if ($accountidx=="0") //new
                        {
                        $accountidx=md5(uniqid(rand()));

                        if(!mkdir(LOGS.$accountidx, MKDIR_MODE))
                                die("Can't create dir ".LOGS.$accountidx);
                        if(!mkdir(LOGS.$accountidx."/certs", MKDIR_MODE))
                                die("Can't create dir ".LOGS.$accountidx."/certs");
                        if(!mkdir(LOGS.$accountidx."/account", MKDIR_MODE))
                                die("Can't create dir ".LOGS.$accountidx."/account");
                        if(!mkdir(LOGS.$accountidx."/students", MKDIR_MODE))
                                die("Can't create dir ".LOGS.$accountidx."/students");

                        $sql = "insert into account_main ( admin_idx, idx ,name , theme, mail_from, mail_sender, contact ,email ) values ( '".$accountadminidx."', '".$accountidx."', '".$accountname."', '".$accounttheme."', '".$accountmailfrom."', '".$accountmailsender."', '".$accountcontact."', '".$accountemail."')";
                        if(mysql_query($sql))
                                echo "<span class=\"succ\">Success</span> - Account ".$accountname." added";
                        else
                                echo "<span class=\"fail\">Error</span> - Failed to update database";

                        }
                else    //edit
                        {
                        $sql = "update account_main set name='".$accountname."', admin_idx='".$accountadminidx."', theme='".$accounttheme."', contact='".$accountcontact."', email='".$accountemail."', mail_from='".$accountmailfrom."', mail_sender='".$accountmailsender."' where idx='".$accountidx."' limit 1";
                        if(mysql_query($sql))
                                echo "<span class=\"succ\">Success</span> - Account ".$accountname." updated";
                        else
                                echo "<span class=\"fail\">Error</span> - Failed to update database";

                        }
                }
        else
                {
                echo "<span class=\"fail\">Failed</span> - All fields not completed";
                }
        echo "<hr>";
        break;
        }


echo "<h1>Accounts</h1>";

$sql = "select ac.idx, ac.name as account, us.name as user, ac.theme, ac.contact, ac.email, ac.mail_from, ac.mail_sender from account_main ac, admin_users us where us.idx=ac.admin_idx order by admin_idx";
$result = mysql_query($sql);
if(mysql_num_rows($result))
        {
        echo "<table class=\"tabular\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"98%\">";
        echo "<tr><th>Account Name</th><th>Course Administrator</th><th>Contact</th><th>Contact Email</th><th>Theme</th><th>Mail From (Alias)</th><th>Mail Sender (Email)</th><th></th></tr>";
        while($row = mysql_fetch_object($result))
                {
                echo "<tr>";
                echo "<td>".$row->account."</td>";
                echo "<td>".$row->user."</td>";
                echo "<td>".$row->contact."</td>";
                echo "<td>".$row->email."</td>";
                echo "<td>".$row->theme."</td>";
                echo "<td>".$row->mail_from."</td>";
                echo "<td>".$row->mail_sender."</td>";
                echo "<td><a href=\"#editaccount\" onClick=\"setaccount('".$row->idx."')\">edit</a></td>";
                echo "</tr>";
                }
        echo "</table>";
        }

//add/edit account
$sql = "select idx, name from admin_users where access_co='1' order by name";
if(mysql_num_rows(mysql_query($sql)))
        {

        echo "<h2><a name=\"editaccount\">Add/Edit Account</a></h2>";
        echo "<form name=\"accountform\" method=\"post\" action=\"\"><table class=\"tabular\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"98%\"><tr>";
        echo "<th>Select</th><th>Account Name</th><th>Administrator</th><th>Contact</th><th>Contact Email</th><th>Theme</th><th>Mail from</th><th>Mail Sender</th><th></th></tr><tr>";
        echo "<td><select name=\"accountidx\" onChange=\"readaccount()\"><option value=\"0\">--- New ---</option>";
        $sql = "select name, idx from account_main order by name";
        $result = mysql_query($sql);
        while ($row = mysql_fetch_object($result))
                echo "<option value=\"".$row->idx."\"> Edit ".$row->name."</option>";
        echo "<td><input type=\"text\" name=\"accountname\" size=15 maxlength=50></td>";
        $sql = "select idx, name from admin_users where access_co='1' order by name";
        $result = mysql_query($sql);
        echo "<td><select name=\"accountadminidx\">";
        while($row = mysql_fetch_object($result))
                echo "<option value=\"".$row->idx."\">".$row->name."</option>";
        echo "</select></td>";
        echo "<td><input type=\"text\" name=\"accountcontact\" size=15, maxlength=50></td>";
        echo "<td><input type=\"text\" name=\"accountemail\" size=20, maxlength=50></td>";
        echo "<td><select name=\"accounttheme\">";
        foreach($acthemes as $actheme)
                echo "<option value=\"".$actheme."\">".$actheme."</option>";
        echo "</select></td>";
        echo "<td><input type=\"text\" name=\"accountmailfrom\" size=15, maxlength=50></td>";
        echo "<td><input type=\"text\" name=\"accountmailsender\" size=15, maxlength=50></td>";
        echo "<td><input type=\"submit\" name=\"submit\" value=\"Update\" onclick=\"return confirm('Are you sure you want to continue')\" title=\"add/edit account\"></td>";
        echo "</tr></table><input type=\"hidden\" name=\"idx\" value=\"".$idx."\"></form>";
        }
else
        echo "<h4>No Accounts - You must add a Course Administrator before you can add an account - Click tab 'Users'</h4>";

?>

<script type="text/javascript" language="javascript">

function readaccount()
{
var idx=document.accountform.accountidx.options[document.accountform.accountidx.selectedIndex].value;
if (idx==0)
        clearaccount();
else
        setaccount(idx);
}

function clearaccount()
{
document.accountform.accountname.value="";
document.accountform.accountadminidx.selectedIndex=0;
document.accountform.accountcontact.value="";
document.accountform.accountemail.value="";
document.accountform.accounttheme.selectedIndex=0;
document.accountform.accountmailfrom.value="";
document.accountform.accountmailsender.value="";
}

function setaccount(idx)
{
var name=new Array();
var adminidx=new Array();
var contact=new Array();
var email=new Array();
var theme=new Array();
var mailfrom=new Array();
var mailsender=new Array();

<?php
$sql = "select idx, name,  admin_idx, theme, contact, email, mail_from, mail_sender from account_main";
$result = mysql_query($sql);
while ($row = mysql_fetch_object($result))
        {
        echo "name[\"".$row->idx."\"] = \"".$row->name."\";\n";
        echo "adminidx[\"".$row->idx."\"] = \"".$row->admin_idx."\";\n";
        echo "theme[\"".$row->idx."\"] = \"".$row->theme."\";\n";
        echo "contact[\"".$row->idx."\"] = \"".$row->contact."\";\n";
        echo "email[\"".$row->idx."\"] = \"".$row->email."\";\n";
        echo "mailfrom[\"".$row->idx."\"] = \"".$row->mail_from."\";\n";
        echo "mailsender[\"".$row->idx."\"] = \"".$row->mail_sender."\";\n";

        }
?>
                                                   
document.accountform.accountidx.value=idx;
document.accountform.accountname.value=name[idx];
document.accountform.accountcontact.value=contact[idx];
document.accountform.accountemail.value=email[idx];
document.accountform.accountmailfrom.value=mailfrom[idx];
document.accountform.accountmailsender.value=mailsender[idx];
document.accountform.accounttheme.value=theme[idx];
document.accountform.accountadminidx.value=adminidx[idx];

}


</script>
Return current item: Tinbox