<?php
// oopsabstract.php
// Some (abstract) class ideas
// RJM Programming
// June, 2015

function __autoload($name) {
  include_once $name . ".php";
}

abstract class publication {
  
  public function NonFooter($thingidea = "", $idea = "") {
    echo "<!doctype html>\n<html>\n<head>";
    $this->DisplayTitle();
    $this->DisplayStyling();
    $this->DisplayScript();
    echo "</head><body>";
    $this->DisplayHeading();
    if ($thingidea == "" && $idea == "") {
      $this->DisplayAuthor();
    } else {
      $this->DisplayThing($thingidea, $idea);
    }
    $this->DisplayPublishDate();
  }
    
  public function Footer($prefix = "") {
    echo $prefix . "</body></html>";
  }

  public function DisplayTitle() {
    echo "<title>" . $this->title . "</title>";
  }
  
  public function DisplayExternalStyling() {
    echo "<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>";
  }
  
  public function DisplayStyling() {
    $this->DisplayExternalStyling();
    echo "<style> ";
    echo " body { background-color: yellow; } ";
    echo " td { border: 3px solid purple; } ";
    echo " </style>";
  }
  
  public function DisplayScript() {
    echo "<script type='text/javascript'> ";
    echo " </script>";
  }
  
  public function DisplayHeading() {
    echo "<h1 align='center'>Title: " . $this->title . "</h1>";
  }
  
  public function DisplayAuthor() {
    echo "<h2 align='center'>Author: " . $this->author . "</h2>";
  }
  
  public function __call($method, $parr) {
    $retval = "";
    if ($method == "DateTimeDisplay") {
      if ($parr[0][1] == "") {
        $retval .= str_replace("-", " ", str_replace(":", " ", $parr[0][0]));
      } else if (is_numeric($parr[0][1])) {
        $months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
        if (sizeof($parr[0]) == 2) {
         $retval .= $months[$parr[0][1] - 1] . " " . $parr[0][0];
        } else if (is_numeric($parr[0][0])) {
         if (strlen($parr[0][0]) > strlen($parr[0][2]) || $parr[0][0] > 31) {
          $retval .= $parr[0][2] . " " . $months[$parr[0][1] - 1];
          $parr[0][2] = $parr[0][0];
          for ($i=2; $i<sizeof($parr[0]); $i++) $retval .= " " . $parr[0][$i];
         } else {
          $retval .= $parr[0][0] . " " . $months[$parr[0][1] - 1];
          for ($i=2; $i<sizeof($parr[0]); $i++) $retval .= " " . $parr[0][$i];
         }
        } else {
         $retval .= $parr[0][0] . " " . $months[$parr[0][1] - 1];
         for ($i=2; $i<sizeof($parr[0]); $i++) $retval .= " " . $parr[0][$i];
        }
      } else if (is_string($parr[0][1])) {
        if (strlen($parr[0][0]) > strlen($parr[0][2]) || $parr[0][0] > 31) {
          $retval .= $parr[0][2] . " " . $parr[0][1];
          $parr[0][2] = $parr[0][0];
          for ($i=2; $i<sizeof($parr[0]); $i++) $retval .= " " . $parr[0][$i];
        } else {
          $retval .= $parr[0][0] . " " . $parr[0][1];
          for ($i=2; $i<sizeof($parr[0]); $i++) $retval .= " " . $parr[0][$i];
        }
      } else {
        $retval .= str_replace("-", " ", str_replace(":", " ", $parr[0][0]));
      }
    }
    return $retval;
  }
  
  
  public function DisplayPublishDate() {
    $dfields = explode("-", str_replace("/", "-", $this->publish_date));
    if (sizeof($dfields) == 1) $dfields = [$this->publish_date, ""];
    $tfields = [];
    if (sizeof($dfields) >= 3) {
      $tfields = explode(":", $dfields[sizeof($dfields) - 1]);
      if (sizeof($tfields) == 1 && $tfields[0] == $dfields[sizeof($dfields) - 1]) $tfields = [];
    }
    echo "<h3 align='center'>Published On: " . $this->DateTimeDisplay(array_merge($dfields, $tfields)); 
  }
  
  public function DisplayThing($nameofthing, $thing) {
    $hthing = "4";
    if ($nameofthing == "Author") $hthing = "2";
    if ($nameofthing == "Publisher") $hthing = "2";
    if ($nameofthing == "Title") $hthing = "1";
    if (strpos($nameofthing, "Date") !== false || strpos($nameofthing, "Published On") !== false || strpos($nameofthing, " On") !== false) {
      $hthing = "3";
      $this->DisplayPublishDate();
    } else {
      echo "<h" . $hthing . " align='center'>" . $nameofthing . ": " . $thing . "</h" . $hthing . ">";
    }
  }
}


class book extends publication {
  public $title;
  public $author;
  public $publish_date;
  
  function __construct($indata1, $indata2, $indata3) {
    $this->title = $indata1;
    $this->author = $indata2;
    $this->publish_date = $indata3;
  }
  
  function __destruct() {
  }
  
  function __get($name) {
    return $this->$name;
  }
  
  function __set($name, $value) {
    $this->$name = $value;
  }
  
  public function Display() {
    $this->NonFooter();
    $this->Footer();
  }

  
}

class magazine extends publication {
  public $title;
  public $publisher;
  public $publish_date;
  
  function __construct($indata1, $indata2, $indata3) {
    $this->title = $indata1;
    $this->publisher = $indata2;
    $this->publish_date = $indata3;
  }
  
  function __destruct() {
  }
  
  function __get($name) {
    return $this->$name;
  }
  
  function __set($name, $value) {
    $this->$name = $value;
  }
  
  public function Display() {
    $this->NonFooter("Publisher", $mymagazine->publisher);
    $this->Footer();
  }

  
}


// Program execution starts here ...
$myformstring = "";
$mybook = new book("To Kill A Mockingbird", "Harper Lee", "1960-07-11");
$mymagazine = new magazine("Cosmopolitan", "Schlicht & Field", "1886");
if (1 == 1) {
 $mymagazine->NonFooter("Publisher", $mymagazine->publisher);
 $concepts = ["Title", "Author", "Publish Date"];
 $j = 0;
 foreach ($mybook as $attrib) {
  $mybook->DisplayThing($concepts[$j], $attrib);
  $j++;
 }
} else {
 $mybook->NonFooter();
 $concepts = ["Title", "Publisher", "Publish Date"];
 $j = 0;
 foreach ($mymagazine as $attrib) {
  $mymagazine->DisplayThing($concepts[$j], $attrib);
  $j++;
 }
}

if (isset($_GET['publisher'])) {
  $mymagazine = new magazine($_GET['title'], $_GET['publisher'], $_GET['publish_date']);
  $concepts = ["Title", "Publisher", "Publish Date"];
  $j = 0;
  foreach ($mymagazine as $attrib) {
   $mymagazine->DisplayThing($concepts[$j], $attrib);
   $j++;
  }
} else if (isset($_POST['publisher'])) {
  $mymagazine = new magazine($_POST['title'], $_POST['publisher'], $_POST['publish_date']);
  $concepts = ["Title", "Publisher", "Publish Date"];
  $j = 0;
  foreach ($mymagazine as $attrib) {
   $mymagazine->DisplayThing($concepts[$j], $attrib);
   $j++;
  }
} else if (isset($_GET['author'])) {
  $mybook = new book($_GET['title'], $_GET['author'], $_GET['publish_date']);
  $concepts = ["Title", "Author", "Publish Date"];
  $j = 0;
  foreach ($mybook as $attrib) {
   $mybook->DisplayThing($concepts[$j], $attrib);
   $j++;
  }
} else if (isset($_POST['author'])) {
  $mybook = new book($_POST['title'], $_POST['author'], $_POST['publish_date']);
  $concepts = ["Title", "Author", "Publish Date"];
  $j = 0;
  foreach ($mybook as $attrib) {
   $mybook->DisplayThing($concepts[$j], $attrib);
   $j++;
  }
}
 
// Create a form for optional interaction ...
$myformstring = "<br><div align='center'><table cellpadding=4 cellspacing=4><tr><th>Your Book</th><th>Your Magazine</th></tr><tr><td>";
$myformstring .= "<form style='background-color: lightblue;' action='./oopsabstract.php' method='POST'>Title: <input name='title' value='' type='text' /><br><br>Author: <input name='author' value='' type='text' /><br><br>Published On: <input name='publish_date' value='' type='text' /><input value='Book' type='submit' /></form>";
$myformstring .= "</td><td>";
$myformstring .= "<form style='background-color: lightgreen;' action='./oopsabstract.php' method='POST'>Title: <input name='title' value='' type='text' /><br><br>Publisher: <input name='publisher' value='' type='text' /><br><br>Published On: <input name='publish_date' value='' type='text' /><input value='Magazine' type='submit' /></form>";
$myformstring .= "</td></tr></table></div>";

$mypublication = clone $mybook;
$mypublication->Footer($myformstring);



?>