From 5a844a22645324c3f1f2ad8b27743271d7280170 Mon Sep 17 00:00:00 2001 From: Daniel Sibitzky Date: Tue, 1 May 2012 11:01:39 -0400 Subject: [PATCH] changed the way threads are rendered --- application/controllers/thread.php | 182 ++++++------------ application/models/thread_dal.php | 1 + application/models/thread_model.php | 243 ++++++++++++++++++++++++ application/views/thread.php | 280 +++++++++++----------------- system/libraries/Controller.php | 57 ++---- 5 files changed, 417 insertions(+), 346 deletions(-) mode change 100644 => 100755 application/controllers/thread.php mode change 100644 => 100755 application/models/thread_dal.php create mode 100755 application/models/thread_model.php mode change 100644 => 100755 application/views/thread.php mode change 100644 => 100755 system/libraries/Controller.php diff --git a/application/controllers/thread.php b/application/controllers/thread.php old mode 100644 new mode 100755 index cd22ee5..911523b --- a/application/controllers/thread.php +++ b/application/controllers/thread.php @@ -4,20 +4,12 @@ class Thread extends Controller { function Thread() { - parent::Controller(); + parent::__construct(); - $this->load->helper(array('url', 'date', 'form', 'content_render', - 'htmlpurifier')); + $this->load->helper(array('url', 'date', 'form', 'content_render', 'htmlpurifier')); $this->load->library(array('form_validation', 'pagination')); $this->load->model('thread_dal'); - - // set all this so we dont have to continually call functions through session - $this->meta = array( - 'user_id' => (int) $this->session->userdata('user_id'), - 'session_id' => $this->session->userdata('session_id'), - 'hide_enemy_posts' => $this->session->userdata('hide_enemy_posts'), - 'comments_shown' => (int) $this->session->userdata('comments_shown') ?: 50, - ); + $this->load->model('thread_model'); } // if the just throw in /thread into the address bar @@ -29,47 +21,24 @@ function index() function load($thread_id) { - // if they roll in with something unexpected - // send them home - if (!is_numeric($thread_id)) - redirect('/'); + $segments = $this->uri->segment_array(); - // grabbing the thread information - $query = $this->thread_dal->get_thread_information($this->meta['user_id'], - $thread_id); + while($seg = next($segments)) + $page = $seg == 'p' ? (int) next($segments) : 0; - // does it exist? - if ($query->num_rows === 0) - redirect('/'); + $thread = $this->thread_model->get_thread($thread_id, $this->meta, $page); + + $uri = '/thread/'. $thread_id .'/'. url_title($thread->information->subject, 'dash', TRUE); - $thread_info = $query->row(); - - $hidden = explode(',', - $this->thread_dal->get_hidden($this->meta['user_id'])); - - $favourites = explode(',', - $this->thread_dal->get_favorites($this->meta['user_id'])); - - // alright we're clear, set some data for the view - $data = array( - 'info' => array( - 'title' => $thread_info->subject, - 'nsfw' => $thread_info->nsfw, - 'closed' => $thread_info->closed, - 'category' => $thread_info->category, - 'acq_type' => (int) $thread_info->type, - 'user_id' => $thread_info->user_id, - 'editable' => time() - strtotime($thread_info->created) < 300 - ), - 'thread_id' => $thread_id, - 'favorites' => $favourites, - 'hidden' => $hidden, - 'meta' => $this->meta - ); - - - // if the thread is closed then we're not accepting any new data - if ($thread_info->closed === '0' || (int) $thread_info->type == 2) { + // if they roll in with something unexpected + // or the thread doesnt exist + // send them home + if ($thread == null) + redirect('/'); + + // if the thread is closed then we're not accepting any new data + if (!$thread->information->closed || $thread->information->author_acquaintance_type == 2) + { // we're going to go ahead and do the form processing for the reply now // if they're submitting data, we're going to refresh the page anyways // so theres no point in running the query below the form validation @@ -81,8 +50,8 @@ function load($thread_id) $content = $this->form_validation->set_value('content'); $ajax = $this->form_validation->set_value('ajax'); - - $this->thread_dal->new_comment(array( + + $this->thread_model->new_comment((object) array( 'thread_id' => $thread_id, 'user_id' => $this->meta['user_id'], 'content' => _process_post($content), @@ -91,87 +60,52 @@ function load($thread_id) $this->user_dal->update_comment_count($this->meta['user_id']); - $db_count = $this->thread_dal->comment_count($thread_id); $shown = $this->meta['comments_shown']; - if (!$shown) { - $shown = 25; - } - $count = (ceil($db_count / $shown) -1) * $shown; - - $url = '/thread/'. $thread_id . '/'. - url_title($thread_info->subject, 'dash', TRUE) . '/p/'. $count . '/' . - $db_count . '#bottom'; + $last_page = (ceil(($thread->information->comment_count + 1) / $this->meta['comments_shown']) + * $this->meta['comments_shown']) - $this->meta['comments_shown']; + + $redirection = $uri .'/p/'. $last_page .'#bottom'; + if ($ajax) { - return send_json($this->output, 201, array('ok' => true, 'url' => $url)); + return send_json($this->output, 201, array('ok' => true, 'url' => $redirection)); } else { - redirect($url); - } - } - } - - $pseg = 0; - $base_url = ''; - $limit_start = 0; - - for($i=1; $i<=count($this->uri->segments); ++$i) { - $base_url .= '/'. $this->uri->segments[$i]; - - if ($this->uri->segments[$i] == 'p') { - if (isset($this->uri->segments[$i+1]) && - is_numeric($this->uri->segments[$i+1])) { - $pseg = $i+1; - $limit_start = (int)$this->uri->segments[$i+1]; - - break; + redirect($redirection); } } } - if ($pseg === 0) - $base_url .= '/p'; - - $data['thread_model'] =& $this->thread_dal; - - $data['comment_result'] = - $this->thread_dal->get_comments($this->meta['user_id'], - $thread_id, - $limit_start, - $this->meta['comments_shown']); - - $data['total_comments'] = $this->thread_dal->comment_count($thread_id); - - $this->pagination->initialize(array( - 'num_links' => 1, - 'base_url' => $base_url, - 'total_rows' => $data['total_comments'], - 'uri_segment' => $pseg, - 'per_page' => $this->meta['comments_shown'], - 'full_tag_open' => '
', - 'full_tag_close' => '
', - 'cur_tag_open' => '
', - 'cur_tag_close' => '
', - 'num_tag_open' => '', - 'num_tag_close' => '' - )); - - $end = min(array($limit_start + $this->meta['comments_shown'], - $data['total_comments'])); - $data['pagination'] = $this->pagination->create_links() . - ''. ($limit_start + 1) .' - '. $end .' of ' . - $data['total_comments'] .' Posts in Threads > ' . - '' . - $data['info']['category'].' > ' . - $data['info']['title'].''; - - $data['starting'] = $limit_start; - - $this->load->helper('content_render'); - - $this->load->view('shared/header', array('page_title' => $thread_info->subject)); - $this->load->view('thread', $data); - $this->load->view('shared/footer'); + $this->pagination->initialize(array( + 'num_links' => 3, + 'base_url' => $uri .= '/p/', + 'total_rows' => $thread->information->comment_count, + 'uri_segment' => 5, + 'per_page' => $this->meta['comments_shown'], + 'full_tag_open' => '
', + 'full_tag_close' => '
', + 'cur_tag_open' => '
', + 'cur_tag_close' => '
', + 'num_tag_open' => '', + 'num_tag_close' => '' + )); + + $uri .= isset($uri_assoc['p']) ? (int) $uri_assoc['p'] : 0; + + $thread->pagination = (object) array( + 'links' => $this->pagination->create_links(), + 'lower_limit' => $page + 1, + 'upper_limit' => min(array($page + $this->meta['comments_shown'], $thread->information->comment_count)), + 'category' => ''. $thread->information->category .'', + 'thread' => ''. $thread->information->subject .'' + ); + + $thread->information->page = $page; + + $this->load->helper('content_render'); + + $this->load->view('shared/header', array('page_title' => $thread->information->subject)); + $this->load->view('thread', $thread); + $this->load->view('shared/footer'); } } diff --git a/application/models/thread_dal.php b/application/models/thread_dal.php old mode 100644 new mode 100755 index f748342..23f1725 --- a/application/models/thread_dal.php +++ b/application/models/thread_dal.php @@ -73,6 +73,7 @@ function get_threads($user_id, $limit, $span, $filtering = '', $ordering = '') authors.username AS author_name, responders.username AS responder_name, responses.created AS response_created, + responses.banned AS responder_banned, IFNULL(acquaintances.type, 0) AS acq, ( SELECT diff --git a/application/models/thread_model.php b/application/models/thread_model.php new file mode 100755 index 0000000..7fcc85e --- /dev/null +++ b/application/models/thread_model.php @@ -0,0 +1,243 @@ +page = $page; + + // set the internal happiness + $this->meta = $meta; + + // get thread information from the database + $this->return = (object) array( + 'information' => $this->get_information($thread_id) + ); + + // if the thread info is null, no need to continue + if ($this->return->information == NULL) + return NULL; + + // get the comments from the database + $this->return->comments = $this->get_comments($thread_id); + + // has to be called DIRECTLY AFTER $this->get_comments() + $this->return->information->comment_count = (int) $this->db->query('SELECT FOUND_ROWS() AS thread_count')->row()->thread_count; + + // send it back + return $this->return; + } + + /* + * Gets basic header information for a given thread + * + * @param int + */ + private function get_information($thread_id) + { + $sql = "SELECT + threads.user_id AS author_id, + subject, + closed, + nsfw, + created, + categories.name AS category, + IFNULL(acquaintances.type, 1) AS author_acquaintance_type, + !ISNULL(favorites.favorite_id) AS favorite, + !ISNULL(hidden_threads.hidden_id) AS hidden + FROM threads + LEFT JOIN categories + ON threads.category = categories.category_id + LEFT JOIN acquaintances + ON acquaintances.user_id = threads.user_id + AND acquaintances.acq_user_id = ? + LEFT JOIN favorites + ON favorites.user_id = ? + AND favorites.thread_id = threads.thread_id + LEFT JOIN hidden_threads + ON hidden_threads.user_id = ? + AND hidden_threads.thread_id = threads.thread_id + WHERE threads.thread_id = ? AND threads.deleted != 1"; + + $result = $this->db->query($sql, array( + $this->meta['user_id'], + $this->meta['user_id'], + $this->meta['user_id'], + $thread_id) + )->row(); + + if (count($result)) + { + return (object) array( + 'thread_id' => $thread_id, + 'author_id' => (int) $result->author_id, + 'subject' => $result->subject, + 'closed' => (bool) $result->closed, + 'nsfw' => (bool) $result->nsfw, + 'created' => $result->created, + 'category' => $result->category, + 'author_acquaintance_type' => (int) $result->author_acquaintance_type, + 'favorite' => (bool) $result->favorite, + 'hidden' => (bool) $result->hidden, + 'editable' => time() - strtotime($result->created) < 300, + 'enemies' => 0, + 'owner' => $this->meta['user_id'] == $result->author_id + ); + } else { + return NULL; + } + } + + /** + * Get a count of all the comments for a given thread id + * + * @param int + * @param int + */ + private function get_comments($thread_id) + { + $sql = "SELECT + SQL_CALC_FOUND_ROWS + comments.comment_id, + comments.content, + comments.original_content, + comments.created, + comments.deleted, + comments.user_id AS author_id, + users.username AS author_name, + users.emoticon, + IFNULL(acquaintances.type, 0) AS author_acquaintance_type + FROM comments + LEFT JOIN users + ON comments.user_id = users.id + LEFT JOIN acquaintances + ON acquaintances.acq_user_id = users.id + AND acquaintances.user_id = ? + WHERE comments.thread_id = ? + ORDER BY comments.created + LIMIT ?, ?"; + + $result = $this->db->query($sql, array( + $this->meta['user_id'], + $thread_id, + $this->page, + $this->meta['comments_shown'] + )); + + if (!$result->num_rows()) + { + return NULL; + } else { + $return = array(); + + $i = 0; + foreach($result->result() as $row) + { + if ($row->deleted == '0') + { + $return[$i] = (object) array( + 'comment_id' => $row->comment_id, + 'content' => $row->content, + 'created' => strtotime($row->created), + 'deleted' => 0, + 'author_id' => (int) $row->author_id, + 'author_name' => $row->author_name, + 'url_safe_author_name' => url_title($row->author_name, 'dash'), + 'emoticon' => (bool) $row->emoticon, + 'author_acquaintance_type' => (int) $row->author_acquaintance_type, + 'author_acquaintance_name' => $row->author_acquaintance_type == 1 ? 'buddy' : ($row->author_acquaintance_type == 2 ? 'enemy' : NULL), + 'owner' => $this->meta['user_id'] == $row->author_id, + 'editable' => ($this->meta['user_id'] == $row->author_id) && ($row->created > time() - (60 * 60 * 24)), + 'show_controls' => $this->page == 0 && ($this->meta['user_id'] == $row->author_id) && !$i + ); + + // update comments if the content doesnt match original + if ($row->content === '' && $row->content == _process_post($row->original_content)) + { + $return[$i]->content = _process_post($row->original_content); + $this->db->query("UPDATE comments SET content = ? WHERE comment_id = ?", + array($return[$i]->content, $row->comment_id)); + } + + if ($return[$i]->author_acquaintance_type == 2) + ++$this->return->information->enemies; + } else { + $return[$i] = (object) array( + 'comment_id' => (int) $row->comment_id, + 'created' => $row->created, + 'deleted' => 1 + ); + } + + ++$i; + } + + return $return; + } + } + + /** + * Insert a new comment into the database + * + * @param array + */ + function new_comment($comment) + { + $created = date("Y-m-d H:i:s", utc_time()); + + $sql = "INSERT + INTO comments ( + thread_id, + user_id, + content, + original_content, + created + ) VALUES (?, ?, ?, ?, ?)"; + + $this->db->query($sql, + array( + $comment->thread_id, + $comment->user_id, + $comment->content, + $comment->original_content, + $created + ) + ); + + $sql = "UPDATE + threads + SET + last_comment_id = ?, + last_comment_created = ? + WHERE thread_id = ?"; + + $this->db->query($sql, + array( + $this->db->insert_id(), + $created, + $comment->thread_id + ) + ); + + $sql = "UPDATE + categories + SET + last_comment_created = ? + WHERE category_id = ( + SELECT category + FROM threads + WHERE thread_id = ?)"; + + $this->db->query($sql, + array( + $created, + $comment->thread_id + ) + ); + } +} \ No newline at end of file diff --git a/application/views/thread.php b/application/views/thread.php old mode 100644 new mode 100755 index 344fd7d..493b4a9 --- a/application/views/thread.php +++ b/application/views/thread.php @@ -1,124 +1,111 @@ sauth->is_logged_in(); -$owner = $logged_in && $this->meta['user_id'] == $info['user_id']; ?>
-
> -

+
owner && $information->editable ? ' class="changeling"' : '' ?>> +

subject) ?>

+ - - - + +
- - - + owner): ?> + + result() as $row) { - //checking if last post was older than 36 hours ago - - $created = strtotime($row->created); - $comment_owner = $row->user_id == $this->meta['user_id']; - - if ($i > 0 && $created - $previous_created > 129600) +foreach($comments as $row) { + //checking if last post was older than 36 hours ago + if ($row->created - $previous_created > 129600) { ?>
- created), 'later');; ?> + created, 'later'); ?>
created); + $previous_created = $row->created; // if the comment has been deleted, // go ahead and just put the deleted thing and start the loop over - if ($row->deleted == '1') { -?> -
- comment by username; ?> deleted -
-deleted) { + ?> +
comment by author_name ?> deleted
+ username, 'dash'); - - switch($row->acq_type) { - case 1: - $acq = ' buddy'; - break; - case 2: - $acq = ' enemy'; - break; - default: - $acq = null; - } - // if the comment belongs to someone you've enemied - if ($row->acq_type == 2) + if ($row->author_acquaintance_type == 2) { - if ($this->meta['hide_enemy_posts'] === '1') continue; - - ++$enemies; + ?> -
- +
+ -
-
+
+
-
- - username;?> + -
created), time()) ?>
-
+
created, time()) ?>
+ - + show_controls) { ?>

THREAD ADMIN

    -
  • ·
  • -
  • ·
  • - +
  • · nsfw ? 'Unm' : 'M';?>ark Naughty
  • +
  • · closed ? 'Open' : 'Close' ?> Thread
  • + editable) {?>
  • · Delete Thread
  • - +
- +
-session->userdata('view_html') === '1' || - $this->session->userdata('view_html') === false); - -if ($row->content === '' && $row->original_content !== '') { - $content = _process_post($row->original_content); - $thread_model->update_comment_cache($row->comment_id, $content); -} else { - $content = $row->content; -} - -echo $view_html ? $content : nl2br(htmlentities($content)); - -?> + content ?>
@@ -177,46 +151,42 @@
- @@ -235,6 +205,7 @@
-
+
- + 1, + 'name' => 'content', + 'id' => 'thread-content-input', + 'value' => set_value('content') + )); ?>
-

I, session->userdata('username'); ?>, - do solemnly swear that in posting this comment I promise to be nice.

- - +

I, meta['username'] ?>, do solemnly swear that in posting this comment I promise to be nice.

+
@@ -313,52 +286,7 @@
- -session->userdata('new_post_notification') === 1) { - -?> -
- -
- - - - -
+
\ No newline at end of file diff --git a/system/libraries/Controller.php b/system/libraries/Controller.php old mode 100644 new mode 100755 index 2eaa7f6..ee12728 --- a/system/libraries/Controller.php +++ b/system/libraries/Controller.php @@ -42,52 +42,17 @@ function Controller() parent::CI_Base(); $this->_ci_initialize(); log_message('debug', "Controller Class Initialized"); - } - - /** - * Initialize - * - * Assigns all the bases classes loaded by the front controller to - * variables in this class. Also calls the autoload routine. - * - * @access private - * @return void - */ - /* - function _ci_initialize() - { - // Assign all the class objects that were instantiated by the - // front controller to local class variables so that CI can be - // run as one big super object. - $classes = array( - 'config' => 'Config', - 'input' => 'Input', - 'benchmark' => 'Benchmark', - 'uri' => 'URI', - 'output' => 'Output', - 'lang' => 'Language', - 'router' => 'Router' - ); - - foreach ($classes as $var => $class) - { - $this->$var =& load_class($class); - } - $this->load =& load_class('Loader'); - $this->load->_ci_autoloader(); - - // redirect to login if they arent currently logged in - - if (!$this->sauth->is_logged_in()) - { - if ((!isset($this->uri->segments[1]) || $this->uri->segments[1] != 'beta')) - { - $this->load->helper(array('url')); - redirect('/beta/'); - } - } - }*/ + $this->meta = array( + 'user_id' => (int) $this->session->userdata('user_id') ?: 0, + 'username' => $this->session->userdata('username') ?: 'guest', + 'session_id' => $this->session->userdata('session_id'), + 'hide_enemy_posts' => $this->session->userdata('hide_enemy_posts'), + 'comments_shown' => (int) $this->session->userdata('comments_shown') ?: 50, + 'threads_shown' => (int) $this->session->userdata('comments_shown') ?: 50, + 'view_html' => (bool) $this->session->userdata('view_html') ?: TRUE, + ); + } // -------------------------------------------------------------------- @@ -126,4 +91,4 @@ function _ci_initialize() // END _Controller class /* End of file Controller.php */ -/* Location: ./system/libraries/Controller.php */ +/* Location: ./system/libraries/Controller.php */ \ No newline at end of file