/**
  * Generate the sequence of a slug field for nation page!!!
  *
  * @author Povstyanoy
  * @param array $slug
  * @return string
  */
 function generatetopicurl($slug = null)
 {
     $objTopic = new Forumtopic();
     $objTopic->contain();
     $topic = $objTopic->find('first', array('conditions' => array('Forumtopic.slug' => $slug)));
     $objBranch = new Forumbranch();
     $forum_path = $objBranch->getpath($topic['Forumtopic']['forumbranch_id']);
     if (empty($forum_path)) {
         return "";
     }
     $slugs = array();
     foreach ($forum_path as $index => $branch) {
         $slugs[$index] = $branch['Forumbranch']['slug'];
     }
     //last topic is not a link
     array_push($slugs, $slug);
     $forumurl = implode("/", $slugs);
     return $forumurl;
 }
 }
 /**
  * Enter description here...
  *
  * @author Povstyanoy
  * @param  array $slugs Array of action unnamed parameters array["pass"];
  * @return array $out
  *
  *         "forum" => id
  *         "topic" => id
  *         "post" => id
  */
 function findIdBySlug($slugs)
 {
     App::import('Model', 'Forumtopic');
     App::import('Model', 'Forumpost');
     $forumtopic = new Forumtopic();
     $forumpost = new Forumpost();
     //$slug = implode("/", $slugs);
     $out = array("Forum" => null, "Topic" => null, "Post" => null);
     if (empty($slugs)) {
         return $out;
     }
     //Find a post
     $post_id = (int) $slugs[count($slugs) - 1];
     // slug contain ID of post
     if ($post_id > 0) {
         // delete post from array
         unset($slugs[count($slugs) - 1]);
         $post = $forumpost->find('first', array('conditions' => array('Forumpost.is_deleted <> 1', 'Forumpost.id' => $post_id)));
         if (!empty($post['Forumpost']['id'])) {
             $out['Post'] = $post['Forumpost']['id'];
         }
     }
     //eof
     //if (!empty($out['Post'])) {
     $topic_slug = $slugs[count($slugs) - 1];
     //} else {
     //	$topic_slug = $slugs[ count($slugs) - 1 ];
     //}
     $forumtopic->contain();
     $topic_id = $forumtopic->find('first', array('conditions' => array('Forumtopic.is_deleted <> 1', 'Forumtopic.slug' => $topic_slug)));
     if (!empty($topic_id)) {
         $out['Topic'] = $topic_id['Forumtopic']['id'];
     }
     if (!empty($out['Topic'])) {
         unset($slugs[count($slugs) - 1]);
     }
     $forum_slug = $slugs[count($slugs) - 1];
     $this->contain();
     $forumbranch = $this->find('first', array('conditions' => array('Forumbranch.is_deleted <> 1', 'Forumbranch.slug' => $forum_slug)));
     if (!empty($forumbranch)) {
         $out['Forum'] = $forumbranch['Forumbranch']['id'];
     }
     return $out;
 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);
 }