forked from dojoVader-dev/Diary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSiteController.php
executable file
·105 lines (78 loc) · 2.76 KB
/
SiteController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Plugin\Diary;
use Plugin\Diary\Forms\CommentForm;
use Plugin\Diary\CommentModel;
class SiteController extends \Ip\Controller
{
public function read($articleName)
{
ipAddCss("assets/css/comment.css");
ipAddJsContent("create","
require(['Diary/CommentController'],function(comment){
comment.init();
});
",80);
$article=new Model();
$data=$article->getArticleByAlias($articleName);
//Assuming Record doesn't exists
if($data === null){
return new \Ip\Response\Redirect(ipHomeUrl());
}
$commentForm=Helper::getCommentForm();
$commentForm->getField('post_id')->setValue((int)$data['id']);
//Let's Fetch comment from the Database
$data['form']=$commentForm;
//Fetch Commentmodel
$comment=new CommentModel();
$comment->post_id=(int)$data['id'];
$paginator=$comment->getComments((int)$data['id'])->render(__DIR__."/view/frontend/comments.php");
$data['comments']=$paginator;
return ipView(__DIR__."/view/frontend/_article.php",$data)->render();
}
public function commentSave(){
//Comment Request
if (ipRequest ()->isPost ()) {
$form = Helper::getCommentForm();
$errors = $form->validate ( ipRequest ()->getPost () );
if ($errors) {
// error
$status = array (
'status' => 'error',
'errors' => $errors,
'message'=>"An Error occurred on the Server"
);
} else {
// success
$comment= new CommentModel();
$comment->email=ipRequest()->getPost("email");
$comment->url=ipRequest()->getPost("url");
$comment->author=ipRequest()->getPost("author");
$comment->content=ipRequest()->getPost("content");
$comment->post_id=(int)ipRequest()->getPost('post_id');
$comment->date=date('Y-m-d H:i:s',time());
try{
if ($id = $comment->save()) {
// Redirect to the Edit Page
//Success
$status=array(
"status"=>"success",
"message"=>"The Comment has been delivered,awaiting moderation"
);
}
}
catch(\Ip\Exception\Db $db){
/**
*@todo Send Error to Admin or Show when on Developer Mode
*/
$status["status"]="error";
$status["message"]=$db->getMessage(). " at ".$db->getLine();
}
catch(\Exception $e){
$status["status"]="error";
$status["message"]=$e->getMessage(). " at ".$e->getLine();
}
}
}
return new \Ip\Response\Json($status);
}
}