diff --git a/blog/admin.php b/blog/admin.php new file mode 100755 index 0000000000000000000000000000000000000000..d377b0f0627b8053c948cc4dcd9a01b7099bbf2b --- /dev/null +++ b/blog/admin.php @@ -0,0 +1,55 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <title>Benjamin Wells Blog</title> + <link rel="stylesheet" href="style.css"> +</head> +<body> + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +error_reporting( E_ALL ); +ini_set( "display_errors", 1 ); + +include_once "views/admin/admin-navigation.php"; + +//my myphp credentials that i use to connet to the database +$dbInfo = "mysql:host=igor.gold.ac.uk;dbname=ma301bw_myblog"; +$dbUser = "ma301bw"; +$dbPassword = "workdal1"; + + +try{ +//connecting to database using pdo +$db = new PDO( $dbInfo, $dbUser, $dbPassword ); +$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); + echo "<h1>Successfully Connected!</h1>"; + +}catch ( Exception $e ) { +//"connection failed!" is printed if you do not successfully connect to the database + echo "<h1>Connection failed!</h1><p>$e</p>"; +} + +$title = "PHP/MySQL blog demo"; +$css="css/blog.css"; +$embeddedStyle = ""; + +include_once "views/header.php"; +include_once "views/admin/admin-navigation.php"; + +$navigation = isset( $_GET['page'] ); +if ( $navigation ) { + //prepare to load corresponding controller + $contrl = $_GET['page']; +} else { + //or prepare to load default controller + $contrl = "Entries"; +} +//load the controller +include_once "controllers/admin/$contrl.php"; + +include_once "views/footer.php"; + +?> diff --git a/blog/controllers/.DS_Store b/blog/controllers/.DS_Store new file mode 100755 index 0000000000000000000000000000000000000000..83bf3a8c0f021b5b21b0aae9024a9da31062006a Binary files /dev/null and b/blog/controllers/.DS_Store differ diff --git a/blog/controllers/._.DS_Store b/blog/controllers/._.DS_Store new file mode 100755 index 0000000000000000000000000000000000000000..95109874405d38e9c6dd139982f20673c2a03e88 Binary files /dev/null and b/blog/controllers/._.DS_Store differ diff --git a/blog/controllers/._blog.php b/blog/controllers/._blog.php new file mode 100755 index 0000000000000000000000000000000000000000..dde4dbc63200b28fccb3f1bdad143dd6d8ae738b Binary files /dev/null and b/blog/controllers/._blog.php differ diff --git a/blog/controllers/admin/editor.php b/blog/controllers/admin/editor.php new file mode 100755 index 0000000000000000000000000000000000000000..ed55774fe851f482d0517c4c224511038f30fcc5 --- /dev/null +++ b/blog/controllers/admin/editor.php @@ -0,0 +1,24 @@ +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +include_once "blog/models/Blog_Entry_Table.class.php"; +$entryTable = new Blog_Entry_Table( $db ); + +//was editor form submitted? +$editorSubmitted = isset( $_POST['action'] ); +if ( $editorSubmitted ) { + $buttonClicked = $_POST['action']; + //was "save" button clicked + $insertNewEntry = ( $buttonClicked === 'save' ); + + if ( $insertNewEntry ) { + $title = $_POST['title']; + $entry = $_POST['entry']; + //save the new entry + $entryTable->saveEntry( $title, $entry ); + } +} + +include_once "blog/views/admin/editor-html.php"; + +?> \ No newline at end of file diff --git a/blog/controllers/admin/entries.php b/blog/controllers/admin/entries.php new file mode 100755 index 0000000000000000000000000000000000000000..c6587c742353ce1a634a01f5484c5429fe9eab29 --- /dev/null +++ b/blog/controllers/admin/entries.php @@ -0,0 +1,13 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +//In entries.php, i include my Blog_Entry_Table class +include_once "models/Blog_Entry_Table.class.php"; +//i then instigate a Blog_Entry_Table Object and then the object assign it to a variable +//using the getAllEntries method +$entryTable = new Blog_Entry_Table( $db ); +$allEntries = $entryTable->getAllEntries(); + +include_once "views/admin/entries-html.php"; +?> \ No newline at end of file diff --git a/blog/controllers/blog.php b/blog/controllers/blog.php new file mode 100755 index 0000000000000000000000000000000000000000..f4635366547ac3ff6e699217c120e625346a01b2 --- /dev/null +++ b/blog/controllers/blog.php @@ -0,0 +1,20 @@ +<? +//code excerpts taken from dans examples on gitlab and sources on the VLE + + +//This selects all the database entries and displays them on the home page of the blog +include_once "models/Blog_Entry_Table.class.php"; +$entryTable = new Blog_Entry_Table( $db ); + +$entryClicked = isset( $_GET['id'] ); +if ($entryClicked ) { + $entryId = $_GET['id']; + $entryData = $entryTable->getEntry( $entryId ); +// print_r($entryData); + include_once "views/entry-html.php"; +} else { + + $entries = $entryTable->getallentries(); + include_once "views/list-entries-html.php"; + +?> \ No newline at end of file diff --git a/blog/coursework-blog.sql b/blog/coursework-blog.sql new file mode 100755 index 0000000000000000000000000000000000000000..9331ce57fe77dca84ae5120b3d98ad778a02b9e9 --- /dev/null +++ b/blog/coursework-blog.sql @@ -0,0 +1,9 @@ +DROP TABLE IF EXISTS blog_entry; +//this creates the neccesary table for you to add blog posts +CREATE TABLE blog_entry ( +entry_id INT NOT NULL AUTO_INCREMENT, +title VARCHAR(140), +entry_text TEXT, +date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, +PRIMARY KEY (entry_id), +); diff --git a/blog/css/.Rhistory b/blog/css/.Rhistory new file mode 100755 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/blog/css/blog.css b/blog/css/blog.css new file mode 100755 index 0000000000000000000000000000000000000000..0b0bcab1df235982051ed58eba18ff417b5cae85 --- /dev/null +++ b/blog/css/blog.css @@ -0,0 +1,25 @@ +//code excerpts taken from dans examples on gitlab and sources on the VLE + +form#editor{ + width: 300px; + margin:0px; + padding:0px; +} + +form#editor label, form#editor input[type='text']{ + display:block; +} + +form#editor #editor-buttons{ + border:none; + text-align:right; +} + +form#editor textarea, form#editor input[type='text']{ + width:90%; + margin-bottom:2em; +} + +form#editor textarea{ + height:10em; +} \ No newline at end of file diff --git a/blog/index.php b/blog/index.php new file mode 100755 index 0000000000000000000000000000000000000000..fd196b7ce62055cec6b67deead94897cbd2c4375 --- /dev/null +++ b/blog/index.php @@ -0,0 +1,20 @@ +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +error_reporting( E_ALL ); +ini_set( "display_errors", 1 ); + +include_once "../../../coursework_blog_config.php"; +$db = new PDO( $dbInfo, $dbUser, $dbPassword ); +$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); + +$title = "PHP/MySQL blog demo"; +$css="css/blog.css"; +$embeddedStyle = ""; +include_once "views/header.php"; + +include_once "controllers/blog.php"; + +include_once "views/footer.php"; + +?> \ No newline at end of file diff --git a/blog/list-entries-html.php b/blog/list-entries-html.php new file mode 100755 index 0000000000000000000000000000000000000000..b891eac669285a7ce945fb4461cfcb405623878f --- /dev/null +++ b/blog/list-entries-html.php @@ -0,0 +1,30 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$entriesFound = isset( $entries ); +if ( $entriesFound === false ) { + trigger_error( 'views/list-entries-html.php needs $entries' ); +} + +$entriesHTML = "<ul id='blog-entries'>"; + +//this while loop will loop over every blog entry and will print/echo the intro and title +//for each blog post + +//the read more link will tell the controller to show that specific blog entry only +while ( $entry = $entries->fetchObject() ) { + $href = "index.php?page=blog&id=$entry->entry_id"; + //create an <li> for each of the entries + $entriesHTML .= "<li> + <h2>$entry->title</h2> + <div>$entry->intro + <p><a href='$href'>Read more</a></p> + </div> + </li>"; +} +$entriesHTML .= "</ul>"; + +echo $entriesHTML; + +?> \ No newline at end of file diff --git a/blog/models/Blog_Entry_Table.class.php b/blog/models/Blog_Entry_Table.class.php new file mode 100755 index 0000000000000000000000000000000000000000..77c039305cc836786ed7ca4195015c4adc29262e --- /dev/null +++ b/blog/models/Blog_Entry_Table.class.php @@ -0,0 +1,42 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +class Blog_Entry_Table { + private $db; + + + public function __construct ( $db ) { + $this->db = $db; + } + + public function saveEntry ( $title, $entry ) { + $entrySQL = "INSERT INTO blog_entry ( title, entry_text ) + VALUES ( ?, ?)"; + $entryStatement = $this->db->prepare( $entrySQL ); + $formData = array( $title, $entry ); + try{ + $entryStatement->execute( $formData ); + } catch (Exception $e){ + $msg = "<p>You tried to run this sql: $entrySQL<p> + <p>Exception: $e</p>"; + trigger_error($msg); + } + } + + public function getAllEntries () { + $entrySQL = "SELECT entry_id, title, SUBSTRING(entry_text, 1, 140) AS intro FROM blog_entry"; + $statement = $this->db->prepare( $entrySQL ); + try{ + $statement->execute(); + } catch (Exception $e){ + $msg = "<p>You tried to run this sql: $entrySQL<p> + <p>Exception: $e</p>"; + trigger_error($msg); + } + return $statement; + } + + +} +?> \ No newline at end of file diff --git a/blog/views/.DS_Store b/blog/views/.DS_Store new file mode 100755 index 0000000000000000000000000000000000000000..83bf3a8c0f021b5b21b0aae9024a9da31062006a Binary files /dev/null and b/blog/views/.DS_Store differ diff --git a/blog/views/._.DS_Store b/blog/views/._.DS_Store new file mode 100755 index 0000000000000000000000000000000000000000..51f491048ba8d50f851a5db01e6bb9e8ebf11e93 Binary files /dev/null and b/blog/views/._.DS_Store differ diff --git a/blog/views/._entry-html.php b/blog/views/._entry-html.php new file mode 100755 index 0000000000000000000000000000000000000000..72b98c31e375da284302b40a77cfb448f9dad89b Binary files /dev/null and b/blog/views/._entry-html.php differ diff --git a/blog/views/._list-entries-html.php b/blog/views/._list-entries-html.php new file mode 100755 index 0000000000000000000000000000000000000000..656b8332b0100b2d68fb85ad66268d20ccbee345 Binary files /dev/null and b/blog/views/._list-entries-html.php differ diff --git a/blog/views/admin/admin-navigation.php b/blog/views/admin/admin-navigation.php new file mode 100755 index 0000000000000000000000000000000000000000..ec466a89925c020c517b8eaac8e38b13aa8e143b --- /dev/null +++ b/blog/views/admin/admin-navigation.php @@ -0,0 +1,13 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$out = " +<nav id='admin-navigation'> + <a href='admin.php?page=entries'>All entries</a> + <a href='admin.php?page=editor'>Editor</a> +</nav>"; + +echo $out; + +?> \ No newline at end of file diff --git a/blog/views/admin/editor-html.php b/blog/views/admin/editor-html.php new file mode 100755 index 0000000000000000000000000000000000000000..e09a10a8da93d639b706313132d65dd319749e25 --- /dev/null +++ b/blog/views/admin/editor-html.php @@ -0,0 +1,26 @@ + +<?php +//code excerpts taken from dans lab examples on gitlab and sources on the VLE + +//loads the blog entry in to the form, enabling you to edit and delete it +$out = " +<form method='post' action='admin.php?page=editor' id='editor'> + <input type='hidden' name='id' value='$entryData->entry_id' /> + <fieldset> + <legend>New Entry Submission</legend> + <label>Title</label> + <input type='text' name='title' maxlength='150' value='$entryData->title' required /> + + <label>Entry</label> + <textarea name='entry'>$entryData->entry_text</textarea> + + <fieldset id='editor-buttons'> + <input type='submit' name='action' value='save' /> + </fieldset> + </fieldset> +</form> +"; + +echo $out; + +?> \ No newline at end of file diff --git a/blog/views/admin/entries-html.php b/blog/views/admin/entries-html.php new file mode 100755 index 0000000000000000000000000000000000000000..2820449d6e28ffbe978b8c93662c943efad66f72 --- /dev/null +++ b/blog/views/admin/entries-html.php @@ -0,0 +1,21 @@ + + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +if ( isset( $allEntries ) === false ) { +trigger_error('views/admin/entries-html.php needs $allEntries'); +} +$entriesAsHTML = "<ul>"; +// this while loop gets the blog entries and displays them in a list. +while ( $entry = $allEntries->fetchObject() ) { + $href = "admin.php?page=editor&id=$entry->entry_id"; + $entriesAsHTML .= "<li><a href='$href'>$entry->title</a></li>"; +} + +$entriesAsHTML .= "</ul>"; + +//this echo then outputs the entries to the screen. +echo $entriesAsHTML; + +?> \ No newline at end of file diff --git a/blog/views/entry-html.php b/blog/views/entry-html.php new file mode 100755 index 0000000000000000000000000000000000000000..7bdc8cfb7b32eb66153b3292b657ebb8727fbbf4 --- /dev/null +++ b/blog/views/entry-html.php @@ -0,0 +1,17 @@ +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +//the entry-html.php file essentially echos the date title and entry that have been +// created +//check if required data is available +$entryDataFound = isset( $entryData ); +if ( $entryDataFound === false ) { + trigger_error('views/entry-html.php needs an $entryData object'); +} +//properties available in $entry: entry_id, title, entry_text, date_created + +echo "<article> + <h1>$entryData->title</h1> + <div class='date'>$entryData->date_created</div> + $entryData->entry_text +</article>"; \ No newline at end of file diff --git a/blog/views/footer.php b/blog/views/footer.php new file mode 100755 index 0000000000000000000000000000000000000000..58d6ef5a01572574bdd3cef4bce28e24da730925 --- /dev/null +++ b/blog/views/footer.php @@ -0,0 +1,11 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$out = " +</body> +</html> +"; + +echo $out; +?> \ No newline at end of file diff --git a/blog/views/header.php b/blog/views/header.php new file mode 100755 index 0000000000000000000000000000000000000000..7bb615dd613677a7f44b536c96646bda07ebd670 --- /dev/null +++ b/blog/views/header.php @@ -0,0 +1,15 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$out = "<!DOCTYPE html> +<html> + <head> + <title>$title</title> + <meta http-equiv='Content-Type' content='text/html;charset=utf-8' /> + <link rel='stylesheet' type='text/css' href='$css'> + <link rel='stylesheet' type='text/css' href='$embeddedStyle'> + </head>"; + +echo $out; +?> \ No newline at end of file diff --git a/blog/views/list-entries-html.php b/blog/views/list-entries-html.php new file mode 100755 index 0000000000000000000000000000000000000000..c81df28e62b47de446e65df1d1188a90a70a506b --- /dev/null +++ b/blog/views/list-entries-html.php @@ -0,0 +1,26 @@ + +<?php +//code excerpts taken from dans examples on gitlab and sources on the VLE + +$entriesFound = isset( $entries ); +if ( $entriesFound === false ) { + trigger_error( 'views/list-entries-html.php needs $entries' ); +} + +$entriesHTML = "<ul id='blog-entries'>"; + +while ( $entry = $entries->fetchObject() ) { + $href = "index.php?page=blog&id=$entry->entry_id"; + //create an <li> for each of the entries + $entriesHTML .= "<li> + <h2>$entry->title</h2> + <div>$entry->intro + <p><a href='$href'>Read more</a></p> + </div> + </li>"; +} +$entriesHTML .= "</ul>"; + +echo $entriesHTML; + +?> \ No newline at end of file