Skip to content
Snippets Groups Projects
Commit 7c459939 authored by Simon Pototzki's avatar Simon Pototzki
Browse files

Copy Seitenklassen into Prak2

parent c6565b8d
No related branches found
No related tags found
No related merge requests found
<?php declare(strict_types=1);
// UTF-8 marker äöüÄÖÜ߀
/**
* Class Page for the exercises of the EWA lecture
* Demonstrates use of PHP including class and OO.
* Implements Zend coding standards.
* Generate documentation with Doxygen or phpdoc
*
* PHP Version 7.4
*
* @file Page.php
* @package Page Templates
* @author Bernhard Kreling, <bernhard.kreling@h-da.de>
* @author Ralf Hahn, <ralf.hahn@h-da.de>
* @version 3.1
*/
/**
* This abstract class is a common base class for all
* HTML-pages to be created.
* It manages access to the database and provides operations
* for outputting header and footer of a page.
* Specific pages have to inherit from that class.
* Each derived class can use these operations for accessing the database
* and for creating the generic parts of a HTML-page.
*
* @author Bernhard Kreling, <bernhard.kreling@h-da.de>
* @author Ralf Hahn, <ralf.hahn@h-da.de>
*/
abstract class Page
{
// --- ATTRIBUTES ---
/**
* Reference to the MySQLi-Database that can be used
* by all operations of the class or inherited classes.
*/
protected MySQLi $_database;
// --- OPERATIONS ---
/**
* Connects to DB and stores
* the connection in member $_database.
* Needs name of DB, user, password.
*/
protected function __construct()
{
error_reporting(E_ALL);
$host = "localhost";
/********************************************/
// This code switches from the the local installation (XAMPP) to the docker installation
if (gethostbyname('mariadb') != "mariadb") { // mariadb is known?
$host = "mariadb";
}
/********************************************/
$this->_database = new MySQLi($host, "public", "public", "YOUR_DATABASE");
if ($this->_database->connect_errno) {
throw new Exception("Connect failed: " . $this->_database->connect_errno);
}
// set charset to UTF8!!
if (!$this->_database->set_charset("utf8")) {
throw new Exception($this->_database->error);
}
}
/**
* Closes the DB connection and cleans up
*/
public function __destruct()
{
// to do: close database
}
/**
* Generates the header section of the page.
* i.e. starting from the content type up to the body-tag.
* Takes care that all strings passed from outside
* are converted to safe HTML by htmlspecialchars.
*
* @param string $title $title is the text to be used as title of the page
* @param string $jsFile path to a java script file to be included, default is "" i.e. no java script file
* @param bool $autoreload true: auto reload the page every 5 s, false: not auto reload
* @return void
*/
protected function generatePageHeader(string $title = "", string $jsFile = "", bool $autoreload = false):void
{
$title = htmlspecialchars($title);
header("Content-type: text/html; charset=UTF-8");
// to do: handle all parameters
// to do: output common beginning of HTML code
}
/**
* Outputs the end of the HTML-file i.e. </body> etc.
* @return void
*/
protected function generatePageFooter():void
{
// to do: output common end of HTML code
}
/**
* Processes the data that comes in via GET or POST.
* If every derived page is supposed to do something common
* with submitted data do it here.
* E.g. checking the settings of PHP that
* influence passing the parameters (e.g. magic_quotes).
* @return void
*/
protected function processReceivedData():void
{
}
} // end of class
// Zend standard does not like closing php-tag!
// PHP doesn't require the closing tag (it is assumed when the file ends).
// Not specifying the closing ? > helps to prevent accidents
// like additional whitespace which will cause session
// initialization to fail ("headers already sent").
//? >
\ No newline at end of file
<?php declare(strict_types=1);
// UTF-8 marker äöüÄÖÜ߀
/**
* Class PageTemplate for the exercises of the EWA lecture
* Demonstrates use of PHP including class and OO.
* Implements Zend coding standards.
* Generate documentation with Doxygen or phpdoc
*
* PHP Version 7.4
*
* @file PageTemplate.php
* @package Page Templates
* @author Bernhard Kreling, <bernhard.kreling@h-da.de>
* @author Ralf Hahn, <ralf.hahn@h-da.de>
* @version 3.1
*/
// to do: change name 'PageTemplate' throughout this file
require_once './Page.php';
/**
* This is a template for top level classes, which represent
* a complete web page and which are called directly by the user.
* Usually there will only be a single instance of such a class.
* The name of the template is supposed
* to be replaced by the name of the specific HTML page e.g. baker.
* The order of methods might correspond to the order of thinking
* during implementation.
* @author Bernhard Kreling, <bernhard.kreling@h-da.de>
* @author Ralf Hahn, <ralf.hahn@h-da.de>
*/
class PageTemplate extends Page
{
// to do: declare reference variables for members
// representing substructures/blocks
/**
* Instantiates members (to be defined above).
* Calls the constructor of the parent i.e. page class.
* So, the database connection is established.
* @throws Exception
*/
protected function __construct()
{
parent::__construct();
// to do: instantiate members representing substructures/blocks
}
/**
* Cleans up whatever is needed.
* Calls the destructor of the parent i.e. page class.
* So, the database connection is closed.
*/
public function __destruct()
{
parent::__destruct();
}
/**
* Fetch all data that is necessary for later output.
* Data is returned in an array e.g. as associative array.
* @return array An array containing the requested data.
* This may be a normal array, an empty array or an associative array.
*/
protected function getViewData():array
{
// to do: fetch data for this view from the database
// to do: return array containing data
}
/**
* First the required data is fetched and then the HTML is
* assembled for output. i.e. the header is generated, the content
* of the page ("view") is inserted and -if available- the content of
* all views contained is generated.
* Finally, the footer is added.
* @return void
*/
protected function generateView():void
{
$data = $this->getViewData(); //NOSONAR ignore unused $data
$this->generatePageHeader('to do: change headline'); //to do: set optional parameters
// to do: output view of this page
$this->generatePageFooter();
}
/**
* Processes the data that comes via GET or POST.
* If this page is supposed to do something with submitted
* data do it here.
* @return void
*/
protected function processReceivedData():void
{
parent::processReceivedData();
// to do: call processReceivedData() for all members
}
/**
* This main-function has the only purpose to create an instance
* of the class and to get all the things going.
* I.e. the operations of the class are called to produce
* the output of the HTML-file.
* The name "main" is no keyword for php. It is just used to
* indicate that function as the central starting point.
* To make it simpler this is a static function. That is you can simply
* call it without first creating an instance of the class.
* @return void
*/
public static function main():void
{
try {
$page = new PageTemplate();
$page->processReceivedData();
$page->generateView();
} catch (Exception $e) {
//header("Content-type: text/plain; charset=UTF-8");
header("Content-type: text/html; charset=UTF-8");
echo $e->getMessage();
}
}
}
// This call is starting the creation of the page.
// That is input is processed and output is created.
PageTemplate::main();
// Zend standard does not like closing php-tag!
// PHP doesn't require the closing tag (it is assumed when the file ends).
// Not specifying the closing ? > helps to prevent accidents
// like additional whitespace which will cause session
// initialization to fail ("headers already sent").
//? >
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment