<?php

	class LoginPage extends Page {

		public $content = <<<LOGINFORM
<form method="post" action="admin.php">
<div class='subject_title'>your name please</div>
<div class='blurb'><input name='username' type='text' maxlength='25'></div>
<div class='subject_title'>and that password sir</div>
<div class='blurb'><input name='password' type='password' maxlength='15'></div>
<div class='subject_title'><input type='submit' name='login' value='Get Dirty'></div>
</form>
LOGINFORM;
		
		function on_page() {
			echo "<div class='alphagrid'>do a little admin is it</div>";
		}
		
	}

class ProcessLogin extends Page {

	public $content = "";
	
	function __construct($form, $conn) {
		$username = htmlspecialchars($form['username']);
		$password = htmlspecialchars($form['password']);
		$sql = "select * from tbladminuser where fldEmail = '".$username."' and fldPassword = '".$password."' limit 1";

        if(!$res = $conn->query($sql)) echo $conn->error;
        if($row = $res->fetch_assoc()) {
            $_SESSION['admin'] = "yes";
            $this->content = "<div class='subject_title'>you have logged right in</div><div class='blurb'><a href='index.php?staging=1'>Here is your deleted hellscape.</a></div>";
        } else {
            $_SESSION['admin'] = "no";
            $this->content = "<div class='subject_title'>that did not work at all</div><div class='blurb'><a href='index.php'>Stop trying to admin.</a><br><br><a href='admin.php'>Try again.</a></div>";
        }
	}
		
	function on_page() {
		echo "<div class='alphagrid'>do a little admin is it</div>";
	}		
}

class EditBlurb extends Page {
    
    public $content = "";
    
    function __construct($blurb, $conn) {
        $blurb = intval($blurb);
        $sql = "select tbladminuser.fldId as adminid, tbladminuser.fldFirstName as adminname, tblsubject.fldId as subjectid, tblsubject.fldName as subjectname, tblsubject.fldStagingId as subjectstaging, tblblurb.fldId as blurbid, tblblurb.fldBlurb as blurb, tblblurb.fldStagingId as blurbstaging
from tbladminuser, tblsubject, tblblurb
where tbladminuser.fldId = tblblurb.fldAdminUserId
and tblsubject.fldId = tblblurb.fldSubjectId
and tblblurb.fldId = $blurb";
        $stagingexp = array("hidden", "deleted", "inactive", "pending", "approved");
        if(!$res = $conn->query($sql)) echo $conn->error;
        $row = $res->fetch_assoc();
        $blurb = stripslashes(str_replace("<br />", "\n", $row['blurb']));
        $formhtml = "<form method='post' action='admin.php' name='blurbform'>";
        $formhtml .= "<input type='hidden' name='subjectid' value='{$row['subjectid']}'>
                     <input type='hidden' name='blurbid' value='{$row['blurbid']}'>
                     <input type='hidden' name='oldadminid', value='{$row['adminid']}'
                     <input type='hidden' name='oldsubjectstaging' value='{$row['subjectstaging']}'>
                     <input type='hidden' name='oldblurbstaging' value='{$row['blurbstaging']}'>";
        $formhtml .= "<p class='subject_title form'><input id='subjectname' name='subjectname' class='subject_title' type='text' value=\"".stripslashes($row['subjectname'])."\"></p>";
        $formhtml .= "<p class='blurb slider'>
                        <input type='range' min='0' max='4' value='{$row['subjectstaging']}' name='subjectstaging' id='subjectstaging' onchange='stagingtext(this.id)'>
                        <span id='subjectstagingfb'>{$stagingexp[$row['subjectstaging']]}</span>
                     </p>";
        $formhtml .= "<p class='blurb form'>
                        <textarea name='blurb' class='blurb' style='width:100%; height:300px;'>{$blurb}</textarea>
                        </p>";
        $formhtml .= "<p class='slider blurb'>
                        <input type='range' min='0' max='4' value='{$row['blurbstaging']}' name='blurbstaging' id='blurbstaging' onchange='stagingtext(this.id)'>
                        <span id='blurbstagingfb'>{$stagingexp[$row['blurbstaging']]}</span>
                      </p>";
        $formhtml .= "<p class='blurb form'>Change admin user to Log?<br>
                        <input type='radio' name='adminclaim' value='no' id='adminno' checked='checked'><label for='adminno'>no, leave it</label>
                        <input type='radio' name='adminclaim' value='yes' id='adminyes'><label for='adminyes'>yeah, grab it</label>
                    </p>";
        $formhtml .= "<p class='blurb'><input type='submit' name='submit' value='Channnnge' class='subject_title'></p></form>";

    $this->content = $formhtml;
    }
    
    function on_page() {
        echo "<div class='alphagrid'>editing a blooorb</div>";
    }
}

class ProcessEdit extends Page {
    public $content = "";
    
    function __construct($form, $conn) {
        extract($form);
        $subjectname = addslashes(htmlspecialchars($subjectname));
        $subjectid = intval($subjectid);
        $blurb = addslashes(str_replace("\n", "<br />", $blurb));
        $subjectstaging = intval($subjectstaging);
        $blurbstaging = intval($blurbstaging);
        $blurbid = intval($blurbid);
        if($adminclaim == "yes") $adminset = ", fldAdminUserId = 2 "; else $adminset = ""; 

        #if we're changing the admin user to log, otherwise set to the old value
        if($adminclaim == "yes") $adminset = 2; else $adminset = $oldadminid;

        
        $stmt = $conn->prepare("update tblblurb set fldblurb = ? , fldStagingId = ? , fldAdminUserId = ? where fldId = ? limit 1");
            if($stmt==false) die("PREPARE FAILED<br>".$conn->error);
        $bindcheck = $stmt->bind_param("siii", $blurb, $blurbstaging, $adminset, $blurbid);
            if(false===$bindcheck) $this->content .= "<br>blurb parameter bind failed: ".$blurbsql->error;
        $executecheck = $stmt->execute();  
            if(false===$executecheck) $this->content .= "<br>blurb execute failed: ".$blurbsql->error;
        $stmt->close(); 
        
        $stmt = $conn->prepare("update tblsubject SET fldName = ?, fldStagingId = ? where fldId = ? limit 1");
            if($stmt==false) die("PREPARE FAILED<br>".$conn->error);
        $bindcheck = $stmt->bind_param("sii", $subjectname, $subjectstaging, $subjectid);
            if(false===$bindcheck) $this->content .= "<br>blurb parameter bind failed: ".$blurbsql->error;
        $executecheck = $stmt->execute();
             if(false===$executecheck) $this->content .= "<br>blurb execute failed: ".$blurbsql->error;
        $stmt->close();
                                     
        
        $this->content .= "<div class='blurb'><a href='index.php?subject=$subjectid'>View the changes</a></div>";
        
    }

    function on_page() {
        "processing an edit";
    }
}
?>