<?php
// filminterface.php
// RJM Programming
// June, 2015

interface Database {
  function listFilms();
  function addFilm($code, $title, $did, $date_prod, $kind, $len);
  function removeFilm($code);
  function amendFilm($code, $title, $did, $date_prod, $kind, $len);
}


class MySqlDatabase implements Database {

  public $connection;
  public $tablename = "films";
  public $indexfield = "fcode";
  public $indexdelim = "'";
  public $result;
  public $sql;
  
  function __construct($host, $databasename, $username, $password, $port = 8889) {
   $this->connection = new mysqli($host, $username, $password, $databasename);
   $this->tablename = "films";
   $this->indexfield = "fcode";
   $this->indexdelim = "'";
  }
  
  function __destruct() {
   $this->connection->close();
  }
  
  function __get($name) {
    return $this->$name;
  }
  
  function __set($name, $value) {
    $this->$name = $value;
  }
  
  function listFilms() {
   $retval = "<div align='center' style='background-color: pink;'><table cellspacing=5 cellpadding=5><tr><th>MySql Film Code</th><th>Title</th><th>Id</th><th>Date</th><th>Kind</th><th>Length</th></tr>";
   $result = $this->connection->query("SELECT * FROM " . $this->tablename);
   while ($row = $result->fetch_array( MYSQLI_NUM )) {
     $retval .= "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td></tr>";
   }
   $result->close();
   $retval .= "</table></div>";
   return $retval;
  }
  
  function addFilm($code = "", $title = "", $did = "", $date_prod = "", $kind = "", $len = "") {
   $this->sql = "";
   if ($code != "") {
    $this->result = $this->connection->query("INSERT INTO " . $this->tablename . " (" . $this->indexfield . ",title,anid,pdate,fkind,flen) values (" . $this->indexdelim . $code . $this->indexdelim . ",'" . $title . "'," . $did . ",'" . $date_prod . "','" . $kind . "','" . $len . "')");
   }
  }
  
  function removeFilm($which) {
   $this->sql = "DELETE from " . $this->tablename . " WHERE " . $this->indexfield . "=" . $this->indexdelim . $which . $this->indexdelim;
   $this->result = $this->connection->query($this->sql);
  }
  
  function amendFilm($code = "", $title = "", $did = "", $date_prod = "", $kind = "", $len = "") {
   $this->sql = "";
   if ($code != "") {
   $delim=" ";
   $this->sql = "UPDATE " . $this->tablename . " SET WHERE " . $this->indexfield . "=" . $this->indexdelim . $code . $this->indexdelim;
   if (isset($_GET['title']) && ($_GET['title'] != "")) {
     $this->sql = str_replace(" WHERE ", $delim . " title='" . $title . "'" . " WHERE ", $this->sql);
     $delim = ", ";
   }
   if (strpos($did,"-") === false && ($did != "")) {
     $this->sql = str_replace(" WHERE ", $delim . " anid=" . $did . " WHERE ", $this->sql);
     $delim = ", ";
   }
   if (($date_prod != "")) { 
     //$this->sql = str_replace(" WHERE ", $delim . " pdate=to_date('" . $date_prod . "','YYYY-MM-DD')" . " WHERE ", $this->sql);
     $this->sql = str_replace(" WHERE ", $delim . " pdate='" . $date_prod . "'" . " WHERE ", $this->sql);
     $delim = ", ";
   }
   if (($kind != "")) {
     $this->sql = str_replace(" WHERE ", $delim . " fkind='" . $kind . "'" . " WHERE ", $this->sql);
     $delim = ", ";
   }
   if (($len != "")) {
     $this->sql = str_replace(" WHERE ", $delim . " flen='" . $len . "'" . " WHERE ", $this->sql);
     $delim = ", ";
   }
   }
   if ($this->sql != "") {
    $this->result = $this->connection->query($this->sql);
   }
  }
}

class PostgreSQLDatabase implements Database {

  public $connection;
  public $tablename = "films";
  public $indexfield = "code";
  public $indexdelim = "'";
  public $result;
  public $sql;
  
  function __construct($host, $databasename, $username, $password, $port = 5432) {
   $this->connection = new PDO('pgsql:host=' . $host . ';port=' . $port . ';dbname=' . $databasename . ';user=' . $username . ';password=' . $password);
   $this->tablename = "films";
   $this->indexfield = "code";
   $this->indexdelim = "'";
  }
  
  function __destruct() {
   $this->connection->close();
  }
  
  function __get($name) {
    return $this->$name;
  }
  
  function __set($name, $value) {
    $this->$name = $value;
  }
  
  function listFilms() {
   $retval = "<div align='center' style='background-color: pink;'><table cellspacing=5 cellpadding=5><tr><th>PostgreSQL Film Code</th><th>Title</th><th>Id</th><th>Date</th><th>Kind</th><th>Length</th></tr>";
   foreach ($this->connection->query("SELECT * FROM " . $this->tablename) as $row) {
     $retval .= "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td></tr>";
   } 
   $retval .= "</table></div>";
   return $retval;
  }
  
  function addFilm($code = "", $title = "", $did = "", $date_prod = "", $kind = "", $len = "") {
   $this->sql = "";
   if ($code != "") {
    $this->result = $this->connection->query("INSERT INTO " . $this->tablename . " (" . $this->indexfield . ",title,did,date_prod,kind,len) values (" . $this->indexdelim . $code . $this->indexdelim . ",'" . $title . "'," . $did . ",to_date('" . $date_prod . "','YYYY-MM-DD'),'" . $kind . "','" . $len . "')");
   }
  }
  
  function removeFilm($which) {
   $this->sql = "DELETE from " . $this->tablename . " WHERE " . $this->indexfield . "=" . $this->indexdelim . $which . $this->indexdelim;
   $this->result = $this->connection->query($this->sql);
  }
  
  function amendFilm($code = "", $title = "", $did = "", $date_prod = "", $kind = "", $len = "") {
   $this->sql = "";
   if ($code != "") {
   $delim=" ";
   $this->sql = "UPDATE " . $this->tablename . " SET WHERE " . $this->indexfield . "=" . $this->indexdelim . $code . $this->indexdelim;
   if (isset($_GET['title']) && ($_GET['title'] != "")) {
     $this->sql = str_replace(" WHERE ", $delim . " title='" . $title . "'" . " WHERE ", $this->sql);
     $delim = ", ";
   }
   if (strpos($did,"-") === false && ($did != "")) {
     $this->sql = str_replace(" WHERE ", $delim . " did=" . $did . " WHERE ", $this->sql);
     $delim = ", ";
   }
   if (($date_prod != "")) { 
     $this->sql = str_replace(" WHERE ", $delim . " date_prod=to_date('" . $date_prod . "','YYYY-MM-DD')" . " WHERE ", $this->sql);
     $delim = ", ";
   }
   if (($kind != "")) {
     $this->sql = str_replace(" WHERE ", $delim . " kind='" . $kind . "'" . " WHERE ", $this->sql);
     $delim = ", ";
   }
   if (($len != "")) {
     $this->sql = str_replace(" WHERE ", $delim . " len='" . $len . "'" . " WHERE ", $this->sql);
     $delim = ", ";
   }
   }
   if ($this->sql != "") {
    $this->result = $this->connection->query($this->sql);
   }
  }
}

// Start of execution code ...
echo "<!doctype html><html><head><title>PostgreSQL or MySql Form - RJM Programming - June, 2015</title><style> td { border:2px red solid; } </style></head><body onload=\"document.getElementById('code').focus(); \" style='background-color:yellow;'><h1 align='center'>PostgreSQL or MySql Form - RJM Programming - May, 2015</h1><br>";

$mydbtype = "PostgreSQL";
$myaltdbtype = "MySql";
if ((isset($_GET['MySql']) || isset($_POST['MySql'])) && (isset($_GET['PostgreSQL']) || isset($_POST['PostgreSQL']))) {  
  if (($_GET['MySql'] . $_POST['MySql']) != '') { 
   $database = new MySqlDatabase("localhost", "filmsdb", "username", "password");
   $myaltdbtype = "PostgreSQL";
   $mydbtype = "MySql";
  } else {
   $database = new PostgreSQLDatabase("localhost", "mydatabase", "username", "password");   
  }
} else if (isset($_GET['MySql']) || isset($_POST['MySql'])) {  
  $database = new MySqlDatabase("localhost", "filmsdb", "username", "password");
  $myaltdbtype = "PostgreSQL";
  $mydbtype = "MySql";
} else {
  $database = new PostgreSQLDatabase("localhost", "mydatabase", "username", "password");   
}

 if (isset($_GET['code']) && ($_GET['code'] != "") && isset($_GET['delete'])) {
   $database->removeFilm($_GET['code']);
 } else if (isset($_GET['code']) && ($_GET['code'] != "") && strpos($_GET['did'],"-") === false && isset($_GET['title']) && isset($_GET['did']) && isset($_GET['date_prod']) && isset($_GET['kind']) && isset($_GET['len']) && ($_GET['title'] != "") && ($_GET['did'] != "") && ($_GET['date_prod'] != "") && ($_GET['kind'] != "") && ($_GET['len'] != "")) {
   $database->addFilm($_GET['code'],$_GET['title'],$_GET['did'],$_GET['date_prod'],$_GET['kind'],$_GET['len']);
 } else if (isset($_GET['code']) && ($_GET['code'] != "")) {
   $database->amendFilm($_GET['code'],$_GET['title'],$_GET['did'],$_GET['date_prod'],$_GET['kind'],$_GET['len']);
 }

 echo $database->listFilms();

 echo "<br><br><div align='center' style='background-color: lightgray;'><form method='GET' action='./filminterface.php'>";
 echo "<table cellspacing=5 cellpadding=5>";
 echo "<table cellspacing=5 cellpadding=5><tr><th>" . $mydbtype . " Film Code: </th><th><input id='code' style='width:220px;' type='text' name='code' value=''></input></th></tr>";
 echo "<tr><th>    Title: </th><th><input style='width:220px;' type='text' name='title' value=''></input></th></tr>";
 echo "<tr><th>       Id: </th><th><input style='width:220px;' type='number' name='did' value='-1'></input></th></tr>";
 echo "<tr><th>     Date: </th><th><input style='width:220px;' type='text' name='date_prod' value='2000-12-31'></input></th></tr>";
 echo "<tr><th>     Kind: </th><th><input style='width:220px;' type='text' name='kind' value=''></input></th></tr>";
 echo "<tr><th>   Length: </th><th><input style='width:220px;' type='text' name='len' value='01:00:00'></input></th></tr>";
 echo "</table><br><input type='hidden' name='" . $mydbtype . "' value=''></input><input type='submit' name='" . $myaltdbtype . "' value='" . $myaltdbtype . "'></input>&nbsp;<input type='submit' value='Update or Insert'></input>&nbsp;<input name='delete' type='submit' value='Delete'></input><br><p>&nbsp;</p></div>";

 echo "</body></html>";
 unset($database);

?>
