Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Controller/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function index()
public function post()
{
$this->oUtil->oPost = $this->oModel->getById($this->_iId); // Get the data of the post
$this->oUtil->oComments = $this->oModel->getPostComments($this->_iId);

$this->oUtil->getView('post');
}
Expand Down
52 changes: 52 additions & 0 deletions Controller/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @author Serge Kishiko <sergekishiko@gmail.com>
* @copyright (c) 2015-2017, Pierre-Henry Soria. All Rights Reserved.
* @license Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
* @link http://hizup.uk
*/

namespace TestProject\Controller;

class Comment
Copy link
Owner

@pH-7 pH-7 Mar 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here. Might be a good idea to extend to Blog, so you won't have to initialize session and you will be able to reuse the parent $oUtil object.

And call the parent constructor in Comment constructor.

{
protected $oUtil, $oModel;

public function __construct()
{
// Enable PHP Session
if (empty($_SESSION))
@session_start();

$this->oUtil = new \TestProject\Engine\Util;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you done what I tell you in my previous above comment, you can just remove the constructor here, so PHP will automatically call the parent constructor


/** Get the Model class in all the controller class **/
$this->oUtil->getModel('Comment');
$this->oModel = new \TestProject\Model\Comment;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then, maybe rename $oModel to oCommentModel to avoid confusion with its parent Blog class

}

public function add()
{
if (!empty($_POST['add_comment']))
{
if (isset($_POST['id_post'], $_POST['content'], $_POST['author_name']))
{
$aData = array(
'id_post' => $_POST['id_post'],
'content' => $_POST['content'],
'author_name' => $_POST['author_name'],
'created_date' => date('Y-m-d H:i:s')
);

if ($this->oModel->add($aData))
header('Location:' . ROOT_URL . '?p=blog&a=post&id=' . $_POST['id_post']);
else
$this->oUtil->sErrMsg = 'Whoops! An error has occurred! Please try again later.';
}
else
{
$this->oUtil->sErrMsg = 'All fields are required and the title cannot exceed 50 characters.';
}
}
}
}
7 changes: 7 additions & 0 deletions Model/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
class Blog
{
protected $oDb;
private $oCommentModel;

public function __construct()
{
$this->oDb = new \TestProject\Engine\Db;
$this->oCommentModel = new \TestProject\Model\Comment;
}

public function get($iOffset, $iLimit)
Expand All @@ -32,6 +34,11 @@ public function getAll()
return $oStmt->fetchAll(\PDO::FETCH_OBJ);
}

public function getPostComments($iPostId)
{
return $this->oCommentModel->getPostComments($iPostId);
}

public function add(array $aData)
{
$oStmt = $this->oDb->prepare('INSERT INTO Posts (title, body, createdDate) VALUES(:title, :body, :created_date)');
Expand Down
33 changes: 33 additions & 0 deletions Model/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @author Serge Kishiko <sergekishiko@gmail.com>
* @copyright (c) 2015-2017, Pierre-Henry Soria. All Rights Reserved.
* @license Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
* @link http://hizup.uk
*/

namespace TestProject\Model;

class Comment
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, extend it to Blog class Comment extends Blog

{
protected $oDb;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then, if so, remove $oDb property


public function __construct()
{
$this->oDb = new \TestProject\Engine\Db;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be better to extend this class to Blog (similar to the Admin model) you can can get rid of the constructor).

}

public function add(array $aData)
{
$oStmt = $this->oDb->prepare('INSERT INTO Comments (idPost, content, authorName, createdDate) VALUES(:id_post, :content, :author_name, :created_date)');
return $oStmt->execute($aData);
}

public function getPostComments($iPostId)
{
$oStmt = $this->oDb->prepare('SELECT * FROM Comments WHERE idPost = :postId');
$oStmt->bindParam(':postId', $iPostId, \PDO::PARAM_INT);
$oStmt->execute();
return $oStmt->fetchAll(\PDO::FETCH_OBJ);
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ However, if you need to build a blog system, again this project can be used as a

* List of blog articles showing the **title**, **body** truncated to 100 characters, and **date**. It should show only the latest 5 articles
* Single blog article showing the **title**, **full body**, and the **date**
* List of article comments showing the **date**, **author name** and **content**

#### The Backend

* List of all the blog articles
* Possibility to add a new blog article with a title and body. The title should allow a maximum of 50 characters
* Possibility to edit an existing blog article
* Possibility to delete an article
* Possibility to comment a blog article by specifying its **author name** and its **content**
* Logout feature for the admin user

HTML and CSS code should be kept to the minimum needed to make the website functional – This project is purely to assess how you approach the problem and not how good it looks.
Expand Down
34 changes: 34 additions & 0 deletions View/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,40 @@
?>
</article>

<br>
<aside>
<h3>Comments</h3>

<?php if (empty($this->oComments)): ?>
<p class="error">This post has any comment. Be the first to comment!</p>
<?php else: ?>

<?php foreach ($this->oComments as $oComment): ?>
<h6>On <?= $oComment->createdDate ?></h6>
<h4><?= $oComment->authorName ?> <em>said:</em></h4>
<p><?= $oComment->content ?></p>
<hr class="clear" /><br />
<?php endforeach ?>

<?php endif ?>

<h4>Add a new comment</h4>
<form action="<?=ROOT_URL?>?p=comment&a=add" method="post">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the & HTML entity &amp; instead?


<input type="hidden" name="id_post" value="<?=$this->oPost->id?>" />

<p><label for="author_name">Your name:</label><br />
<input type="text" name="author_name" id="author_name" required="required" />
</p>

<p><label for="content">Content:</label><br />
<textarea name="content" id="content" rows="5" cols="35" required="required"></textarea>
</p>

<p><input type="submit" name="add_comment" value="Post a comment" /></p>
</form>
</aside>

<?php endif ?>

<?php require 'inc/footer.php' ?>
11 changes: 11 additions & 0 deletions db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ INSERT INTO Posts (title, body, createdDate) VALUES
(@sPostTitle, @sPostBody, @sPostDate);


CREATE TABLE IF NOT EXISTS Comments (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
content longtext NOT NULL,
authorName varchar(60) NOT NULL,
idPost int(10) unsigned,
createdDate datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id),
FOREIGN KEY (idPost) REFERENCES Posts(id)
) DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS Admins (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
email varchar(120) NOT NULL,
Expand Down