php class Blog_Controller extends Controller public function add_post

 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
<?php
class Blog_Controller extends Controller {
public function add_post()
{
$form=new Post_Form;
if($form->validate())
{
$post=new Post_Model;
$post->title= (string) $form['title'];
$post->text= (string) $form['text'];
$post->save();//Save post
url::redirect('blog');
}
else
{
$view=new View('add_post');
$view->form=$form->render();
$view->render(true);
}
}
public function index()
{
$view=new View('blog');
$view->posts=ORM::factory('post')->orderby('id','desc')->find_all();
$view->render(true);
}
public function view_post($id)
{
$post=ORM::factory('post',(int) $id);
$view=new View('view_post');
$view->post=$post;
$view->comments=$post->comments;
$view->render(true);
}
public function add_comment($id)
{
$post=ORM::factory('post',(int) $id);
$form=new Formation;
$form->add_element('input','name')->add_rule('Rule_Required');
$form->add_element('email','email')->add_rule('Rule_Required');
$form->add_element('textarea','text')->add_rule('Rule_Required');
$form->add_element('submit','Submit');
if($form->validate())
{
$comment=new Comment_Model;
$comment->name= (string) $form['name'];
$comment->email= (string) $form['email'];
$comment->text= (string) $form['text'];
$comment->post_id = (int) $id;
$comment->save($comment);
url::redirect('blog/view_post/'.(int) $id);
}
else
{
$view=new View('add_comment');
$view->form=$form->render();
$view->render(true);
}
}
}