Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2010  |  syntax: PHP  |  size: 3.93 KB  |  hits: 261  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * Handles all discussion topic actions.
  4.  * @author Nils W.
  5.  *
  6.  */
  7. class TopicController extends AppController
  8. {
  9.         protected $requireLogin = array('post_reply', 'edit_post');
  10.        
  11.         /**
  12.          * Shows the content of a given discussion topic, applying pagination etc.
  13.          * @param int $id The topic ID to show.
  14.          * @param int $page The current page ID.
  15.          * @param int $pageLength The max amount of posts per page.
  16.          */
  17.         function view($id = 0, $page = 0, $pageLength = 25)
  18.         {
  19.                 // Find the topic
  20.                 if(!$topic = DiscussionHelper::fetch_topic(intval($id)))
  21.                 {
  22.                         $this->set_flash(TDE_TOPICNOTFOUND, 'Topic #' . $id . ' does not exist.');
  23.                         return;
  24.                 }
  25.                
  26.                 // Topic not visited before during this session?
  27.                 if(!in_array($topic->id, $this->session->readTopics))
  28.                 {
  29.                         $this->db->exec("UPDATE tde_topics SET views=views + 1 WHERE id = '" . $id . "'");
  30.                         array_push($this->session->readTopics, $topic->id);
  31.                 }
  32.                
  33.                 // Set page title and breadcrumbs
  34.                 $this->title = $topic->forum_code . ' / ' . $topic->title;
  35.                 $this->breadcrumb($topic->forum, mkurl('forum', $topic->forum_id));
  36.                 $this->breadcrumb($topic->title);
  37.                
  38.                 // Push topic + content to view for presentation
  39.                 $this->push('topic', $topic);
  40.                 $this->push('posts', $this->db->multiple
  41.                 (
  42.                         "SELECT
  43.                                 p.id AS id,
  44.                                 p.icon AS icon,
  45.                                 p.timestamp AS timestamp,
  46.                                 p.text AS text,
  47.                                 p.lastedit AS lastedit,
  48.                                 p.editpct AS editpct,
  49.                                
  50.                                 u.id AS user_id,
  51.                                 u.username AS user_username,
  52.                                 u.subtitle AS user_subtitle,
  53.                                 u.signature AS user_signature,
  54.                                 u.xtag AS user_xtag
  55.                         FROM tde_posts AS p
  56.                         JOIN users AS u ON u.id = p.user_id
  57.                         WHERE p.topic_id = '" . $id . "'"
  58.                 ));
  59.         }
  60.  
  61.         /**
  62.          * Allows the user to reply to a discussion topic.
  63.          * @param int $id the ID of the topic to reply to.
  64.          */
  65.         function post_reply($id = 0)
  66.         {
  67.                 // Find the topic and check if it's valid
  68.                 if(!$topic = DiscussionHelper::fetch_topic(intval($id)))
  69.                 {
  70.                         $this->set_flash(TDE_TOPICNOTFOUND, 'Topic #' . $id . ' does not exist, so you cannot reply to it.');
  71.                         return;
  72.                 }
  73.                 if($topic->closed && !false)
  74.                 {
  75.                         $this->set_flash(TDE_TOPICCLOSED, 'This topic is closed and you do not have the right to post in closed topics.');
  76.                         return;
  77.                 }
  78.                
  79.                 // Apply page title and breadcrumbs
  80.                 $this->title = 'Replying to topic: ' . $topic->title;
  81.                 $this->breadcrumb($topic->forum, mkurl('forum', $topic->forum_id));
  82.                 $this->breadcrumb($this->title, mkurl('topic', $topic->id));
  83.                
  84.                 // Push topic to view
  85.                 $this->push('topic', $topic);
  86.                
  87.                 // Post submitted?
  88.                 if($this->form->is_postback())
  89.                 {
  90.                         $posticon = intval($this->form->pi);
  91.                         $text = $this->form->text;
  92.                         if(strlen($text) < 2)
  93.                         {
  94.                                 $this->set_flash(TDE_POSTREPLY_ERROR, 'Please provide a useful message!', TRUE);
  95.                                 $this->push('posticon', $posticon);
  96.                         }
  97.                         else
  98.                         {
  99.                                 $post_id = DiscussionHelper::create_post($topic->id, $this->session->user->id, $posticon, $text);
  100.                                 $this->db->update('tde_topics', $topic->id, array
  101.                                 (
  102.                                         'lastpost_id' => $post_id,
  103.                                         'lastpost_user_id' => $this->session->user->id,
  104.                                         'lastpost_time' => tun_now()
  105.                                 ));
  106.                                 $this->set_flash(TDE_POSTREPLY_OK, 'Reply added successfully! Redirecting...', FALSE, mkurl('topic', $topic->id, $post_id));
  107.                         }
  108.                 }
  109.         }
  110.        
  111.         /**
  112.          * Allows a user to edit his/her post, or others post when having moderator status.
  113.          * @param int $id The ID of the post to edit.
  114.          */
  115.         function edit_post($id)
  116.         {
  117.                 // Fetch the post
  118.                 $post = DiscussionHelper::fetch_post(intval($id));
  119.                 if($post == NULL || $post->user_id != $this->session->user->id)
  120.                 {
  121.                         $this->set_flash(TDE_POSTNOTFOUND, 'Post not found, or you do not have rights to edit this post.');
  122.                         return;
  123.                 }
  124.                
  125.                 // Update post
  126.                 $this->db->update('tde_posts', $post->id, array
  127.                 (
  128.                         'lastedit_time' => tun_now(),
  129.                         'lastedit_pct' => 100 // Calculate procentual difference between old text and new text, atleast 1% and max 100%
  130.                 ));
  131.         }
  132. }
  133. ?>