Example #1
0
 public static function form($controller, $item, $_action = FALSE, $captcha = FALSE)
 {
     // Set default comment form action
     $action = Request::current()->uri();
     $view = View::factory('comment/form')->set('use_captcha', $captcha)->set('action', $action)->set('is_edit', FALSE)->set('auth', Auth::instance())->set('destination', array())->set('item', $item)->bind('errors', $errors)->bind('post', $post);
     // Set form action either from model or action param
     if ($item->url) {
         $action = (string) $item->url;
     } elseif ($_action) {
         $action = $_action;
     }
     // Set if captcha necessary
     if ($captcha) {
         $captcha = Captcha::instance();
         $view->set('captcha', $captcha);
     }
     // Load the comment model
     $post = ORM::factory('comment');
     if ($controller->valid_post('comment')) {
         $values = Arr::merge(array('post_id' => $item->id, 'type' => $item->type), $_POST);
         try {
             $post->values($values)->save();
             if ($post->status != 'publish') {
                 Message::success(__('Your comment has been queued for review by site administrators and will be published after approval.'));
             } else {
                 Message::success(__('Your comment has been posted.', array(':title' => $post->title)));
             }
             // Save the anonymous user information to a cookie for reuse.
             if (User::is_guest()) {
                 User::cookie_save(array('name' => $post->guest_name, 'email' => $post->guest_email, 'url' => $post->guest_url));
             }
             Log::info('Comment: :title has posted.', array(':title' => $post->title));
             // Redirect to post page
             $controller->request->redirect(Request::current()->uri());
         } catch (ORM_Validation_Exception $e) {
             // @todo Add messages
             $errors = $e->errors('models', TRUE);
         }
     }
     return $view;
 }