Skip to content

Commit

Permalink
changed the way threads are rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Sibitzky committed May 1, 2012
1 parent 4d5e7b3 commit 5a844a2
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 346 deletions.
182 changes: 58 additions & 124 deletions application/controllers/thread.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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' => '<div class="main-pagination">',
'full_tag_close' => '</div>',
'cur_tag_open' => '<div class="selected-page">',
'cur_tag_close' => '</div>',
'num_tag_open' => '',
'num_tag_close' => ''
));

$end = min(array($limit_start + $this->meta['comments_shown'],
$data['total_comments']));
$data['pagination'] = $this->pagination->create_links() .
'<span class="paging-text">'. ($limit_start + 1) .' - '. $end .' of ' .
$data['total_comments'] .' Posts in <a href="/">Threads</a> &gt; ' .
'<a href="/f/'.strtolower($data['info']['category']).'">' .
$data['info']['category'].'</a> > <a href="/thread/'. $thread_id.'/' .
url_title($data['info']['title'], 'dash', TRUE) .'">' .
$data['info']['title'].'</a></span>';

$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' => '<div class="main-pagination">',
'full_tag_close' => '</div>',
'cur_tag_open' => '<div class="selected-page">',
'cur_tag_close' => '</div>',
'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' => '<a href="/f/'. strtolower($thread->information->category) .'">'. $thread->information->category .'</a>',
'thread' => '<a href="/thread/'. $thread_id .'/'. url_title($thread->information->subject, 'dash', TRUE) .'">'. $thread->information->subject .'</a>'
);

$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');
}
}

Expand Down
1 change: 1 addition & 0 deletions application/models/thread_dal.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 5a844a2

Please sign in to comment.