-
-
Notifications
You must be signed in to change notification settings - Fork 79
Comment feature #2
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
Changes from all 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ class Blog | |
{ | ||
const MAX_POSTS = 5; | ||
|
||
protected $oUtil, $oModel; | ||
protected $oUtil, $oModel, $oCommentModel; | ||
private $_iId; | ||
|
||
public function __construct() | ||
|
@@ -26,6 +26,7 @@ public function __construct() | |
/** Get the Model class in all the controller class **/ | ||
$this->oUtil->getModel('Blog'); | ||
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'm pretty sure you will need |
||
$this->oModel = new \TestProject\Model\Blog; | ||
$this->oCommentModel = new \TestProject\Model\Comment; | ||
|
||
/** Get the Post ID in the constructor in order to avoid the duplication of the same code **/ | ||
$this->_iId = (int) (!empty($_GET['id']) ? $_GET['id'] : 0); | ||
|
@@ -44,6 +45,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->oCommentModel->getPostComments($this->_iId); | ||
|
||
$this->oUtil->getView('post'); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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 extends Blog | ||
{ | ||
public function __construct() | ||
{ | ||
$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->oCommentModel = new \TestProject\Model\Comment; | ||
} | ||
|
||
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->oCommentModel->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,26 @@ | ||
<?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 extends Blog | ||
{ | ||
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); | ||
} | ||
} |