Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Ifrah Shahid
lab-exercises
Commits
c0ef033b
Commit
c0ef033b
authored
Mar 16, 2016
by
danmcquillan
Browse files
coursework blog steps 10 & 11
parent
70532741
Changes
42
Hide whitespace changes
Inline
Side-by-side
coursework-blog/step-10/admin.php
0 → 100644
View file @
c0ef033b
<?php
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
"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"
;
?>
coursework-blog/step-10/controllers/admin/editor.php
0 → 100644
View file @
c0ef033b
<?php
include_once
"models/Table.class.php"
;
include_once
"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'
];
$id
=
$_POST
[
'id'
];
$save
=
(
$buttonClicked
===
'save'
);
$insertNewEntry
=
(
$save
and
$id
===
'0'
);
$updateEntry
=
(
$save
and
$insertNewEntry
===
false
);
$deleteEntry
=
(
$buttonClicked
===
'delete'
);
$title
=
$_POST
[
'title'
];
$entry
=
$_POST
[
'entry'
];
if
(
$insertNewEntry
)
{
$savedEntryId
=
$entryTable
->
saveEntry
(
$title
,
$entry
);
}
else
if
(
$updateEntry
){
$entryTable
->
updateEntry
(
$id
,
$title
,
$entry
);
$savedEntryId
=
$id
;
}
else
if
(
$deleteEntry
)
{
$entryTable
->
deleteEntry
(
$id
);
}
}
$entryRequested
=
isset
(
$_GET
[
'id'
]
);
$entrySaved
=
isset
(
$savedEntryId
);
if
(
$entryRequested
)
{
$id
=
$_GET
[
'id'
];
$entryData
=
$entryTable
->
getEntry
(
$id
);
$entryData
->
entry_id
=
$id
;
$entryData
->
message
=
""
;
}
else
if
(
$entrySaved
)
{
$entryData
=
$entryTable
->
getEntry
(
$savedEntryId
);
$entryData
->
message
=
"Entry was saved"
;
}
else
{
$entryData
=
new
StdClass
();
$entryData
->
entry_id
=
0
;
$entryData
->
title
=
""
;
$entryData
->
entry_text
=
""
;
$entryData
->
message
=
""
;
}
include_once
"views/admin/editor-html.php"
;
?>
coursework-blog/step-10/controllers/admin/entries.php
0 → 100644
View file @
c0ef033b
<?
include_once
"models/Table.class.php"
;
include_once
"models/Blog_Entry_Table.class.php"
;
$entryTable
=
new
Blog_Entry_Table
(
$db
);
$allEntries
=
$entryTable
->
getAllEntries
();
include_once
"views/admin/entries-html.php"
;
?>
coursework-blog/step-10/controllers/admin/users.php
0 → 100644
View file @
c0ef033b
<?php
include_once
"models/Table.class.php"
;
include_once
"models/Admin_Table.class.php"
;
$createNewAdmin
=
isset
(
$_POST
[
'new-admin'
]
);
if
(
$createNewAdmin
)
{
$newEmail
=
$_POST
[
'email'
];
$newPassword
=
$_POST
[
'password'
];
$adminTable
=
new
Admin_Table
(
$db
);
try
{
$adminTable
->
create
(
$newEmail
,
$newPassword
);
$adminFormMessage
=
"New user created"
;
}
catch
(
Exception
$e
)
{
$adminFormMessage
=
$e
->
getMessage
();
}
}
include_once
"views/admin/new-admin-form-html.php"
;
coursework-blog/step-10/controllers/blog.php
0 → 100644
View file @
c0ef033b
<?
include_once
"models/Table.class.php"
;
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"
;
}
?>
coursework-blog/step-10/coursework-blog.sql
0 → 100644
View file @
c0ef033b
-- this will create a table for blog entries
CREATE
TABLE
blog_entry
(
entry_id
INT
NOT
NULL
AUTO_INCREMENT
,
title
VARCHAR
(
150
),
entry_text
TEXT
,
date_created
TIMESTAMP
DEFAULT
CURRENT_TIMESTAMP
,
PRIMARY
KEY
(
entry_id
)
)
-- this will create a table for admin users
CREATE
TABLE
admin
(
admin_id
INT
NOT
NULL
AUTO_INCREMENT
,
email
TEXT
,
password
VARCHAR
(
32
),
PRIMARY
KEY
(
admin_id
)
)
coursework-blog/step-10/css/blog.css
0 → 100644
View file @
c0ef033b
/* code listing for blog/css/blog.css */
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
;
}
coursework-blog/step-10/index.php
0 → 100644
View file @
c0ef033b
<?php
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"
;
?>
coursework-blog/step-10/models/Admin_Table.class.php
0 → 100644
View file @
c0ef033b
<?
class
Admin_Table
extends
Table
{
public
function
create
(
$email
,
$password
)
{
//check if e-mail is available
$this
->
checkEmail
(
$email
);
//encrypt password with MD5
$sql
=
"INSERT INTO admin ( email, password )
VALUES( ?, SHA1(?) )"
;
$data
=
array
(
$email
,
$password
);
$this
->
makeStatement
(
$sql
,
$data
);
}
private
function
checkEmail
(
$email
)
{
$sql
=
"SELECT email FROM admin WHERE email = ?"
;
$data
=
array
(
$email
);
$this
->
makeStatement
(
$sql
,
$data
);
$statement
=
$this
->
makeStatement
(
$sql
,
$data
);
if
(
$statement
->
rowCount
()
===
1
)
{
$e
=
new
Exception
(
"Error: '
$email
' already used!"
);
throw
$e
;
}
}
}
coursework-blog/step-10/models/Blog_Entry_Table.class.php
0 → 100644
View file @
c0ef033b
<?php
class
Blog_Entry_Table
extends
Table
{
public
function
saveEntry
(
$title
,
$entry
)
{
$entrySQL
=
"INSERT INTO blog_entry ( title, entry_text ) VALUES ( ?, ?)"
;
$formData
=
array
(
$title
,
$entry
);
$entryStatement
=
$this
->
makeStatement
(
$entrySQL
,
$formData
);
return
$this
->
db
->
lastInsertId
();
}
public
function
getAllEntries
()
{
$sql
=
"SELECT entry_id, title, SUBSTRING(entry_text, 1, 150) AS intro FROM blog_entry"
;
$statement
=
$this
->
makeStatement
(
$sql
);
return
$statement
;
}
public
function
getEntry
(
$id
){
$sql
=
"SELECT entry_id, title, entry_text, date_created FROM blog_entry WHERE entry_id = ?"
;
$data
=
array
(
$id
);
$statement
=
$this
->
makeStatement
(
$sql
,
$data
);
$model
=
$statement
->
fetchObject
();
return
$model
;
}
public
function
updateEntry
(
$id
,
$title
,
$entry
)
{
$sql
=
"UPDATE blog_entry
SET title = ?,
entry_text = ?
WHERE entry_id = ?"
;
$data
=
array
(
$title
,
$entry
,
$id
);
$statement
=
$this
->
makeStatement
(
$sql
,
$data
)
;
return
$statement
;
}
public
function
deleteEntry
(
$id
)
{
$sql
=
"DELETE FROM blog_entry WHERE entry_id = ?"
;
$data
=
array
(
$id
);
$statement
=
$this
->
makeStatement
(
$sql
,
$data
);
}
}
?>
coursework-blog/step-10/models/Table.class.php
0 → 100644
View file @
c0ef033b
<?php
class
Table
{
protected
$db
;
public
function
__construct
(
$db
)
{
$this
->
db
=
$db
;
}
public
function
makeStatement
(
$sql
,
$data
=
NULL
)
{
$statement
=
$this
->
db
->
prepare
(
$sql
);
try
{
$statement
->
execute
(
$data
);
}
catch
(
Exception
$e
)
{
$exceptionMessage
=
"<p>You tried to run this sql:
$sql
<p>
<p>Exception:
$e
</p>"
;
trigger_error
(
$exceptionMessage
);
}
return
$statement
;
}
}
coursework-blog/step-10/views/admin/admin-navigation.php
0 → 100644
View file @
c0ef033b
<?php
$out
=
"
<nav id='admin-navigation'>
<a href='admin.php?page=entries'>All entries</a>
<a href='admin.php?page=editor'>Editor</a>
<a href='admin.php?page=users'>Create admin user</a>
</nav>"
;
echo
$out
;
?>
coursework-blog/step-10/views/admin/editor-html.php
0 → 100644
View file @
c0ef033b
<?php
$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='delete' />
<input type='submit' name='action' value='save' />
<p id='editor-message'>
$entryData->message
</p>
</fieldset>
</fieldset>
</form>
"
;
echo
$out
;
?>
coursework-blog/step-10/views/admin/entries-html.php
0 → 100644
View file @
c0ef033b
<?php
if
(
isset
(
$allEntries
)
===
false
)
{
trigger_error
(
'views/admin/entries-html.php needs $allEntries'
);
}
$entriesAsHTML
=
"<ul>"
;
while
(
$entry
=
$allEntries
->
fetchObject
()
)
{
$href
=
"admin.php?page=editor&id=
$entry->entry_id
"
;
$entriesAsHTML
.
=
"<li><a href='
$href
'>
$entry->title
</a></li>"
;
}
$entriesAsHTML
.
=
"</ul>"
;
echo
$entriesAsHTML
;
?>
coursework-blog/step-10/views/admin/new-admin-form-html.php
0 → 100644
View file @
c0ef033b
<?php
//complete code for views/admin/new-admin-form-html.php
if
(
isset
(
$adminFormMessage
)
===
false
)
{
$adminFormMessage
=
""
;
}
$out
=
"<form method='post' action='admin.php?page=users'>
<fieldset>
<legend>Create new admin user</legend>
<label>e-mail</label>
<input type='text' name='email' required/>
<label>password</label>
<input type='password' name='password' required/>
<input type='submit' value='create user' name='new-admin'/>
</fieldset>
<p id='admin-form-message'>
$adminFormMessage
</p>
</form>"
;
echo
$out
;
coursework-blog/step-10/views/entry-html.php
0 → 100644
View file @
c0ef033b
<?php
//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>"
;
coursework-blog/step-10/views/footer.php
0 → 100644
View file @
c0ef033b
<?php
$out
=
"
</body>
</html>
"
;
echo
$out
;
?>
coursework-blog/step-10/views/header.php
0 → 100644
View file @
c0ef033b
<?php
$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
;
?>
coursework-blog/step-10/views/list-entries-html.php
0 → 100644
View file @
c0ef033b
<?php
$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
;
?>
coursework-blog/step-11/admin.php
0 → 100644
View file @
c0ef033b
<?php
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
"models/Table.class.php"
;
include_once
"models/Admin_User.class.php"
;
$admin
=
new
Admin_User
();
include_once
"controllers/admin/login.php"
;
if
(
$admin
->
isLoggedIn
()
)
{
include_once
"views/admin/admin-navigation.php"
;
$navigation
=
isset
(
$_GET
[
'page'
]
);
if
(
$navigation
)
{
$contrl
=
$_GET
[
'page'
];
}
else
{
$contrl
=
"entries"
;
}
include_once
"controllers/admin/
$contrl
.php"
;
include_once
"views/admin/logout-form-html.php"
;
}
include_once
"views/footer.php"
;
?>
Prev
1
2
3
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment