-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comment feature #2
base: master
Are you sure you want to change the base?
Changes from 2 commits
ed2430a
5beeb54
f46d835
4ad2bf3
1e4972e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* @author Serge Kishiko <[email protected]> | ||
* @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 | ||
{ | ||
protected $oUtil, $oModel; | ||
|
||
public function __construct() | ||
{ | ||
// Enable PHP Session | ||
if (empty($_SESSION)) | ||
@session_start(); | ||
|
||
$this->oUtil = new \TestProject\Engine\Util; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And then, maybe rename |
||
} | ||
|
||
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.'; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* @author Serge Kishiko <[email protected]> | ||
* @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, extend it to Blog |
||
{ | ||
protected $oDb; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you use the & HTML entity |
||
|
||
<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' ?> |
There was a problem hiding this comment.
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.