Advertisement
Hellgorn

FuelPHP ControllerPosts

Jan 23rd, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.03 KB | None | 0 0
  1. <?php
  2.  
  3. class Controller_Posts extends Controller_Base
  4. {
  5.  
  6.     public function action_index()
  7.     {
  8.        
  9.         $view = View::forge('posts/index');
  10.        
  11.         $pagination = Model_Post::count(array(
  12.             'where' => array(
  13.                 array('active', 1),
  14.             ),
  15.         ));
  16.  
  17.         $config = array(
  18.             'pagination_url' => 'posts/index/',
  19.             'total_items' => $pagination,
  20.             'per_page' => 2,
  21.             'uri_segment' => 3,
  22.         );
  23.  
  24.         Paginationbootstrap::set_config($config);
  25.  
  26.         $view->posts = Model_Post::find('all', array(
  27.             'where' => array(
  28.                 array('active', 1),
  29.             ),
  30.             'order_by' => array('created_at' => 'desc'),
  31.             'limit' => Paginationbootstrap::$per_page,
  32.             'offset' => Paginationbootstrap::$offset,
  33.         ));
  34.  
  35.         $view->set('pagination', Paginationbootstrap::create_links(), false);
  36.  
  37.         $this->template->title = "Blog";
  38.         $this->template->content = $view;
  39.  
  40.     }
  41.  
  42.     public function action_view($slug)
  43.     {
  44.         $view = View::forge('posts/view');
  45.  
  46.         $view->set('post', Model_Post::query()->where('slug', $slug)->related('comments')->order_by('comments.created_at', 'desc')->get_one(), false);
  47.  
  48.         $this->template->title = $view->post->title;
  49.         $this->template->date = $view->post->created_at;
  50.         $this->template->content = $view;
  51.  
  52.     }
  53.  
  54.     public function action_comment($slug)
  55.     {
  56.         if (Input::method() == 'POST')
  57.         {
  58.             $val = Model_Comment::validate('create');
  59.  
  60.             if ($val->run())
  61.             {
  62.                 $comment = Model_Comment::forge(array(
  63.                     'pseudo' => Input::post('pseudo'),
  64.                     'email' => Input::post('email'),
  65.                     'message' => Input::post('message'),
  66.                     'post_id' => Input::post('post_id'),
  67.                     'user_id' => Input::post('user_id'),
  68.                 ));
  69.  
  70.                 if ($comment and $comment->save())
  71.                 {
  72.                     Session::set_flash('success', 'Votre commentaire a bien été envoyé.');
  73.  
  74.                     Response::redirect('posts/view/'.$slug);
  75.                 }
  76.  
  77.                 else
  78.                 {
  79.                     Session::set_flash('error', 'Impossible d\'envoyer votre commentaire.');
  80.                 }
  81.             }
  82.             else
  83.             {
  84.                 Session::set_flash('error', $val->show_errors());
  85.             }
  86.         }
  87.         Response::redirect('posts/view/'.$slug);
  88.  
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement