Update index.php
[clinton/MarylandElectronicPetitionSignature.git] / admin / index.php
index 0c40257..983bbb4 100644 (file)
@@ -9,35 +9,101 @@ if ($_COOKIE['level'] == 'manager'){
   slack_general('ADMIN: Redirect Manager Home ('.$_COOKIE['name'].') ('.$_COOKIE['level'].')','md-petition');
   header('Location: manager_home.php');
 }
-
 include_once('header.php');
-
+if ($_POST['name']){   
+       $name           = $petition->real_escape_string($_POST['name']);
+       $email          = $petition->real_escape_string($_POST['email']);
+       $sec_level      = $petition->real_escape_string($_POST['sec_level']);
+       $group_id       = $petition->real_escape_string($_POST['group_id']);
+       $petition_id    = $petition->real_escape_string($_POST['petition_id']);
+       $q = "insert into users (name, email, sec_level, group_id, petition_id) values ('$name','$email','$sec_level','$group_id','$petition_id') ";
+       $petition->query($q);
+       slack_general_admin('SQL: '.$q,'md-petition-signed');
+         include_once('../email.php');
+         $pass = rand(1000,9999);
+          $salt = md5(rand(1000,9999));
+          $hash = md5($pass.$salt);
+          $encrypted = $hash.':'.$salt;
+         $subject = 'MD Petition Login';
+         $body = 'Login with '.$email.' and your new password '.$pass.' at https://www.md-petition.com/admin/login.php';
+          meps_mail($email,$body,$subject);
+          $petition->query("update users set pass = '$encrypted' WHERE email = '$email'");
+          echo "<h1>Password has been Sent.</h1>";
+         slack_general_admin('DEBUG: '.$body,'md-petition-signed');
+}
+if(isset($_GET['approve'])){
+  $id = $_GET['approve'];
+  $petition->query("update petitions set admin_status = 'approved' where petition_id = '$id' ");
+}
 slack_general('ADMIN: Home Page Loaded ('.$_COOKIE['name'].') ('.$_COOKIE['level'].')','md-petition');
 ?>
 
 <h1>Admin Home</h1>
+<div id="chartContainer1" style="height: 400px; width: 100%; margin: 0px auto;"></div>
+<div id="chartContainer2" style="height: 400px; width: 100%; margin: 0px auto;"></div>
+
+<table><tr><td valign='top'>
+<h2>Admin Sessions</h2>
+<div>Last 50</div><ol>
+<?PHP
+$q="SELECT * FROM admin_sessions where username <> 'Patrick McGuire' ORDER BY id DESC LIMIT 50";
+$r = $petition->query($q);
+while($d = mysqli_fetch_array($r)){ 
+    echo "<li title='$d[action_on]'>$d[ip] $d[username] $d[php_page]</li>"; 
+}
+?></ol>        
+       
+       </td><td valign='top'>
+
+       <h2>User Sessions</h2>
+<div>Last 50</div><ol>
+<?PHP
+$q="SELECT * FROM presign ORDER BY id DESC LIMIT 50";
+$r = $petition->query($q);
+while($d = mysqli_fetch_array($r)){ 
+    echo "<li title='$d[action_on]'>$d[php_page] $d[browser_string]</li>"; 
+}
+?></ol>
+       
+       </td></tr></table>
+
 
 <h1>Users</h1>
+<form method='post'>
+  <table>
+       <tr><td>Name</td><td><input name='name' required></td></tr>
+       <tr><td>E-Mail</td><td><input name='email' required></td></tr>    
+       <tr><td>Security Level</td><td><input name='sec_level' required></td></tr>
+       <tr><td>Group ID</td><td><input name='group_id' required></td></tr>
+       <tr><td>Petition ID</td><td><input name='petition_id' required></td></tr>
+       <tr><td></td><td><input type='submit' value='New User'></td></tr>
+  </table>
+</form>
 <?PHP
 $q="SELECT * FROM users";
 $r = $petition->query($q);
 while($d = mysqli_fetch_array($r)){
- echo "<li>$d[id] $d[email] $d[name] $d[group_id] $d[sec_level]</li>"; 
+  $alert='';
+  if ($d[pass] == ''){
+    $alert='NEEDS PASSWORD RESET';    
+  }
+ echo "<li>ID $d[id] EM $d[email] NM $d[name] GI $d[group_id] PI $d[petition_id] SL $d[sec_level] $alert</li>"; 
 }
 ?>
 
-<h1>Groups</h1>
+<h1>New Petitions</h1>
 <?PHP
-$q="SELECT * FROM groups";
+$q="SELECT * FROM petitions where admin_status='new'";
 $r = $petition->query($q);
 while($d = mysqli_fetch_array($r)){
- echo "<li>$d[id] $d[name]</li>"; 
+ echo "<li><a href='?approve=$d[petition_id]'>$d[petition_id] $d[web_short_name] $d[web_color] $d[group_id] $d[petition_name] $d[eligibleVoterListField] $d[eligibleVoterListEquals] $d[eligibleVoterListEnforce]</a></li>"; 
 }
 ?>
 
-<h1>Petitions</h1>
+
+<h1>Approved Petitions</h1>
 <?PHP
-$q="SELECT * FROM petitions";
+$q="SELECT * FROM petitions where admin_status = 'approved'";
 $r = $petition->query($q);
 while($d = mysqli_fetch_array($r)){
  echo "<li>$d[petition_id] $d[web_short_name] $d[web_color] $d[group_id] $d[petition_name] $d[eligibleVoterListField] $d[eligibleVoterListEquals] $d[eligibleVoterListEnforce]</li>"; 
@@ -45,6 +111,18 @@ while($d = mysqli_fetch_array($r)){
 ?>
 
 
+<h1>Groups</h1>
+<?PHP
+$q="SELECT * FROM groups";
+$r = $petition->query($q);
+while($d = mysqli_fetch_array($r)){
+ echo "<li>$d[id] $d[name]</li>"; 
+}
+?>
+
+
+
+
 <h1>Website</h1>
 <?PHP
 $q="SELECT * FROM website_text";
@@ -54,7 +132,203 @@ while($d = mysqli_fetch_array($r)){
 }
 ?>
 
+<?PHP
+       $pID = 1;
+       //echo "<div id=\"chartContainer$pID\" style=\"height: 400px; width: 100%; margin: 0px auto;\"></div>";
+       $chart='';
+       $chart2='';
+       $chart3='';
+       $q3 = "SELECT just_date FROM signatures where just_date <> '0000-00-00' group by just_date";
+       //echo "<li>$q3</li>";
+       $r3 = $petition->query($q3);
+       $total=0;
+       $total2=0;
+       $goal = $d['signature_goal'];
+       if ($goal == 0){
+               $goal = 10000;
+       }
+       $trader_sma_timePeriod=0;
+       while ($d3 = mysqli_fetch_array($r3)){
+         $just_date = $d3['just_date'];
+         $q2 = "SELECT * FROM signatures where just_date = '$just_date' and signature_status = 'verified'  ";
+         //echo "<li>$q2</li>";
+         $r2 = $petition->query($q2);
+         $count  = mysqli_num_rows($r2);
+         $chart .=  '{ label: "'.$just_date.'", y: '.intval($count).' }, ';
+         $total = $total + intval($count);
+         $chart2 .=  '{ label: "'.$just_date.'", y: '.intval($total).' }, ';
+         $goal = $goal - intval($count);
+         $chart3 .=  '{ label: "'.$just_date.'", y: '.intval($goal).' }, ';
+         $q2 = "SELECT * FROM follow_up where date_sent = '$just_date'  ";
+         $r2 = $petition->query($q2);  
+         $count  = mysqli_num_rows($r2);
+         $chart4 .=  '{ label: "'.$just_date.'", y: '.intval($count).' }, ';
+         $total2 = $total2 + $count;
+         $chart5 .=  '{ label: "'.$just_date.'", y: '.intval($total2).' }, ';
+         
+         $q2 = "SELECT * FROM presign where only_date = '$just_date'  ";
+         $r2 = $petition->query($q2);  
+         $count  = mysqli_num_rows($r2);
+         if($count > 0){ // start this graph when there is data
+                 $chart6 .=  '{ label: "'.$just_date.'", y: '.intval($count).' }, ';
+                 // graph latest sma
+                 $trader_sma_real[] = intval($count);
+                 $trader_sma_timePeriod++;
+                 $trader_sma = trader_sma($trader_sma_real,7);
+                 //print_r($trader_sma);
+                 $the_index = $trader_sma_timePeriod - 1;
+                 $this_sma = $trader_sma[$the_index]; // should be last value?
+                 $chart7 .=  '{ label: "'.$just_date.'", y: '.intval($this_sma).' }, ';
+         }
+       }
+       
+       $chart = rtrim(trim($chart), ",");
+       $chart2 = rtrim(trim($chart2), ",");
+       $chart3 = rtrim(trim($chart3), ",");
+       $chart4 = rtrim(trim($chart4), ",");
+        $chart5 = rtrim(trim($chart5), ",");
+       $chart6 = rtrim(trim($chart6), ",");
+        $chart7 = rtrim(trim($chart7), ",");
+
+       
+
+       ob_start(); ?>
+
+       var chart<?PHP echo $pID;?> = new CanvasJS.Chart("chartContainer<?PHP echo $pID;?>", {
+               theme:"light2",
+               animationEnabled: true,
+               exportEnabled: true,
+               title:{
+                       text: "MD-Petition.com Signature Tracker"
+               },
+               axisY :{
+                       includeZero: false,
+                       title: "Number of Signatures",
+                       suffix: "",
+           scaleBreaks: {
+                                       autoCalculate: true
+                               }
+               },
+               toolTip: {
+                       shared: "true"
+               },
+               legend:{
+                       cursor:"pointer",
+                       itemclick : toggleDataSeries
+               },
+               data: [{
+                       type: "line",
+                       visible: true,
+                       showInLegend: true,
+                       yValueFormatString: "#####",
+                       name: "Total Signatures Count",
+                       dataPoints: [
+                               <?PHP echo $chart2; ?>
+                       ]
+               },{
+                       type: "line",
+                       visible: true,
+                       showInLegend: true,
+                       yValueFormatString: "#####",
+                       name: "Total Follow Up Count",
+                       dataPoints: [
+                               <?PHP echo $chart5; ?>
+                       ]
+               },{
+                       type: "column",
+                       visible: true,
+                       showInLegend: true,
+                       yValueFormatString: "#####",
+                       name: "New Daily Signatures",
+                       dataPoints: [
+                               <?PHP echo $chart; ?>
+                       ]
+               },{
+                       type: "column",
+                       visible: true,
+                       showInLegend: true,
+                       yValueFormatString: "#####",
+                       name: "Follow Up Sent",
+                       dataPoints: [
+                               <?PHP echo $chart4; ?>
+                       ]
+               }]
+       }
+
+
+                                     );
+       chart<?PHP echo $pID; $pID = $pID + 1; ?>.render();
+
+         var chart<?PHP echo $pID;?> = new CanvasJS.Chart("chartContainer<?PHP echo $pID;?>", {
+               theme:"light2",
+               animationEnabled: true,
+               exportEnabled: true,
+               title:{
+                       text: "MD-Petition.com Web Monitor"
+               },
+               axisY :{
+                       includeZero: false,
+                       title: "Number of Pages Loaded",
+                       suffix: "",
+           scaleBreaks: {
+                                       autoCalculate: true
+                               }
+               },
+               toolTip: {
+                       shared: "true"
+               },
+               legend:{
+                       cursor:"pointer",
+                       itemclick : toggleDataSeries
+               },
+               data: [{
+                       type: "line",
+                       visible: true,
+                       showInLegend: true,
+                       yValueFormatString: "#####",
+                       name: "Web Activity",
+                       dataPoints: [
+                               <?PHP echo $chart6; ?>
+                       ]
+               },{
+                       type: "line",
+                       visible: true,
+                       showInLegend: true,
+                       yValueFormatString: "#####",
+                       name: "7 Day Simple Moving Average",
+                       dataPoints: [
+                               <?PHP echo $chart7; ?>
+                       ]
+               }]
+       }
 
+
+                                     );
+       chart<?PHP echo $pID;?>.render();
+
+       <?PHP $javascript .= ob_get_clean(); ?>
+
+
+
+<script>
+window.onload = function () {
+
+<?PHP echo $javascript;?>
+
+function toggleDataSeries(e) {
+       if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible ){
+               e.dataSeries.visible = false;
+       } else {
+               e.dataSeries.visible = true;
+       }
+       chart.render();
+}
+
+}
+</script>
+<script src="../files/canvasjs.min.js"></script>
 <?PHP
+
+
 include_once('footer.php');
 ?>