Guest

Untitled

By: a guest on Jul 31st, 2010  |  syntax: PHP  |  size: 4.15 KB  |  hits: 436  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php
  2. /**
  3.  * Handles listing of topics and subforums in a forum, etc.
  4.  *
  5.  * @author Nils W.
  6.  *
  7.  */
  8. class ForumController extends AppController
  9. {
  10.         protected $requireLogin = array('post_topic');
  11.        
  12.         private function check_access($forum)
  13.         {
  14.                 // Find forum and see if it's really a forum, accessible, etc
  15.                 if($forum == NULL || $forum->is_category)
  16.                 {
  17.                         $this->set_flash(TDE_FORUMNOTFOUND, 'Invalid forum number. (#' . $id . ')');
  18.                         return FALSE;
  19.                 }
  20.                 // TODO: passworded forums or for specified users only etc
  21.                
  22.                 return TRUE;
  23.         }
  24.        
  25.         /**
  26.          * Shows the forum index with all categories + (sub)forums collapsed.
  27.          */
  28.         function index()
  29.         {
  30.                 $this->title = 'There are over 9000 users online, and 320 of them are guests';
  31.                 if(!$categories = ObjectCache::fetch('forumindex'))
  32.                 {
  33.                         $categories = $this->db->multiple
  34.                         (
  35.                                 "SELECT id,name,description
  36.                                 FROM tde_forums
  37.                                 WHERE is_category = '1'
  38.                                 ORDER BY orderid ASC"
  39.                         );
  40.                         foreach($categories as $category)
  41.                         {
  42.                                 $category->forums = DiscussionHelper::fetch_forums($category->id, 999);
  43.                         }
  44.                         ObjectCache::store('forumindex', $categories);
  45.                 }
  46.                 $this->push('categories', $categories);
  47.         }
  48.        
  49.         /**
  50.          * Lists a forum and all contents.
  51.          * @param int $id The ID of the forum to list.
  52.          */
  53.         function view($id = 0)
  54.         {
  55.                 // Find forum
  56.                 $forum = DiscussionHelper::fetch_forum(intval($id));
  57.                 if(!$this->check_access($forum)) return;
  58.                
  59.                 // Set the title of the page and add a breadcrumb <3
  60.                 $this->title = $forum->name;
  61.                 $this->push('forum', $forum);
  62.                 if($forum->parent_id != -1)
  63.                 {
  64.                         $parent = DiscussionHelper::fetch_forum($forum->parent_id);
  65.                         if(!$parent->is_category)
  66.                         {
  67.                                 $this->push('parent', $parent);
  68.                                 $this->breadcrumb($parent->name, mkurl('forum', $parent->id));
  69.                         }
  70.                 }
  71.                 $this->breadcrumb($forum->name);
  72.                
  73.                 // Fetch subforums (only show these at 0 depth)
  74.                 $this->push('subforums', DiscussionHelper::fetch_forums($forum->id), 0);
  75.                
  76.                 // Fetch topics
  77.                 $this->push('topics', $this->db->multiple
  78.                 (
  79.                         "SELECT
  80.                                 t.id AS id,
  81.                                 t.title AS title,
  82.                                 t.icon AS icon,
  83.                                 t.closed AS closed,
  84.                                 t.sticky AS sticky,
  85.                                 t.views AS views,
  86.                                 (SELECT COUNT(0)-1 FROM tde_posts AS p WHERE p.topic_id = t.id) AS posts,
  87.                                 t.lastpost_id AS lastpost_id,
  88.                                 t.lastpost_time AS lastpost_time,
  89.                                
  90.                                 u.id AS starter_id,
  91.                                 u.username AS starter,
  92.                                 (SELECT username FROM users WHERE id = t.lastpost_user_id) AS lastpost_user
  93.                         FROM tde_topics AS t
  94.                         LEFT JOIN users AS u ON u.id = t.user_id
  95.                         WHERE t.forum_id = '" . $id . "'
  96.                         ORDER BY t.sticky DESC,t.closed ASC,t.lastpost_time DESC"
  97.                 ));
  98.         }
  99.        
  100.         /**
  101.          * Opens a new topic in this forum.
  102.          * @param The ID of the forum to post the topic in.
  103.          */
  104.         function post_topic($forum_id = 0)
  105.         {
  106.                 // Find out forum
  107.                 $forum = DiscussionHelper::fetch_forum(intval($forum_id));
  108.                 if(!$this->accesscheck($forum)) return;
  109.                
  110.                 // Add breadcrumbs
  111.                 $this->title = 'Start new topic';
  112.                 $this->breadcrumb($forum->name, mkurl('forum', $forum->id));
  113.                 $this->breadcrumb('Start new topic');
  114.                
  115.                 // Push the id to the view
  116.                 $this->push('forum_id', $forum->id);
  117.                
  118.                 // Exit here when this is no postback
  119.                 if(!$this->form->is_postback())
  120.                 {
  121.                         return;
  122.                 }
  123.                
  124.                 // Get topic data
  125.                 $title = $this->form->title;
  126.                 $posticon = intval($this->form->pi);
  127.                 $text = $this->form->text;
  128.                 if(strlen($text) < 2 || strlen($title) < 2)
  129.                 {
  130.                         $this->set_flash(3283, 'Invalid title/text for new topic.', TRUE);
  131.                         return;
  132.                 }
  133.  
  134.                 // Store the new topic info
  135.                 $now = tun_now();
  136.                 $topic_id = $this->db->insert('tde_topics', array
  137.                 (
  138.                         'forum_id' => $forum->id,
  139.                         'user_id' => $this->session->user->id,
  140.                         'title' => $title,
  141.                         'icon' => $posticon,
  142.                         'timestamp' => $now,
  143.                         'lastpost_time' => $now,
  144.                 ));
  145.                
  146.                 // Store the topic content as the 'OP' and increment postcount of user
  147.                 $post_id = DiscussionHelper::create_post($topic_id, $this->session->user->id, $posticon, $text);
  148.                
  149.                 // Topic done, redirect to the forum!
  150.                 $this->set_flash(TDE_POSTTOPIC_OK, 'Your topic has been posted! You are being redirected to it\'s forum now...', FALSE, mkurl('forum', $forum->id));
  151.         }
  152. }
  153. ?>