Advertisement
algore87

kunde.php

Jan 10th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.22 KB | None | 0 0
  1. <?php   // UTF-8 marker äöüÄÖÜ߀
  2. /**
  3.  * Class PageTemplate for the exercises of the EWA lecture
  4.  * Demonstrates use of PHP including class and OO.
  5.  * Implements Zend coding standards.
  6.  * Generate documentation with Doxygen or phpdoc
  7.  *
  8.  * PHP Version 5
  9.  *
  10.  * @category File
  11.  * @package  Pizzaservice
  12.  * @author   Bernhard Kreling, <b.kreling@fbi.h-da.de>
  13.  * @author   Ralf Hahn, <ralf.hahn@h-da.de>
  14.  * @license  http://www.h-da.de  none
  15.  * @Release  1.2
  16.  * @link     http://www.fbi.h-da.de
  17.  */
  18.  
  19. // TODO: Sessions nur Kundenpizzas zeigen (passende ID in kundenstatus.php wählen)
  20. require_once 'Page.php';
  21.  
  22. /**
  23.  * This is a template for top level classes, which represent
  24.  * a complete web page and which are called directly by the user.
  25.  * Usually there will only be a single instance of such a class.
  26.  * The name of the template is supposed
  27.  * to be replaced by the name of the specific HTML page e.g. baker.
  28.  * The order of methods might correspond to the order of thinking
  29.  * during implementation.
  30.  
  31.  * @author   Bernhard Kreling, <b.kreling@fbi.h-da.de>
  32.  * @author   Ralf Hahn, <ralf.hahn@h-da.de>
  33.  */
  34. class Kunde extends Page
  35. {
  36.     // to do: declare reference variables for members
  37.     // representing substructures/blocks
  38.     protected $kundenstatus = null;
  39.  
  40.     /**
  41.      * Instantiates members (to be defined above).  
  42.      * Calls the constructor of the parent i.e. page class.
  43.      * So the database connection is established.
  44.      *
  45.      * @return none
  46.      */
  47.     protected function __construct()
  48.     {
  49.         parent::__construct();
  50.         // to do: instantiate members representing substructures/blocks
  51.         require_once './divs/kundenstatus.php';
  52.         $this->kundenstatus = new Kundenstatus($this->database);
  53.     }
  54.    
  55.     /**
  56.      * Cleans up what ever is needed.  
  57.      * Calls the destructor of the parent i.e. page class.
  58.      * So the database connection is closed.
  59.      *
  60.      * @return none
  61.      */
  62.     protected function __destruct()
  63.     {
  64.         parent::__destruct();
  65.     }
  66.  
  67.     /**
  68.      * Fetch all data that is necessary for later output.
  69.      * Data is stored in an easily accessible way e.g. as associative array.
  70.      *
  71.      * @return none
  72.      */
  73.     protected function getViewData()
  74.     {
  75.         // to do: fetch data for this view from the database
  76.  
  77.     }
  78.    
  79.     /**
  80.      * First the necessary data is fetched and then the HTML is
  81.      * assembled for output. i.e. the header is generated, the content
  82.      * of the page ("view") is inserted and -if avaialable- the content of
  83.      * all views contained is generated.
  84.      * Finally the footer is added.
  85.      *
  86.      * @return none
  87.      */
  88.     protected function generateView()
  89.     {
  90.         $this->getViewData();
  91.         $this->generatePageHeader("Kunde", true);
  92.         echo <<< EOT
  93. <div id="site_body">
  94.     <form action="kunde.php" method="post">
  95.         <fieldset><legend>Kunde</legend>
  96.             <table>
  97.                 <tr>
  98.                     <td></td>
  99.                     <td>bestellt</td>
  100.                     <td>im Ofen</td>
  101.                     <td>gebacken</td>
  102.                     <td>unterwegs</td>
  103.                 </tr>
  104. EOT;
  105.         /* INHALT GENERATE BESTELLTE PIZZEN*/
  106.         $this->kundenstatus->generateView("pizza");
  107.         echo <<< EOT
  108.             </table>
  109.             <p><a class="button" href="bestellung.php">Bestellung</a></p>
  110.         </fieldset>
  111.     </form>
  112. EOT;
  113.  
  114.         // to do: output view of this page
  115.         $this->generatePageFooter();
  116.     }
  117.    
  118.     /**
  119.      * Processes the data that comes via GET or POST i.e. CGI.
  120.      * If this page is supposed to do something with submitted
  121.      * data do it here.
  122.      * If the page contains blocks, delegate processing of the
  123.      * respective subsets of data to them.
  124.      *
  125.      * @return none
  126.      */
  127.     protected function processReceivedData()
  128.     {
  129.         parent::processReceivedData();
  130.         // to do: call processReceivedData() for all members
  131.  
  132.         /** Serverseitige Überprüfung ob alle Felder ausgefüllt sind und Pizzen im Warenkorb */
  133.         if (isset($_POST["Warenkorb"]) AND
  134.             isset($_POST["nachname"]) AND
  135.             isset($_POST["strasse"]) AND
  136.             isset($_POST["plz"]) AND
  137.             isset($_POST["ort"])) {
  138.  
  139.             /** Pizzaüberprüfung (Pizza im Warenkorb auch in Auswahl? TODO) */
  140.             /*
  141.             $pizzaNameArray = [];
  142.             $isPizzaIn = true;
  143.             $pizzaTable = $this->database->query("SELECT PizzaName FROM Angebot");
  144.             while (($row = $pizzaTable->fetch_assoc()) != null)
  145.                 $pizzaNameArray[] = $row["PizzaName"];
  146.             foreach ($pizzaNameArray as $pizza) // check if all pizzas are in pizzaNameArray (isPizzaIn true)
  147.                 echo $pizza . " ";
  148.             echo "<br>";
  149. */
  150.  
  151.             /** Bestellung hinzufügen */
  152.             $query = $this->database->prepare("INSERT INTO `Bestellung` (`BestellungID`, `Adresse`, `Bestellzeitpunkt`) VALUES (NULL, ?, CURRENT_TIMESTAMP)");
  153.             $adress = $_POST["nachname"] . ", " . $_POST["strasse"] . ", " . $_POST["plz"] . " " . $_POST["ort"];
  154.             $query->bind_param('s', mysqli_real_escape_string($this->database,$adress));
  155.             $query->execute();
  156.             $order_id = $query->insert_id;
  157.  
  158.             /** BestellungsID als SessionID setzen */
  159.             setcookie("SessionID", "", time() - 3600);
  160.             setcookie("SessionID", $order_id, time() + 3600);
  161.  
  162.             /** Bestellte Pizzen der Bestellung hinzufügen */
  163.             foreach ($_POST["Warenkorb"] as $pizza) {
  164.                 $query2 = $this->database->prepare("INSERT INTO `BestelltePizza` (`PizzaID`, `fBestellungID`, `fPizzaName`) VALUES (NULL, ?, ?)");
  165.                 $query2->bind_param('is', $order_id, mysqli_real_escape_string($this->database,$pizza)); // mysql_real_escapestring binden!
  166.                 $query2->execute();
  167.             }
  168.         }
  169.     }
  170.  
  171.     /**
  172.      * This main-function has the only purpose to create an instance
  173.      * of the class and to get all the things going.
  174.      * I.e. the operations of the class are called to produce
  175.      * the output of the HTML-file.
  176.      * The name "main" is no keyword for php. It is just used to
  177.      * indicate that function as the central starting point.
  178.      * To make it simpler this is a static function. That is you can simply
  179.      * call it without first creating an instance of the class.
  180.      *
  181.      * @return none
  182.      */    
  183.     public static function main()
  184.     {
  185.         try {
  186.             $page = new Kunde();
  187.             $page->processReceivedData();
  188.             $page->generateView();
  189.         }
  190.         catch (Exception $e) {
  191.             header("Content-type: text/plain; charset=UTF-8");
  192.             echo $e->getMessage();
  193.         }
  194.     }
  195. }
  196.  
  197. // This call is starting the creation of the page.
  198. // That is input is processed and output is created.
  199. Kunde::main();
  200.  
  201. // Zend standard does not like closing php-tag!
  202. // PHP doesn't require the closing tag (it is assumed when the file ends).
  203. // Not specifying the closing ? >  helps to prevent accidents
  204. // like additional whitespace which will cause session
  205. // initialization to fail ("headers already sent").
  206. //? >
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement