public function action_index($username)
 {
     $user = User::where_username($username)->first();
     $user_messages = null;
     if (!is_null($user)) {
         $user_messages = Forummessage::with('topic')->where_user_id($user->id)->order_by('created_at', 'DESC')->take(5)->get();
     }
     return View::make('panel::profile.show', compact('user', 'user_messages'));
 }
 public function saveWithMessage(Forummessage $message)
 {
     $this->user_id = Auth::user()->id;
     $incSlug = 0;
     $originalSlug = $this->slug;
     $this->sticky = false;
     do {
         try {
             parent::save();
             $incSlug = 0;
         } catch (Exception $e) {
             if ($e->getCode() == 23000) {
                 $incSlug++;
             }
             $this->slug = $originalSlug . '-' . $incSlug;
         }
     } while ($incSlug != 0);
     $this->category->nb_topics++;
     $this->category->save();
     $message->forumtopic_id = $this->id;
     $message->forumcategory_id = $this->forumcategory_id;
     $message->save();
     return $this;
 }
 public function action_postedit($topic_slug, $topic_id, $message_id)
 {
     $topic = Forumtopic::find($topic_id);
     if (is_null($topic)) {
         return Event::first('404');
     }
     $category = $topic->category;
     $message = Forummessage::find($message_id);
     if (is_null($message)) {
         return Event::first('404');
     }
     if ($message->user->id != Auth::user()->id && !Auth::user()->is('Forumer')) {
         return Event::first('404');
     }
     $rules = array('content' => 'required|min:30');
     $content = trim(Input::get('content'));
     $toValidate = compact('content');
     $editTitle = false;
     if ($topic->messages[0]->id == $message->id && trim(Input::get('title')) != $topic->title) {
         $editTitle = true;
         $rules['title'] = 'required|min:2';
         $title = trim(Input::get('title'));
         $toValidate['title'] = $title;
     }
     $validator = Validator::make($toValidate, $rules);
     if ($validator->fails()) {
         return Redirect::back()->with_errors($validator)->with_input();
     }
     if ($editTitle) {
         $topic->title = $toValidate['title'];
         $topic->slug = Str::slug($toValidate['title']);
         $originalSlug = $topic->slug;
         $incSlug = 0;
         do {
             try {
                 $topic->save();
                 $incSlug = 0;
             } catch (Exception $e) {
                 if ($e->getCode() == 23000) {
                     $incSlug++;
                 }
                 $topic->slug = $originalSlug . '-' . $incSlug;
             }
         } while ($incSlug != 0);
     }
     $message->content = BBCodeParser::parse($content);
     $message->content_bbcode = $content;
     $message->save();
     $topic->touch();
     $topic_id = $topic->id;
     $topic_slug = $topic->slug;
     $url = URL::to_action('forums::topic@index', compact('topic_slug', 'topic_id')) . '?page=last#message' . $message->id;
     return Redirect::to($url);
 }